/* modifSQLI - 200700703 = mise à jour CI-WEB 2G */

// Actions des champs textes
function txt_keyup(ev)	{
	Obj = (BrowserDetect.browser == "Explorer") ? event.srcElement : this;
	
	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version == 6) {
    	if (!Obj.isKeyup) {
            Obj.isKeyup = true;
        } else {
            Obj.isKeyup = false;
        }
    } else {
        Obj.isKeyup = true;
    }

	if (Obj.isKeyup) {
        RgxAlert = new RegExp("^alerte");
    	Alert = Obj.parentNode.parentNode;
    	
    	while(Alert.previousSibling) {
    		Alert = Alert.previousSibling;
    		if(RgxAlert.test(Alert.className) == true && Obj.value.length < 56) {
    			Alert.style.display = "none";
    			break;
    		}
    	}
    	
    	var AlertGalPb;
    	var indexF = 0;
    	switch (Obj.form.id) {
    		case "formAccueil" :
    			AlertGalPb = getElementsByClassName(document, "div", "al_pb_general");		
    			break;
    		case "formHaut" :
    			AlertGalPb = getElementsByClassName(document, "div", "al_pbi_general");		
    			break;
    		default :
    			AlertGalPb = getElementsByClassName(document, "div", "al_pbi_general");
    			indexF = 1;
    	}
    	
    	if (AlertGalPb.length > 0) {
    		AlertGalPb[indexF].style.display = 'none';
    	}
    	
    	
    	// On cache "à la main" le div d'alerte 55 caractères pour le champ localité.
    	// En effet, le traitement précédent ne détecte que le div "al_pb_localite"
    	AlertTailleLoc = getElementsByClassName(document, "div", "al_pb_55carac_localite");
    	if (Obj.form.id != "formAccueil") {
    		AlertTailleLoc = getElementsByClassName(document, "div", "al_pbi_55carac_localite");
    	}
    	form = Obj.form;
    	if (form.id != "AnnonceurForm") {
    	    if (AlertTailleLoc.length > 0 && form.localite.value.length < 56) {
    	        AlertTailleLoc[indexF].style.display = 'none';
    	    }
    	} else if (AlertTailleLoc.length > 0 && form.libelleLieu.value.length < 56) {
    	    AlertTailleLoc[indexF].style.display = 'none';
    	}
	
	}
}
/* fin modifSQLI - 200700703 */

/* Modif 20070529 */
function txt_keyup_next(ev)	{
	Obj = (BrowserDetect.browser == "Explorer") ? event.srcElement : this;
	RgxAlert = new RegExp("^alerte");
	Alert = Obj.parentNode.parentNode;
	while(Alert.nextSibling) {
		Alert = Alert.nextSibling;
		if(RgxAlert.test(Alert.className) == true) {
			Alert.style.display = "none";
			break;
		}
	}
}
/* /Modif 20070529 */
	
// Actions du formulaire express
function frm_exp_submit()	{
	boite_close_all();
	Ok = true;
	Ipt = this.getElementsByTagName("input");
	for(i=0; i<Ipt.length; i++) {
		// Bloquage du formulaire si un champs obligatoire est vide
		if( (Ipt[i].name == "quoiqui") || (Ipt[i].name == "ou") || (Ipt[i].name == "quoiquih") || (Ipt[i].name == "ouh") || (Ipt[i].name == "quoiquib") || (Ipt[i].name == "oub") )	{
			
			// Expressions régulières
			RgxAlert = new RegExp("^alerte");
			RgxBtddg = new RegExp("^boite ambiguite");
			RgxGuide = new RegExp("^boite guide");
				
			// Affichage des alertes si un champs obligatoire est vide
			if(Ipt[i].value == "") {
				Alert = Ipt[i].parentNode.parentNode;
				while(Alert.previousSibling) {
					Alert = Alert.previousSibling;
					if(RgxAlert.test(Alert.className) == true) {
						Alert.style.display = "block";
						break;
					}
					if(Alert.nodeName.toLowerCase() == "div") {
						break;
					}
				}
				// Return du formulaire
				Ok = false;
			}
			
			// Affichage des ambiguités si le champ "Où" est rempli
			else if(((Ipt[i].name == "ou") || (Ipt[i].name == "ouh") || (Ipt[i].name == "oub"))
					&& is_chaine_vide(Ipt[i].value)) {
				Btddg = Ipt[i].parentNode.parentNode;
				while(Btddg.nextSibling) {
					Btddg = Btddg.nextSibling;
					if(RgxBtddg.test(Btddg.className) == true) {
						Btddg.style.display = "block";
						break;
					}
					if( (Btddg.nodeName.toLowerCase() == "div") && (RgxGuide.test(Btddg.className) == false) ) {
						break;
					}
				}
				// Return du formulaire
				Ok = false;
			}
			
		} // Fin if champs obligatoire	
		
	} // Fin for boucle sur les champs	
	
	return Ok;
} // Fin function

