// SI LA FONCTION GETELEMENTBYID N'EXISTE PAS
if(!document.getElementById) {
	if(document.all) {
		document.getElementById=function() {
			if(typeof document.all[arguments[0]]!="undefined") { return document.all[arguments[0]] }
			else { return null }
		}
	}
	else if(document.layers) {
		document.getElementById=function() {
			if(typeof document[arguments[0]]!="undefined") { return document[arguments[0]] }
			else { return null }
		}
	}
}

// VERIFICATION CONFORMITE D'UN EMAIL
function checkMail(x) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}

// VERIFICATION QUE LA VARIABLE EST UN CHIFFRE
function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) {
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

// VERIFICATION QUE LA VARIABLE EST UN TEXTE SANS CHIFFRE
function IsText(sText) {
	var ValidChars2 = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ-éôûàçâ";
	var IsT=true;
	var Char;
	for (i = 0; i < sText.length && IsT == true; i++) {
		Char = sText.charAt(i);
		if (ValidChars2.indexOf(Char) == -1) {
			IsT = false;
		}
	}
	return IsT;
}

// GESTION DU FORMULAIRE DU PANIER
function champ_vide(inp,def){
	if(inp.value==def){
		inp.value="";
	}
	else if(inp.value==""){
		inp.value=def;
	}
}

// VERIFICATION A LA SAISIE D'UN NOMBRE
function verif_nombre(champ) {
	var chiffres=new RegExp("[0-9]");
	var verif;
	for (x=0;x<champ.value.length;x++) {
		verif=chiffres.test(champ.value.charAt(x));
		if (verif==false) {
			champ.value = champ.value.substr(0,x)+champ.value.substr(x+1,champ.value.length-x+1);
			x--;
		}
	}
}

// GESTION DU CHARGEMENT ET DECHARGEMENT DU LOADING
function chargement(){
	document.getElementById('chargement').style.display='block';
	document.getElementById('loading').style.display='block';
}
function dechargement(){
	document.getElementById('chargement').style.display='none';
	document.getElementById('loading').style.display='none';
}
function dechargement_tempo_off(){
	document.getElementById('chargement').style.display='none';
	document.getElementById('loading').style.display='none';
}
function dechargement_tempo(tempo){
	window.setTimeout("dechargement_tempo_off()",tempo);
}

// FONCTION AJAX
var xhr = null;
function getXhr(){
	if(window.XMLHttpRequest) xhr = new XMLHttpRequest();
	else if(window.ActiveXObject) {
		try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
	}
	else {
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		xhr = false;
	}
}

// LIENS DU MENU
function colorer_lien(nom_id,nb,couleur,type){
	if(type==1) {
		var style=couleur;
		var color='#FFFFFF';
	}
	else {
		var style='';
		var color='';
	}
	for(i=0; i<nb; i++) {
		document.getElementById(nom_id+'_'+i).style.background=style;
		document.getElementById(nom_id+'_'+i+'a').style.color=color;
	}
	document.getElementById(nom_id+'_'+nb).style.background=style;
	document.getElementById(nom_id+'_'+nb+'a').style.color=color;
}

// LIENS DU SOUS-MENU
function efface_ss_menu(nom,id,type){
	if(type==0) {
		document.getElementById('ssmenu'+id).style.display='none';
		document.getElementById('li_'+nom).style.width='375px';
		document.getElementById('li_'+nom).style.background='';
	}
}
function affiche_ss_menu(nom,id,type){
	if(type==1) {
		document.getElementById('ssmenu'+id).style.display='block';
		document.getElementById('li_'+nom).style.width='853px';
		document.getElementById('li_'+nom).style.display='block';
		document.getElementById('li_'+nom).style.background='url("./image/site/spacer_ie6.gif") repeat top left';
	}
	else {
		//window.setTimeout('efface_ss_menu("'+nom+'","'+id+'",'+type+')',800);
		//new Effect.Opacity('ssmenu'+id, { from: 1.0, to: 0.0, duration: 3.0 });
		document.getElementById('ssmenu'+id).style.display='none';
		document.getElementById('li_'+nom).style.width='375px';
		document.getElementById('li_'+nom).style.background='';
	}
}

// POPUP POUR L'IMPRESSION
function popup(url,x,y,scrl) {
	SH   = screen.height;
	SW   = screen.width;
	TheX = (SW - x)/2;
	TheY = (SH - y)/2;
	window.open(url,"","width="+x+",height="+y+",left="+TheX+",top="+TheY+",location=no,hotkeys=no,directories=no,menubar=no,scrollbars="+scrl+",toolbar=no,status=no, dependant=yes, resizable=yes");
}
function imprimer() {
	popup("plan",640,400,"yes");
}

// ENVOI FORMULAIRE CONATCT
function envoi_formulaire(){
	chargement();
	var erreur = "";
	var soc=document.getElementById("soc").value;
	var act=document.getElementById("act").value;
	var nom=document.getElementById("nom").value;
	var ema=document.getElementById("ema").value;
	var tel=document.getElementById("tel").value;
	var fax=document.getElementById("fax").value;
	var que=document.getElementById("que").value;
	if(soc=="") { erreur += 'Veuillez saisir le nom de votre société SVP.\n'; }
	if(nom=="") { erreur += 'Veuillez saisir votre nom et / ou prénom SVP.\n'; }
	if(ema=="") { erreur += 'Remplissez le champ email avec une adresse valide SVP.\n'; }
	else if(!checkMail(ema)) { erreur += 'Votre adresse email n\'est pas valide, veuillez verifier SVP.\n'; }
	if(tel=="") { erreur += 'Veuillez saisir votre numéro de téléphone SVP.\n'; }
	else if(!IsNumeric(tel)) { erreur += 'Votre téléphone n\'est pas valide, veuillez verifier SVP.\n'; }
	else if(tel.length!=10) { erreur += 'Votre telephone doit comporter 10 chiffres et non '+tel.length+', veuillez verifier SVP.\n'; }
	if(que=="") { erreur += 'Veuillez saisir une question SVP.\n'; }
	if(erreur==""){
		var xhr = null;
		if(window.XMLHttpRequest) xhr = new XMLHttpRequest();
		else if(window.ActiveXObject){
		   try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); }
		   catch (e) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
		}
		else {
		   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		   xhr = false;
		}
		var data     = null;
		var filename = "./ajax/contact.php";
		data = filename+"?nom="+nom+"&soc="+soc+"&act="+act+"&ema="+ema+"&tel="+tel+"&fax="+fax+"&que="+que;
		xhr.open("GET", data, true);
		xhr.onreadystatechange = function anonymous() {
			if(xhr.readyState == 4){
				var resu = xhr.responseText;
				if(resu.substring(0,2)=="ok") {
					alert('L\'email a été envoyé...');
				}
				else{
					alert('Erreur, veuillez recommencer plus tard...');
				}
			};
		}
		xhr.send(null);
	}
	else {
		alert(erreur);
	}
	dechargement();
}
