/* Retrieve data */
function makeRequest(url, callback, temps) {

	var httpRequest = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { httpRequest = false; }
		}
	}
	if (!httpRequest) {
		alert('Abandon : Impossible de créer une instance XMLHTTP');
		return false;
	}
	
	if (callback != null)
		httpRequest.onreadystatechange = function() { eval(callback + '(httpRequest, ' + temps + ')'); };
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

/* Retrieve shopping cart content */
function getPanier(httpRequest) {
	var zonepanier = document.getElementById('cart_block_form');
	zonepanier.style.display = 'block';
	var zonetitrepanier = document.getElementById('basket_text_area');
	
	panier_html = '';
	panier_html = '<input type="hidden" name="action" value="update"/>';
	panier_html += '<input type="hidden" name="code_coupon" value="">';

	if (httpRequest.readyState == 4 && httpRequest.status == 200) {
		var xmldoc = httpRequest.responseXML;
		var RootNode = xmldoc.getElementsByTagName('panier').item(0);
		var is_vide = RootNode.attributes[0].nodeValue;
		var total_sans_fp = RootNode.attributes[1].nodeValue;
		var total_panier = RootNode.attributes[2].nodeValue;
		var nb_produits = RootNode.attributes[3].nodeValue;
		var frais_port = RootNode.attributes[4].nodeValue;
		var libelle_panier = RootNode.attributes[5].nodeValue;
		var libelle_fp_offer = RootNode.attributes[6].nodeValue;
		
		nb_produits_panier = nb_produits;
		
		if (is_vide == 'false') {
			panier_html += '<table cellspacing="0" class="cart-products-list">';
			panier_html += '<tfoot>';
			panier_html += '<tr><td>'+label_sous_total+'</td><td></td><td valign="middle" align="right"><span class="price">'+total_sans_fp+' '+devise+'</span></td></tr>';
			panier_html += '<tr><td>'+label_FraisPort+'</td><td></td><td valign="middle" align="right"><span class="price">'+frais_port+' '+devise+'</span></td></tr>';
			panier_html += '<tr><td class="basket-table-cell-total" style="color:#E70438!important;font-weight:bold;">'+label_Total+'</td><td></td><td valign="middle" align="right"><span class="price" style="color:#E70438!important;font-weight:bold;">'+total_panier+' '+devise+'</span></td></tr>';
			panier_html += '<tr><td colspan="3" style="border-top:1px solid #bdc1c0;padding-bottom:5px;"><b>'+libelle_fp_offer+'</b></td></tr>';
			//panier_html += '<tr class="validate"><td colspan="3" style="padding-left:10px; padding-right:9px;"><a href="'+pagesURL+'panier.cfm" title="'+label_voir_panier+'">'+label_voir_panier+'</a></td></tr>';
			panier_html += '</tfoot><tbody>';
			
			// Parse les produits
			for(var i=0;i<RootNode.childNodes.length;i++) {
				if(RootNode.childNodes[i].nodeType == 1) {
					var current = RootNode.childNodes[i].getAttribute('iscurrent');
					var produit_id = RootNode.childNodes[i].getAttribute('id');
					var panier_id = RootNode.childNodes[i].getAttribute('panier_id');
					var image = RootNode.childNodes[i].getElementsByTagName('image').item(0).firstChild.data;
					//alert(image);
					var prix_ttc = RootNode.childNodes[i].getAttribute('prix');
					var quantite = RootNode.childNodes[i].getAttribute('quantite');
					var attribs = RootNode.childNodes[i].getElementsByTagName('attribs')[0];
					var taille = attribs.getElementsByTagName('taille')[0].firstChild.data;
					var couleur = attribs.getElementsByTagName('couleur')[0].firstChild.data;
					var ean = RootNode.childNodes[i].getAttribute('code_ean');

					if (RootNode.childNodes[i].getElementsByTagName('url').item(0).firstChild == null)
						var produit_url = '';
					else {
						var produit_url = RootNode.childNodes[i].getElementsByTagName('url').item(0).firstChild.data;
					}
					var nom = RootNode.childNodes[i].getElementsByTagName('nom').item(0).firstChild.data;
					if (RootNode.childNodes[i].getElementsByTagName('nom_court').item(0).firstChild.data == null) {
						var nom_court = nom;
					}
					else {
						var nom_court = RootNode.childNodes[i].getElementsByTagName('nom_court').item(0).firstChild.data;
					}
					
					if(current == 'true'){
						getProduit(produit_id,prix_ttc,image,taille,couleur,nom,nom_court);
					}
					panier_html += '<tr>';
					panier_html += '<td>';
					if (produit_url == '') {
						panier_html += '<a title='+nom+'>'+nom_court+'</a><br/>'
					}
					else {
						panier_html += '<a href="'+produit_url+'" title="'+nom+'">'+nom_court+'</a><br/>';
					}
					panier_html += '<span class="attribs">'+couleur+' - '+taille+'</span></td>';
					panier_html += '<td></td>';
					panier_html += '<td valign="middle" align="right"><span class="price">'+prix_ttc+' '+devise+'</span></td>';
					panier_html += '</tr>';
				}
			}
			panier_html += '</tbody></table>';
			panier_html += '<table class="cart-view-bag"><tr class="validate"><td><a href="'+pagesURL+'panier.cfm" title="'+label_voir_panier+'">'+label_voir_panier+'</a></td></tr></table>';
			zonepanier.innerHTML = panier_html;
			zonetitrepanier.innerHTML = libelle_panier+'<span class="total"><strong>'+total_panier+' '+devise+'</strong></span>';
		}
		else {
			nb_produits_panier = 0;
			zonepanier.innerHTML = panier_html;
			zonetitrepanier.innerHTML = libelle_panier;
		}
	}
}

