function SpcItem(prodId,qte,qteMaxVente,stockDispo,libelleWeb,prixU,send,proCug,prodIdFdr,topIdent,typeStockPanier,stockCirculantDispo,lieu){
	this.prodId = prodId ;
	this.prodIdFdr = prodIdFdr ;
	this.proCug = proCug;
	this.qteMaxVente = qteMaxVente;
	this.stockDispo = stockDispo;
	this.topIdent=topIdent;
	this.lieu=lieu;
	var tmpLibelle = libelleWeb.replace(/&acute;/g, "'");
	tmpLibelle = tmpLibelle.replace(/&quot;/g, "\"");
	tmpLibelle = tmpLibelle.replace(/&amp;amp;/g, "&");
	tmpLibelle = tmpLibelle.replace(/&amp;/g, "&");
	tmpLibelle = tmpLibelle.replace(/&#039;/g, "'");

	this.libelleWeb = tmpLibelle;
	this.prixU = prixU;
	this._qty = 0;
	
	this.setQte = function(qty,envoie){
		var laQty = parseInt(qty,10);
		this._qty = 0;
		
		if(laQty > 0){
			if(laQty > this._getQteMaxVente()){
				alert($("#quantiteMaxText").val() + " "+ this._getQteMaxVente()+")");
				laQty = parseInt(this._getQteMaxVente(),10);
			}
			this._qty = laQty;
			if(envoie) {
				persistantSPC.addProduct(this.prodId,this.prodIdFdr,this.lieu,this._qty);
			}
		} 
		else{
			if(this._getQteMaxVente()!=0){
				if(confirm($("#coffreSupprimerText").val() +" "+this.libelleWeb+" "+$("#coffreSupprimerDuCoffreText").val())){
					leCoffre.removeProduit(this.prodId,this.prodIdFdr);
					persistantSPC.removeProduct(this.prodId,this.prodIdFdr,this.lieu);
				} else {
					this.setQte(1);
				}
			}
		}
	}
	
	this.getQte = function(){
		return this._qty;
	}
	 
	this._getQteMaxVente = function(){
		var qteMax = parseInt(this.stockDispo);
		if(typeStockPanier=="NO")
		{
			if(this.qteMaxVente != 0 && this.qteMaxVente < qteMax){
				qteMax = parseInt(this.qteMaxVente);
			}
		}
		if(typeStockPanier=="SE_ACTIVE" || typeStockPanier=="SE_NON_ACTIVE")
		{
			if(this.qteMaxVente!=0)
				qteMax = parseInt(this.qteMaxVente);
			else
				qteMax = 50;
		}
		if(typeStockPanier=="SC_ACTIVE" || typeStockPanier=="SC_NON_ACTIVE_CRENEAU_OK")
		{
			qteMax=parseInt(this.stockDispo +  parseInt(stockCirculantDispo));
			if(this.qteMaxVente !=0 && this.qteMaxVente < qteMax)
			{
				qteMax = parseInt(this.qteMaxVente);
			}
		}
		return qteMax;
	}
	
	this.getTotalAmount = function(){
		var total = this._qty * this.prixU / 100;
		return total;
	}
	
	this.getTotalAmountStr = function(){
		var amount = this.getTotalAmount();
		
		var integerPart = Math.floor(amount);
		var decimalPart = Math.round(amount * 100 - integerPart * 100);

		if(decimalPart >= 100) {
			integerPart += decimalPart / 100;
			decimalPart %= 100;
		}
		var string = String(integerPart) + ',';
		if(decimalPart < 10)
			string += '0';
		string += String(decimalPart);
		
		return string;
	}
	
	this.setQte(qte,send);
}

function ShoppingCart(){

	this.elements = new Array();
	popupShown = false;
	
	this._ajaxOk = function(){
		var retour = true;

		if(!initDone)
			retour = false;
		if(!document.getElementById("basketArea"))
			retour = false;
			
		return retour;
	}
	
	this._searchProductIndex = function(idProd,idProdFdr) {

		if(this.elements==null || this.elements=='')  return (-1);
		for(var index=0 ; index<this.elements.length ; index++) {
			if(this.elements[index].prodId == idProd || this.elements[index].prodId == idProdFdr) {
				return index;
			}
		}
		return (-1);
	}
	
	
	this.triggerAdhesionPopin = function(shouldShow){
		if(shouldShow != "1"){
			return;
		}
		if((typeof(showAdhesionPopin) != "undefined") && showAdhesionPopin){
			this.showPopinAdhesion();
		}
	}
	
	this.showPopinAdhesion = function(){
		var popupContainer = $("#popupContentData");
		tb_show("", "/magasin/components/popinAdhesion-view.jsp?width=440&height=350", false);
		$("#TB_ajaxContent").css("height", "auto");
		showAdhesionPopin = false;
	}
	
	this.openPopupAdhesion = function(){
		var popupUrl = "http://www.auchan.fr/espaceCarte/inscription/inscription.jsp";
		window.open(popupUrl, "ouverture");
	}
	
	this.neverShowAdhesionPopinAgain = function(){
		$.get("/magasin/components/popinAdhesion-view.jsp",
			{ "desactivate" : "1" },
			function(response){ });
	}
	
	
	this.addProduit = function(idProd,qte,qteMaxVente,stock,libelle,prix,ajax,cug,idProdFdr,topIdent,typeStockPanier,stockCirculantDispo,lieu){
		if(this._ajaxOk()){
			var index = this._searchProductIndex(idProd,idProdFdr);
			if(index > -1){
				this.updateQte(idProd,qte,ajax,cug,idProdFdr,typeStockPanier,stockCirculantDispo);
			}
			else{
				if(qteMaxVente!=0 && qte > qteMaxVente)
					qte = qteMaxVente;
				
				if(typeStockPanier=="NO")
				{
					if(qte > stock)
						qte = stock;
				}
				
				if(typeStockPanier=="SC_ACTIVE" || typeStockPanier=="SC_NON_ACTIVE_CRENEAU_OK")
				{
					if(qte > (parseInt(stock) + parseInt(stockCirculantDispo)))
					{
						qte = stock;
					}
				}
				
				if(qte >= 0){
					if(ajax){
						var tmpSpc = new Array();
						tmpSpc[0] = new SpcItem(idProd,qte,qteMaxVente,stock,libelle,prix,true,cug,idProdFdr,topIdent,typeStockPanier,stockCirculantDispo,lieu);
						for(var i = 1;i<=this.elements.length;i++){
							tmpSpc[i] = this.elements[i-1];				
						}
						this.elements = tmpSpc;
						this.draw();
					}
					else{
						var longueur = this.elements.length;
						this.elements[longueur] = new SpcItem(idProd,qte,qteMaxVente,stock,libelle,prix,false,cug,idProdFdr,topIdent,typeStockPanier,stockCirculantDispo,lieu);
					}
				}
			}
			return false;
		}
		else{
			return true;
		}
	}
	
	
	this.initialize = function(idProd,qte,qteMaxVente,stock,libelle,prix,cug,idProdFdr,topIdent,typeStockPanier,stockCirculantDispo,lieu){
		if(qteMaxVente!=0 && qte > qteMaxVente)
			qte = qteMaxVente;
		if(qte > stock)
			qte = stock;
		if(qte >= 0){
				var longueur = this.elements.length;
				this.elements[longueur] = new SpcItem(idProd,qte,qteMaxVente,stock,libelle,prix,false,cug,idProdFdr,topIdent,typeStockPanier,stockCirculantDispo,lieu);
		}
	}
	
	
	this.updateQte = function(idProd,qte,ajax,cug,idProdFdr,typeStockPanier,stockCirculantDispo){

		var laQty = parseInt(qte,10);

		spcItem = this.elements[this._searchProductIndex(idProd,idProdFdr)];
		if(spcItem.prodId == idProd || spcItem.prodId == idProdFdr){
			spcItem.setQte(spcItem.getQte()+laQty,true);
			dansCoffre[cug] = spcItem.getQte();
		}
		
		this.draw();
		if(!ajax)
			return false;
	}
	
	this.removeProduit = function(idProd,idProdFdr){
		var index = this._searchProductIndex(idProd,idProdFdr);
		if(index > -1){
			var i = 0;
			var j = 0;
			
			var tmpSpc = new Array();
			
			while(i < this.elements.length){
				if(i != index){
					tmpSpc[j] = this.elements[i];
					j++;					
				}
				i++;
			}
		}
		this.elements = tmpSpc;
	}
	
	this.getTotalAmount = function(){
		var totalAmount = 0;
		for(var i=0;i<this.elements.length;i++){
			totalAmount = totalAmount+this.elements[i].getTotalAmount();
		}
		return totalAmount;
	}
	
	this.getTotalQte = function(){
		var totalQte = 0;
		for(var i=0;i<this.elements.length;i++){
			totalQte = totalQte + this.elements[i].getQte();
		}
		return totalQte;
	}
	
	this.getTotalQte = function(){
		var totalQte = 0;
		for(var i=0;i<this.elements.length;i++){
			totalQte = totalQte+this.elements[i].getQte();
		}
		return totalQte;
	}
	
	this.draw = function(){
		// on recupere le div conteneur
		var container = document.getElementById("basketArea");
		var taille = this.elements.length;
		
		if(taille ==3 && !popupShown)
		{
			showInciteOrder();
			persistantSPC.setPopupShown();
			popupShown = true;
		}
		
		if(taille>4){
			taille = 4;
		}
		if(taille != 0)
		{	
			var currencyLabel = "€";
			var currencyLabelInput = document.getElementById("currencyLabelText");
			if(currencyLabelInput){
				currencyLabel = currencyLabelInput.value;
			}
			
			var li,a, strong, text,br, a1, a2, a3, span, img;
			var ul = document.createElement("ul");
			ul.className = "listSelected";
			ul.setAttribute("id","produits_coffre");
			
			for(var i=0 ; i<taille; i++) {
				var item = this.elements[i];
			
				li = document.createElement("li");
				if(i==0)
					li.className = "first highlight";
				
				a1 = $("<a></a>")
					.addClass("selectedArticleName")
					.attr("href", "/magasin/components/voirProduit.jsp?produitId="+item.prodId+"&cug="+item.proCug)
					.text(item.libelleWeb)
					.attr("prodId", item.prodId)
					.attr("proCug", item.proCug)
					.click(function(event){
						chargeFicheProduit(976,"produitId="+$(this).attr("prodId")+"&cug="+$(this).attr("proCug"));
						return false;
					})[0];
				
				span = document.createElement("span");
				span.className = "selectedArticlePrice";
				strong = document.createElement("strong");
				text = document.createTextNode(item.getTotalAmountStr()+" "+currencyLabel);
				strong.appendChild(text);
				input = document.createElement("input");
				input.setAttribute("value",""+item.getQte());
				input.setAttribute("readOnly","readonly");
				input.setAttribute("id","selectedQty1");
				input.className = "inputQuantity";
				a2 = document.createElement("a");
				var qtePlus = item.getQte()+1;
				a2.setAttribute("href","javascript:leCoffre.updateQte( "+item.prodId+",1,true,"+item.proCug+","+item.prodIdFdr+");");
				a2.className = "btPlus";
				img = document.createElement("img");
				img.setAttribute("src","/img/common/pict_plus_cr.gif");
				img.setAttribute("alt","+");
				img.setAttribute("height","8");
				img.setAttribute("width","7");
				a2.appendChild(img);
				
				a3 = document.createElement("a");
				var qteMoins = item.getQte()-1;
				a3.setAttribute("href","javascript:leCoffre.updateQte( "+item.prodId+",-1,true,"+item.proCug+","+item.prodIdFdr+");");
				a3.className = "btMoins";
				img = document.createElement("img");
				img.setAttribute("src","/img/common/pict_moins_cr.gif");
				img.setAttribute("alt","-");
				img.setAttribute("height","7");
				img.setAttribute("width","7");
				a3.appendChild(img);
				
				span.appendChild(strong);
				span.appendChild(input);
				span.appendChild(a2);
				span.appendChild(a3);
				
				li.appendChild(a1);
				li.appendChild(span);
				
				ul.appendChild(li);

			}
			
			
			if(document.getElementById("produits_coffre")) {
				container.replaceChild(ul, document.getElementById("produits_coffre"));
			} else if(document.getElementById("emptyArea")){
				container.replaceChild(ul, document.getElementById("emptyArea"));
			}else{
				container.appendChild(ul);
			}
			ul = document.createElement("ul");
			ul.setAttribute("id","total_coffre");
			ul.className = "basketHeaderTotal";
			li = document.createElement("li");
			strong = document.createElement("strong");

			text = document.createTextNode(this.getTotalQte() +" " + $("#coffreArticleText").val());
			strong.appendChild(text);
			
			li.appendChild(strong);
			ul.appendChild(li);
			li = document.createElement("li");
			strong = document.createElement("strong");
			strong.className = "colorTxt";
			text = document.createTextNode(this.getTotalAmountStr()+" "+currencyLabel);
			strong.appendChild(text);
			li.appendChild(strong);
			ul.appendChild(li);
			li = document.createElement("li");
			li.setAttribute("id","urlAllerAuCoffre");
			a = document.createElement("a");
			a.setAttribute("href",$("#urlAllerAuCoffre").find('a').attr('href'));
			text = document.createTextNode($("#coffreCaisseText").val());
			a.appendChild(text);
			
			li.appendChild(a);
			ul.appendChild(li);
			
			if(document.getElementById("total_coffre")) {
				container.replaceChild(ul, document.getElementById("total_coffre"));
			} else {
				container.appendChild(ul);
			}
			
			Coffre_limiteNbCaract();
			
			if(document.getElementById("headerBasketBlock").getAttributeNode("class"))
			{
				if(document.getElementById("headerBasketBlock").getAttributeNode("class").value == "hiddenDiv")
				{
					if(document.getElementById("headerMeaBlock")){
						document.getElementById("headerMeaBlock").style.display = "none";
					}
					 if(document.getElementById("replaceLogDeco")){
						document.getElementById("replaceLogDeco").getAttributeNode("id").value = "logDeco";
					}
					document.getElementById("headerBasketBlock").getAttributeNode("class").value= "";
				}
			}
		}
		else{
			var div,h,p,a,text,br,strong,li,ul;
			
			div = document.createElement("div");
			div.setAttribute("id","emptyArea");
			h = document.createElement("h3");
			text = document.createTextNode($("#coffreVideText").val());
			h.appendChild(text);
			p = document.createElement("p");
			a = document.createElement("a");
			a.setAttribute("href","#");
			a.className = "popFirstVisitLink";
			text = document.createTextNode($("#DCACText").val());
			a.appendChild(text);
			p.appendChild(a);
			div.appendChild(h);
			div.appendChild(p);

			if(document.getElementById("produits_coffre")) {
				container.replaceChild(div, document.getElementById("produits_coffre"));
			} else if(!document.getElementById("emptyArea")){
				container.appendChild(div);
			}
			if(document.getElementById("total_coffre")) {
				ul = document.createElement("ul");
				ul.setAttribute("id","total_coffre");
				ul.className="basketHeaderTotal";
				br = document.createElement("br");
				strong = document.createElement("strong");
				text = document.createTextNode("0 " + $("#coffreArticleText").val());
				strong.appendChild(text);
				ul.appendChild(br);
				
				li = document.createElement("li");
				li.appendChild(strong);
				ul.appendChild(li);
				
				strong = document.createElement("strong");
				strong.className="colorTxt";
				
				var currencyLabel = "€";
				var currencyLabelInput = document.getElementById("currencyLabelText");
				if(currencyLabelInput){
					currencyLabel = currencyLabelInput.value;
				}
				text = document.createTextNode("0,00 "+currencyLabel);
				strong.appendChild(text);
				
				li = document.createElement("li");
				li.appendChild(strong);
				ul.appendChild(li);
				
				li = document.createElement("li");
				li.setAttribute("id","urlAllerAuCoffre");
				li.className="hiddenDiv";
				a = document.createElement("a");
				a.setAttribute("href",$("#urlAllerAuCoffre").find('a').attr('href'));
				li.appendChild(a);
				ul.appendChild(li);
				container.replaceChild(ul, document.getElementById("total_coffre"));
			}
			if(document.getElementById("headerMeaBlock")){
				if(document.getElementById("logDeco")){
					document.getElementById("logDeco").getAttributeNode("id").value = "replaceLogDeco";
				}
				document.getElementById("headerBasketBlock").getAttributeNode("class").value = "hiddenDiv";
				document.getElementById("headerMeaBlock").style.display="block";
			}
			initFirstVisit();
		}
	}
	
	this.getTotalAmountStr = function(){
		var amount = this.getTotalAmount();
		
		var integerPart = Math.floor(amount);
		var decimalPart = Math.round(amount * 100 - integerPart * 100);

		if(decimalPart >= 100) {
			integerPart += decimalPart / 100;
			decimalPart %= 100;
		}
		var string = String(integerPart) + ',';
		if(decimalPart < 10)
			string += '0';
		string += String(decimalPart);
		
		return string;
	}
	
	this.emptyBasket = function(){
		if(confirm($("#coffreViderText").val())){
			this.elements = new Array();
			persistantSPC.emptyBasket();
			this.draw();
		}
		return false;
	}
}

function addProdToListe(creation,description,nom,productRef){
	try{
		if(nom != null && nom !=""){
  		  m=nom.replace(/~@/g,"E");
			if(productRef!=null && productRef !=""){
				persistantSPC.addProductToList(creation,description,nom,productRef);
			}
			if(creation){
				try {
					document.getElementById("newlistname").value = "";
					document.getElementById("comments").value = "";
					var option = document.createElement("option");
					option.setAttribute("value",nom);
					var text = document.createTextNode(nom);
					option.appendChild(text);
					document.getElementById("existList").appendChild(option);
					document.getElementById("submitAddToList2").setAttribute("onclick","javascript:return false;");
					document.getElementById("existList").disabled = "";
				} catch (e) {
				}
			}
			hideAddList();
		} else {
			alert($("#listeRenseignerText").val());
		}
		return false;
	} catch(e){
		return false;


	}
}

function Coffre_limiteNbCaract()
{
	$("#basketArea").children("ul").children("li").children('.selectedArticleName').each(function()
		{
			var chaine=$(this).html();
			if (chaine.length>40)
			{
			chaine2=chaine.substr(0,40)+"&hellip;";
			$(this).html(chaine2);
			}
			}
		)
}

var leCoffre = new ShoppingCart();
var persistantSPC = new RPCcall();
