function affCache(idDiv) {
	var div = document.getElementById(idDiv);
	if (div.style.display == "")
	div.style.display = "none";
	else
	div.style.display = "";
}

function Affiche(idDiv) {
	var div = document.getElementById(idDiv);
	div.style.display = "";
}

function Cache(idDiv) {
	var div = document.getElementById(idDiv);
	div.style.display = "none";
}

function effaceApp() {
	document.forms["piscine"].pass.value = "";
	document.forms["piscine"].code_apporteur.value = "";
	document.forms["piscine"].nom_cabinet.value = "";
	document.forms["piscine"].civilite_app.value = "";
	document.forms["piscine"].nom_app.value = "";
	document.forms["piscine"].prenom_app.value = "";
	document.forms["piscine"].adresse_app.value = "";
	document.forms["piscine"].cp_app.value = "";
	document.forms["piscine"].ville_app.value = "";
	document.forms["piscine"].telephone_app.value = "";
	document.forms["piscine"].fax_app.value = "";
	document.forms["piscine"].email_app.value = "";
	document.forms["piscine"].type_app.value = "";
	document.forms["piscine"].enseigne.value = "";
}

function verifApp() {
	var xhr_object = null; 
     
	if(window.XMLHttpRequest) // Firefox 
		xhr_object = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
		else { // XMLHttpRequest non supporté par le navigateur 
			alert("APPORTEUR : Votre navigateur ne permet pas d'utiliser le système d'identification. \n Utiliser le navigateur \"Internet Explorer\" ou \"Firefox\"."); 
			return; 
		}
	xhr_object.open("POST","../verifApp.php",true);
	
	xhr_object.onreadystatechange = function() { 
		if(xhr_object.readyState == 4) {
			if(xhr_object.responseText == "FAUX") {
				Cache('pass_app');
				effaceApp();
				document.forms["piscine"].app_assumarisk[0].checked = false;
				alert("APPORTEUR : Vous n'êtes pas un apporteur enregistré de notre réseau.");
			}
			else {
				var res = xhr_object.responseText.split(":");
				document.forms["piscine"].code_apporteur.value = res[0];
				document.forms["piscine"].nom_cabinet.value = res[1];
				document.forms["piscine"].civilite_app.value = res[2];
				document.forms["piscine"].nom_app.value = res[3];
				document.forms["piscine"].prenom_app.value = res[4];
				document.forms["piscine"].adresse_app.value = res[5];
				document.forms["piscine"].cp_app.value = res[6];
				document.forms["piscine"].ville_app.value = res[7];
				document.forms["piscine"].telephone_app.value = res[8];
				document.forms["piscine"].fax_app.value = res[9];
				document.forms["piscine"].email_app.value = res[10];
				document.forms["piscine"].type_app.value = res[11];
				document.forms["piscine"].enseigne.value = res[12];
				Affiche('form_app');
				alert("APPORTEUR : Vous êtes authentifié en tant que " + res[0] + ".");
			}
		}
	}
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	var data = "pass="+document.forms["piscine"].pass.value;
	xhr_object.send(data); 
}

function blockEnter(event) {
	// Compatibilité IE / Firefox
	if(!event&&window.event) {
		event=window.event;
	}
	/*
	// IE
	if(event.keyCode == 13) {
		event.returnValue = false;
		event.cancelBubble = true;
		verifApp();
	}
	*/
	// DOM
	if(event.which == 13) {
		event.preventDefault();
		event.stopPropagation();
		verifApp();
	}
}

function chiffres(event) {
	// Compatibilité IE / Firefox
	if(!event&&window.event) {
		event=window.event;
	}
	// IE
	if(event.keyCode < 48 || event.keyCode > 57) {
		if (event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 37 || event.keyCode == 39) return;
		event.returnValue = false;
		event.cancelBubble = true;
	}
	// DOM
	if(event.which < 48 || event.which > 57) {
		if (event.which == 8 || event.which == 9 || event.which == 37 || event.which == 39) return;
		event.preventDefault();
		event.stopPropagation();
	}
}	

function decimales(event) {
	// Compatibilité IE / Firefox
	if(!event&&window.event) {
		event=window.event;
	}
	// IE
	if(event.keyCode < 48 || event.keyCode > 57) {
		if (event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 37 || event.keyCode == 39) return;
		event.returnValue = false;
		event.cancelBubble = true;
	}
	// DOM
	if(event.which < 48 || event.which > 57) {
		if (event.which == 8 || event.which == 9 || event.which == 37 || event.which == 39) return;
		event.preventDefault();
		event.stopPropagation();
	}
}

function radioCoche(obj) {
	for(i=0;i<obj.length;i++) {
		if(obj[i].checked) {
			return true;
		}
	}
	return false;
}