/* modifSQLI - 200700703 = mise à jour CI-WEB 2G */
// Actions du formulaire détaillé
function frm_det_submit()	{
	boite_close_all();
	Ok = true;

	localite = this.localiteLibelle;
	departement = this.depRegLibelle;
	adresse = this.voieLibelle;
	// Expressions régulières
	RgxAlert = new RegExp("^alerte");

	// Bloquage du formulaire si un champs obligatoire est vide
	if (is_champ_vide(this)) {
		Ok = false;
		if(	is_chaine_vide(localite.value) &&(is_chaine_vide(departement.value)
				|| !is_chaine_vide(adresse))) {
			if(this.nom && this.nom.focus ) { this.nom.blur(); }
			// Affichage des alertes si un champs obligatoire est vide
			Alert = localite.parentNode.parentNode;
			while(Alert.previousSibling) {
				Alert = Alert.previousSibling;
				if(RgxAlert.test(Alert.className) == true) {
					Alert.style.display = "block";
					break;
				}
				if(Alert.nodeName.toLowerCase() == "div") {
					break;
				}
			}
		}
	} // Fin if champs obligatoire	
	
	// On recupere la liste des alertes pour les champs dont la taille est supérieur à la limite
	AlertTaille = is_erreur_taille(this);
	var index = 0;
	if (this.id == 'formBas') {
		index = 1;
	}
	if (AlertTaille != null && AlertTaille.length > 0) {
		// On parcourt la liste, on affiche et on envoie les stats
		for(i=0; i < AlertTaille.length; i++) {
			myAlert = AlertTaille[i];
			var monClassName = myAlert[index].className;
			var stat = "";
			if (monClassName == "alerte al_pb_55carac_adresse" || monClassName == "alerte al_pbi_55carac_adresse") {
				stat = '<img ALT="" src="http:\/\/e.pagesjaunes.fr\/m\/web\/226026167793?g=231031167783&&p=PJ-FR-PB-ALERT_GEO_ADR&c=PB&st=0&sjs=0" border=0 width=1 height=1>';
			} else if (monClassName == "alerte al_pb_55carac_localite" || monClassName == "alerte al_pbi_55carac_localite") {
				stat = '<img ALT="" src="http:\/\/e.pagesjaunes.fr\/m\/web\/226026167793?g=231031167783&&p=PJ-FR-PB-ALERT_GEO_LOC&c=PB&st=0&sjs=0" border=0 width=1 height=1>';
			} else if (monClassName == "alerte al_pb_55carac_departement" || monClassName == "alerte al_pbi_dept") {
				stat = '<img ALT="" src="http:\/\/e.pagesjaunes.fr\/m\/web\/226026167793?g=231031167783&&p=PJ-FR-PB-ALERT_GEO_DEPT&c=PB&st=0&sjs=0" border=0 width=1 height=1>';
			}
			if (myAlert[index].innerHTML.match('e.pagesjaunes.fr') == null) {
				myAlert[index].innerHTML = myAlert[index].innerHTML + stat;				
			} else {
				//myAlert[index].innerHTML = myAlert[index].innerHTML.substring(1, myAlert[index].innerHTML.length - stat.length - 26) + stat;
				// bug #0009596: Formulaire PB (HP ou LR) -> clics répétés sur le bouton trouver avec saisie de + de 55 caractères
				myAlert[index].innerHTML = myAlert[index].innerHTML.substring(0, myAlert[index].innerHTML.length - stat.length - 26) + stat;
			}
			myAlert[index].style.display = "block";
		}
		Ok = false;
	} else {
		// Affichage de l'erreur générale s'il y a au moins une erreur
		AlertGal = getElementsByClassName(document, "div", "al_general");
		AlertGalPb = getElementsByClassName(document, "div", "al_pb_general");
		AlertGalPbi = getElementsByClassName(document, "div", "al_pbi_general");
		AlertGalPbiB = getElementsByClassName(document, "div", "al_pbib_general");
		
		if(Ok == false) {
			if(AlertGal.length > 0) {AlertGal[index].style.display = "block";}
			if(AlertGalPb.length > 0) {AlertGalPb[index].style.display = "block";}
			if(AlertGalPbi.length > 0) {AlertGalPbi[index].style.display = "block";}
			if(AlertGalPbiB.length > 0) {AlertGalPbiB[index].style.display = "block";}
		}
		else {
			if(AlertGal.length > 0) {AlertGal[index].style.display = "none";}
			if(AlertGalPb.length > 0) {AlertGalPb[index].style.display = "none";}
			if(AlertGalPbi.length > 0) {AlertGalPbi[index].style.display = "none";}
			if(AlertGalPbiB.length > 0) {AlertGalPbiB[index].style.display = "none";}
		}
	}
	return Ok;
} // Fin function
/* modifSQLI - 200700703 */

