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


function affiche_message_erreur_stock(texte){
	
	alpha_on();
	
	chaine=texte.split(";");
	var alerte="";
	
	
	document.getElementById("popup_cmd_expresse").style.display = "block";
	document.getElementById("img_attention_b").style.display = "block";
	document.getElementById("fermer_cmd_expresse").style.display = "block";
	document.getElementById("texte_cmd_expresse").style.top = "20px";
	
	for (var i=0; i<chaine.length-1; i=i+2) {
	alerte= alerte + "<font style='font-size:12px;'> Attention le stock( actuellement :" +chaine[i+1] + ") de l'article : " +chaine[i]+ " ne permet pas de le commander<br></font>";
	}
	
	
	document.getElementById("texte_cmd_expresse").innerHTML = alerte;
	
}

function verif_stock(){
		
	
	var xhrcd = getXHR();
	
	
	xhrcd.onreadystatechange = function() {
		if (xhrcd.readyState == 4) 
		{
			reponse = xhrcd.responseText;
			
			if(reponse == "true"){
				window.location.href = "commande_part.php";
			}else{
				affiche_message_erreur_stock(reponse);
			}
		}
	}
	
	xhrcd.open("GET", "ajax/verifStockDorise.php?cc="+Math.random(), true); 
	xhrcd.send(null);
	
	return false;
}


function adresse_montre()
{
	document.getElementById("changement_adresse").style.visibility = "visible";
	alpha_on();
	
	var id_selected = document.form.elements['adresse'].options[document.form.elements['adresse'].selectedIndex].value;
	if(id_selected == 0) 
	{
		document.getElementById("changement_adresse").style.height = "190px";
		document.getElementById("changement_adresse_nouv1").style.visibility = "visible";
		document.getElementById("changement_adresse_nouv2").style.visibility = "visible";
		document.getElementById("changement_adresse_nouv3").style.visibility = "visible";
	}
	else
	{
		document.getElementById("changement_adresse").style.height = "160px";
		document.getElementById("changement_adresse_nouv1").style.visibility = "hidden";
		document.getElementById("changement_adresse_nouv2").style.visibility = "hidden";
		document.getElementById("changement_adresse_nouv3").style.visibility = "hidden";
		document.getElementById("changement_adresse_act").innerHTML = document.getElementById("adr_"+document.form.elements['adresse'].options[document.form.elements['adresse'].selectedIndex].value).value;;
	}
}


function adresse_cache()
{
	xh = getXHR();
	
	xh.onreadystatechange = function() {
	    if (xh.readyState == 4) 
		{	
			document.getElementById("changement_adresse").style.visibility = "hidden";
			document.getElementById("changement_adresse_nouv1").style.visibility = "hidden";
			document.getElementById("changement_adresse_nouv2").style.visibility = "hidden";
			document.getElementById("changement_adresse_nouv3").style.visibility = "hidden";
			alpha_off();
			
			if(text != "") document.getElementById("adresse").innerHTML = text;
	    }
    } 

	var id_selected = document.form.elements['adresse'].options[document.form.elements['adresse'].selectedIndex].value;
	if(id_selected == 0) 
	{
		var textAjax = document.forms['form'].elements['adr1'].value;
		textAjax += ":!"+document.forms['form'].elements['adr2'].value;
		textAjax += ":!"+document.forms['form'].elements['adr3'].value;
		textAjax += ":!"+document.forms['form'].elements['adr4'].value;
		textAjax += ":!"+document.forms['form'].elements['cp'].value;
		textAjax += ":!"+document.forms['form'].elements['ville'].value;
		textAjax += ":!"+document.forms['form'].elements['pays'].value;
		textAjax += ":!"+document.forms['form'].elements['tel'].value;
		textAjax += ":!"+document.forms['form'].elements['fax'].value;
		
		var text = document.forms['form'].elements['adr1'].value;
		text += "<br/>"+document.forms['form'].elements['adr2'].value;
		text += "<br/>"+document.forms['form'].elements['adr3'].value;
		text += "<br/>"+document.forms['form'].elements['adr4'].value;
		text += "<br/>"+document.forms['form'].elements['cp'].value;
		text += " "+document.forms['form'].elements['ville'].value;
		text += " "+document.forms['form'].elements['pays'].value;
		text += "<br/>Tel: "+document.forms['form'].elements['tel'].value;
		text += "<br/>Fax: "+document.forms['form'].elements['fax'].value;
	}
	else
	{
		var textAjax = document.getElementById("adr_"+document.form.elements['adresse'].options[document.form.elements['adresse'].selectedIndex].value+"_ajax").value;
		var text = document.getElementById("adr_"+document.form.elements['adresse'].options[document.form.elements['adresse'].selectedIndex].value).value;
	}
	
	xh.open("GET", "ajax/adrSession.php?adr="+textAjax, true); 
	xh.send(null);
}


