//=============================================================================
//				S U I V I  D E S  M O D I F I C A T I O N S 
//============================================================================= 

//<script language=javascript>
var glbRegion;
var C_SEP = '£¤£';
var C_SEP_LIST = ':';
var	glbstrNbrMin = "";
var glbstrNbrMax = "";

function InitPage() {
	var arrURL;
	var objForm;
	var blnBidon;

	window.scrollTo(0, gintScrollTop)
	
	//Il faut garder en mémoire tous les choix de la liste déroulante "employe minimum" (elle est dynamique)
	for (i = 0; i < document.MAIN_FORM.cboNbrEmpMin.length; i++) {		
		glbstrNbrMin = glbstrNbrMin + document.MAIN_FORM.cboNbrEmpMin.options[i].text + C_SEP;
		glbstrNbrMin = glbstrNbrMin + document.MAIN_FORM.cboNbrEmpMin.options[i].value;
		if (i != document.MAIN_FORM.cboNbrEmpMin.length - 1) glbstrNbrMin = glbstrNbrMin + C_SEP_LIST;
	}

	//Il faut garder en mémoire tous les choix de la liste déroulante "employe maximum" (elle est dynamique)
	for (i = 0; i < document.MAIN_FORM.cboNbrEmpMax.length; i++) {
		glbstrNbrMax = glbstrNbrMax + document.MAIN_FORM.cboNbrEmpMax.options[i].text + C_SEP;
		glbstrNbrMax = glbstrNbrMax + document.MAIN_FORM.cboNbrEmpMax.options[i].value;
		if (i != document.MAIN_FORM.cboNbrEmpMax.length - 1) glbstrNbrMax = glbstrNbrMax + C_SEP_LIST;
	}

	//Peupler et mettre les bonnes valeurs dans les combos employés min et max
	if (document.MAIN_FORM.cboRechEnt.value == "01" || document.MAIN_FORM.cboRechEnt.value == "04" || document.MAIN_FORM.cboRechEnt.value == "") {
		PeuplerComboMinMax ('cboNbrEmpMax', document.MAIN_FORM.cboNbrEmpMin, document.MAIN_FORM.cboNbrEmpMax);
		PeuplerComboMinMax ('cboNbrEmpMin', document.MAIN_FORM.cboNbrEmpMin, document.MAIN_FORM.cboNbrEmpMax);
	}
	
	//Peupler et mettre la bonne valeur dans le combo sous-secteur
	if (document.MAIN_FORM.cboRechEnt.value == "01" && document.MAIN_FORM.cboSectActv.selectedIndex != 0) {
		if (gstrProvMTG322 == "") {
			blnBidon = blnPeuplerCombo('cboSousSectActv', gstrSect);
			document.MAIN_FORM.cboSousSectActv.value = gstrSousSect;
		}
		else {
			//Si on provient de la page MTG322, on devra peut-être recharger la page...
			ValiderPage("cboSectActv");
		}
	}
	
	//Inscrire la description SCIAN dans l'encadré jaune selon le code SCIAN le plus détaillé
	if (document.MAIN_FORM.cboRechEnt.value == "01") {
		if (gstrClasseActv != "" && gstrClasseActv != " ") {
			document.getElementById("blockDescScian").style.display = 'block';
			document.getElementById("blockDescScian").innerHTML = document.MAIN_FORM.cboClasseActv.options[document.MAIN_FORM.cboClasseActv.selectedIndex].text
		}
		else if (gstrGroupeActv != "" && gstrGroupeActv != " ") {
			document.getElementById("blockDescScian").style.display = 'block';
			document.getElementById("blockDescScian").innerHTML = document.MAIN_FORM.cboGroupeActv.options[document.MAIN_FORM.cboGroupeActv.selectedIndex].text
		}
		else if (gstrSousSect != "" && gstrSousSect != " ") {
			document.getElementById("blockDescScian").style.display = 'block';
			document.getElementById("blockDescScian").innerHTML = document.MAIN_FORM.cboSousSectActv.options[document.MAIN_FORM.cboSousSectActv.selectedIndex].text
		}
		else if (gstrSect != "" && gstrSect != " ") {
			document.getElementById("blockDescScian").style.display = 'block';
			document.getElementById("blockDescScian").innerHTML = document.MAIN_FORM.cboSectActv.options[document.MAIN_FORM.cboSectActv.selectedIndex].text
		}
	}
}