// Actions des boutons d'aide "?"
function hlp_click(ev){
	boite_close_all();
	Obj = check_event(this);
	RgxGde = new RegExp(" guide");
	Gde = Obj;
	while(Gde.parentNode) {
		Gde = Gde.parentNode;
		if( (Gde.nodeName.toLowerCase() == "div") && (Gde.className.toLowerCase().substring(0,6) == "sclear") ) {
			break;
		}
	}
	while(Gde.nextSibling) {
		Gde = Gde.nextSibling;
		if(RgxGde.test(Gde.className) == true) {
			Gde.style.display = "block";
			break;
		}
	}
}

/* modifSQLI - 20070703 = mise à jour CI-WEB 2G */
// Détecte si le formulaire n'est pas correctement renseigné
function is_champ_vide(form) {
	nom = form.nom;
	prenom = form.prenom;
	adresse = form.adresse;
	localite = form.localite;
	departement = form.departementRegion;

	vide = false;
	if (is_chaine_vide(localite.value)) {
		if (is_chaine_vide(departement.value) || !is_chaine_vide(adresse.value)) {
			vide = true;
		}
		if (vide == false && is_chaine_vide(nom.value)) {
			vide = true;
		}
	} else {
		if (is_chaine_vide(nom.value)) {
			if (!is_chaine_vide(prenom.value) || is_chaine_vide(adresse.value)) {
			vide = true;
		}
	}
	}

	return vide;
}

// Vérifie si une chaine de caractère est vide ou contient que des espace
function is_chaine_vide(x) {
    var i = 0;
    var vide = false;
    while (i<x.length && x.charAt(i)==' ')  i++; 
    if (i==x.length) vide = true; 
	return vide;
}

// Détecte si l'un des champs contient une valeur dont la taille est supérieur à 55 caractères
function is_erreur_taille(form) {

    nom = form.nom;
    prenom = form.prenom;
    adresse = form.adresse;
    localite = form.localite;
    departement = form.departementRegion;

	var AlertTaille = new Array;
	var indice = 0;
	if (nom.value.length > 55) {
		AlertTaille[indice] = getElementsByClassName(document, "div", "al_pb_55carac_nom");
		if (form.id != "formAccueil") {
			AlertTaille[indice] = getElementsByClassName(document, "div", "al_pbi_55carac_nom");
		}
		indice++;
	}
	if (prenom.value.length > 55) {	
		AlertTaille[indice] = getElementsByClassName(document, "div", "al_pb_55carac_prenom");
		if (form.id != "formAccueil") {
			AlertTaille[indice]= getElementsByClassName(document, "div", "al_pbi_55carac_prenom");
		}
		indice++;		
	}		
	if (adresse.value.length > 55) {
		AlertTaille[indice] = getElementsByClassName(document, "div", "al_pb_55carac_adresse");
		if (form.id != "formAccueil") {
			AlertTaille[indice] = getElementsByClassName(document, "div", "al_pbi_55carac_adresse");
		}
		indice++;		
	}
	if (localite.value.length > 55) {
		AlertTaille[indice] = getElementsByClassName(document, "div", "al_pb_55carac_localite");
		AlertLocVide = getElementsByClassName(document, "div", "al_pb_localite");
		i = 0;
		if (form.id != "formAccueil") {
			AlertLocVide = getElementsByClassName(document, "div", "al_pbi_localite");
			AlertTaille[indice] = getElementsByClassName(document, "div", "al_pbi_55carac_localite");
			if (form.id == "formBas") {
				i = 1;
			}
		}
		AlertLocVide[i].style.display = "none";
		indice++;		
	}
	if (departement.value.length > 55) {
		AlertTaille[indice] = getElementsByClassName(document, "div", "al_pb_55carac_departement");
		if (form.id != "formAccueil") {
			AlertTaille[indice] = getElementsByClassName(document, "div", "al_pbi_dept");
		}
		indice++;		
	}	
	
	return AlertTaille;
}

// Levée d'ambiguité
function select_ambiguite(code, libelle, champ) {

    var inputCode = document.getElementsByName("codeLieu");
	var inputLibelle = document.getElementsByName("localiteLibelle");
	
    if(champ == "DEPARTEMENT") {
		inputLibelle = document.getElementsByName("depRegLibelle");
	} else if(champ == "REGION") {
		inputLibelle = document.getElementsByName("depRegLibelle");
	}

	inputLibelle[0].value = libelle;
	inputCode[0].value = code;
	
	boite_close_all();
	
	formulaire = getElementsByClassName(document, "form", "N2_formulaire_detaille");
	formulaire[0].submit();
}