function check_Form(){
	var mess="";
	var premErreur;
	
	// Vérification contact
	var App = document.forms["piscine"].app_assumarisk;
	var PassApp = document.forms["piscine"].pass.value;
	var CodeApp = document.forms["piscine"].code_apporteur.value;
	var TelApp = document.forms["piscine"].telephone_app.value;
	var EmailApp = document.forms["piscine"].email_app.value;
	var TelRepLeg = document.forms["piscine"].tel_rep_leg.value;
	var EmailRepLeg = document.forms["piscine"].email_rep_leg.value;
	
	if(radioCoche(App)) {
		// apporteur
		if(document.forms["piscine"].app_assumarisk[0].checked == true) {
			// apporteur enregistré
			if(PassApp == "") {
				if(!premErreur) {
					premErreur = document.forms["piscine"].pass;
				}
				mess+= "\n- APPORTEUR : Mot de passe";
			}
			else {
				if(CodeApp == "") verifApp();
			}
		}
		else {
			// apporteur non enregistré
			if(EmailApp) {
				indexAroba = EmailApp.indexOf('@');
				indexPoint = EmailApp.indexOf('.');
				if ((indexAroba < 0) || (indexPoint < 0)) {
					// Dans le cas ou il manque soit '.' soit '@' : modification de la couleur d'arrière plan du champ et message d'alerte
					if(!premErreur) {
						premErreur = document.forms["piscine"].email_app;
					}
					mess+= "\n- APPORTEUR : L'adresse mail est incorrecte (ex : exemple@societe.com)";
				}
			}
		}
	}
	else {
		// client direct
		if(!TelRepLeg && !EmailRepLeg) {
			if(!premErreur) {
				premErreur = document.forms["piscine"].tel_rep_leg;
			}
			mess+= "\n- RESPONSABLE LÉGAL : Indiquez au moins un moyen de vous joindre (Téléphone et/ou Email)";
		}
		else {
			if(EmailRepLeg) {
				indexAroba = EmailRepLeg.indexOf('@');
				indexPoint = EmailRepLeg.indexOf('.');
				if ((indexAroba < 0) || (indexPoint < 0)) {
					// Dans le cas ou il manque soit '.' soit '@' : modification de la couleur d'arrière plan du champ et message d'alerte
					if(!premErreur) {
						premErreur = document.forms["piscine"].email_rep_leg;
					}
					mess+= "\n- RESPONSABLE LÉGAL : L'adresse mail est incorrecte (ex : exemple@societe.com)";
				}
			}
		}
	}

	// Vérification champs "input" non vides
	var ListeChampsInput = new Array('raison_sociale_soc','adresse_soc','cp_soc','ville_soc','date_creation','nom_rep_leg','prenom_rep_leg','adresse_rep_leg','cp_rep_leg','ville_rep_leg','annee_exp_rep_leg','CA_dernier','CA_negoceCEE','CA_negoce_horsCEE','CA_decennale','CA_negoce_soumis');
	var ListeLibInput = new Array('SOCIÉTÉ : Raison Sociale','SOCIÉTÉ : Adresse','SOCIÉTÉ : Code Postal','SOCIÉTÉ : Ville','SOCIÉTÉ : Date de création de l\'affaire','RESPONSABLE LÉGAL : Nom','RESPONSABLE LÉGAL : Prénom','RESPONSABLE LÉGAL : Adresse','RESPONSABLE LÉGAL : Code Postal','RESPONSABLE LÉGAL : Ville','RESPONSABLE LÉGAL : Nombre d\'année d\'expérience dans la profession','ÉLÉMENTS ÉCONOMIQUES : CA Dernier','ÉLÉMENTS ÉCONOMIQUES : CA Négoce CEE','ÉLÉMENTS ÉCONOMIQUES : CA Négoce hors CEE','ÉLÉMENTS ÉCONOMIQUES : CA soumis à décennale','ÉLÉMENTS ÉCONOMIQUES : CA négoce soumis à décennale');	
		
	for(n=0;n<ListeChampsInput.length;n++) {
		Champ=eval("document.piscine." + ListeChampsInput[n]);
		if(!Champ.value) {
			if(!premErreur) {
				premErreur = Champ;
			}
			mess+= "\n- "+ListeLibInput[n];
		}			
	}
	
	// Vérification CA
	var CA_dernier = parseInt(document.forms["piscine"].CA_dernier.value);
	var CA_negoceCEE = parseInt(document.forms["piscine"].CA_negoceCEE.value);
	var CA_negoce_horsCEE = parseInt(document.forms["piscine"].CA_negoce_horsCEE.value);
	var CA_decennale = parseInt(document.forms["piscine"].CA_decennale.value);
	var CA_negoce_soumis = parseInt(document.forms["piscine"].CA_negoce_soumis.value);
		
	var total=CA_negoceCEE+CA_negoce_horsCEE+CA_decennale+CA_negoce_soumis;
	if(total!=CA_dernier || total==0) {
		mess+="\n- ÉLÉMENTS ÉCONOMIQUES : CA dernier doit être égal à CA Négoce CEE + CA Négoce hors CEE + CA Décennale + CA Négoce soumis à décennale et ne doit pas être égal à zéro";
		if(!premErreur) {
			premErreur = document.forms["piscine"].CA_negoceCEE;
		}
	}
	
	// Vérification champs "radio" cochés
	var ListeChampsRadio = new Array('certification','qualification','federation','assureur_RC_produit','assureur_RC_decennale','Preseau');
	var ListeLibRadio = new Array('CONDITION DE SOUSCRIPTION : Certification','CONDITION DE SOUSCRIPTION : Qualification','CONDITION DE SOUSCRIPTION : Fédération','ANTÉCÉDENTS D\'ASSURANCE : Précédent assureur RC exploitation/RC produits livrés','ANTÉCÉDENTS D\'ASSURANCE : Précédent assureur RC décennale','PIECES À JOINDRE AU DOSSIER : Type de piscinier');
	
	for(n=0;n<ListeChampsRadio.length;n++) {
		Champ=eval("document.piscine." + ListeChampsRadio[n]);
		if(radioCoche(Champ) == false) {
			mess+= "\n- "+ListeLibRadio[n];
		}			
	}

	if (mess != "")	{
		alert("Veuillez remplir tous les champs obligatoires : "+"\n"+mess);
		if(premErreur) {
			premErreur.focus();
		}
		return false;
	}
	else return true;
}