// Cette fonction permet de valider le contenu de la page selon l'action
// portée sur un champ.
function ValiderPage(pstrIdCtrl) {			
	var blnValid;
	var blnAutoSousSect;	

	blnValid = true;
	with (document.MAIN_FORM) {
		switch(pstrIdCtrl) {
			case 'all':

				//Valider si une région à été choisi (pour toute les choix)
				if (blnValid && document.MAIN_FORM.cboregionEnt.selectedIndex == 0) {
					ShowAlert(DC919572, '', document.MAIN_FORM.cboregionEnt.name);
					blnValid = false;
					break;
				}
				if (blnValid && cboregionEnt.value == "ZZ") {
					ShowAlert(DC919578, '', document.MAIN_FORM.cboregionEnt.name);
					blnValid = false;									
				}

				//Validations par type de recherche
				if (blnValid) {
					switch (document.MAIN_FORM.cboRechEnt.options[document.MAIN_FORM.cboRechEnt.selectedIndex].value) {
						case "01": //Recherche par secteur d'Activité
							blnValid = ValiderRechParSect();
							break;

						case "02": //Recherche par mot 
							blnValid = ValiderRechParMot();
							break;

						case "03": //Recherche par Lettre
							//Pas de validation
							break;

						case "04": //Recherche par code SCIAN
							blnValid = ValiderRechParSCIAN();
							break;
					}				
				}

				// Si tout est valide, on change le nom du bouton cliqué car il y en a plusieurs
				// dans la page et ils ont tous des noms différents.
				if (blnValid) {
					// On s'assure d'appeler la page des paramètres de recherche
					document.MAIN_FORM.action = gstrURLPageRech;
					posted.value = "RESULTATRECH";
				}
				break;

			case "cboregionEnt":				
				document.getElementById("blnChanReg").value = "OUI";
				document.getElementById("blockLienMunVide").style.display = "block";
				document.getElementById("blockLienMun").style.display = "none";
				break;
				
			case "cboRechEnt":				
				var objBlockRech1 = document.getElementById("BlockRech1")
				var objBlockRech2 = document.getElementById("BlockRech2")
				var objBlockRech3 = document.getElementById("BlockRech3")
				var objBlockRech4 = document.getElementById("BlockRech4")
				var objBlockRechEmp = document.getElementById("BlockRechEmp")
			
				if (document.MAIN_FORM.cboRechEnt.options[document.MAIN_FORM.cboRechEnt.selectedIndex].value == '')
					{
					objBlockRech1.style.display = "none";
					objBlockRech2.style.display = "none";
					objBlockRech3.style.display = "none";
					objBlockRech4.style.display = "none";					
					objBlockRechEmp.style.display = "block";
					}
			
				if (document.MAIN_FORM.cboRechEnt.options[document.MAIN_FORM.cboRechEnt.selectedIndex].value == '01')
					{
					objBlockRech1.style.display = "block";
					objBlockRech2.style.display = "none";
					objBlockRech3.style.display = "none";
					objBlockRech4.style.display = "none";	
					objBlockRechEmp.style.display = "block";
					}
				if (document.MAIN_FORM.cboRechEnt.options[document.MAIN_FORM.cboRechEnt.selectedIndex].value == '02')
					{
					objBlockRech2.style.display = "block";
					objBlockRech1.style.display = "none";
					objBlockRech3.style.display = "none";
					objBlockRech4.style.display = "none";
					objBlockRechEmp.style.display = "none";
					}
					
				if (document.MAIN_FORM.cboRechEnt.options[document.MAIN_FORM.cboRechEnt.selectedIndex].value == '03')
					{
					objBlockRech3.style.display = "block";
					objBlockRech1.style.display = "none";
					objBlockRech2.style.display = "none";
					objBlockRech4.style.display = "none";
					objBlockRechEmp.style.display = "none";
					}

				if (document.MAIN_FORM.cboRechEnt.options[document.MAIN_FORM.cboRechEnt.selectedIndex].value == '04')
					{
					objBlockRech4.style.display = "block";
					objBlockRech1.style.display = "none";
					objBlockRech2.style.display = "none";
					objBlockRech3.style.display = "none";
					objBlockRechEmp.style.display = "block";
					}
				
				ViderBlock();
				
				break;
				
			// ATTENTION: Il faut garder ce [case "cboSectActv"] juste avant le
			//			  [case "cboSousSectActv":] à cause du [break] qui peut être ignoré
			case "cboSectActv":								
				blnAutoSousSect = false;
				ViderCombo (cboClasseActv, true);
				ViderCombo (cboGroupeActv, true);
				ViderCombo (cboSousSectActv, true);

				if (document.MAIN_FORM.cboSectActv.selectedIndex != 0) {
					//Peupler le combo "sous-secteur SCIAN" avec la valeur du combo "secteur SCIAN"
					blnAutoSousSect = blnPeuplerCombo("cboSousSectActv", document.MAIN_FORM.cboSectActv.options[document.MAIN_FORM.cboSectActv.selectedIndex].value);
					document.getElementById("blockDescScian").style.display = 'block';
					document.getElementById("blockDescScian").innerHTML = document.MAIN_FORM.cboSectActv.options[document.MAIN_FORM.cboSectActv.selectedIndex].text
				}
				else {
					document.getElementById("blockDescScian").style.display = 'none';
					document.getElementById("blockDescScian").innerHTML = "";
				}

				//Si un sous-secteur a été choisi automatiquement dans sa liste déroulante
				//(parce qu'il est le seul dans la liste), on continue le code dans le prochain "case"
				if (!blnAutoSousSect) break;

			// ATTENTION: Il faut garder ce [case "cboSousSectActv"] à la suite du
			//			  [case "cboSectActv":] à cause du [break] précédent qui peut être ignoré
			case "cboSousSectActv":
				ViderCombo (cboClasseActv, true);
				ViderCombo (cboGroupeActv, true);

				if (document.MAIN_FORM.cboSousSectActv.selectedIndex != 0) {
					// Recharge la page pour peupler le reste des listes
					document.getElementById("blockDescScian").style.display = 'block';
					document.getElementById("blockDescScian").innerHTML = document.MAIN_FORM.cboSousSectActv.options[document.MAIN_FORM.cboSousSectActv.selectedIndex].text;
					
					if (document.body.scrollTop == 0)
						document.getElementById("ScrollTop").value = document.documentElement.scrollTop;
					else
						document.getElementById("ScrollTop").value = document.body.scrollTop;

					posted.value = "RECHARGEMENT";

					// On s'assure d'appeler la page des paramètres de recherche
					document.MAIN_FORM.action = gstrURLPageRech;
					submit();
				}
				else {
					document.getElementById("blockDescScian").style.display = 'none';
					document.getElementById("blockDescScian").innerHTML = "";
				}
				break;
				
			case "cboGroupeActv":
			
				ViderCombo (cboClasseActv, true);
				
				if (document.MAIN_FORM.cboGroupeActv.selectedIndex != 0) {
					// Recharge la page pour peupler le reste des listes			
					document.getElementById("blockDescScian").style.display = 'block';						
					document.getElementById("blockDescScian").innerHTML = document.MAIN_FORM.cboGroupeActv.options[document.MAIN_FORM.cboGroupeActv.selectedIndex].text

					if (document.body.scrollTop == 0)
						document.getElementById("ScrollTop").value = document.documentElement.scrollTop;
					else
						document.getElementById("ScrollTop").value = document.body.scrollTop;
					
					posted.value = "RECHARGEMENT";

					// On s'assure d'appeler la page des paramètres de recherche
					document.MAIN_FORM.action = gstrURLPageRech;
					submit();
				}
				else {
					document.getElementById("blockDescScian").style.display = 'none';
					document.getElementById("blockDescScian").innerHTML = "";
				}
				break;
				
			case "cboClasseActv":
				if (document.MAIN_FORM.cboClasseActv.selectedIndex != 0) {
					document.getElementById("blockDescScian").style.display = 'block';
					document.getElementById("blockDescScian").innerHTML = document.MAIN_FORM.cboClasseActv.options[document.MAIN_FORM.cboClasseActv.selectedIndex].text
				}
				else {
					document.getElementById("blockDescScian").style.display = 'none';
					document.getElementById("blockDescScian").innerHTML = "";
				}
				break;
				
			case "cboNbrEmpMax":
			case "cboNbrEmpMin":
				PeuplerComboMinMax ('cboNbrEmpMax', document.MAIN_FORM.cboNbrEmpMin, document.MAIN_FORM.cboNbrEmpMax);
				break;			
		}
	}	
	
	return blnValid;
}


