/* MODIF IFRAME_PANIER */
//Ajout du 17/08/09 JESSON Val stagiaire AWL (RI) pour un iframe contenant le panier
//Recherche du nom de l'iframe, s'il n'existe pas j'en cré un arbitraire pour ne pas faire planter le code
if(typeof(window.nom_iframe) == "undefined")
{
	var nom_iframe = "iframe_panier";
}

//fonction permettant d'afficher ou non un span(ou autre) selon le navigateur (ajout de IE en fin d'id pour IE)
function affichage_layer_panier(id_span,bool_affichage)
{
	if(bool_affichage)
	{
		if(document.all)
		{
			document.getElementById(id_span+"IE").style.display = "block";
		}
		else
		{
			document.getElementById(id_span).style.display = "block";
		}
	}
	else
	{
		if(document.all)
		{
			document.getElementById(id_span+"IE").style.display = "none";
		}
		else
		{
			document.getElementById(id_span).style.display = "none";
		}
	}
}


 //fonction permettant d'ajouter la derniere commande au panier
 // numero de la commande
 
 function AWL_addLastOrderInShoppingCart(numCmde, confirmAdress, verifCodePostal)
 {
 		
 		if (confirmAdress)
		{
		
			document.getElementById("verifCodePostal").value=0;
			addOrderInShoppingCart(numCmde);
		}
		else
 		if (parseInt(verifCodePostal)==0)
		{
			
			addOrderInShoppingCart(numCmde);
		}
		else
		if (parseInt(verifCodePostal)==1) 
		{
			
			AWL_getMessage('clientconnupremierevisiteOrder',numCmde,0,0,"0");
			//document.getElementById("verifCodePostal").value=0;
			
			
		}
		
 }



  //  
  // ** Classe AWL_ShoppingCartItem
  // Classe de conception du panier
  // entree : irefc, identifiant de l'article
  //          libellecourt , 
  //            qty, quantitié
  //            price, prix dans le panier
  //            maxAutorise, maximum autorisé
  //            promo, ??
  //            purchasePageData,
  //            updateServerInfo : mise à jour du serveur (true) ou non (false)
  
  
function AWL_ShoppingCartItem( irefc,libellecourt , qty, price, maxAutorise ) {
    
    
		this.qty = parseInt(qty, 10);
    this.getTotalPrice = function() {
			return(this.price * this.qty);
		}
		this.irefc = irefc ; // identifiant du produit
		if(libellecourt) this.libellecourt= libellecourt.replace(/&acute;/g, "'").replace(/&quot;/g, "\"");
		this.price = price;
		this.maxAutorise = parseInt(maxAutorise, 10);
		
	}


  //  
  // ** Classe AWL_Basket
  // Classe de manipulation du panier
  // entree : irefc, identifiant de l'article
  //          libellecourt , 
  //            qty, quantitié
  //            price, prix dans le panier
  //            maxAutorise, maximum autorisé
  //            promo, ??
  //            purchasePageData,
  //            updateServerInfo : mise à jour du serveur (true) ou non (false)
  