/* Retrieve product data */
function getProduit(produit_id,prix_ttc,image,taille,couleur,nom,nom_court) {
	/*var zone_ajoute = document.getElementById('cart_block_added');
	var bloc_html = '';
	
			
	bloc_html += '<h1 style="font-size:12px;">'+label_product_added+'</h1>';
	
	bloc_html += '<table cellspacing="0" class="cart-products-list">';
	bloc_html += '<colgroup><col style="width:175px;"/><col style="width:50px;"/><col style="width:15px;"/></colgroup>';
	bloc_html += '<tfoot>';
	//bloc_html += '<tr class="validate"><td colspan="3" style="padding-left:10px; padding-right:9px;"><a href="'+pagesURL+'panier.cfm" title="'+label_voir_panier+'">'+label_voir_panier+'</a></td></tr>';
	bloc_html += '</tfoot><tbody>';
	
	bloc_html += '<tr><td colspan="3" style="text-align:center;"><img src="'+image+'" alt="'+nom+'"/></td></tr>';
	bloc_html += '<tr>';
	bloc_html += '<td style="padding-left:10px;"><b>'+nom_court+'</b><br/><span class="attribs">'+couleur+' - '+taille+'</span></td>';
	bloc_html += '<td><span class="price">'+prix_ttc+' '+devise+'</span></td>';
	bloc_html += '<td>&nbsp;</td>';
	bloc_html += '</tr></tbody></table>';
	bloc_html += '<table class="cart-view-bag"><tr class="validate"><td style="padding-left:10px; padding-right:9px;"><a href="'+pagesURL+'panier.cfm" title="'+label_voir_panier+'">'+label_voir_panier+'</a></td></tr></table>';
	zone_ajoute.innerHTML =bloc_html ;
	switchProductAdded();
	// Masque au bout de 10 secondes
	setTimeout("switchProductAdded();", 5000);*/
}