// Le lien "municipalité" a été cliqué, valider avant de poster le formulaire
function ValiderMun() {
	if (document.MAIN_FORM.cboregionEnt.selectedIndex == 0) {
		ShowAlert(DC919572, '', document.MAIN_FORM.cboregionEnt.name);
		blnValid = false;
	}
	else {
		if (document.MAIN_FORM.cboregionEnt.value == "ZZ") {
			ShowAlert(DC919578, '', document.MAIN_FORM.cboregionEnt.name);
			blnValid = false;
		}
		else {
			//On configure le nom du bouton pressé
			document.MAIN_FORM.posted.value = "municipalite";
			
			// On s'assure d'appeler la page des paramètres de recherche
			document.MAIN_FORM.action = gstrURLPageRech;
			document.MAIN_FORM.submit();						 
		}
	} 
}

function ValiderRechParSect()
{
	var blnValide = true;

	//Valider Si un secteru à été choisi
	if (document.MAIN_FORM.cboSectActv.selectedIndex == 0 && document.MAIN_FORM.cboSousSectActv.selectedIndex == 0)
	{
		ShowAlert(DC919573, '', document.MAIN_FORM.cboSectActv.name);
		blnValide = false;
	}
	return blnValide;
}
function ValiderRechParMot()
{

	var blnValide = true;
	var strACorr = document.MAIN_FORM.txtrechent.value;
	var strInfo = fgCorrText(Trim(strACorr));
	var intLongChamp = strInfo.length;	
	var i;
	var c;
	
	//Valider Si à blanc
	if (intLongChamp == 0)
	{
		ShowAlert(DC919567, '', document.MAIN_FORM.txtrechent.name);
		blnValide = false;
	}
	else
	{
		//Valider Si seulement des lettres
		for (i = 0; i < intLongChamp && blnValide; i++) 
		{
			c = strInfo.charAt(i);
			if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z' || c <= ' '))
				continue;
			else
			{
				ShowAlert(DC919568, '', document.MAIN_FORM.txtrechent.name);
				blnValide = false;				
			}
		}
		
		
		if (blnValide)
		{
			var blnUnMotPlus3 = false
			
			var arrInfo = strInfo.split(' ');		
			for (i = 0; i < arrInfo.length; i++)
			{
				if (arrInfo[i].length > 2)
				{
					blnUnMotPlus3 = true;
				}
							
			}
			if (!blnUnMotPlus3)
			{
				ShowAlert(DC919571, '', document.MAIN_FORM.txtrechent.name);
				blnValide = false;			
			}
			
		}
	}
	return blnValide;
}



