// JavaScript Document
function ajax(fichier) {
	if(window.XMLHttpRequest) // FIREFOX
	  xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
	  xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
	  return(false);
	xhr_object.open("GET", fichier, false);
	xhr_object.send(null);
	if(xhr_object.readyState == 4) return(xhr_object.responseText);
	else return(false);
}


function valider_mail(email){
	/*var x = email;
	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;*/
				
	var modele = /^[a-zA-Z0-9\.\-_]+@[a-zA-Z0-9\-_]+\.[a-zA-Z]{2,5}$/i;
	if (modele.test(email))
	  return true;
	else
	  return false;
	return false;
}

function showHideDiv(divid) {
	var	div = document.getElementById(divid);
	var fond = document.getElementById('bg_popup');
	
	if (div.style.display == "none") {
		div.style.display = "block";
		fond.style.display = "block";
	}
	else
	{
		div.style.display = "none";
		fond.style.display = "none";
	}
}

function showErreur(div, msg_erreur) {
	//alert(msg_erreur);
	//var fond2 = document.getElementById('bg_div');
	//	fond2.style.display = "none";
	var div2 = document.getElementById(div);
	
	div2.style.display = "block";
	div2.innerHTML = msg_erreur;
}

function getPourcentage(p1, p2) {
	//p1 prix de base
	//p2 prix de vente
	//alert(p1 + " // " + p2);
	p1 = parseFloat(p1.replace(",","."));
	p2 = parseFloat(p2.replace(",","."));
	pourcentage = Math.round(((p1 - p2) / p1) * 10) * 10;
	return pourcentage;
}

	function getTrad(param) {
		
		// Langue par dfaut
		LANG = document.getElementById('LANG').value;
		alert(LANG);
		msg_err = "";
		var maintenant = new Date();
		var temps = maintenant.getTime();	
		ObjAJAX = new Ajax();
		ObjAJAX.setParam ({					  
			url          : "/include/xml/lang/" + LANG + ".xml",//?d=" + temps,
			returnFormat : "xml", //  OU returnFormat : "txt"
			method       : "GET", // OU method       : "POST"
			data         : "",
			asynchronus  : false, // OU asynchronus  : false
			onComplete   : "var element = response.getElementsByTagName('" + param + "').item(0);msg_err = element.firstChild.data;/*return(msg_err);*/"
			});
		ObjAJAX.execute();
		return(msg_err);
	}
	
	// Synthaxe : Ajax () Objet
	// Objet centrale de la classe
	function Ajax() {
		// Variable //
		this.asyn = true;
		this.data = "";
		this.url = "";
		this.method = "GET";
		this.returnFormat = "txt";
		this.obj;
		this.init();
	}

	// Synthaxe : httprequest () Objet XMLHttpRequest
	// Cre l'objet XMLHttpRequest //
	Ajax.prototype.init = function() {
		this.obj = null;
		if (window.XMLHttpRequest)
			this.obj = new XMLHttpRequest();
		else if (window.ActiveXObject) {
			// If IE
			var ieversions = ['Msxml2.XMLHTTP','Microsoft.XMLHTTP','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0'];
			for(var i=0; !this.obj && i<ieversions.length; i++) {
				try {
					this.obj = new ActiveXObject(ieversions[i]);
				}
				catch(e) { }
			}
		}
	}

	// Lorsque la requte ne russit pas
	Ajax.prototype.onFailure = function (errorCode) {
	// ...
	}
	// Lorsque la requte russit
	Ajax.prototype.onComplete = function (response) {
	// ...
	}
	
	// Synthaxe : setParamFromForm ([HTML Form || String Name || Int Index])
	// Ajoute les paramtres et les donnes d'un formulaire  celui de la requte
	Ajax.prototype.setParamFromForm = function (obj) {
		if (!isNaN(obj))
			obj = document.forms[obj];
		if (typeof obj == "string")
			eval("obj = document."+obj);
		if (!isset(obj)) {
			return ErreurObj("Donne Invalide");
		}
		//this.method = (isset(obj.method) && (["GET","POST"].inArray(obj.method.toUpperCase()))) ? obj.method.toUpperCase() : this.method;
		this.method = "GET";
		this.url = obj.action;
		for (i=0;i<obj.elements.length;i++) {
			if (["file","button","reset","submit"].inArray(obj.elements[i].type.toLowerCase()))
				continue;
			if (this.data != null)
				this.data += "&";
			this.data += obj.elements[i].name + "=" + escape(obj.elements[i].value);
		}
	}
	
	// Synthaxe : setParam ([Array data])
	// Ajoute les paramtres  la requte  partir d'un tableau
	Ajax.prototype.setParam = function (arr) {
		if (typeof arr != "object" && !isset(arr)) {
			return ErreurObj("Donne Invalide");
		}
		for(k in arr) {
			switch (k) {
				case "url" : this.url = arr[k]; break;
				//case "method" : this.method = (["GET","POST"].inArray(arr[k].toUpperCase())) ? arr[k].toUpperCase() : this.method; break;
				case "method" : this.method = "GET"; break;
				case "data" :
					if (typeof arr[k] == "string") {
						if (this.data != "")
							this.data += "&";
						this.data += arr[k];
					} else {
						if (typeof arr[k] != "object")
							break;
						for (j in arr[k]) {
							if (this.data != "")
								this.data += "&";
							this.data += j + "=" + escape(arr[k][j]);
						}
					}
					break;
				case "asynchronus" : this.asyn = arr[k]; break;
				case "onComplete" : this.onComplete = arr[k];break;
				case "onFailure" : this.onFailure = arr[k];break;
				case "returnFormat" : this.returnFormat = arr[k];break;
			}
		}
	}
	
	// Synthaxe : Function execRequest () //
	// Excute la requte, ainsi que le callback
	Ajax.prototype.execute = function () {
		this.obj.open(this.method,this.url,this.asyn);
		if (this.method == "POST")
			this.obj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		if (this.asyn) {
			_tempAJAX_Reference_ = this; // Cre une copie de l'objet AJAX courant pour pouvoir le rcuprer aprs //
			this.obj.onreadystatechange = function () {
				if (_tempAJAX_Reference_.obj.readyState == 4 && _tempAJAX_Reference_.obj.status == 200) {
					if (_tempAJAX_Reference_.returnFormat != "txt")
						response = _tempAJAX_Reference_.obj.responseXML;
					else
						response = _tempAJAX_Reference_.obj.responseText;
				
					if (typeof _tempAJAX_Reference_.onComplete == "string")
						eval (_tempAJAX_Reference_.onComplete);
					else
						_tempAJAX_Reference_.onComplete(response);
				} else if (_tempAJAX_Reference_.obj.readyState == 4) {
					errorCode = _tempAJAX_Reference_.obj.status;
					if (typeof _tempAJAX_Reference_.onFailure == "string")
						eval(_tempAJAX_Reference_.onFailure);
					else
						_tempAJAX_Reference_.onFailure(errorCode);
				}
			}
			
			this.obj.send(this.data);
		} else {
			this.obj.send(this.data);
			if (this.obj.status == "200") {
				if (this.returnFormat != "txt")
					response = this.obj.responseXML;
				else
					response = this.obj.responseText;
				
				if (typeof this.onComplete == "string")
					eval (this.onComplete);
				else
					this.onComplete(response);
			} else {
				errorCode = this.obj.status;
				if (typeof this.onFailure == "string")
					eval (this.onFailure);
				else
					this.onFailure(errorCode);
			}
		}
	}