/* Retrieve wishlist content */
function getWishlist(httpRequest) {
	var zonewishlist = document.getElementById('wishlist_block_form');
	var zonetitrewishlist = document.getElementById('wishlist_text_area');
	
	var wishlist_html = '';

	if (httpRequest.readyState == 4 && httpRequest.status == 200) {
		var xmldoc = httpRequest.responseXML;
		var RootNode = xmldoc.getElementsByTagName('wishlist').item(0);
		var is_vide = RootNode.attributes[0].nodeValue;
		var nb_produits = RootNode.attributes[1].nodeValue;
		var libelle_wishlist = RootNode.attributes[2].nodeValue;
		nb_produits_wishlist = nb_produits;
		
		if (is_vide == 'false') {
			wishlist_html += '<table cellspacing="0" class="cart-products-list">';
			
			wishlist_html += '<tbody>';
			
			// Parse les produits
			for(var i=0;i<RootNode.childNodes.length;i++) {
				if(RootNode.childNodes[i].nodeType == 1) {
					var produit_id = RootNode.childNodes[i].getAttribute('id');
					var attribs = RootNode.childNodes[i].getElementsByTagName('attribs')[0];
					var taille = attribs.getElementsByTagName('taille')[0].firstChild.data;
					var couleur = attribs.getElementsByTagName('couleur')[0].firstChild.data;
					
					if (RootNode.childNodes[i].getElementsByTagName('url').item(0).firstChild == null)
						var produit_url = '';
					else {
						var produit_url = RootNode.childNodes[i].getElementsByTagName('url').item(0).firstChild.data;
					}
					var nom_court = RootNode.childNodes[i].getElementsByTagName('nom_court').item(0).firstChild.data;
					
					wishlist_html += '<tr>';
					wishlist_html += '<td>';
					if (produit_url == '') {
						wishlist_html += '<a title='+nom_court+'>'+nom_court+'</a><br/>'
					}
					else {
						wishlist_html += '<a href="'+produit_url+'" title="'+nom_court+'">'+nom_court+'</a><br/>';
					}
					wishlist_html += '<span class="attribs">'+couleur+' - '+taille+'</span></td>';
					wishlist_html += '</tr>';
				}
			}
			wishlist_html += '</tbody></table>';
			wishlist_html += '<tfoot><table class="cart-view-bag"><tr class="validate"><td><a href="'+pagesURL+'compte_panier.cfm" title="'+label_voir_wishlist+'">'+label_voir_wishlist+'</a></td></tr></table></tfoot>';
			
			zonewishlist.innerHTML = wishlist_html;
			zonetitrewishlist.innerHTML = libelle_wishlist;
		}
		else {
			nb_produits_wishlist = 0;
			wishlist_html = '';
			zonewishlist.innerHTML = wishlist_html;
			zonetitrewishlist.innerHTML = libelle_wishlist;
		}
	}
}

/* Retrieve wishlist content */
function getLogin(httpRequest,temps) {
	var zonelogin = document.getElementById('block_login_entete');
	
	if (httpRequest.readyState == 4 && httpRequest.status == 200) {
		var textDoc = httpRequest.responseText;
		zonelogin.innerHTML = textDoc;
		}
		
	
}