function adresse_select()
{
	var id_selected = document.form.elements['adresse'].options[document.form.elements['adresse'].selectedIndex].value;
	if(id_selected == 0) 
	{
		document.getElementById("changement_adresse_nouv1").style.visibility = "visible";
		document.getElementById("changement_adresse_nouv2").style.visibility = "visible";
		document.getElementById("changement_adresse_nouv3").style.visibility = "visible";
		document.getElementById("changement_adresse_act").style.display = "none";
		document.getElementById("changement_adresse").style.height = "190px";
	}
	else
	{
		document.getElementById("changement_adresse_nouv1").style.visibility = "hidden";
		document.getElementById("changement_adresse_nouv2").style.visibility = "hidden";
		document.getElementById("changement_adresse_nouv3").style.visibility = "hidden";
		document.getElementById("changement_adresse_act").style.display = "block";
		document.getElementById("changement_adresse").style.height = "160px";
		document.getElementById("changement_adresse_act").innerHTML = document.getElementById("adr_"+document.form.elements['adresse'].options[document.form.elements['adresse'].selectedIndex].value).value;;
	}
}

function sauvCdeCli(cde)
{
	xhr = getXHR();
	xhr.open("GET", "ajax/sauvCdeClient.php?cde="+cde, true); 
	xhr.send(null);
}

function sauvInfoCmd(info)
{
	xhr = getXHR();
	info = info.replace(/\n/g, ":!");
	
	xhr.open("GET", "ajax/sauvInfoCmd.php?info="+info, true); 
	xhr.send(null);
}

function controleChiffre(input)
{
	if(isNaN(input.value))
	{	
		var text = input.value;
		var nouv_text = text.replace(/[^0-9]/g, '');
		input.value = nouv_text;
	}
}

function upper(input)
{
	input.value = input.value.toUpperCase();
}


function ajoute(cd)
{
	var qte_origine = document.forms["formulaire"].elements["nb_"+cd].value;
	document.forms["formulaire"].elements["nb_"+cd].value = parseInt(1)+parseInt(qte_origine);
	modifierPrix(cd, parseInt(1)+parseInt(qte_origine));
}

function supprime(cd)
{
	var qte_origine = document.forms["formulaire"].elements["nb_"+cd].value;
	if(qte_origine>1)
	{
		document.forms["formulaire"].elements["nb_"+cd].value = Math.ceil(qte_origine)-parseInt(1);
		modifierPrix(cd, Math.ceil(qte_origine)-parseInt(1));
	}
}

function changeQte(cd, qte)
{
	qte = qte.replace(/,/g, '.');
	qte = qte.replace(/[^0-9.]/g, '');
	
	modifierPrix(cd, qte);
	document.forms["formulaire"].elements["nb_"+cd].value = qte;
}

function modifierPrix(cd, qte)
{
	xhr = getXHR();
	
	xhr.onreadystatechange = function() {
	    if (xhr.readyState == 4) 
		{
			reponse = xhr.responseXML;

			document.getElementById("puht_"+cd).innerHTML 	= reponse.getElementsByTagName('PRIX_U')[0].firstChild.nodeValue;
			document.getElementById("ptot_"+cd).innerHTML 	= reponse.getElementsByTagName('PRIX_T')[0].firstChild.nodeValue;
			document.getElementById("pht").innerHTML 		= "<strong>"+reponse.getElementsByTagName('TOTAL_HT')[0].firstChild.nodeValue+" &euro;</strong>";
			document.getElementById("tva").innerHTML 		= reponse.getElementsByTagName('TVA')[0].firstChild.nodeValue+" &euro;";
			document.getElementById("fdp").innerHTML 		= reponse.getElementsByTagName('FDP')[0].firstChild.nodeValue+" &euro;";
			document.getElementById("pttc").innerHTML 		= reponse.getElementsByTagName('TOTAL_TTC')[0].firstChild.nodeValue+" &euro;"; 
	    }
    } 
	
	var unite = document.getElementById("unite_"+cd).innerHTML;

	xhr.open("GET", "ajax/modifierPrix.php?cd="+cd+"&qte="+qte+"&unite="+unite, true); 
	xhr.send(null);
}


