/* modified by f.rykhtik, bleuroy
principle: hierarchical dynamic select. each upper level of selectboxes will rule over levels situated under it.
current hierarchy of selectboxes : filiere => categorie => grade => mode_acces
*/


function filterMain(fil,cat, grade, mode) {
/* takes Mainlist, select corresponding to condition elements*/
    var r=new Array();
    
	for(i=0; i<MainList.length; i++ ) {
		if ( (fil=='' || fil == MainList[i].filiere) &&
			(cat=='' || cat == MainList[i].categorie) &&
			(grade=='' || grade == MainList[i].grade) &&
			(mode=='' || mode == MainList[i].mode_acces) ) {
		r[r.length]=new ListItem(MainList[i].nvalue,MainList[i].filiere,MainList[i].categorie,MainList[i].grade,MainList[i].mode_acces);		
		}
	}
	return r; 
    /* r - array of selected elements */
}

function changeFiliere(flag) {
/* Inititalization of the form */
    theForm = document.form1;

/* checking of current values */
	filInd = theForm.filiere.selectedIndex;
	strFil = '';
	if (filInd>=0 && theForm.filiere.length>0) {
		strFil = theForm.filiere[filInd].value;
	}
	catInd = theForm.categorie.selectedIndex;
	strCat = '';
	if (catInd>=0 && theForm.categorie.length>0) {
		strCat = theForm.categorie[catInd].value;
	}
	
	gradeInd = theForm.grade.selectedIndex;
	strGrade = '';
	if (gradeInd>=0 && theForm.grade.length>0) {
		strGrade = theForm.grade[gradeInd].value;
	}

	modeInd = theForm.mode_acces.selectedIndex;
	strMode = '';
	if (modeInd>=0 && theForm.mode_acces.length>0) {
		strMode = theForm.mode_acces[modeInd].value;
	}
	
	/* Get full list of elements, add to "options"*/
	theForm.filiere.options[0]=new Option('Toutes les filières', '');
	strFilTempo='';
	var msg_local = ''
	nOption=1;
	var selInd=0;
	tab=new Array();
	filteredTab = filterMain('', '', '', '');
	for (i=0;i<filteredTab.length;i++) {
		if(strFilTempo== '' || strFilTempo!=filteredTab[i].filiere) {
			tab[tab.length]=filteredTab[i].filiere;
			strFilTempo=filteredTab[i].filiere;
		}
	}
	
	tab.sort();
	theForm.filiere.length=tab.length;
	for(i=0;i<tab.length;i++) {
		theForm.filiere.options[nOption]=new Option(tab[i], tab[i]);
		if(tab[i]==strFil) selInd = nOption;
		nOption++;
	}
	if (selInd >=0) {
		theForm.filiere.selectedIndex = selInd;
	} else {
		theForm.filiere.selectedIndex = 0;
	}
    
    /* initialization of each level, situated under current */
    theForm.categorie.length = 0;
    theForm.categorie.options[0]=new Option('Toutes les catégories', '');
    theForm.categorie.selectedIndex = 0;
    
    theForm.grade.length = 0;
    theForm.grade.options[0]=new Option('Tous les grades', '');
    theForm.grade.selectedIndex = 0;
    
    theForm.mode_acces.length = 0;
    theForm.mode_acces.options[0]=new Option('Tous les modes', '');
    theForm.mode_acces.selectedIndex = 0;
    
    if (flag==false) {
	changeCategorie(true); 
    /*only category will be proceed*/
    }
}

function changeCategorie(flag) {
	theForm = document.form1;
    
    if (flag==false) {
	    theForm.grade.length = 0;
        theForm.grade.options[0]=new Option('Tous les grades', '');
        theForm.grade.selectedIndex = 0;
	}

	filInd = theForm.filiere.selectedIndex;
	strFil = '';
	if (filInd>=0 && theForm.filiere.length>0) {
		strFil = theForm.filiere[filInd].value;
	}
	
	catInd = theForm.categorie.selectedIndex;
	strCat = '';
	if (catInd>=0 && theForm.categorie.length>0) {
		strCat = theForm.categorie[catInd].value;
	}

	modeInd = theForm.mode_acces.selectedIndex;
	strMode = '';
	if (modeInd>=0 && theForm.mode_acces.length>0) {
		strMode = theForm.mode_acces[modeInd].value;
	}

	gradeInd = theForm.grade.selectedIndex;
	strGrade = '';
	if (gradeInd>=0 && theForm.grade.length>0) {
		strGrade = theForm.grade[gradeInd].value;
	}

	theForm.categorie.options[0]=new Option('Toutes les catégories','');
	strCatTempo='';
	nOption=1;
	var selInd=0;
	tab=new Array();
	filteredTab = filterMain(strFil, '', strGrade, strMode);
    /* conditional selection - all the parameters except current one - not to replace values */
	for (i=0;i<filteredTab.length;i++) {
		if(strCatTempo== '' || strCatTempo.indexOf(filteredTab[i].categorie)<0) {
			tab[tab.length]=filteredTab[i].categorie;
			strCatTempo+=filteredTab[i].categorie;
		}
	}
	
	tab.sort();
	theForm.categorie.length=tab.length;
	for(i=0;i<tab.length;i++) {
	theForm.categorie.options[nOption]=new Option(tab[i], tab[i]);
		if(tab[i]==strCat) selInd = nOption;
		nOption++;
	}
	if (selInd >=0) {
		theForm.categorie.selectedIndex = selInd;
	} else {
		theForm.categorie.selectedIndex = 0;
	}
	
	
	if (flag==false) {
		changeGrade(true);
        theForm.mode_acces.length = 0;
        theForm.mode_acces.options[0]=new Option('Tous les modes', '');
        theForm.mode_acces.selectedIndex = 0;
	}
}

