function createXMLHttpRequest(){
 var xmlhttp;

 if (window.XMLHttpRequest) {
   xmlhttp = new XMLHttpRequest();
   // evite un Bug du navigateur Safari :
   if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType("text/xml");
 }
 else if (window.ActiveXObject){
   try {
   	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  	 } 
  catch (e){   
     try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     } catch (e){return false;}
   }
 }
 return xmlhttp; 
}

function rougeBord(id) {
	//alert("border " + id);
	document.getElementById(id).style.borderColor = '#FF0303';
}
function noBord(id) {
	document.getElementById(id).style.borderColor = '';
}

function rougeText(id) {
	//alert("text " + id);
	document.getElementById(id).style.fontSize = '12px';
	document.getElementById(id).style.color = '#FF0303';
}

function vertText(id) {
	//alert("text " + id);
	document.getElementById(id).style.fontSize = '16px';
	document.getElementById(id).style.color = '#12654B';
}

function noColor(id) {
	document.getElementById(id).style.color = '';
}

function writetag(tag, content) {
	//alert(tag);
    document.getElementById(tag).innerHTML = content;
}

function trim(val) {
	var retour = ""
	for(i=0;i < val.length;i++)
		if(val.charAt(i) != " ") {
			retour += val.charAt(i)
		}
	return retour
}

function isNumeric(val) {
  var re = new RegExp("(^[0-9]{1,20}$)")
 // var re2 = new RegExp("(^\/+[0-9]{1,20}$)");
 // if (re.test(val)||re2.test(val)) return true;
  if (re.test(val)) return true;
  else  return false;
}

function writeTagErrorEmail(ret) {
	if (ret == 1 ){
		rougeText('erreurFormLocabri');
		writetag('erreurFormLocabri', '');
		noBord('email');
		noColor('label_email');
		return true;
	}
	else if(ret == 2){
		rougeText('erreurFormLocabri');
		rougeBord('email');
		rougeText('label_email');
		writetag('erreurFormLocabri', '<img src="images/croix_rouge.gif" alt="email exist" /> l\'adresse email existe déjà');
		return false;		
	}
	else {
		rougeText('erreurFormLocabri');
		rougeBord('email');
		rougeText('label_email');
		writetag('erreurFormLocabri', '<img src="images/croix_rouge.gif" alt="Votre adresse email est invalide" /> Votre adresse email est invalide');
		return false;
	}
}

function check_email() {
	if (xmlhttp_check_mail.readyState==4) {	
		if (xmlhttp_check_mail.status==200) {		
			var ret = xmlhttp_check_mail.responseText;
			writeTagErrorEmail(ret);
		} 
	}
}

var xmlhttp_check_mail;

function verifEmail(email, mode) {
	if (email.value.length == 0) {
		writeTagErrorEmail(0)
		return false;
	}
	var modele = /^[a-z0-9\-_\.]+@[a-z0-9.-]{2,}[.][a-z]{2,5}$/i;
	if (!modele.test(email.value)){
		writeTagErrorEmail(0);
		return false;
	}
	//writeTagErrorEmail(2);
	xmlhttp_check_mail = createXMLHttpRequest();
	if (xmlhttp_check_mail!=null) {
		if (mode == true) {
			xmlhttp_check_mail.onreadystatechange = check_email;
		}
		xmlhttp_check_mail.open("GET",'/index.php?p=90&sp=4&email='+escape(email.value)+'&source=LOCABRI-FORMULAIRE',mode);
		xmlhttp_check_mail.send(null);
		if (mode == false) {
			var ret = xmlhttp_check_mail.responseText;
			//writeTagErrorEmail(ret);
			return ret;
		}
	}
}
var xmlhttp_check_newsletter ;

