var _xmlHttp = null; //l'objet xmlHttpRequest utilisé pour contacter le serveur
var _piedHttp = null; //l'objet xmlHttpRequest utilisé pour contacter le serveur
function getXMLHTTP(){
	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) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e1) {
				xhr = null;
			}
		}
	}else { // XMLHttpRequest non supporté par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	}
	return xhr;
}

var _resultListeID=new Object(); // recup des id des valeurs textes
function traiteXmlSuggestions(xmlDoc) {
  var options = xmlDoc.getElementsByTagName('option');
  var optionsListe = new Array();
  for (var i=0; i < options.length; ++i) {
    optionsListe.push(options[i].firstChild.data);
    _resultListeID[options[i].firstChild.data] = options[i].attributes.getNamedItem("value").value;
  }
  return optionsListe;
}

function call_infos_transporteurs(url,type){
	if(_xmlHttp && _xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}	
	
	_xmlHttp=getXMLHTTP();
	
	if(_xmlHttp){
		//appel à l'url distante
		_xmlHttp.open("GET",url,true);
		_xmlHttp.onreadystatechange=function() {
			//alert(_xmlHttp.readyState + " - " + _xmlHttp.responseXML);
			if(_xmlHttp.readyState==4 && _xmlHttp.responseXML) {
				while (document.modif.documents_transporteurs_id.options.length>0){
					document.modif.documents_transporteurs_id.options[0] = null;
				}
				var liste = traiteXmlSuggestions(_xmlHttp.responseXML);
				
				document.modif.documents_transporteurs_id.options.length = liste.length;
				
				for(var f=0; f<liste.length; ++f){
					document.modif.documents_transporteurs_id.options[f].value = _resultListeID[liste[f]];
					document.modif.documents_transporteurs_id.options[f].text = liste[f];
				}
				
				
			}
		};
		// envoi de la requete
		_xmlHttp.send(null);	
		//call_pied(type);
	}
}

function call_transporteurs(transporteur_id,type){
	if(_xmlHttp && _xmlHttp.readyState!=0){
		_xmlHttp.abort()
	}	

	/*_xmlHttp=getXMLHTTP();
	
	if(_xmlHttp){
		//appel à l'url distante
		_xmlHttp.open("GET","p_prix_transporteurs.php?id="+transporteur_id.value,true);
		_xmlHttp.onreadystatechange=function() {
			//alert(_xmlHttp.readyState + " - " + _xmlHttp.responseXML);
			if(_xmlHttp.readyState==4 && _xmlHttp.responseXML) {
				
			}
		};
		// envoi de la requete
		_xmlHttp.send(null);	
		call_pied(type);
	}*/
}



function call_pied(type){
	if(_piedHttp && _piedHttp.readyState!=0){
		_piedHttp.abort()
	}	

	_piedHttp=getXMLHTTP();
	
	if(_piedHttp){
		//appel à l'url distante
		_piedHttp.open("GET","p_pied_page.php",true);
		_piedHttp.onreadystatechange=function() {
			//alert(_xmlHttp.readyState + " - " + _xmlHttp.responseXML);
			if(_piedHttp.readyState==4 && _xmlHttp.responseXML) {
				var options = _piedHttp.responseXML.getElementsByTagName('option');
				if(type == 1){
					for (var i=0; i < options.length; ++i) {
						if(options[i].attributes.getNamedItem("value").value == "totaux_machandise_ht"){
							document.getElementById("L_16").innerHTML = "";
							document.getElementById("L_16").innerHTML = '<td class="champ" align="left" valign="top" width="150">Marchandise : H.T.</td><td class="value" align="left" valign="top"><b>'+options[i].firstChild.data+'</b>&nbsp;</td>';
						}
						
						if(options[i].attributes.getNamedItem("value").value == "totaux_port_ht"){
							document.getElementById("L_17").innerHTML = "";
							document.getElementById("L_17").innerHTML = '<td class="champ" align="left" valign="top" width="150">Frais de port H.T.</td><td class="value" align="left" valign="top"><b>'+options[i].firstChild.data+'</b>&nbsp;</td>';
						}
						
						if(options[i].attributes.getNamedItem("value").value == "totaux_total_ht"){
							document.getElementById("L_18").innerHTML = "";
							document.getElementById("L_18").innerHTML = '<td class="champ" align="left" valign="top" width="150">Total H.T.</td><td class="value" align="left" valign="top"><b>'+options[i].firstChild.data+'</b>&nbsp;</td>';
						}
						
						if(options[i].attributes.getNamedItem("value").value == "totaux_montant_tva"){
							document.getElementById("L_19").innerHTML = "";
							document.getElementById("L_19").innerHTML = '<td class="champ" align="left" valign="top" width="150">Total T.V.A.</td><td class="value" align="left" valign="top"><b>'+options[i].firstChild.data+'</b>&nbsp;</td>';
						}
						
						if(options[i].attributes.getNamedItem("value").value == "totaux_total_ttc"){
							document.getElementById("L_20").innerHTML = "";
							document.getElementById("L_20").innerHTML = '<td class="champ" align="left" valign="top" width="150">Total T.T.C.</td><td class="value" align="left" valign="top"><b>'+options[i].firstChild.data+'</b>&nbsp;</td>';
						}
					}
				}else{
					for (var i=0; i < options.length; ++i) {
						if(options[i].attributes.getNamedItem("value").value == "totaux_machandise_ht"){
							document.getElementById("L_26").innerHTML = "";
							document.getElementById("L_26").innerHTML = '<td class="champ" align="left" valign="top" width="150">Marchandise : H.T.</td><td class="value" align="left" valign="top"><b>'+options[i].firstChild.data+'</b>&nbsp;</td>';
						}
						
						if(options[i].attributes.getNamedItem("value").value == "totaux_port_ht"){
							document.getElementById("L_27").innerHTML = "";
							document.getElementById("L_27").innerHTML = '<td class="champ" align="left" valign="top" width="150">Frais de port H.T.</td><td class="value" align="left" valign="top"><b>'+options[i].firstChild.data+'</b>&nbsp;</td>';
						}
						
						if(options[i].attributes.getNamedItem("value").value == "totaux_total_ht"){
							document.getElementById("L_28").innerHTML = "";
							document.getElementById("L_28").innerHTML = '<td class="champ" align="left" valign="top" width="150">Total H.T.</td><td class="value" align="left" valign="top"><b>'+options[i].firstChild.data+'</b>&nbsp;</td>';
						}
						
						if(options[i].attributes.getNamedItem("value").value == "totaux_montant_tva"){
							document.getElementById("L_29").innerHTML = "";
							document.getElementById("L_29").innerHTML = '<td class="champ" align="left" valign="top" width="150">Total T.V.A.</td><td class="value" align="left" valign="top"><b>'+options[i].firstChild.data+'</b>&nbsp;</td>';
						}
						
						if(options[i].attributes.getNamedItem("value").value == "totaux_total_ttc"){
							document.getElementById("L_30").innerHTML = "";
							document.getElementById("L_30").innerHTML = '<td class="champ" align="left" valign="top" width="150">Total T.T.C.</td><td class="value" align="left" valign="top"><b>'+options[i].firstChild.data+'</b>&nbsp;</td>';
						}
					}
				}
				document.getElementById("total").innerHTML = "";
				document.getElementById("total").innerHTML = options[4].firstChild.data;
			}
		};
		// envoi de la requete
		_piedHttp.send(null);	
		
	}
}

