/***** BASKET *****/
/* 
Pour q'un élément soit draggable dans le panier il doit posséder la class : PI_boxProduct ou PI_boxProductList ou PI_boxProductDetail et comporter un id unique
Au sein de cet élément un div du type " <div class="PI_none" title="Barilla">1.40-1-278</div> " doit etre placé, il comporte le nom du produit (title), le prix et sepéraré d'un tiret la quantité et l'irefc (1.40-1-278)


/* 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";
}



/******************/
var Basket = Class.create();
Basket.prototype = {
	initialize: function(_div) {
		table=null;
		
		if($('basket')){
			myBasketDiv=_div;
			Droppables.add(myBasketDiv,{
				onDrop: this.updateAddBasket.bind(this)
			});
		}
		jar = new CookieJar({  
			expires:3600,   // seconds   
			path: '/'  
		});
		
		jar.remove('myCookieBasket');
		allItem={
			"product_name":[],
			"product_integral_name":[],
			"product_quantite":[],
			"product_ref":[],
			"product_id":[],
			"product_price":[],
			"product_html":[],
			"product_variety":[],
			"product_brand":[],
			"product_productName":[],
			"product_pricetotal" : []
		};
		awl_basket.AWL_getShoppingCart();
  	},
	
	updateAddBasket:function(element){
		droppableElement=gobalObj;
		this.add(droppableElement.id.substr(3,droppableElement.id.length));
	},
	add:function(id){
		indexItem=allItem.product_html.indexOf(id);
		/* MODIF IFRAME_PANIER */
		if($(id)!=null)
		{
			itemInfo=$(id).down(0);
			infoTab=itemInfo.innerHTML.split("-");
		}
		else
		{
			infoTab = new Array();
			infoTab[0] = "0.00";
			infoTab[1] = "1";
			var ref_prod = id.substring(1);
			infoTab[2] = ref_prod;
		}
		
		// récupération de la variété (irefc du produit)
		var productVariety=null;
		/* MODIF IFRAME_PANIER */
	   	if ($('choixVariete')) {
	   		productVariety=$('choixVariete').value;
	   	}
	   	else
	   	{
	   		if(window.frames[nom_iframe] && window.frames[nom_iframe].$('choixVariete'))
			{
				productVariety=window.frames[nom_iframe].$('choixVariete').value;
			}
	   	}
	   	/* MODIF IFRAME_PANIER */
	   	if ($('choixVariete'+infoTab[2]))
	   	{
	   		productVariety=$('choixVariete'+infoTab[2]).value;
	   	}
	   	else
	   	{if(window.frames[nom_iframe] && window.frames[nom_iframe].$('choixVariete'+infoTab[2]))
			{
				productVariety=window.frames[nom_iframe].$('choixVariete'+infoTab[2]).value;
			}
	   	}
    	// Ajout AWL
    	if(parseInt(infoTab[1])>0){
    		awl_basket.AWL_addProductInShoppingCart(infoTab[2],infoTab[0],parseInt(infoTab[1]),null,productVariety);
		}
	},
	refresh:function(){
		this.clearAllContent();
        table = Builder.node('table',{id:"tablelistitembasket",border:0,width:227,cellpadding:0,cellspacing:0});
        tbody = Builder.node('tbody');
		totalPrice=0;
		nbArticle=0;
    	if(allItem.product_name.length!=0)
    	{
	        for (xx=allItem.product_name.length-1;xx>-1;xx--)
	        {
				tr=this.addLineTab({_quantite:allItem.product_quantite[xx],_title:allItem.product_name[xx],_price:(allItem.product_pricetotal[xx])+"\u20ac",indexTab:xx,id:allItem.product_id[xx],_nomLong:allItem.product_integral_name[xx],_brand:allItem.product_brand[xx],_productName:allItem.product_productName[xx]});
				tbody.appendChild(tr);
				nbArticle+=allItem.product_quantite[xx];
				totalPrice+=allItem.product_quantite[xx]*allItem.product_price[xx]
				
			}
			tbody.appendChild(tr);
		}
		table.appendChild(tbody);	
		totalPrice=Math.round(totalPrice)/100;	
		var totalPriceView = ""+totalPrice;
		
		if(totalPriceView.length-3 != totalPriceView.indexOf(".") && totalPriceView.indexOf(".")!= -1){
			totalPriceView = totalPriceView+"0";
		}
		/* MODIF IFRAME_PANIER */
		if($('basket')) 
		{
			$('basketItems').appendChild(table);
		}
		else
		{
			if(window.frames[nom_iframe] && window.frames[nom_iframe].$('basket'))
			{
				window.frames[nom_iframe].$('basketItems').appendChild(table);
			}
		}
		
		if (nbArticle>1){
			plurielArt="s";
		}else{
			plurielArt="";
		}
		/* MODIF IFRAME_PANIER */
		if (nbArticle>0)
		{
			if($('AWL_linksPanier'))
			{
				$('AWL_linksPanier').style.display = "block";
			}
			else
			{
				if(window.frames[nom_iframe] && window.frames[nom_iframe].$('AWL_linksPanier'))
				{
					//Je masque cet ID pour l'iframe
					window.frames[nom_iframe].$('AWL_linksPanier').style.display = "none";
				}
			}
			if($('AWL_addOrderInBasket'))
			{
				$('AWL_addOrderInBasket').style.display = "none";
			}
			else
			{
				if(window.frames[nom_iframe] && window.frames[nom_iframe].$('AWL_addOrderInBasket'))
				{
					window.frames[nom_iframe].$('AWL_addOrderInBasket').style.display = "none";
				}
			}
			if($('basket'))
			{
				$('deleteAll').getElementsByTagName('a')[0].style.display = "block";
				if($$('#basketDetail .PI_puce').length>0) $$('#basketDetail .PI_puce')[0].style.display = "none";
			}
			else
			{
				if(window.frames[nom_iframe] && window.frames[nom_iframe].$('basket'))
				{
					window.frames[nom_iframe].$('deleteAll').getElementsByTagName('a')[0].style.display = "block"; 
					if($$('#basketDetail .PI_puce').length>0) $$('#basketDetail .PI_puce')[0].style.display = "none";
				}
			}
		}
		else
		{
			if($('AWL_linksPanier'))
			{
				$('AWL_linksPanier').style.display = "none";
			}
			else
			{
				if(window.frames[nom_iframe] && window.frames[nom_iframe].$('AWL_linksPanier'))
				{
					window.frames[nom_iframe].$('AWL_linksPanier').style.display = "none";
				}
			}
			if($('AWL_addOrderInBasket'))
			{
				$('AWL_addOrderInBasket').style.display = "block";
			}
			else
			{
				if(window.frames[nom_iframe] && window.frames[nom_iframe].$('AWL_addOrderInBasket'))
				{
					window.frames[nom_iframe].$('AWL_addOrderInBasket').style.display = "block";
				}
			}
			if($('basket')){
				$('deleteAll').getElementsByTagName('a')[0].style.display = "none"; 
				if($$('#basketDetail .PI_puce').length>0) $$('#basketDetail .PI_puce')[0].style.display = "block";
			}
			else
			{
				if(window.frames[nom_iframe] && window.frames[nom_iframe].$('basket'))
				{
					window.frames[nom_iframe].$('deleteAll').getElementsByTagName('a')[0].style.display = "none"; 
					if($$('#basketDetail .PI_puce').length>0) $$('#basketDetail .PI_puce')[0].style.display = "block";
				}
			}
		}
		/* MODIF IFRAME_PANIER */
		 if($('basket')) {
			//$('article').update("<strong>"+nbArticle+"</strong> : article"+plurielArt);
			if(totalPriceView=="0")
			 totalPriceView="0,00"
			$('price').update("<strong>"+totalPriceView+" \u20ac</strong>");
			this.setScroller();
		}
		
		/* MODIF IFRAME_PANIER */
		if($('basketFull')){
			$('articleFull').update("<strong>"+nbArticle+"</strong> : article"+plurielArt);
			$('priceFull').update("<strong>"+totalPriceView+" \u20ac</strong>");
		} 
	},
	
	addLineTab:function(obj){
		tr = Builder.node('tr');
		
		//remove item		
		imgmoins = Builder.node('img',{src:"/img/btn/moins-sm.gif",height:12,alt:"enlever un produit",style:"cursor:pointer"});
		td = Builder.node('td',{className:"PI_delProduct"},imgmoins);
		tr.appendChild(td);
		
		//quantitee
		td = Builder.node('td',{className:"PI_numberProduct"},obj._quantite);
		
		tr.appendChild(td);
		
		//additem		
		imgadd = Builder.node('img',{src:"/img/btn/plus-sm.gif",height:12,alt:"ajouter un produit",style:"cursor:pointer"});
		td = Builder.node('td',{className:"PI_addProduct"},imgadd);
		tr.appendChild(td);

		//nom
		td = Builder.node('td',{className:"PI_name"});
		divname = Builder.node('div',{title:obj._productName+" "+obj._brand,style:"cursor:default"},obj._title);
		td.appendChild(divname);
		tr.appendChild(td);	
		
		//prix
		td = Builder.node('td',{className:"PI_prix"});	
		divprice = Builder.node('div',obj._price);
		td.appendChild(divprice);
		tr.appendChild(td);
		
		//remove all item		
		imgsupp = Builder.node('img',{src:"/img/btn/del.gif",height:12,alt:"supprimer un produit",style:"cursor:pointer"});
		td = Builder.node('td',{className:"PI_suppProduct"},imgsupp);
		tr.appendChild(td);
		
		Event.observe(imgmoins, 'click', this.removeItem.bind(this,obj.id));
		Event.observe(imgadd, 'click', this.addItem.bind(this,obj.id));
		Event.observe(imgsupp, 'click', this.removeFullItem.bind(this,obj.id));
		return tr;
	},
	
	removeFullItem:function(id){
		e = this.getItem(id);
		
			// ajout AWL
     	   awl_basket.AWL_removeProductFromShoppingCart(allItem.product_ref[e]);

			allItem.product_name[e]		= null;
			allItem.product_quantite[e]	= null;
			allItem.product_integral_name[e]	= null;			
			allItem.product_ref[e]		= null;
			allItem.product_price[e]	= null;
			allItem.product_html[e]		= null;
			allItem.product_id[e]		= null;
			allItem.product_variety[e] = null;
			allItem.product_brand[e] = null;
			allItem.product_productName[e] = null;
			
			
			allItem.product_name	= allItem.product_name.compact();
			allItem.product_integral_name	= allItem.product_integral_name.compact();			
			allItem.product_quantite= allItem.product_quantite.compact();
			allItem.product_ref		= allItem.product_ref.compact();
			allItem.product_price	= allItem.product_price.compact();
			allItem.product_html 	= allItem.product_html.compact();
			allItem.product_id 		= allItem.product_id.compact();
			allItem.product_variety = allItem.product_variety.compact();
			allItem.product_brand = allItem.product_brand.compact();
			allItem.product_productName = allItem.product_productName.compact();
				// retrait AWL this.refresh();
			
			return true;
		
		
	},
	
	removeItem:function(id){
	  
		e = this.getItem(id);
		
		//alert("Qty : "+allItem.product_quantite[e]);
		
		if (allItem.product_quantite[e]==1)
		{
			this.removeFullItem(id);
		}
		else
		{
			allItem.product_quantite[e]-=1;
			if( allItem.product_quantite[e]<1){
				allItem.product_quantite[e]=1;
			}
			// ajout AWL
			var tabIrefcNIrefcVariety=allItem.product_ref[e].split("|");
			var irefc=tabIrefcNIrefcVariety[0];
			var irefcVariety='';
			if(tabIrefcNIrefcVariety.length>1) irefcVariety=tabIrefcNIrefcVariety[1];
			awl_basket.AWL_decrement(irefc,irefcVariety);
		}
		// retrait AWL this.refresh();
	},
	
	addItem:function(id){
		e = this.getItem(id);
		allItem.product_quantite[e]+=1;
		// ajout AWL
		var tabIrefcNIrefcVariety=allItem.product_ref[e].split("|");
		var irefc=tabIrefcNIrefcVariety[0];
		var irefcVariety='';
		if(tabIrefcNIrefcVariety.length>1) irefcVariety=tabIrefcNIrefcVariety[1];
		awl_basket.AWL_increment(irefc,irefcVariety);
	},
	
	addItemWithQty:function(id,qty){
		e = this.getItem(id);
		allItem.product_quantite[e]=qty;
		// ajout AWL
		var tabIrefcNIrefcVariety=allItem.product_ref[e].split("|");
		var irefc=tabIrefcNIrefcVariety[0];
		var irefcVariety='';
		if(tabIrefcNIrefcVariety.length>1) irefcVariety=tabIrefcNIrefcVariety[1];
		awl_basket.AWL_setShoppingCartQuantity(irefc,irefcVariety,qty);
	},
	
	clearAllContent:function(){	
	/* MODIF IFRAME_PANIER */
		if(window.frames[nom_iframe] || window.frames[nom_iframe]!=null)
		{
			if(window.frames[nom_iframe].$('tablelistitembasket'))
			{
				window.frames[nom_iframe].$('tablelistitembasket').remove();
			}
		}
		else
		{
			if($('tablelistitembasket'))
			{
				try
				{
					$('tablelistitembasket').remove();
				}
				catch (error)
				{
					//alert("PI_basket.js _ clearAllContent : "+error.message);
				}
			}
		}
	},
	
	deleteAll:function(){
		allItem={
			"product_name":[],
			"product_integral_name":[],			
			"product_quantite":[],
			"product_ref":[],
			"product_price":[],
			"product_id":[],
			"product_html":[],
			"product_brand":[],
			"product_productName":[],
			"product_variety":[]
		};
		// ajout AWL

		awl_basket.AWL_removeAllItems();
		// retrait AWL this.refresh();
		
	},
	// DEBUT ajout AWL
	deleteAllWhitoutAsk:function(){
		allItem={
			"product_name":[],
			"product_integral_name":[],			
			"product_quantite":[],
			"product_ref":[],
			"product_price":[],
			"product_id":[],
			"product_html":[],
			"product_brand":[],
			"product_productName":[],
			"product_variety":[]
		};
		awl_basket.AWL_removeAllItemsFromDeconnection();
	},
	//FIN Ajout AWL
	
	getItem:function(id){
		for (xx=0;xx<allItem.product_id.length;xx++){
			if(allItem.product_id[xx] == id){
				return xx;
			}
		}
	},
	
	
	synchronize:function(){
		//jsonrpc.AWL_getShoppingCart();
	},
	
	setScroller:function(e){
	/* MODIF IFRAME_PANIER */
		if(!$('basketItems')) return;
		//if((!$('basketItems') && window.frames[nom_iframe] == null) || (window.frames[nom_iframe] != null && window.frames[nom_iframe].$('basketItems') == null)) return;
		
		//if(window.frames[nom_iframe] == null)
		//{
			if(!myScrollBasket ){
				$('maskbasketItems').style.height = $('basketDetail').getHeight()-$('maskbasketItems').offsetTop+"px";
				$('scrollToolsbasket').style.top = $('maskbasketItems').offsetTop+"px";
				$('scrollToolsbasket').style.height = $('maskbasketItems').getHeight()+"px";
				$('scDnbasket').style.top = $('maskbasketItems').getHeight()-13+"px";
				$('cursorbasket').style.height = $('maskbasketItems').getHeight()-26+"px";
	
				if($("basketItems").offsetHeight<$('maskbasketItems').offsetHeight) return;
				
				myScrollBasket =  new Scroller ("vertical",{
									container:$("basketItems"),
									scrollbar:$('scrollToolsbasket'),
									mask:$("maskbasketItems"),
									scUp:$("scUpbasket"),
									scDn:$("scDnbasket"),
									cursorV:$("cursorbasket")
								},10,5);
				myScrollBasket.init();
			}
			else{
				if($("basketItems").getHeight() != this.scrollHeight)
				myScrollBasket.reload();
				
			}
			this.scrollHeight = $("basketItems").getHeight();
		/* }
		else
		{
			if(!myScrollBasket ){
				window.frames[nom_iframe].$('maskbasketItems').style.height = window.frames[nom_iframe].$('basketDetail').getHeight()-window.frames[nom_iframe].$('maskbasketItems').offsetTop+"px";
				window.frames[nom_iframe].$('scrollToolsbasket').style.top = window.frames[nom_iframe].$('maskbasketItems').offsetTop+"px";
				window.frames[nom_iframe].$('scrollToolsbasket').style.height = window.frames[nom_iframe].$('maskbasketItems').getHeight()+"px";
				window.frames[nom_iframe].$('scDnbasket').style.top = window.frames[nom_iframe].$('maskbasketItems').getHeight()-13+"px";
				window.frames[nom_iframe].$('cursorbasket').style.height = window.frames[nom_iframe].$('maskbasketItems').getHeight()-26+"px";
	
				if(window.frames[nom_iframe].$("basketItems").offsetHeight<window.frames[nom_iframe].$('maskbasketItems').offsetHeight) return;
				
				myScrollBasket =  new Scroller ("vertical",{
									container:$("basketItems"),
									scrollbar:$('scrollToolsbasket'),
									mask:$("maskbasketItems"),
									scUp:$("scUpbasket"),
									scDn:$("scDnbasket"),
									cursorV:$("cursorbasket")
								},10,5);
				myScrollBasket.init();
			}
			else{
				if(window.frames[nom_iframe].$("basketItems").getHeight() != this.scrollHeight)
				myScrollBasket.reload();
				
			}
			this.scrollHeight = window.frames[nom_iframe].$("basketItems").getHeight();
		}*/
	},
	
	/* 
	*	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)
	*/
	checkCPwithFunction:function(_function)
	{
		awl_basket.AWL_checkCPwithFunction(_function);
	}
	,
	
	/* 
	*	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)
	*/
	CPconfirme:function()
	{
		awl_basket.AWL_CPconfirme();
	}
}
