function go(c){
	if(!c.data.replace(/\s/g,''))
	c.parentNode.removeChild(c);
}

function clean(d){
	var bal=d.getElementsByTagName('*');
	
	for(i=0;i<bal.length;i++){
		a=bal[i].previousSibling;
		if(a && a.nodeType==3) 
			go(a);
		b=bal[i].nextSibling;
		if(b && b.nodeType==3)
			go(b);
	}
	return d;
} 

function getXhr(){
	var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et autres
		xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	} else { // XMLHttpRequest non supporté par le navigateur 
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		xhr = false; 
	} 
	return xhr;
}

function changevisuel(id){
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = clean(xhr.responseXML.documentElement);
			var id=reponse.getElementsByTagName("id")[0].firstChild.nodeValue;
			var image=reponse.getElementsByTagName("image")[0].firstChild.nodeValue;
			document.getElementById('visuel').innerHTML = '<img src="../../images/produit/resizedetail/'+image+'">';
			document.getElementById('agrandir').innerHTML = '<a href="javascript:zoom(\''+id+'\');"><img src="../../images/p1.gif"> Zoom</a>';
		}
	}
	
	xhr.open("POST","../../ajaxchangevisuel.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id="+id);
}

function checkcode() {
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = clean(xhr.responseXML.documentElement);
			var offre=reponse.getElementsByTagName("offre")[0].firstChild.nodeValue;
			var total=reponse.getElementsByTagName("total")[0].firstChild.nodeValue;
			var ports=reponse.getElementsByTagName("ports")[0].firstChild.nodeValue;
			var totalports=reponse.getElementsByTagName("totalports")[0].firstChild.nodeValue;
			// document.getElementById('offre').innerHTML = offre;
			document.getElementById('total').innerHTML = total;
			document.getElementById('ports').innerHTML = ports;
			document.getElementById('totalports').innerHTML = totalports;
			if(offre!='Aucune') {
				document.getElementById('offre').innerHTML = '<div class="offre">'+offre+'</div>';
			} else {
				document.getElementById('offre').innerHTML = '';
			}
		}
	}
		
	xhr.open("POST","ajaxcheckcode.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("c="+document.getElementById('code').value);

}

function changequantite(id,quantite) {
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = clean(xhr.responseXML.documentElement);
			var id=reponse.getElementsByTagName("id")[0].firstChild.nodeValue;
			var totalttc=reponse.getElementsByTagName("totalttc")[0].firstChild.nodeValue;
			var total=reponse.getElementsByTagName("total")[0].firstChild.nodeValue;
			var ports=reponse.getElementsByTagName("ports")[0].firstChild.nodeValue;
			var totalports=reponse.getElementsByTagName("totalports")[0].firstChild.nodeValue;
			var nb=reponse.getElementsByTagName("nb")[0].firstChild.nodeValue;
			document.getElementById('total'+id).innerHTML = totalttc;
			document.getElementById('total').innerHTML = total;
			document.getElementById('ports').innerHTML = ports;
			document.getElementById('totalports').innerHTML = totalports;
			document.getElementById('cart').innerHTML = nb;
		}
	}
		
	xhr.open("POST","ajaxchangequantite.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id="+id+"&q="+quantite);

}

function delproduit(id) {
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = clean(xhr.responseXML.documentElement);
			var id=reponse.getElementsByTagName("id")[0].firstChild.nodeValue;
			var total=reponse.getElementsByTagName("total")[0].firstChild.nodeValue;
			var ports=reponse.getElementsByTagName("ports")[0].firstChild.nodeValue;
			var totalports=reponse.getElementsByTagName("totalports")[0].firstChild.nodeValue;
			var nb=reponse.getElementsByTagName("nb")[0].firstChild.nodeValue;
			var lang=reponse.getElementsByTagName("lang")[0].firstChild.nodeValue;
			if (document.getElementById) {
				if(total!='0.00') {
					document.getElementById('line'+id).innerHTML = '';
					document.getElementById('line'+id).style.height = '0';
					document.getElementById('line'+id).style.margin = '0';
					document.getElementById('line'+id).style.padding = '0';
					document.getElementById('line'+id).style.visibility = 'hidden';
					document.getElementById('total').innerHTML = total;
					document.getElementById('ports').innerHTML = ports;
					document.getElementById('totalports').innerHTML = totalports;
					document.getElementById('cart').innerHTML = nb;
				} else {
					if(lang=='fr') {
						document.getElementById('content').innerHTML='<div align="center"><b><span style="color:#ff0000;font-size:0.9em;">Votre panier est vide</span></b></div>';
					} else {
						document.getElementById('content').innerHTML='<div align="center"><b><span style="color:#ff0000;font-size:0.9em;">Your Shopping Bag is empty</span></b></div>';
					}
					document.getElementById('cart').innerHTML = '';
				}
			} else if (document.all) {
				document.all["line"+id].innerHTML = "";
				document.all["total"].innerHTML = total;
				document.all["ports"].innerHTML = ports;
				document.all["totalports"].innerHTML = totalports;
				document.all["cart"].innerHTML = nb;
			}
		}
	}
		
	xhr.open("POST","ajaxdelproduit.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id="+id);

}