function ValiderRechParSCIAN()
{

	var blnValide = true;
	var strACorr = document.MAIN_FORM.txtrechscian.value;
	var strInfo = fgCorrText(Trim(strACorr));
	var intLongChamp = strInfo.length;	
	var i;
	var c;
	

	//Valider Si seulement des chiffres
	for (i = 0; i < intLongChamp && blnValide; i++) 
	{
		c = strInfo.charAt(i);
		if (c >= '0' && c <= '9')
			continue;
		else
		{
			ShowAlert(DC919570, '', document.MAIN_FORM.txtrechscian.name);
			blnValide = false;				
		}
	}

	//Valider Si à blanc ou 1 de long
	if (blnValide)
	{
		if (intLongChamp == 0 || intLongChamp == 1)
		{
			ShowAlert(DC919570, '', document.MAIN_FORM.txtrechscian.name);
			blnValide = false;
		}
	}	
	
	return blnValide;
}

function ValiderLettre(strLettre) {
	document.MAIN_FORM.txtLettre.value = strLettre;

	if (ValiderPage('all')) {
		// On s'assure d'appeler la page des paramètres de recherche
		document.MAIN_FORM.action = gstrURLPageRech;
		document.MAIN_FORM.submit();
	}
}

// Fonction pour vider les combobox
//		objCombo = Objet combobox à vider
//		blnGarderPremier = Conserver la première valeur
function ViderCombo(objCombo, blnGarderPremier) {	
	if (objCombo.length == 0) return;
	
	var i = objCombo.length;
	var intFin = 0;
	
	if (blnGarderPremier) intFin = 1;
	
	for (var x = i; x > intFin; x--) {
		objCombo.remove(x - 1);
	}
	
	if (objCombo.length) objCombo.selectedIndex = 0;
}