function select_ambiguite_voie(code, libelle, numero) {
	if (numero != "") {
		document.getElementsByName("voieLibelle")[0].value = numero + " " + libelle;	
	}else {
	document.getElementsByName("voieLibelle")[0].value = libelle;
	}	
	document.getElementsByName("codeLieu")[0].value = code;
	document.getElementsByName("numeroVoie")[0].value = numero;
	
	boite_close_all();
	
	formulaire = getElementsByClassName(document, "form", "N2_formulaire_detaille");
	formulaire[0].submit();	
}

function relance_recherche(relance) {
	formulaire = getElementsByClassName(document, "form", "N2_formulaire_detaille");
	console.debug("formulaire",formulaire);
	if(relance == "SANS_VOIE"){
		formulaire[0].adresse.value = "";
	} else if(relance == "SANS_LOCALITE"){
		formulaire[0].localite.value = "";
	} else {
		formulaire[0].departementRegion.value = "";
	}
	formulaire[0].submit();
}

// Formulaires express / détaillé
function PJ_init_forms( iScan ){
    if( bINITFORMS == false ){
    	var maxScan = 10;// nbre max de recherche
    	var lvScan = (typeof(iScan) != 'undefined' ) ? iScan : 0;// compteur de recherche
    	
    	PJ_init_boites();
    
    	// Actions des champs textes
    	Txts = getElementsByClassName(document, "input", "N4_form_txt");
    	for(i=0; i<Txts.length; i++) {
   		  addEvent(Txts[i], "keyup", txt_keyup, true);
    	}
    	
		// Formulaire Express - Gestion des boîtes de dialogues
		FrmsExp = getElementsByClassName(document, "form", "N2_formulaire_express");
		if(FrmsExp.length > 0){
			for(i=0; i<FrmsExp.length; i++){
				if(typeof(FrmsExp[i].onsubmit) != 'function'){
					FrmsExp[i].onsubmit = frm_exp_submit;
				}
			}
		}
		
    	// Formulaire Détaillé - Gestion des boîtes de dialogues
    	FrmsDet = getElementsByClassName(document, "form", "N2_formulaire_detaille");
    	if(FrmsDet.length > 0) {
    		for(i=0; i<FrmsDet.length; i++) {
    			FrmsDet[i].onsubmit = frm_det_submit;
    		}
    	}
    	
    	// Rechargement de la fonction si les formulaires ne sont pas chargés.
    	//if( (FrmsExp.length <= 0) && (FrmsDet.length <= 0) && (lvScan < maxScan )){
    	if( (FrmsDet.length <= 0) && (lvScan < maxScan )){
    		window.setTimeout( 
    			function(){
    				PJ_init_forms(lvScan)
    			},
    			200
    		);
    	} else {
            bINITFORMS = true;
        }
    }
    addEvent(window, "load", function(){ PJ_init_forms(maxScan) }, false);
}

// Ce script de tag nécessite une div d'id "TagStats" dans la page appelante pour fonctionner.
// ex : <div id="TagStats" style="display:none;"></div>
function tagStat(tag){
var maDivStat=document.getElementById("TagStats");
codeStat='<img ALT="" src="http:\/\/e.pagesjaunes.fr\/m\/web\/226026167793?g=231031167783&&p='+tag+'&c=PB&st=0&sjs=0" border=0 width=1 height=1>';
	maDivStat.innerHTML = codeStat;				
}

// Vérification du nom et du prénom du formulaire vers quidonc
function check_nfr(IdObj, IdAlerteNomVide, IdAlerteNomLong, IdAlertePrenomLong) {
	var obj = document.getElementById(IdObj);
	var nom = rmallsp(obj.quidonc_nom.value);
	var prenom = rmallsp(obj.quidonc_prenom.value);
    var i = 0;
    var correct = true;
    while (i < nom.length && nom.charAt(i)==' ') {
    	i++;
   	}
	if (i == nom.length) {
		obj.quidonc_nom.blur();
		show(IdAlerteNomVide);
		correct = false;
	}
	if (nom.length > 55) {
		obj.quidonc_nom.blur();
		show(IdAlerteNomLong);
		correct = false;
	}
	if (prenom.length > 55) {
		obj.quidonc_nom.blur();
		show(IdAlertePrenomLong);
		correct = false;
	}
	return correct;
}

// Suppression des espaces
function rmallsp(x) {
	var i = 0;
	var c='';
	for (i = 0; i<x.length; i++){
		if (x.charAt(i)!=' ')
		c = c + x.charAt(i)
	}
	return c
}

//-------------------------------------------

// Initialisation des evts forms
PJ_init_forms();

