var isMozilla = (navigator.userAgent.toLowerCase().indexOf('gecko')!=-1) ? true : false;
var regexp = new RegExp("[\r]","gi");

// Ouverture requete ajax
function getXMLHttpRequest(){
	var xhr = null;

	if(window.XMLHttpRequest || window.ActiveXObject) {
			if(window.ActiveXObject) {
					try {
							xhr = new ActiveXObject("Msxml2.XMLHTTP");
					} catch(e) {
							xhr = new ActiveXObject("Microsoft.XMLHTTP");
					}
			} else {
					xhr = new XMLHttpRequest();
			}
	} else {
			alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
			return null;
	}

	return xhr;
}

// Variable IE
var testie = Math.floor(Math.random() * 1000000000);
//Var temp
var quantiteMax = 10;//Quantite maximun
var hauteur = document.documentElement.clientHeight;//Hauteur du body

//Pousuivre ces achats après un ajout au panier
function Poursuivre(){
	document.getElementById("Grisement").style.display = 'none';
	document.getElementById("PopupPanier").style.display = 'none';
}
//////////////////////////////////////////

//AFFICHAGE DES MESSAGE VIA POPUP
	//Affichage message erreur quantité
	function AffMessageErreurQuantite(){window.alert("La quantité doit être entre 0 et "+quantiteMax+".");}
	//Affichage message ajout panier
	function AffMessageAjoutPanier(idart, quantite){window.alert("Vous venez d'ajouter "+quantite+" article n°"+idart+"."+hauteur+"");}
	//Affichage message ajout panier
	function AffMessageErreur(){window.alert("Erreur Script");}
///////////////////////////////////////

//Changer la quantité
function ChangeQuant(idart, val){
	var quant = document.getElementById(idart);
	var quantite = document.getElementById(idart).value;
	var quantitesvg = quantite;
	if (val == -1){quantite--;}
	else if (val == 1){quantite++;}
	else {AffMessageErreur()}
	if (quantite > quantiteMax){var quantite = quantiteMax;}
	else if (quantite < 1){var quantite = 1;}
	else{}
	quant.value = quantite;
}
/////////////////////////////

//Changer la quantité directement au panier
function ChangeQuantPanier(idproduit, idproduitbis, val, id_commande){
	var quant = document.getElementById(idproduitbis);
	var quantite = document.getElementById(idproduitbis).value;
	var quantitesvg = quantite;
	if (val == -1){quantite--;}
	else if (val == 1){quantite++;}
	else {AffMessageErreur()}
	if (quantite > quantiteMax){var quantite = quantiteMax;}
	else if (quantite < 1){var quantite = 1;}
	else{}
	quant.value = quantite;
	var xhr = getXMLHttpRequest();
	if(xhr && xhr.readyState != 0) {xhr.abort();}
	xhr.open("POST", "ChangeQuantitePanier.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send("ie="+testie+"&quant="+quantite+"&idproduit="+idproduit);
	AffProduitTotal(idproduit);
	AffPanierFin(id_commande);
	AffPanier(id_commande);
}
/////////////////////////////

// Ajout au panier
function AjoutPanier(idart, rayon, id_commande){
	var quant = document.getElementById(idart);
	var quantite = document.getElementById(idart).value;
	if (quantite <= quantiteMax){
		var xhr = getXMLHttpRequest();
		if(xhr && xhr.readyState != 0) {xhr.abort();}
		xhr.open("GET", "AjoutPanier.php?idart="+idart+"&quantite="+quantite+"&rayon="+rayon+"&id_commande="+id_commande+"&ie="+testie+"", true);
		xhr.send(null);
		//quant.value = 1;
		AffPanier(id_commande);
		document.getElementById("Grisement").style.display = 'block';
		document.getElementById("Grisement").style.height = hauteur;
		document.getElementById("PopupPanier").style.display = 'block';
		//AffMessageAjoutPanier(idart, quantite);
	}
	else{
		quant.value = 1;
		AffMessageErreurQuantite();
	}
}
///////////////////////////

// Ajout commentaires
function AjoutComment(){
	document.getElementById("Grisement").style.display = 'block';
	document.getElementById("Grisement").style.height = hauteur;
	document.getElementById("PopupCommentaire").style.display = 'block';
}
///////////////////////////

// Affichage du panier
function AffPanier(id_commande){
	var xhr = getXMLHttpRequest();
	if(xhr && xhr.readyState != 0) {xhr.abort();}
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				document.getElementById('panier').innerHTML = xhr.responseXML.getElementsByTagName('message')[0].firstChild.nodeValue;
				testie = testie + 1;
		}
		else if(xhr.readyState == 2 || xhr.readyState == 3) {}
	}
	xhr.open("POST", "AffPanier.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send("id_commande="+id_commande+"ie="+testie);
}
////////////////////////////////

// Affichage du panier fin commande
function AffPanierFin(id_commande){
	var xhr = getXMLHttpRequest();
	if(xhr && xhr.readyState != 0) {xhr.abort();}
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				document.getElementById('TotalArticles').innerHTML = xhr.responseXML.getElementsByTagName('message')[0].firstChild.nodeValue;
				document.getElementById('TotalCommande').innerHTML = xhr.responseXML.getElementsByTagName('message')[1].firstChild.nodeValue;
				testie = testie + 1;
		}
		else if(xhr.readyState == 2 || xhr.readyState == 3) {}
	}
	xhr.open("POST", "AffPanierFin.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send("id_commande="+id_commande+"ie="+testie);
}
////////////////////////////////


// Affichage total produit
function AffProduitTotal(idproduit){
	var xhr = getXMLHttpRequest();
	if(xhr && xhr.readyState != 0) {xhr.abort();}
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				document.getElementById(idproduit).innerHTML = xhr.responseXML.getElementsByTagName('message')[0].firstChild.nodeValue;
				testie = testie + 1;
		}
		else if(xhr.readyState == 2 || xhr.readyState == 3) {}
	}
	xhr.open("POST", "AffProduitTotal.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
	xhr.send("ie="+testie+"&idproduit="+idproduit);
}
////////////////////////////////
