if (navigator.appName == "Microsoft Internet Explorer") var ie = true; else var ie = false;
var idReq = 0;
var HttpList = createRequestObject();
var HttpListCadd = createRequestObject();

// ----------------------------
// CADDIE
// ----------------------------

// liaison référence -> titre, idpdt et prix
function getListRef(ref,i,totalLignes)
{
	var req = "caddie_updator_ref.php?ref="+ref+"&i="+i+"&totalLignes="+totalLignes+"&idReq="+idReq++;
	HttpListCadd.open('GET', req, true);
	HttpListCadd.onreadystatechange = handleXmlListCad;
	HttpListCadd.send(null);
}
function handleXmlListCad()
{
	//si le serveur répond
	if(HttpListCadd.readyState == 4)
	{
		repXml = clean(HttpListCadd.responseXML.documentElement);		
		var xmlListTab = new Array();
		var attTab = new Array("url","id","i");
		xmlListTab = XmlTabMaker(repXml,attTab);
		if(xmlListTab['error'] != '')
		{
			loadRef(xmlListTab);
		}
	}
}
function loadRef(XMLtab) 
{
	var titre = XMLtab['element'][0]['titre'];
	var idpdt = XMLtab['element'][0]['id'];
	var prix = XMLtab['element'][0]['prix'];
	var totalLignes = XMLtab['element'][0]['totalLignes'];
	var prix = Math.round(prix * 100)/100;
	var i = XMLtab['element'][0]['i'];

	// ecrit le titre
	var monTitre = document.getElementById('titre_' + i);
	monTitre.innerHTML = titre;
	// recupere l'id, prix du produit,total, quantité
	var monIdpdt = document.getElementById("idPdt_" + i).value = idpdt;
	var monPrix = document.getElementById("prixU_" + i).value = prix;
	var ecritureTotalPdt = document.getElementById('totalPdt_' + i).value = prix;
	var ecritureQt = document.getElementById('qtID_' + i).value = "1";
	// appel la fonction qui calcul le montant total
	calculTotal(totalLignes);
}

// liaison quantité -> prix total du pdt
function calcTotalPdt(prixTmp,qtTmp,i,totalLignes) 
{
	// on  convertit les chaines de caractères en FLOAT pour pouvoir les travailler
	var prix = parseFloat(prixTmp);
	var qt = parseFloat(qtTmp);
	// calcul du total et écriture
	var totalPdtTmp = prix * qt;
	var totalPdt = Math.round(totalPdtTmp * 100)/100;
	var ecritureTotalPdt = document.getElementById('totalPdt_' + i).value = totalPdt;
	// appel la fonction qui calcul le montant total
	calculTotal(totalLignes);
}

// Calcul le montant total du panier SANS les éventuelles réductions
function calculTotal(totalLignes) {
	var total;
	var totalTTC = 0;
	var totalPdtTmp;
	var totalPdt = parseFloat(totalPdtTmp);
	// on boucles sur les lignes présentes pour récuperer les prix totaux
	for(var i=0;i<=totalLignes;i++) {
		var monPrixTmp = document.getElementById('totalPdt_' + i).value;
		if(monPrixTmp != '') {
			var monPrix = parseFloat(monPrixTmp);
			totalTTC = monPrix + totalTTC;
		}
	}
	
	// on écrit
	var locationTotal = document.getElementById('totalPanier');
	locationTotal.innerHTML = "<b>" +totalTTC + " &euro;</b>";
}

// -------------------
// FONCTION POPUP
// -------------------
function PopupCentrer(page,largeur,hauteur,options) {
	var top=(screen.height-hauteur)/2;
	var left=(screen.width-largeur)/2;
	window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}

// ----------------------------------------------
// COORDONNÉES POUR LA PAGE COMMANDE_VALIDER.PHP
// ----------------------------------------------
function affiche_liv()
{
	var liv_perso_tab = document.getElementById('liv_perso_tab');
	var liv_mag_tab = document.getElementById('liv_mag_tab');
	/*if(document.getElementById('liv_perso').checked) {
		liv_perso_tab.style.display = "";
		liv_mag_tab.style.display = "none";
	}
	if(document.getElementById('liv_mag').checked) {
		liv_perso_tab.style.display = "none";
		liv_mag_tab.style.display = "";
	}*/
	liv_perso_tab.style.display = "";
	liv_mag_tab.style.display = "none";
}

/*FONCTIONS COMMUNES A AJAX*/

//creation de noeuds (XML et Html)
//d = objet de destination (id de la div...)
//t = type
//a = attributs exemple: ((attName1,attVal1),(attName2,attVal2),(...),...)
//v = value
function NodeCreator(d,t,a,v){
	
	//création de la nouvelle balise
	var ce = document.createElement(t);
	
	//init des valeurs
	var cv;
	
	//si valeurs
	if (v != "")
	{	
		//création de la valeure (txt)
		var cv = document.createTextNode(v);
		//assignation de la valeure
		ce.appendChild(cv);
	}
	
	//assignation des attributs
	for (var i=0; i<a.length; i++)
		{
		var tmpAttName = a[i][0];
		var tmpAttVal = a[i][1];
		
		var ca = document.createAttribute(tmpAttName);
		
		ca.nodeValue = tmpAttVal;
		ce.setAttributeNode(ca);
		}		
	
	//assignation du champ
	d.appendChild(ce);
	
	return ce;
	}