function change_transporteurs(pay_l_id, type){
	var url="p_transporteurs.php";
	
	var _url = url+"?pays_id="+pay_l_id.value;
	call_infos_transporteurs(_url, type);
}

function verif_login(theForm) {
	strerreurs='';
	if (theForm.up.value=='') { strerreurs+='- Le mot de passe est un champ obligatoire.\n'; }
	if (theForm.ul.value=='') { strerreurs+='- L\'email est obligatoire.\n'; }
	else {
		re = /^[\+\w._-]+@[\w.-]{2,}[.][a-zA-Z]{2,4}$/;
		if (! re.test(theForm.ul.value)) {
			strerreurs+='- L\'email est mal saisi ou invalide.\n';
		}
		else {
			re = /(@\.)|(\.@)|(\.\.)/;
			if ( re.test(theForm.ul.value)) {
				strerreurs+='- L\'email est mal saisi ou invalide.\n';
			}
		}
	}
	if (strerreurs.length>0) {
		alert('Erreurs : \n'+strerreurs);
		return false;
	} else {	
		return true;
	}
}

function delete_file2(myLinks) {
	url = myLinks[document.getElementById("idcours").value];
	if(confirm('Voulez-vous vraiment supprimer ce fichier ?')) {
		document.location.href=url;
	}else{
		return false;
	}
}

function delete_file(url) {
	//alert(id);
	//document.fichier_delete.fid.value=id;
	if(confirm('Voulez-vous vraiment supprimer ce fichier ?')) {
		//document.fichier_delete.submit();
		document.location.href=url;
	}else{
		return false;
	}
}

function delete_img(url) {
	if(confirm('Etes-vous sur de vouloir supprimer l\'image?')){
		document.location.href=url;
	}else{
		return false;
	}
}
function confirm_delete(url){
	//alert(url);
	if(confirm('Voulez-vous vraiment supprimer cette information ?')) {
		document.location.href=url;
	}else{
		return false;
	}
}
function confirm_delete_fichier(){
	if(confirm('Voulez-vous vraiment supprimer ce fichier ?')) {
		return true;
	}else{
		return false;
	}
}
function changeimg( sens ) { //sens -> 1 ou -1
	img_encours += sens;
	objDiv1.style.display = "block";
	objDiv2.style.display = "block";
	if (img_encours == 0) {
	objDiv1.style.display = "none";
	}
	if (img_encours == myImages.length-1) {
	objDiv2.style.display = "none";
	}
	document.getElementById("idcours").value = img_encours;
	document.photo.src=myImages[img_encours].src;
	//document.form_suppr.fichiers_nom.value=myImages[img_encours].value;
	document.photo.width=myImages[img_encours].width;
	document.photo.height=myImages[img_encours].height;
}
function lignes_affiche(d, f) {
	//alert("d : "+d+" - f : "+f);
	var debut = d*1;
	var fin = f*1;
	while (debut <= fin) {
		//alert("ligne : "+debut+" - f : "+fin);
		objNav_to_open = document.getElementById("L_"+debut);
		//alert(objNav_to_open);
		if (objNav_to_open != null) {
			if (objNav_to_open.style.display=="none") {
				objNav_to_open.style.display="";
			} else {
				objNav_to_open.style.display="none";
			}
		}
		debut++;
	}
}
