// 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 }
		}
	}
}

// FONCTION TYPE explode DE PHP
function explode(item,delimiter) {
	tempArray=new Array(1);
	var Count=0;
	var tempString=new String(item);
	while (tempString.indexOf(delimiter)>0) {
		tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
		tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
		Count=Count+1;
	}
	tempArray[Count]=tempString;
	return tempArray;
}

// FONCTION QUI RETOURNE L'EXTENSION D'UN FICHIER SELECTIONNE PAR UN INPUT FILE
function retourner_extension(div) {
	var file=document.getElementById(div).value;
	tab_tmp=file.split(".");
	var nb=tab_tmp.length;
	return tab_tmp[(nb-1)].toLowerCase();
}

// 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;
}

// VIDE UN CHAMP INPUT SUIVANT LA VALEUR
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--;
		}
	}
}

// VERIFICATION A LA SAISIE D'UN NOMBRE + VIRGULE [OU POINT]
function verif_nombre_virgule(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--;
		}
	}
}

// MODIFICATION SUR UN LAYER
function layer_visible(div,style){
	document.getElementById(div).style.visibility=style;
}
function show_div(id) {
	document.getElementById(id).style.display='block';
}
function hide_div(id) {
	document.getElementById(id).style.display='none';
}
function show_mouvement(id) {
	if(document.getElementById(id).style.display=='none') {
		document.getElementById(id).style.opacity='0.0';
		document.getElementById(id).style.filter='alpha(opacity=00)';
		show_div(id);
		new Effect.Opacity(id, { from: 0.0, to: 1.0, duration: 0.4 });
		//new Effect.SlideDown(id, { duration: 0.6 });
	}
}
function hide_mouvement(id) {
	new Effect.Opacity(id, { from: 1.0, to: 0.0, duration: 0.2, afterFinish: function(){
		document.getElementById(id).style.opacity='0.0';
		document.getElementById(id).style.filter='alpha(opacity=00)';
		hide_div(id);
	} });
}
function change_class(id,new_class) {
	document.getElementById(id).className=new_class;
}

// GESTION DU CHARGEMENT ET DECHARGEMENT DU LOADING
function chargement(){
	show_div('chargement');
	show_div('loading');
}
function dechargement(){
	hide_div('chargement');
	hide_div('loading');
}
function dechargement_tempo(tempo){
	window.setTimeout('dechargement()',tempo);
}
function loading(){
	show_div('loading');
}
function deloading(){
	hide_div('loading');
}

// 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;
	}
}

// CHARGEMENT DU LAYER POUR SELECTION DU CODE POSTAL ET / OU VILLE --> SE DECLENCHE A LA SORTIE DU CHAMP CP OU VILLE
function code_postal(){
	loading();
	var aff=0;
	var cp=document.getElementById('cp').value;
	var vi=document.getElementById('vi').value;
	if(cp.length==5) {
		if(vi.length==0) {
			aff=1;
		}
	}
	if(vi.length>3) {
		if(cp.length==0) {
			aff=1;
		}
	}
	// ON AFFICHE LE LAYER POUR L'AFFICHAGE DES RESULTATS
	if(aff==1) {
		var xhr = null;
		getXhr();
		var data     = null;
		var filename = '../ajax/ajax.codepostal.php';
		data = filename+'?cp='+cp+'&vl='+vi;
		//alert(data);
		xhr.open("GET", data, true);
		xhr.onreadystatechange = function anonymous() {
			if(xhr.readyState == 4){
				var resu = xhr.responseText;
				if(resu != "0") {
					document.getElementById('aff_lst_cp').innerHTML = resu;
					show_div('adresse');
				}
				deloading();
			};
		}
		xhr.send(null);
		return true;
	}
	else {
		deloading();
		return true;
	}
}
function code_postal_ferme(){
	deloading();
	hide_div('adresse');
}
function code_postal_autre(inp){
	document.getElementById(inp).focus();
	deloading();
	hide_div('adresse');
}
function code_postal_selectionne(cp,vi){
	document.getElementById('cp').value=cp;
	document.getElementById('vi').value=vi;
	deloading();
	hide_div('adresse');
}

// RECHERCHE
function verif_recherche(def) {
	var erreur = "";
	var search=document.getElementById('form_search').search.value;
	if(search=="") { erreur+='Veuillez saisir une recherche SVP.\n'; }
	if(search==def) { erreur+='Veuillez saisir une recherche SVP.\n'; }
	if(search.length<2) { erreur+='Saisissez une recherche d\'au moins 2 caracteres !\n'; }
	if(erreur===""){
		return true;
	}
	else {
		alert(erreur);
		return false;
	}
}
function lajax(e){
	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 IE5 = false;
	if (!e) var e = window.event;
	if (e.keyCode) { IE5= true; code = e.keyCode;}
	else if (e.which) code = e.which ;
	if(code!=13){
		if(document.getElementById('search').value.length>1){
			var data     = null;
			var filename = "../ajax/ajax.recherche.php";
			var mot      = document.getElementById('search').value;
			data=filename+"?cherche="+escape(mot);
			xhr.open("GET", data, true);
			xhr.onreadystatechange = function anonymous() {
				if(xhr.readyState == 4){
					var resu = xhr.responseText;
					if(resu!="0"){
			            document.getElementById('resultat').innerHTML=resu;
			            show_div('resultat');
					}
					else{
						document.getElementById('resultat').innerHTML="";
			        	hide_div('resultat');
					}
	        	};
			}
			xhr.send(null);
		}
		else{
			hide_div('resultat');
		}
	}
}

