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["fabricant"].pass.value = "";
	document.forms["fabricant"].code_apporteur.value = "";
	document.forms["fabricant"].nom_cabinet.value = "";
	document.forms["fabricant"].civilite_app.value = "";
	document.forms["fabricant"].nom_app.value = "";
	document.forms["fabricant"].prenom_app.value = "";
	document.forms["fabricant"].adresse_app.value = "";
	document.forms["fabricant"].cp_app.value = "";
	document.forms["fabricant"].ville_app.value = "";
	document.forms["fabricant"].telephone_app.value = "";
	document.forms["fabricant"].fax_app.value = "";
	document.forms["fabricant"].email_app.value = "";
	document.forms["fabricant"].type_app.value = "";
	document.forms["fabricant"].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["fabricant"].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["fabricant"].code_apporteur.value = res[0];
					document.forms["fabricant"].nom_cabinet.value = res[1];
					document.forms["fabricant"].civilite_app.value = res[2];
					document.forms["fabricant"].nom_app.value = res[3];
					document.forms["fabricant"].prenom_app.value = res[4];
					document.forms["fabricant"].adresse_app.value = res[5];
					document.forms["fabricant"].cp_app.value = res[6];
					document.forms["fabricant"].ville_app.value = res[7];
					document.forms["fabricant"].telephone_app.value = res[8];
					document.forms["fabricant"].fax_app.value = res[9];
					document.forms["fabricant"].email_app.value = res[10];
					document.forms["fabricant"].type_app.value = res[11];
					document.forms["fabricant"].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["fabricant"].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 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["fabricant"].app_assumarisk;
	var PassApp = document.forms["fabricant"].pass.value;
	var CodeApp = document.forms["fabricant"].code_apporteur.value;
	var TelApp = document.forms["fabricant"].telephone_app.value;
	var EmailApp = document.forms["fabricant"].email_app.value;
	var TelRepLeg = document.forms["fabricant"].tel_rep_leg.value;
	var EmailRepLeg = document.forms["fabricant"].email_rep_leg.value;
	
	if(radioCoche(App)) {
		// apporteur
		if(document.forms["fabricant"].app_assumarisk[0].checked == true) {
			// apporteur enregistré
			if(PassApp == "") {
				if(!premErreur) {
					premErreur = document.forms["fabricant"].pass;
				}
				mess+= "\n- APPORTEUR : Mot de passe";
			}
			else {
				if(CodeApp == "") verifApp();
			}
		}
		else {
			// apporteur non enregistré
			if(!TelApp && !EmailApp) {
				if(!premErreur) {
					premErreur = document.forms["fabricant"].telephone_app;
				}
				mess+= "\n- APPORTEUR : Indiquez au moins un moyen de vous joindre (Téléphone et/ou Email)";
			}
			else {
				if(EmailApp) {
					indexAroba = EmailApp.indexOf('@');
					indexPoint = EmailApp.indexOf('.');
					if ((indexAroba < 0) || (indexPoint < 0)) {
						// Dans le cas ou il manque soit '.' soit '@'
						if(!premErreur) {
							premErreur = document.forms["fabricant"].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["fabricant"].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["fabricant"].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','act_declar','CA_neg_n','CA_fab_n');
	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','RENSEIGNEMENTS RELATIFS AUX MATÉRIAUX DE CONSTRUCTION VENDUS : Activité','RENSEIGNEMENTS RELATIFS AUX MATÉRIAUX DE CONSTRUCTION VENDUS : Chiffre d\'affaires total','RENSEIGNEMENTS RELATIFS AUX MATÉRIAUX DE CONSTRUCTION VENDUS : CA Négociant Exercice en cours (mettre 0 si vous n\'avez pas d\'activité de négociant)','RENSEIGNEMENTS RELATIFS AUX MATÉRIAUX DE CONSTRUCTION VENDUS : CA Fabricant Année N (mettre 0 si vous n\'avez pas d\'activité de fabricant)');	
	
	for(n=0;n<ListeChampsInput.length;n++) {
		Champ=eval("document.fabricant." + ListeChampsInput[n]);
		if(!Champ.value) {
			if(!premErreur) {
				premErreur = Champ;
			}
			mess+= "\n- "+ListeLibInput[n];
		}			
	}
	
	// Vérification CA
	var CA_neg_n = parseInt(document.fabricant.CA_neg_n.value);
	var CA_fab_n = parseInt(document.fabricant.CA_fab_n.value);
	var CA_total = parseInt(document.fabricant.CA_total.value);
	var total = CA_neg_n + CA_fab_n;
	if(total == 0) {
		mess+="\n- CHIFFRE D'AFFAIRES : CA Fabricant Année N + CA Négociant Année N ne doit pas être égale à zéro";
		if(!premErreur) {
			premErreur = document.fabricant.CA_neg_n;
		}
	}
	else {
		if(total > CA_total) {
			mess+="\n- CHIFFRE D'AFFAIRES : CA Fabricant Année N + CA Négociant Année N ne doit pas être supérieure à CA Total";
			if(!premErreur) {
				premErreur = document.fabricant.CA_total;
			}
		}
	}
	
	if (mess != "")	{
		alert("Veuillez remplir tous les champs obligatoires : "+"\n"+mess);
		premErreur.focus();
		return false;
	}
	else return true;
}