//Fonction pour peupler le combo "sous-secteurs SCIAN"
//	pstrNomCbo = nom du combo (seulement "cboSousSectActv" supporté pour l'instant)
//	pstrSect = valeur "secteur SCIAN" qui permettra de savoir quelles sous-secteurs insérer dans la liste
//Retourne "true" si un seul sous-secteur est inscrit dans la liste déroulante et qu'il a été choisi automatiquement
function blnPeuplerCombo(pstrNomCbo, pstrSect) {
	var i;
	var blnRetour = false;

	switch(pstrNomCbo) {
		case "cboSousSectActv":
			//Peupler le combo "sous-secteurs SCIAN" (avec la valeur du combo "secteur SCIAN")
			var nbrSousSect
			var arrSousSect = new Array;
			var arrSousSectInd = new Array(3);
	
			arrSousSect = gstrTousSousSect.split(C_SEP_LIST);
			nbrSousSect = arrSousSect.length;

 			//Populer la liste des sous-secteurs
			for (i = 0; i < nbrSousSect; i++) {
				arrSousSectInd = arrSousSect[i].split(C_SEP);
				if (arrSousSectInd[2] == pstrSect)
					document.MAIN_FORM.cboSousSectActv[document.MAIN_FORM.cboSousSectActv.length] = new Option(arrSousSectInd[1], arrSousSectInd[0]);
			}

			//S'il existe un seul sous-secteur, choisir automatiquement ce sous-secteur.
			//On retourne "true" pour spécifier à la fonction appelante qu'un sous-secteur a été
			//choisi automatiquement.
			if (document.MAIN_FORM.cboSousSectActv.length == 2) {
				document.MAIN_FORM.cboSousSectActv.selectedIndex = 1;
				blnRetour = true;
			}
			break;	
	}
	
	return blnRetour;
}