function addproduit(id) {
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = clean(xhr.responseXML.documentElement);
			var id=reponse.getElementsByTagName("id")[0].firstChild.nodeValue;
			var nb=reponse.getElementsByTagName("nb")[0].firstChild.nodeValue;
			var nbproduit=reponse.getElementsByTagName("nbproduit")[0].firstChild.nodeValue;
			if (document.getElementById) {
					document.getElementById('cart').innerHTML = nb;
					if(document.getElementById('b'+id)) {
						document.getElementById('b'+id).innerHTML = '<a href="javascript:addproduit(\''+id+'\');"><img src="images/add-cart-mini-used.gif" class="minicart"></a> ('+nbproduit+')';
					}
					if(document.getElementById('td'+id)) {
						document.getElementById('td'+id).innerHTML = '<a href="javascript:addproduit(\''+id+'\');"><img src="images/add-cart-used.gif"></a> ('+nbproduit+')';
					}
			} else if (document.all) {
				document.all["cart"].innerHTML = nb;
			}
		}
	}
		
	xhr.open("POST","ajaxaddproduit.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id="+id);

}

function delPalette(id) {
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = clean(xhr.responseXML.documentElement);
			var id=reponse.getElementsByTagName("id")[0].firstChild.nodeValue;
			var nb=reponse.getElementsByTagName("nb")[0].firstChild.nodeValue;
			var time=reponse.getElementsByTagName("time")[0].firstChild.nodeValue;
			if (document.getElementById) {
					document.getElementById('cart').innerHTML = nb;
					document.getElementById('mapalette').innerHTML = '<img src="images/affpalette-'+time+'.png" />';
					document.getElementById('comp'+id).innerHTML = '';
					document.getElementById('b'+id).innerHTML = '<a href="javascript:addToPalette(\''+id+'\');" title="Ajouter"><img src="images/add-cart-mini.gif" class="minicart"></a>';
					document.getElementById('complete').innerHTML = '';
			} else if (document.all) {
				document.all["cart"].innerHTML = nb;
				document.all["mapalette"].innerHTML = '<img src="images/affpalette-'+time+'.png" />';
			}
		}
	}
		
	xhr.open("POST","ajaxdelpalette.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id="+id);

}

function addToPalette(id) {
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = clean(xhr.responseXML.documentElement);
			var id=reponse.getElementsByTagName("id")[0].firstChild.nodeValue;
			var pos=reponse.getElementsByTagName("pos")[0].firstChild.nodeValue;
			var nb=reponse.getElementsByTagName("nb")[0].firstChild.nodeValue;
			var time=reponse.getElementsByTagName("time")[0].firstChild.nodeValue;
			var libelle=reponse.getElementsByTagName("libelle")[0].firstChild.nodeValue;
			var libelle_cat=reponse.getElementsByTagName("libellecat")[0].firstChild.nodeValue;
			var complete=reponse.getElementsByTagName("complete")[0].firstChild.nodeValue;
			var lang=reponse.getElementsByTagName("lang")[0].firstChild.nodeValue;
			if (document.getElementById) {
					document.getElementById('cart').innerHTML = nb;
					if(pos != '-1') {
						document.getElementById('mapalette').innerHTML = '<img src="images/affpalette-'+time+'.png" />';
						if(!document.getElementById('comp'+id)) {
							document.getElementById('listecomposer').innerHTML = document.getElementById('listecomposer').innerHTML + '<div id="comp'+id+'">'+libelle_cat+' - <a href="javascript:delPalette(\''+id+'\');">'+libelle+'</a><img src="images/dot.gif"></div>';
						} else {
							document.getElementById('comp'+id).innerHTML=libelle_cat+' - <a href="javascript:delPalette(\''+id+'\');">'+libelle+'</a><img src="images/dot.gif">';
						}
						document.getElementById('b'+id).innerHTML = '<a href="javascript:delPalette(\''+id+'\');" title="Supprimer"><img src="images/add-cart-mini-used.gif" class="minicart"></a>';
					}
					if(complete=='1') {
						if(lang=='fr') {
							document.getElementById('complete').innerHTML = 'Votre palette est à présent complete, souhaitez vous <a href="panier.php">terminer votre commande</a>, <a href="composer-palette.php">créer une nouvelle palette</a> ou <a href="index.php">continuer vos achats</a> ?';
						} else {
							document.getElementById('complete').innerHTML = 'Your palette is now complete. Do you want to <a href="panier.php">check out</a>, <a href="composer-palette.php">create a new palette</a> or <a href="index.php">continue shopping</a> ?';	
						}
					}
			} else if (document.all) {
				document.all["cart"].innerHTML = nb;
				document.all["mapalette"].innerHTML = '<img src="images/affpalette-'+time+'.png" />';
			}
		}
	}
		
	xhr.open("POST","ajaxaddtopalette.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id="+id);

}

function zoom(id) {
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = clean(xhr.responseXML.documentElement);
			var id=reponse.getElementsByTagName("id")[0].firstChild.nodeValue;
			var image=reponse.getElementsByTagName("image")[0].firstChild.nodeValue;
			var width=reponse.getElementsByTagName("width")[0].firstChild.nodeValue;
			var height=reponse.getElementsByTagName("height")[0].firstChild.nodeValue;
			window.open("../../zoomphoto.php?i="+id,"Zoom","directories=no,width="+width+",height="+height+",location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
		}
	}
	
	xhr.open("POST","../../ajaxgetsizevisuel.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id="+id);
	
}

function deplietechnique() {
	if (document.getElementById) {
		if(document.getElementById('technique').style.display=='block') {
			document.getElementById('technique').style.display='none';
		} else {
			document.getElementById('technique').style.display='block';
		}
		
	}
}