function changeGrade(flag) {

    if (flag==false) {
        theForm.mode_acces.length = 0;
        theForm.mode_acces.options[0]=new Option('Tous les modes', '');
        theForm.mode_acces.selectedIndex = 0;
	}
	
	theForm = document.form1;

	filInd = theForm.filiere.selectedIndex;
	strFil = '';
	if (filInd>=0 && theForm.filiere.length>0) {
		strFil = theForm.filiere[filInd].value;
	}	
	
	catInd = theForm.categorie.selectedIndex;
	strCat = '';
	if (catInd>=0 && theForm.categorie.length>0) {
		strCat = theForm.categorie[catInd].value;
	}

	gradeInd = theForm.grade.selectedIndex;
	strGrade = '';
	if (gradeInd>=0 && theForm.grade.length>0) {
		strGrade = theForm.grade[gradeInd].value;
	}
	modeInd = theForm.mode_acces.selectedIndex;

	strMode = '';
	if (modeInd>=0 && theForm.mode_acces.length>0) {
		strMode = theForm.mode_acces[modeInd].value;
	}
	
	
	theForm.grade.options[0]=new Option('Tous les grades', '');
	strGradeTempo='';
	nOption=1;
	var selInd=0;
	tab=new Array();
	filteredTab = filterMain(strFil, strCat, '', strMode);
	for (i=0;i<filteredTab.length;i++) {
		if(strGradeTempo== '' || strGradeTempo.indexOf(filteredTab[i].grade+";")<0) {
			tab[tab.length]=filteredTab[i].grade;			
			strGradeTempo+=filteredTab[i].grade+";";
		}
	}
	
	tab.sort();
	theForm.grade.length=tab.length;
	for(i=0;i<tab.length;i++) {
		theForm.grade.options[nOption]=new Option(tab[i], tab[i]);
		if(tab[i]==strGrade) selInd = nOption;
		nOption++;
	}
	if (selInd >=0) {
		theForm.grade.selectedIndex = selInd;
	} else {
		theForm.grade.selectedIndex = 0;
	}

    if (flag==false) {
		changeMode(true);
	}
    
}

function changeMode(flag) {
	theForm = document.form1;

	filInd = theForm.filiere.selectedIndex;
	strFil = '';
	if (filInd>=0 && theForm.filiere.length>0) {
		strFil = theForm.filiere[filInd].value;
	}
	catInd = theForm.categorie.selectedIndex;
	
	strCat = '';
	if (catInd>=0 && theForm.categorie.length>0) {
		strCat = theForm.categorie[catInd].value;
	}

	gradeInd = theForm.grade.selectedIndex;
	strGrade = '';
	if (gradeInd>=0 && theForm.grade.length>0) {
		strGrade = theForm.grade[gradeInd].value;
	}

	modeInd = theForm.mode_acces.selectedIndex;

	strMode = '';
	if (modeInd>=0 && theForm.mode_acces.length>0) {
		strMode = theForm.mode_acces[modeInd].value;
	}
	
	
	theForm.mode_acces.options[0]=new Option('Tous les modes','');
	nOption=1;
	strModeTempo='';
	var msg_local = ''
	var selInd=0;
	tab=new Array();
	filteredTab = filterMain(strFil, strCat, strGrade, strMode);
	for (i=0;i<filteredTab.length;i++) {
		if(strModeTempo== '' || strModeTempo.indexOf(filteredTab[i].mode_acces+";")<0) {
			tab[tab.length]=filteredTab[i].mode_acces;			
			strModeTempo+=filteredTab[i].mode_acces+";";
		}
	}
	
	tab.sort();
	theForm.mode_acces.length=tab.length;
	for(i=0;i<tab.length;i++) {
		theForm.mode_acces.options[nOption]=new Option(tab[i], tab[i]);
		if(tab[i]==strMode) selInd = nOption;
		nOption++;
	}
	if (selInd >=0) {
		theForm.mode_acces.selectedIndex = selInd;
	} else {
		theForm.mode_acces.selectedIndex = 0;
	}
	
}


PrepareData();
