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 AJAX_change_forme(id,pid,lang){
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = clean(xhr.responseXML.documentElement);
			var produit_id=reponse.getElementsByTagName("produit_id")[0].firstChild.nodeValue;
			var forme_id=reponse.getElementsByTagName("forme_id")[0].firstChild.nodeValue;
			var lang=reponse.getElementsByTagName("lang")[0].firstChild.nodeValue;
			var tailles=reponse.getElementsByTagName("taille");
			var taille_id;
			var taille_libelle;
			if(lang=='fr') {
				var html="<p><b>Etape 2</b> Taille de l'abat jour :  <select name=\"taille\"  onChange=\"javascript:AJAX_change_taille(this.options[this.selectedIndex].value,"+produit_id+","+forme_id+",'"+lang+"');\"><option value=\"0\"></option>";
			} else {
				var html="<p><b>Step 2</b> Your shade size :  <select name=\"taille\"  onChange=\"javascript:AJAX_change_taille(this.options[this.selectedIndex].value,"+produit_id+","+forme_id+",'"+lang+"');\"><option value=\"0\"></option>";
			}
			for(var u=0;u<tailles.length;u++) {
				//taille_id=tailles[u].firstChild.getAttribute("id");
				taille_id=reponse.getElementsByTagName("taille")[u].getAttribute("id");
				taille_libelle=tailles[u].firstChild.nodeValue;
				html+="<option value=\""+taille_id+"\">"+taille_libelle+"</option>";
			}
			html+="</select></p>";
			
			document.getElementById('seltaille').innerHTML = html;
		}
	}
	
	xhr.open("POST","../ajax_change_forme.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id="+id+"&pid="+pid+"&lang="+lang);
}

function AJAX_change_taille(tid,pid,id,lang){
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = clean(xhr.responseXML.documentElement);
			var produit_id=reponse.getElementsByTagName("produit_id")[0].firstChild.nodeValue;
			var forme_id=reponse.getElementsByTagName("forme_id")[0].firstChild.nodeValue;
			var taille_id=reponse.getElementsByTagName("taille_id")[0].firstChild.nodeValue;
			var lang=reponse.getElementsByTagName("lang")[0].firstChild.nodeValue;
			var couleurs=reponse.getElementsByTagName("couleur");
			var couleur_id;
			var couleur_html;
			var ajps_id;
			if(lang=='fr') {
				var html="<p><b>Etape 3</b> Couleur de l'abat jour :</p><p>";
			} else {
				var html="<p><b>Step 3</b> Your shade color :</p><p>";
			}
			for(var u=0;u<couleurs.length;u++) {
				couleur_id=couleurs[u].getAttribute("id");
				ajps_id=couleurs[u].getAttribute("ajps");
				couleur_html=couleurs[u].firstChild.nodeValue;
				html+=" <a href=\"#\" class=\"palette\" style=\"color:#"+couleur_html+";background-color:#"+couleur_html+";\" onClick=\"javascript:AJAX_change_abatjour("+produit_id+","+ajps_id+");\">o</a>";
			}
			html+="</p>";
			
			document.getElementById('selcouleur').innerHTML = html;
		}
	}
	
	xhr.open("POST","../ajax_change_taille.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id="+id+"&pid="+pid+"&tid="+tid+"&lang="+lang);
}

function AJAX_change_abatjour(pid,ajpsid) {
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			reponse = clean(xhr.responseXML.documentElement);
			var produit_id=reponse.getElementsByTagName("produit_id")[0].firstChild.nodeValue;
			var forme_id=reponse.getElementsByTagName("forme_id")[0].firstChild.nodeValue;
			var taille_id=reponse.getElementsByTagName("taille_id")[0].firstChild.nodeValue;
			var couleur_id=reponse.getElementsByTagName("couleur_id")[0].firstChild.nodeValue;
			var matiere_id=reponse.getElementsByTagName("matiere_id")[0].firstChild.nodeValue;
			var aj_id=reponse.getElementsByTagName("aj_id")[0].firstChild.nodeValue;
			var ajp_id=reponse.getElementsByTagName("ajp_id")[0].firstChild.nodeValue;
			var forme=reponse.getElementsByTagName("forme")[0].firstChild.nodeValue;
			var taille=reponse.getElementsByTagName("taille")[0].firstChild.nodeValue;
			var couleur=reponse.getElementsByTagName("couleur")[0].firstChild.nodeValue;
			var matiere=reponse.getElementsByTagName("matiere")[0].firstChild.nodeValue;
			var prix=reponse.getElementsByTagName("prix")[0].firstChild.nodeValue;
			document.getElementById('faj').innerHTML = forme;
			document.getElementById('daj').innerHTML = taille;
			document.getElementById('maj').innerHTML = matiere;
			document.getElementById('caj').innerHTML = '<b>'+couleur+'</b>';
			document.getElementById('lpdf').href = 'topdf-'+produit_id+'-'+ajp_id+'.pdf';
			document.getElementById('lpdf2').href = 'topdf-'+produit_id+'-'+ajp_id+'.pdf';
			document.getElementById('visuel_detail').src='../mam-'+produit_id+'-'+ajp_id+'.jpg';
			var oprix=parseFloat(document.getElementById('phaj').innerHTML);
			var nprix=parseFloat(oprix)+parseFloat(prix);
			document.getElementById('paaj').innerHTML = nprix;
			if(document.getElementById('lpanier')) {
				document.getElementById('lpanier').href = 'panier.php?act=add&i='+produit_id+'&a='+ajp_id;
			}
			
		}
	}
	
	
	xhr.open("POST","../ajax_change_abatjour.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("pid="+pid+"&ajpsid="+ajpsid);
}