/* Add product to shopping bag */
function reloadPage(){
	window.location=window.location;
}
function addToCart(formulaire) {

	var qte_souhaitee = 1;
	
	if(isPopup) {
		var the_form = window.cart_prod_fiche.document.forms[formulaire];
	} else var the_form = document.forms[formulaire];
	try {
	
		if(getObj('qte_select').options[getObj('qte_select').selectedIndex].value) qte_souhaitee = getObj('qte_select').options[getObj('qte_select').selectedIndex].value;
		
		var produit_id = the_form.elements['produit_id'].value;
		
		if(isPopup) {
			var panier_id = the_form.elements['panier_id'].value;
			if (the_form.attribs == null) {
				makeRequest(pagesURL+'utils/addToCart.cfm?is_xml=true&frmaction=add&produit_id='+produit_id+'&panier_id='+panier_id+'&quantite_souhaitee='+qte_souhaitee,'getPanier');
			} else {
				code_ean = the_form.elements['code_ean'].value;
				//alert(code_ean);
				//alert(panier_id);
				makeRequest(pagesURL+'utils/addToCart.cfm?is_xml=true&frmaction=add&produit_id='+produit_id+'&panier_id='+panier_id+'&quantite_souhaitee='+qte_souhaitee+'&attribs=1&code_ean='+code_ean,'getPanier');
			}
			setTimeout("reloadPage()", 750);
			
			dspPopUp(0,"poped_content_cart_prod");
			//isPopup=false;
		} else {
			if (the_form.attribs == null) {
				makeRequest(pagesURL+'utils/addToCart.cfm?is_xml=true&frmaction=add&produit_id='+produit_id+'&quantite_souhaitee='+qte_souhaitee,'getPanier');
			} else {
				code_ean = the_form.elements['code_ean'].value;
				makeRequest(pagesURL+'utils/addToCart.cfm?is_xml=true&frmaction=add&produit_id='+produit_id+'&quantite_souhaitee='+qte_souhaitee+'&attribs=1&code_ean='+code_ean,'getPanier');
			}
		}
		/* Retrieve product added */
		//setTimeout("makeRequest(pagesURL+'utils/getProduit.cfm?is_xml=true&produit_id='+produit_id, 'getProduit')", 750);
		// Affiche le bloc
		//switchProductAdded();
		// Masque au bout de 10 secondes
		//setTimeout("switchProductAdded();", 5000);
		//makeRequest(pagesURL+'utils/getPanier.cfm', 'getPanier');
	}
	catch (e) {
		the_form.submit();
	}
}

/* Add product to wishlist */
function addToWishlist(formulaire) {
	//alert('test');
	try {
		var the_form = document.forms[formulaire];
		var produit_id = the_form.elements['produit_id'].value;
		var option_id = the_form.elements['option_id'].value;
		
		if(isPopup) {
			var panier_save_id = the_form.elements['panier_save_id'].value;
			
			if (the_form.attribs == null) {
				makeRequest(pagesURL+'utils/addToCart.cfm?is_xml=true&frmaction=add_whishlist&produit_id='+produit_id+'&panier_save_id='+panier_save_id +'&option_id='+option_id+'&quantite_souhaitee=1');
			}
			else {
				code_ean = the_form.elements['code_ean'].value;
				makeRequest(pagesURL+'utils/addToCart.cfm?is_xml=true&frmaction=add_whishlist&produit_id='+produit_id+'&panier_save_id='+panier_save_id+'&option_id='+option_id+'&quantite_souhaitee=1&attribs=1&code_ean='+code_ean);
			}
			//setTimeout("reloadPage()", 750);
			
			dspPopUp(0,"poped_content_cart_prod");
		}
		else{
			if (the_form.attribs == null) {
				makeRequest(pagesURL+'utils/addToCart.cfm?is_xml=true&frmaction=add_whishlist&produit_id='+produit_id+'&option_id='+option_id+'&quantite_souhaitee=1');
			}
			else {
				code_ean = the_form.elements['code_ean'].value;
				makeRequest(pagesURL+'utils/addToCart.cfm?is_xml=true&frmaction=add_whishlist&produit_id='+produit_id+'&option_id='+option_id+'&quantite_souhaitee=1&attribs=1&code_ean='+code_ean);
			}
		}
		setTimeout("makeRequest(pagesURL+'utils/getWishlist.cfm', 'getWishlist')", 750);
	}
	catch (e) {
		document.forms[formulaire].submit();
	}
}

/* Remove product from cart */
function removeProductFromCart(panier_id) {
	makeRequest(pagesURL+'utils/addToCart.cfm?is_xml=true&frmaction=delete&panier_id='+panier_id,'getPanier');
	if(window.location.href.indexOf('/panier.cfm'))
		setTimeout("reloadPage()", 1250);
	
	//setTimeout("makeRequest(pagesURL+'utils/getPanier.cfm', 'getPanier')", 500);
}

/* Display 'product added' block */
function switchProductAdded() {
	var productadded_area = document.getElementById('cart_block_added');
	switch (productadded_area.style.display) {
		case "block":
			productadded_area.style.display = 'none';
			break;
		case "none":
			productadded_area.style.display = 'block';
			break;
		default:
			productadded_area.style.display = 'none';
			break;
	}
}