function supArt(cd)
{
	xhr2 = getXHR();
	
	xhr2.onreadystatechange = function() {
	    if (xhr2.readyState == 4) 
		{
			reponse = xhr2.responseXML;

			document.getElementById("pht").innerHTML 		= "<strong>"+reponse.getElementsByTagName('TOTAL_HT')[0].firstChild.nodeValue+" &euro;</strong>";
			document.getElementById("tva").innerHTML 		= reponse.getElementsByTagName('TVA')[0].firstChild.nodeValue+" &euro;";
			document.getElementById("fdp").innerHTML 		= reponse.getElementsByTagName('FDP')[0].firstChild.nodeValue+" &euro;";
			document.getElementById("pttc").innerHTML 		= reponse.getElementsByTagName('TOTAL_TTC')[0].firstChild.nodeValue+" &euro;"; 
			
			document.getElementById("tr1_"+cd).style.display = "none";
			document.getElementById("tr2_"+cd).style.display = "none";
			
			var nbArt = reponse.getElementsByTagName('NB_ART')[0].firstChild.nodeValue;
			var t2 = "Votre panier est vide";
			var t3 = "";
			if(nbArt == 1)
			{
				t2 = "Votre panier comporte:";
				t3 = "1 article";
			}
			else if(nbArt > 1)
			{
				t2 = "Votre panier comporte:";
				t3 = nbArt+" articles";
			}
			
			document.getElementById("zone8_texte2").innerHTML = t2;
			document.getElementById("zone8_texte3").innerHTML = t3;
			
			
			// on remonte un article du dessous
			var noeuds = document.getElementById("tbody");
	
			var fini1 = false;
			var fini2 = false
			var i = 1;
			
			while(fini2 == false){
				if(noeuds.childNodes[i].nodeName == "TR"){
					if(noeuds.childNodes[i].style.visibility == "hidden"){
						if(fini1 == true){
							fini2 = true
						}
						fini1 = true;
						noeuds.childNodes[i].style.visibility = "visible";
					}
				}
				i++
			}
				
			//le bouton suivant est-il encore nécéssaire ?
			var href = getVar('deb');
			if(nbArt-href < 7)
			{
				document.getElementById("suivant").style.visibility = "hidden";
			}
			
			
	    }
	    window.location.reload();
    } 

	xhr2.open("GET", "ajax/supprimerArt.php?cd="+cd, true); 
	xhr2.send(null);
	
	
}


function supTout()
{
	xhr3 = getXHR();
	
	xhr3.onreadystatechange = function() {
	    if (xhr3.readyState == 4) 
		{
			reponse = xhr3.responseXML;

			document.getElementById("tbody").style.display 	= "none";
			document.getElementById("pht").innerHTML 		= "<strong>0,00 &euro;</strong>";
			document.getElementById("tva").innerHTML 		= "0,00 &euro;";
			document.getElementById("fdp").innerHTML 		= "0,00 &euro;";
			document.getElementById("pttc").innerHTML 		= "0,00 &euro;"; 
			
			document.getElementById("zone8_texte2").innerHTML = "Votre panier est vide";
			document.getElementById("zone8_texte3").innerHTML = "";
			
			document.getElementById("panier_vide").style.visibility = "visible";
			
			document.getElementById("btn_suiv").innerHTML = '<img src="img/interface/btn_etape_suiv.gif" alt="etape suivante" class="opacity"/>';
			document.getElementById("btn_eff").innerHTML = '<img src="img/interface/btn_tout_effacer.gif" style="margin-right:2px;" alt="tout effacer" class="opacity"/>';
	   
			document.getElementById("suivant").style.visibility = "hidden";
			document.getElementById("precedent").style.visibility = "hidden";	
		}
    } 

	xhr3.open("GET", "ajax/supprimerTout.php", true); 
	xhr3.send(null);
}


function getVar(param){
var strParam = new String(param);
var position = window.location.search.indexOf('&' + strParam + '=');
if (position==-1)
{
	position = window.location.search.indexOf('?' + strParam + '=');
}
if (position==-1)
{
	return '';
}
return (window.location.search.indexOf('&', position + strParam.length + 2)==-1) ? window.location.search.substr(position + strParam.length + 2) : window.location.search.substr(position + strParam.length + 2, window.location.search.indexOf('&', position + strParam.length + 2) - (position + strParam.length + 2));
}


function cmd_trans()
{
	xhr3 = getXHR();
	
	xhr3.onreadystatechange = function() {
	    if (xhr3.readyState == 4) 
		{
			alpha_on();
			document.getElementById("cmd_trans").style.display = "block";	
		}
    } 

	//control de l'email
	var email = document.getElementById("email").value;
	var regex = /^[a-zA-Z0-9\.\-_]+@[a-zA-Z0-9-]+\.[a-zA-Z\.]{2,10}$/i;
	if(regex.test(email))	
	{
		updateMail(email);
		
		//controle du champs CdeClient
		if(document.getElementById("cde_client").value != "")
		{
			xhr3.open("GET", "ajax/maj_commande.php?act=trans", true); 
			xhr3.send(null);
		}
		else
		{
			alert("Veuillez renseigner un code de commande");
			document.getElementById("cde_client").focus();
		}
	}
	else
	{
		alert("Veuillez renseigner une adresse e-mail valide");
		document.getElementById("email").focus();
	}
}