function check_newsletter() {
	var email = document.getElementById("newsletter-email");
	if (email.value.length == 0) {
		alert("Veuillez fournir une addresse email.");
		return false;
	}
	var modele = /^[a-z0-9\-_\.]+@[a-z0-9.-]{2,}[.][a-z]{2,5}$/i;
	if (!modele.test(email.value)){
		alert("Le format de l'email est invalide.");
		return false;
	}
	xmlhttp_check_newsletter = createXMLHttpRequest();
	if (xmlhttp_check_newsletter!=null) {
		xmlhttp_check_newsletter.open("GET",'/index.php?p=80&sp=5&e='+escape(email.value),false);
		xmlhttp_check_newsletter.send(null);
		var ret = xmlhttp_check_newsletter.responseText;
		switch (ret){
			case "0" : 
				alert("Veuillez fournir une addresse email.");
				return false;
			case "1":
				alert("Votre email a été pris en compte.")
				return false;
				break;
			case "2":
				alert("Le format de l'email est invalide.");
				return false;
				break;
			case "3":
				alert("L'email est invalide.");
				return false;
				break;
			case "4":
				alert("Votre email est déjà existant.");
				return false;
				break;
			default : 
				return false;
		}
	}
}

function check_formulaire_locabri() {

	var list_erreur = '';
	var tab_id_error = Array('error_nom', 'error_societe',	'error_cp', 'error_email','error_tel');
	var tab_id_label = Array('label_nom', 'label_societe',	'label_cp', 'label_email','label_tel');
	var tab_id_input = Array('nom',       'societe',      	'cp',       'email',      'tel');
	var no_problem = true;
	
	writetag("erreurFormLocabri","");
	for (var n = 0; n < tab_id_error.length; n++) {
		noBord(tab_id_input[n]);
		noColor(tab_id_label[n]);
	}
	var tagList = "";
	var tagErrorList = "";
	var messageErrorList = "";
	for (var n = 0; n < tab_id_error.length; n++) {
		var input = document.getElementById(tab_id_input[n]);
		var span_error = document.getElementById(tab_id_error[n]);
		switch (input.type){
			case 'text' : 		
				if (trim(input.value) == '') {
					tagList = tagList + (tagList != '' ? ',' : '') + input.id ;
					champ = input.id
					if (champ == 'societe')champ = 'soci&eacute;t&eacute; ';
					if (champ == 'cp')champ = 'code Postal ';
					if (champ == 'tel')champ = 't&eacute;l&eacute;phone ';
					//messageErrorList = messageErrorList + (messageErrorList != '' ? ', ' : '') + champ;
					no_problem = false;
				}else{			
					if (input.name == "tel"){
						if(!isNumeric(input.value)){
							tagList = tagList + (tagList != '' ? ',' : '') + input.id;
							messageErrorList = messageErrorList + (messageErrorList != '' ? ',' : '') + " - Votre num&eacute;ro de t&eacute;l&eacute;phone est invalide ";
							
							no_problem = false;
						}
					}
					if (input.name == "email"){
						result = verifEmail(input,false)
						if (result == 0) {
							tagList = tagList + (tagList != '' ? ',' : '') + input.id;
							messageErrorList = messageErrorList + (messageErrorList != '' ? ',' : '') + " - Votre adresse email est invalide ";
							no_problem = false;
						}
						else if (result == 2) {
							tagList = tagList + (tagList != '' ? ',' : '') + input.id;
							messageErrorList = messageErrorList + (messageErrorList != '' ? ',' : '') + " - l'adresse email existe déjà ";
							no_problem = false;
						}
					}
				} break ;
			/*case 'textarea' :
				if (trim(input.value) == '') {
					tagList = tagList + (tagList != '' ? ',' : '') + input.id ; 
					no_problem = false;
				}
				break;*/ 
			
		}
	}	
	//alert(no_problem);
	if (no_problem == false) {
		list = tagList.split(',');
		//listError = tagErrorList.split(',');
		//listMessage = messageErrorList.split(',');
		
		for(i=0;i<list.length;i++){
			rougeText("label_"+list[i]);
			rougeBord(list[i]);
		}
		
		rougeText("erreurFormLocabri");
		listMessage = tagList.replace('tel','t&eacute;l&eacute;phone');
		listMessage = listMessage.replace('societe','soci&eacute;t&eacute;');
		listMessage = listMessage.replace('cp','code postal');
		listMessage = listMessage.replace(/(,)/g,', ');
		
		writetag("erreurFormLocabri",'Les champs suivants sont obligatoires : <br />'+listMessage+'<br />'+ messageErrorList.replace(/(,)/igm,'<br />') );
		return false;
	}else return true;
	
}