function AWL_Basket() {
  this.site;
	this.shop_id;
	// Hashmap containing shoppingCartIem with key=irefc
	this.shoppingCart = new Array();
	this.AWLbasket2PI= {
				"product_name":[],
				"product_integral_name":[],				
				"product_quantite":[],
				"product_ref":[],
				"product_id":[],
				"product_price":[],
				"product_html":[],
				"product_brand":[],
				"product_productName":[],
				"product_variety":[]
				}

	this.AWL_transformAWLBasket2PI = function() {
	    var j;
			this.AWLbasket2PI= {
				"product_name":[],
				"product_integral_name":[],				
				"product_quantite":[],
				"product_ref":[],
				"product_id":[],
				"product_price":[],
				"product_html":[],
				"product_variety":[],
				"product_pricetotal" : [],
				"product_productName" : [],
				"product_brand" : []
				}
      for (var j=0 ; j<this.shoppingCart.length ; j++) {

        	this.AWLbasket2PI.product_name[j]=this.shoppingCart[j].libelleTronque;
	       	this.AWLbasket2PI.product_integral_name[j]=this.shoppingCart[j].libellecourt;
		    this.AWLbasket2PI.product_quantite[j]=this.shoppingCart[j].qty;
		    this.AWLbasket2PI.product_ref[j]=this.shoppingCart[j].irefc;
		    this.AWLbasket2PI.product_price[j]=this.shoppingCart[j].price;
   		    this.AWLbasket2PI.product_pricetotal[j]=this.shoppingCart[j].totalPriceFormate;
   		    this.AWLbasket2PI.product_id[j]=this.shoppingCart[j].irefc;
		    this.AWLbasket2PI.product_html[j]="";
		    this.AWLbasket2PI.product_variety[j]=this.shoppingCart[j].irefc_variety;
		    this.AWLbasket2PI.product_productName[j]=this.shoppingCart[j].productName;
		    this.AWLbasket2PI.product_brand[j]=this.shoppingCart[j].brand;
		}
		return this.AWLbasket2PI;
  }
  // ** Fonction AWL_searchProductIndex
  //  fonction de recherche d'index  du produit dans le panier 
  // entree : irefc, identifiant de l'article
   
  // sortie : int ligne du produit dans le panier, -1 si non trouvé
  
	this.AWL_searchProductIndex = function(irefc) {
		var index;
		
		for(var index=0 ; index<this.shoppingCart.length ; index++) {
			if(this.shoppingCart[index].irefc == irefc) {
				return index;
			}
		}

		return (-1);
	}

  //  
  // ** Fonction AWL_addProductInShoppingCart
  // Add item in basket
  // entree : irefc, identifiant de l'article
  //          qty, quantité 
  //          price est inutile car recalculé 
  //          confirmAdresse, booleen de confirmation de l'adresse ou code postal lors du premier ajout au panier
  //          irefc_variety, si irefc est une offre irefc_variety correspond au produit choisi (variété)
    
  // sortie : boolean true : ajout effectué, false : ajout non effectué
 
	this.AWL_addProductInShoppingCart = function(irefc,price,qty,confirmAdresse,irefc_variety) {
		//alert("confirmAdresse : "+confirmAdresse+", verifCodePostal : "+document.getElementById("verifCodePostal").value);
		/* MODIF IFRAME_PANIER */
		if($('presScrollMaskLay'))
		{
			scrollTop=$('presScrollMaskLay').scrollTop;
		}
		else
		{
			if(window.frames[nom_iframe] && window.frames[nom_iframe].$('presScrollMaskLay'))
			{
				scrollTop=window.frames[nom_iframe].$('presScrollMaskLay').scrollTop;
			}
		}
		qty = parseInt(qty, 10);
		/* MODIF IFRAME_PANIER */
		if (!document.getElementById("verifCodePostal"))
		{
			//remoteCall.AWLRPC_addProductInShoppingCart(irefc,irefc_variety,price,qty);
			remoteCall.AWLRPC_verifBasketBeforeAddProduct(irefc,irefc_variety,1,price,qty);
		}
		else
		{
			if(document.getElementById("verifCodePostal"))
			{
				if (confirmAdresse)
				{
					document.getElementById("verifCodePostal").value=0;
					//remoteCall.AWLRPC_addProductInShoppingCart(irefc,irefc_variety,price,qty);
					remoteCall.AWLRPC_verifBasketBeforeAddProduct(irefc,irefc_variety,1,price,qty);
				}
				else
				if (document.getElementById("verifCodePostal").value==1) 
				{
					//document.getElementById("verifCodePostal").value=0;
					AWL_getMessage('clientconnupremierevisite',irefc,price,qty,irefc_variety);

				}
				else
				if (document.getElementById("verifCodePostal").value==2) 
				{
					AWL_getMessage('cpconnupremierevisite',irefc,price,qty,irefc_variety);
				}
				else
				if (document.getElementById("verifCodePostal").value==3) 
				{
					//remoteCall.AWLRPC_addProductInShoppingCart(irefc,irefc_variety,price,qty);
					remoteCall.AWLRPC_verifBasketBeforeAddProduct(irefc,irefc_variety,1,price,qty);
					//AWL_getMessage('verificationcp',irefc,price,qty,irefc_variety);
				}
				else
				{
					//remoteCall.AWLRPC_addProductInShoppingCart(irefc,irefc_variety,price,qty);
					remoteCall.AWLRPC_verifBasketBeforeAddProduct(irefc,irefc_variety,1,price,qty);
				}
			}
			else
			{
				if(window.frames[nom_iframe].document.getElementById("verifCodePostal"))
				{
					if (confirmAdresse)
					{
						window.frames[nom_iframe].document.getElementById("verifCodePostal").value=0;
						//remoteCall.AWLRPC_addProductInShoppingCart(irefc,irefc_variety,price,qty);
						remoteCall.AWLRPC_verifBasketBeforeAddProduct(irefc,irefc_variety,1,price,qty);
					}
					else
					if (window.frames[nom_iframe].document.getElementById("verifCodePostal").value==1) 
					{
						//document.getElementById("verifCodePostal").value=0;
						AWL_getMessage('clientconnupremierevisite',irefc,price,qty,irefc_variety);
					}
					else
					if (window.frames[nom_iframe].document.getElementById("verifCodePostal").value==2) 
					{
						AWL_getMessage('cpconnupremierevisite',irefc,price,qty,irefc_variety);
					}
					else
					if (window.frames[nom_iframe].document.getElementById("verifCodePostal").value==3) 
					{
						//remoteCall.AWLRPC_addProductInShoppingCart(irefc,irefc_variety,price,qty);
						remoteCall.AWLRPC_verifBasketBeforeAddProduct(irefc,irefc_variety,1,price,qty);
						AWL_getMessage('verificationcp',irefc,price,qty,irefc_variety);
					}
					else
					{
						//remoteCall.AWLRPC_addProductInShoppingCart(irefc,irefc_variety,price,qty);
						remoteCall.AWLRPC_verifBasketBeforeAddProduct(irefc,irefc_variety,1,price,qty);
					}
				}
			}
		}
		maked = true;
		return maked;
	}


	this.AWL_CPconfirme = function() 
	{
		if (document.getElementById("verifCodePostal") )
		{
			document.getElementById("verifCodePostal").value=0;
			//Mets à jour les balises du code postal
			refreshInfolivraison();
			
			
			// lance la methode a exécuter si elle existe
			if (document.getElementById("lanceMethode"))
			{
				if (document.getElementById("lanceMethode").value==1)
				{
					eval(document.getElementById("methodeAExecuter").value);
				}
				document.getElementById("lanceMethode").value=0;
				document.getElementById("methodeAExecuter").value="";
			}
		}
	}
	
	/* 
	*	Vérifie le code postal avant de la lancer la fonction
	*	(permet d'éviter d'avoir un seul article alors qu'on commande plusieurs articles
	*	si le systeme demande de confirmer le CP)
	*/
	this.AWL_checkCPwithFunction = function(_function) {
		
		if (document.getElementById("verifCodePostal"))
		{
			//mise à jour des balises qui permettront de lancer la function après la vérification du CP
			document.getElementById("lanceMethode").value=1;
			document.getElementById("methodeAExecuter").value=_function;		
			
			if (document.getElementById("verifCodePostal").value==0) 
			{
				this.AWL_CPconfirme();
			}
			else
			if (document.getElementById("verifCodePostal").value==1) 
			{
				AWL_getMessage('clientconnupremierevisite',"","","","",true);
			}
			else
			if (document.getElementById("verifCodePostal").value==2) 
			{
				AWL_getMessage('cpconnupremierevisite',"","","","",true);
			}
			else
			if (document.getElementById("verifCodePostal").value==3) 
			{
				AWL_getMessage('verificationcp',"","","","",true);
			}
		}
		return;
	}
	
  //  
  // ** Fonction AWL_removeProductFromShoppingCart
  // remove item of basket, return boolean
  // entree : irefc, identifiant de l'article
  
  // sortie : boolean true : retrait effectué, false : retrait non effectué
	
  this.AWL_removeProductFromShoppingCart = function(irefc) { 
		remoteCall.AWLRPC_removeProductFromShoppingCart(irefc);		
		return  true;
	}

  //  
  // ** Fonction AWL_setShoppingCartQuantity
  // fixe la quantité d'un article dans le panier
  // entree : irefc, identifiant de l'article
  //          qty, quantité
  //            
  // sortie : boolean true : ajout effectué, false : ajout non effectué
  
	this.AWL_setShoppingCartQuantity = function(irefc, irefcVariety, qty) {
//		alert("[AWL_setShoppingCartQuantity FUNCTION]");
		var index = -1 ;
	   	var isMaked = false;
	   	var find = false;
	   	// searching of item 
		for (var j=0 ; j<this.shoppingCart.length ; j++) {
			if(irefcVariety!=null && this.shoppingCart[j].irefc ==irefc+"|"+irefcVariety && !find){
				 index =  j;
				 find = true;
      		}else if(this.shoppingCart[j].irefc ==irefc && !find){
      			index =  j;
				find = true;
      		}
		}
		//alert("find:"+find+"|index:"+index+"|irefc:"+irefc+"|irefcVariety:"+irefcVariety);
	  	if(index!= -1 ){
		  var spcItem = this.shoppingCart[index];
		  remoteCall.AWLRPC_verifBasketBeforeAddProduct(irefc,irefcVariety,2,0,qty-spcItem.qty);	
		  spcItem.qty=spcItem.qty+(qty-spcItem.qty);
		  //alert("qty calculer : "+(spcItem.qty+(qty-spcItem.qty)));
		  this.shoppingCart[index]=spcItem;
		  isMaked = true; 
		}
		return isMaked;
	  
	}
	

	
  // ** Fonction AWL_decrement
  // Decrement the item quantity
  // entree : irefc, identifiant de l'article

  // sortie : boolean true :  effectué, false :  non effectué

	this.AWL_decrement = function(irefc,irefcVariety ){
		var index = -1 ;
	   	var isMaked = false;
	   	var find = false;
	   	// searching of item 
		for (var j=0 ; j<this.shoppingCart.length ; j++) {
			if(irefcVariety!=null && this.shoppingCart[j].irefc ==irefc+"|"+irefcVariety && !find){
				 index =  j;
				 find = true;
      		}else if(this.shoppingCart[j].irefc ==irefc && !find){
      			index =  j;
				find = true;
      		}
		}
	  	if(index!= -1 ){
		  var spcItem = this.shoppingCart[index];
	  	  remoteCall.AWLRPC_verifBasketBeforeAddProduct(irefc,irefcVariety,2,0,-1);	
		  spcItem.qty=spcItem.qty-1;
		  this.shoppingCart[index]=spcItem;
		  isMaked = true;
		}
		return isMaked;
	}

  
  // ** Fonction AWL_increment
  // Increment the item quantity
  // entree : irefc, identifiant de l'article

  // sortie : boolean true :  effectué, false :  non effectué

	this.AWL_increment = function(irefc,irefcVariety) {
		var index = -1 ;
	   	var isMaked = false;
	   	var find = false;
	   	// searching of item 
		for (var j=0 ; j<this.shoppingCart.length ; j++) {
			if(irefcVariety!=null && this.shoppingCart[j].irefc ==irefc+"|"+irefcVariety && !find){
				 index =  j;
				 find = true;
      		}else if(this.shoppingCart[j].irefc ==irefc && !find){
      			index =  j;
				find = true;
      		}
		}
	  	if(index!= -1 ){
		  var spcItem = this.shoppingCart[index];
   		  remoteCall.AWLRPC_verifBasketBeforeAddProduct(irefc, irefcVariety, 2,0, 1);	
		  spcItem.qty=spcItem.qty+1;
		  //alert("Qty item "+index+" : "+spcItem.qty);
		  this.shoppingCart[index]=spcItem;
		  isMaked = true;
		}
		 return isMaked;
	}


  //AWL_getTotalAmount: getTotalAmount
	this.AWL_getTotalAmount = function() {
		var amount = 0.0;
		if(this.shoppingCart.length == 0)
		  return 0.0;
		else if(this.shoppingCart.length != 0) {
	   	   for(var i=0 ; i<this.shoppingCart.length ; i++) {
			    amount += this.shoppingCart[i].qty * this.shoppingCart[i].price;
		    }
      return amount;
    }
		return  -1;
	}

 	// AWL_removeAllItems, remove all items of basket
  	this.AWL_removeAllItems = function() {
    	remoteCall.AWLRPC_removeAllItems();
    	return true; 
	}
	
	// AWL_removeAllItems if user deconnect, remove all items of basket
	this.AWL_removeAllItemsFromDeconnection = function() {
    	remoteCall.AWLRPC_removeAllItemsFromDeconnection();
    	return true; 
	}
	
	//AWL_getShoppingCart:  Retour Array contenant des ShoppingCartItem
	this.AWL_getShoppingCart= function() {
    	// cherche le panier
    	remoteCall.AWLRPC_getShoppingCart();
    	return this.shoppingCart;
	}

}



var basket_minAmount = 1;
var basket_maxAmount = 5000;//          basket_maxAmount , montant du panier maximal


var awl_basket = new AWL_Basket(); // panier manipulé par AWL

var remoteCall = new RPCcall();   // AJAX