function cmd_trans_part()
{
window.location = "paiement.php";
}	


function cmd_trans_workflow()
{
	xhr3 = getXHR();
	
	xhr3.onreadystatechange = function() {
	    if (xhr3.readyState == 4) 
		{
			alpha_on();
			document.getElementById("cmd_trans").style.display = "block";	
		}
    } 

	xhr3.open("GET", "ajax/maj_commande.php?act=trans_wf", true); 
	xhr3.send(null);
}



function fin_cmd_trans()
{
	document.getElementById("cmd_trans").style.display = "none";
	alpha_off();
	window.location = "index.php";
}



function cmd_sauv()
{
	xhr3 = getXHR();
	
	xhr3.onreadystatechange = function() {
	    if (xhr3.readyState == 4) 
		{
			alpha_on();
			document.getElementById("cmd_sauv").style.display = "block";	
		}
    } 

	xhr3.open("GET", "ajax/maj_commande.php?act=sauv", true); 
	xhr3.send(null);
}



function fin_cmd_sauv()
{
	document.getElementById("cmd_sauv").style.display = "none";
	alpha_off();
	window.location = "commande_histo.php";
}


function cmd_raz()
{
	xhr = getXHR();
	
	xhr.onreadystatechange = function() {
	    if (xhr.readyState == 4) 
		{
			window.location = "index.php";
		}
    } 
	
	xhr.open("GET", "ajax/supprimerTout.php", true); 
	xhr.send(null);
	
}


// historique des commandes
function com_trans_popup(num)
{
	alpha_on();
	document.getElementById("popup_id_com_trans").innerHTML = num;
	document.getElementById("com_trans_popup").style.display = "block";
}

function com_trans_annule()
{
	document.getElementById("com_trans_popup").style.display = "none";
	alpha_off();
}

function com_trans_visu()
{
	com_trans_annule();
	window.location.href = "commande_imprimer.php?ecuvente="+document.getElementById("popup_id_com_trans").innerHTML;
}

function com_trans_charge_panier()
{
	com_trans_annule();
	window.location.href = "commande_histo.php?ecuvente="+document.getElementById("popup_id_com_trans").innerHTML;
}


function com_non_trans_popup(num)
{
	alpha_on();
	document.getElementById("popup_id_com_non_trans").innerHTML = num;
	document.getElementById("com_non_trans_popup").style.display = "block";
}

function com_non_trans_annule()
{
	document.getElementById("com_non_trans_popup").style.display = "none";
	alpha_off();
}

function com_non_trans_charge_panier()
{
	com_non_trans_annule();
	window.location.href = "commande_histo.php?com_id="+document.getElementById("popup_id_com_non_trans").innerHTML;
}

function bulle_profil_montre(){
	document.getElementById("img_bulle_profil").style.display = "block";
}

function bulle_profil_cache(){
	document.getElementById("img_bulle_profil").style.display = "none";
}


function workflow_restore(COM_ID)
{
	xhr = getXHR();
	
	xhr.onreadystatechange = function() {
	    if (xhr.readyState == 4) 
		{
			window.location = "commande_detail.php";
		}
    } 
	
	xhr.open("GET", "ajax/workflow_restore.php?com_id="+COM_ID, true); 
	xhr.send(null);
}

function supToutWf()
{
	xhr = getXHR();
	
	xhr.onreadystatechange = function() {
	    if (xhr.readyState == 4) 
		{
			window.location = "index.php";
		}
    } 
	
	xhr.open("GET", "ajax/workflow_delete.php", true); 
	xhr.send(null);
}


function updateMail(email)
{
	xhrm = getXHR();
	xhrm.open("GET", "ajax/update_mail_cmd.php?email="+email, true); 
	xhrm.send(null);
}
function load_commande(date)
{
chaine_date=date.value;


if(chaine_date!=1 && chaine_date!=0)
{
chaine_date=date.value;
var reg=new RegExp("[-]", "g");
tab_date=chaine_date.split(reg);
window.location.href = "commande_histo.php?date=" + tab_date[0] + "-" + tab_date[1];
}
/*else
{
window.document.getElementById("devis_mois").style.display="block";
window.document.getElementById("commandes_soldees").style.display="none";
}*/

}