//Fonction permettant le traitement d'un XML pour le retourner 
//en tableau asscociatif (à chaque balise).
function XmlTabMaker(xml,attr){
	//Initialisation du tableau à retourner
	var tmpRefNodeTab = new Array();
	//Début du scrutage du noeud Parent
	if (xml.firstChild)
		{
		var niv = xml.childNodes;
		//Départ de la boucle de traitements des noeuds enfants
		for ( var i=0; i<niv.length; i++)
			{
			//Début de détection des attributs pour chaque noeud enfant
			//Initialisation du FLAG si attributs à faux.
			var isAttr = false;
			//Vérification de la présence d'attributs.
			if (niv[i].attributes) { isAttr = true; }else{ isAttr = false; }
			
			//Si des la présence est avérée,
			if (isAttr)
				{
				//si le nombre d'attributs est supperieur à 0,
				if (niv[i].attributes.length > 0)
					{
					//Initialisation du tableau contenant les attributs.
					var attTmpTab = new Array();

					//Départ des boucles de traitement des attributs du noeud
					//pour chaques attributs,
					for ( var j=0; j<niv[i].attributes.length; j++)
						{ 
						//insérer sa val dans le tableau des attributs à la case de son nom. 
						attTmpTab[niv[i].attributes[j].name] = niv[i].getAttribute(niv[i].attributes[j].name)
						}//Fin des boucles de traitement des attributs.
					}
				}//Fin de vérification des attributs du noeud enfant.

			//Début de la vérification du type de noeud enfant.
			//Si le noeud enfant contient d'autres noeud
			if (niv[i].firstChild)
				{ 
				//Initialisation du tableau contenant les autres noeuds.
				var tmpEnglobTab = new Array();
				
				//si le contenu n'est pas un tableau xhtml
				if (niv[i].nodeName != "table")
					{
					//Affectation des noeuds inferieur à tmpEnglobTab
					//(la fonction s'appel elle même.)
					tmpEnglobTab["c"] = XmlTabMaker(niv[i],attr);
					}
				else
					{
					//sinon affecter le tableau à tmpEnglobTab
					tmpEnglobTab["c"] = niv[i];
					}
				
				//Affectation attTmpTab à tmpEnglobTab si il contient des attributs.
				if (isAttr){ tmpEnglobTab["a"] = attTmpTab; }
				
				//Si le nom du noeud à déjà été référencé,
				if (tmpRefNodeTab[niv[i].nodeName])
					//affectation de tmpEnglobTab au tableau à retourner.
					{ tmpRefNodeTab[niv[i].nodeName].push(tmpEnglobTab); }
				//Sinon,
				else
					{
					//créer un tableau globale dans le noeud au nom de balise,
					var tmpCountNodeTab = new Array;
					//affecter tmpEnglobTab à la première place de tmpCountNodeTab,
					tmpCountNodeTab[0] = tmpEnglobTab;
					//affecter tmpCountNodeTab au tableau de retour.
					tmpRefNodeTab[niv[i].nodeName] = tmpCountNodeTab;
					}
				}
			//Si il ne contient pas d'autres noeuds
			else
				{  
				//Si il contient une valeure on retourne la valeure
				if (niv[i].nodeValue != null){ return niv[i].nodeValue; }
				//Si le noeud parent contient un noeud d'attributs 
				else
					{
					//Si référencé
					if (tmpRefNodeTab[niv[i].nodeName])
						{ 
						//affectation du noeud d'attributs au tableau de retour
						if (isAttr){ tmpRefNodeTab[niv[i].nodeName].push(attTmpTab); }
						else{ tmpRefNodeTab[niv[i].nodeName].push("empty attribute node"); }
						}
					//Sinon
					else
						{ 
						//Référencement et affectation du noeud d'attributs au tableau de retour 
						var tmpCountNodeAttTab = new Array; 
						
						if (isAttr){ tmpCountNodeAttTab[0] = attTmpTab; }
						else { tmpCountNodeAttTab[0] = "empty attribute node"; }
						
						tmpRefNodeTab[niv[i].nodeName] = tmpCountNodeAttTab;
						}
					
					}
				}
			}
		//retour du tableau
		return tmpRefNodeTab;
		}
	}

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
		{ ro = new ActiveXObject("Microsoft.XMLHTTP"); }
	else
		{ ro = new XMLHttpRequest(); }
    return ro;
}

//transforme un objet XML en string
function xmlToString(xml){
	var XmlToString = (new XMLSerializer()).serializeToString(xml);
	return XmlToString
	}

// Node cleaner
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;
}

// COMMENTAIRES
function affCom()
{
	var com = document.getElementById('addComents');
	if(com.style.display == "block") com.style.display = "none";
	else com.style.display = "block";
}

function mep(id)
{
	document.getElementById(id).value = '';
}

/* Montre / Cache un div */
function change_onglet(name)
{
    document.getElementById('onglet_'+anc_onglet).className = 'onglet';
    document.getElementById('onglet_'+name).className = 'active onglet';
    document.getElementById('contenu_onglet_'+anc_onglet).style.display = 'none';
    document.getElementById('contenu_onglet_'+name).style.display = 'block';
    anc_onglet = name;
}

function zoomOption( id )
{
    var element = $( 'zoom_'+id );
/* 	if( element.length > 0  )
	{
		element.style.display = 'block';
	} */

	element.style.display = 'block';
}

function dezoomOption( id )
{
    var element = $( 'zoom_'+id );
	/* 
    if( element.length > 0  )
    {
        element.style.display = 'none';
    }
 */	element.style.display = 'none';
}