function PeuplerComboMinMax (strNomCbo, cboMin, cboMax) {	

	switch (strNomCbo) {
		case "cboNbrEmpMax":
					
			var nbrElem = glbstrNbrMax.split(C_SEP_LIST).length;	
			var arrElem = new Array(nbrElem);
			var arrElemInd = new Array(2);
			var intSelectdMin;
			var blnInsertOpt = false;
			
					
			arrElem = glbstrNbrMax.split(C_SEP_LIST);

			intSelectdMin = cboMin.options[cboMin.selectedIndex].value;

			if (intSelectdMin == "") intSelectdMin = 0;

			parseInt(intSelectdMin);
	
			// Populer la liste du nombre d'employé MAX		
			// remetrre le combo en état initiale sans vider le combo
			for (i = 0; i < nbrElem; i++) {	
				arrElemInd = arrElem[i].split(C_SEP);
				blnInsertOpt = false;
				
				if (cboMax.options[i] == null)
					blnInsertOpt = true;
				else {
					if (cboMax.options[i].value != arrElemInd[1])
						blnInsertOpt = true;
				}

				if (blnInsertOpt) {
					var elOptNew = document.createElement('option');
					elOptNew.text = arrElemInd[0];
					elOptNew.value = arrElemInd[1];
					cboMax.add(elOptNew, i);
				}
			}

			//Épurer les comb NbrEmplMax selon la valeur du comb NbrEmplMin
			for (i = nbrElem - 1; i > -1; i--) {				
				if (parseInt(cboMax.options[i].value) <= intSelectdMin) {				
					cboMax.remove(i);
				}
			}		
			break;
			
		case "cboNbrEmpMin":
			
			var nbrElem = glbstrNbrMin.split(C_SEP_LIST).length;
			var arrElem = new Array(nbrElem);
			var arrElemInd = new Array(2);
			var intSelectdMax;
			var blnInsertOpt = false;
			
			arrElem = glbstrNbrMin.split(C_SEP_LIST);

			intSelectdMax = cboMax.options[cboMax.selectedIndex].value;
			
			if (intSelectdMax == "") intSelectdMax = 0;

			parseInt(intSelectdMax);
	
			// Populer la liste du nombre d'employé MIN
			// remettre le combo en état initiale sans vider le combo
			for (i = 0; i < nbrElem; i++) {				
				arrElemInd = arrElem[i].split(C_SEP);
				blnInsertOpt = false;
				
				if (cboMin.options[i] == null)
					blnInsertOpt = true;
				else {
					if (cboMin.options[i].value != arrElemInd[1])
						blnInsertOpt = true;
				}

				if (blnInsertOpt) {				
					var objOptNew = document.createElement('option');
					objOptNew.text = arrElemInd[0];
					objOptNew.value = arrElemInd[1];
					cboMin.add(objOptNew, i);
				}
			}

			//
			//Épurer les comb NbrEmplMin selon la valeur du comb NbrEmplMax
			//
			
			for (i = nbrElem - 1; i > 0; i--) {			
				if (parseInt(cboMin.options[i].value) >= intSelectdMax && intSelectdMax != 0)
					cboMin.remove(i);
			}		
			break;
	}
}


//Vider chacun des block de recherche selon l'option de recherche choisi
function ViderBlock() {
	var objFormDoc = document.MAIN_FORM;
	
	objFormDoc.cboSectActv.value = " ";
	ViderCombo (objFormDoc.cboSousSectActv, true);
	ViderCombo (objFormDoc.cboGroupeActv, true);
	ViderCombo (objFormDoc.cboClasseActv, true);
	objFormDoc.cboNbrEmpMin.selectedIndex = 0;
	objFormDoc.txtrechent.value = "";
	objFormDoc.cboNbrEmpMax.value = "9999999999999";
	objFormDoc.txtrechscian.value = "";	
	objFormDoc.txtLettre.value = "";
	PeuplerComboMinMax ('cboNbrEmpMax', objFormDoc.cboNbrEmpMin, objFormDoc.cboNbrEmpMax);
	PeuplerComboMinMax ('cboNbrEmpMin', objFormDoc.cboNbrEmpMin, objFormDoc.cboNbrEmpMax);
	document.getElementById("blockDescScian").style.display = 'none';
}


function cboOnFocusMouseOver(objCbo, i) {
	if (objCbo.selectedIndex != 0) {
		document.getElementById("blockDescScian").style.display = 'block';
		document.getElementById("blockDescScian").innerHTML = objCbo.options[objCbo.selectedIndex].text
	}
}




//</script>