// Auteur David Delpierre
// ---------------------------- Element graphique
/*
	Pour chaque produit <i> :
		img_ divCol_ selCol_ divTaille_ selTaille_ selQte_
		decote_ prixBarre_ depuis_ euro_ cents_ 
		radical_ dispo_ 
		
	Recap "panier" :  recapPrix  recapRemise recapStatus
*/

function getValueOfElement(sId) {
	oObj = document.getElementById(sId);
	if (oObj==null) return -1;
	return parseInt(oObj.value);
}

function getInnerHTMLOfElement(sId) {
	oObj = document.getElementById(sId);
	if (oObj==null) return null;
	return oObj.innerHTML;
}

function displayBlock(sId, bIsOn){
	var oDiv=document.getElementById(sId) ;
	if (bIsOn) {
		oDiv.style.visibility='visible';
		oDiv.style.display='block';
	} else {
		oDiv.style.visibility='hidden';
		oDiv.style.display='none';
	}
}

// ------------------------------ Evenement
/* DI 395 : Envoi à une amie : liens de renvoi sur le site HS*/
// ajout d'un parametre url a la fonction
function openSendFriend(iArt, url) {
	var oImg, sImgUrl;
	oImg = document.getElementById('img_' + iArt);
	if (oImg) {
	    sImgUrl = escape(oImg.src) ;
	} else {
	    sImgUrl = "";
	}
	
	/* CBL DI 269 : Ajout du chemin de l'image en paramètre */
	var ochemin, chemin;
	ochemin = document.getElementById('chemin_' + iArt);
	if (ochemin) {
		chemin = escape(ochemin.value) ;
	} else {
		chemin = "";
	}
	
	a=window.open("/VAD/PopupSendFriend?chemin="+chemin+"&urlPageProduit="+url, "_blank", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=590,height=370") ;
	a.focus();
}

function openDetail(iArt)	{
	document.getElementById('d_' + iArt).style.display='block';
	document.getElementById('dlo_' + iArt).style.display='none';
	document.getElementById('dlf_' + iArt).style.display='block';
}

function closeDetail(iArt) {
	document.getElementById('d_' + iArt).style.display='none';
	document.getElementById('dlo_' + iArt).style.display='block';
	document.getElementById('dlf_' + iArt).style.display='none';
}

function openAide() {
	var wPopup = window.open("/style1/pdf/Quelle-Guide-des-tailles.pdf","aide","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=720,height=600");
	wPopup.focus();
} 
 
function openCoinCoul() {
	var wPopup = window.open("http://www.quelle.fr/style1/HTML/popUps/ConditionOffre.html","aide","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=720,height=600");
	wPopup.focus();
} 

function zoomImage(sUrl, title) {
	var wPopup =window.open(sUrl, "_blank", "toolbar=0, location=0, directories=0, status=0, scrollbars=0, resizable=0, copyhistory=0, menuBar=0, width=520, height=600");
	wPopup.focus();
}

function openAutresVues(sUrl, title) {
	var wPopup = window.open(sUrl,"_blank","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=520,height=600");
	wPopup.focus();
}

function openServices(sUrl) {
	var wPopup = window.open(sUrl,"Aide","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=790,height=600");
	wPopup.focus();
}

// evenement sur les s?lection de couleur / taille / quantit? 

function changeCouleur(iArt){
	oDivRadical = document.getElementById("radical_" + iArt);
	iRad = getValueOfElement("selCol_" + iArt);
	if (iRad==-1) {
		oDivRadical.innerHTML = '';
	} else {
		oDivRadical.innerHTML = aArticle[iArt].aRadical[iRad].sRadicalId ;
	}
	displayTaille(iArt);
	document.getElementById("selQte_" + iArt).selectedIndex=0;
	
	f_totalOffre();
	//calculPanier();
}


function changeTaille(iArt){
	oSelTaille=document.getElementById("selTaille_" + iArt);
	iRad = getValueOfElement("selCol_" + iArt);
	if (oSelTaille== null) return;
	if (oSelTaille.options[oSelTaille.selectedIndex].disabled != false) {
		// la taille selectionnee par l'internaute est indisponible => recalage
		for (i=0;i<oSelTaille.options.length;i++) {
			if (! oSelTaille.options[i].disabled) {
				oSelTaille.selectedIndex=i;
				break;
			}
		}
	}
	iRefV = oSelTaille.value;
	displayPrix("Prix :", aArticle[iArt].aRadical[iRad].aRefVente[iRefV]);
	document.getElementById("selQte_" + iArt).selectedIndex=0;
	
	f_totalOffre();
	//calculPanier();
}

function changeQte(iArticle) {
	//calculPanier();
	f_totalOffre();
}

// ajouter panier pour chaque sélection
function addProdPanier(iArt) {
	
	$(".bout_ajout_pan").hide();
	$(".moul_ajout_pan").show();
	
	// modif
	iRad=getValueOfElement('selCol_'+iArt); 
	iRefVente=getValueOfElement('selTaille_'+iArt);
	iQuantity=getValueOfElement('selQte_'+iArt);
	
	if (iRad>=0 && iRefVente>=0 && iQuantity >0) {	
		//lightbox('lb16');
		
		Box.open({
			idBox: 'lb16',
			show:function(){
			
				oArticle = aArticle[iArt];
				oRad = aArticle[iArt].aRadical[iRad]
				oRefVente = oRad.aRefVente[iRefVente];
				
				var location = document.location.href;
				var host = 	document.location.host
				var urlFp = location.substr(host.length+7, location.length);
		
				Ajax_Call_AjoutPanier ( oArticle.sNiveauOper, 
					oRad.sRadicalId, 
					oArticle.iArtId,
					$(":hidden[name='lib_art_" + iArt + "']").val(),
					oRad.sCouleur, 
					oRefVente.sTailleCode, 
					oRefVente.sTailleLib, 
					iQuantity, 
					oRefVente.fPrix, 
					oArticle.sImgVignArt,
					urlFp,'1'
				);

				
				$(".moul_ajout_pan").hide();
				$(".bout_ajout_pan").show();
				
			}
		});


	} else {
		alert('Veuillez compléter la selection de votre article');
		
		$(".moul_ajout_pan").hide();
		$(".bout_ajout_pan").show();
		
	}
	
}

function addProdPanierUrlOffer(iArt, urlParam) {
	var nParam = "vis_offre=" + encodeURIComponent($(":hidden[name='vis_offre']").val());
	nParam = nParam + "&" + urlParam;
	addProdPanierUrl(iArt, nParam);
}

//ajouter panier pour chaque sélection
function addProdPanierUrl(iArt, urlParam) {
	
	$("#btnAjoutPanier,#loadingPanier").toggle();
	
	
	// modif
	iRad=getValueOfElement('selCol_'+iArt); 
	iRefVente=getValueOfElement('selTaille_'+iArt);
	
	iQuantity=getValueOfElement('qte_' + iArt);	
	iQuantity=getValueOfElement('selQte_'+iArt);
	
	
	if (iArt == -1) {
		// ajout de tous les articles selectionnes
		
		
		
	}
	
	
	if (iRad>=0 && iRefVente>=0 && iQuantity >0) {	

		oArticle = aArticle[iArt];
		oRad = aArticle[iArt].aRadical[iRad]
		oRefVente = oRad.aRefVente[iRefVente];
				
		var location = document.location.href;
		var host = 	document.location.host
		var urlFp = location.substr(host.length+7, location.length);
		
		
		//alert($(":hidden[name='lib_art_" + iArt + "']").length);
		//alert($(":hidden[name='lib_art_" + iArt + "']").val());
		Ajax_Call_CrossSelling ( oArticle.sNiveauOper, 
			oRad.sRadicalId, 
			oArticle.iArtId,
			$(":hidden[name='lib_art_" + iArt + "']").val(),
			oRad.sCouleur, 
			oRefVente.sTailleCode, 
			oRefVente.sTailleLib, 
			iQuantity, 
			oRefVente.fPrix, 
			oArticle.sImgVignArt,
			urlFp,'1',urlParam
		);
		$("#btnAjoutPanier,#loadingPanier").toggle();
	} else {
		alert('Veuillez compléter la selection de votre article');
		
		$("#btnAjoutPanier,#loadingPanier").toggle();
	}
	
}


function RAZ(){	
	document.forms['productform'].reset();
}

// ----------------------------------------------------- Struct Promo
var oCurrPromo = null;

function CreatePromo(sPromo, sRefVente) {
	oCurrPromo = new Promo (sPromo, sRefVente);
}

function Promo(sPromo, sRefVente){
	aTmp = sPromo.split(';');
	
	this.sPromotionType = aTmp[0] ;
	this.iAmountCond = aTmp[1] ;
	this.iQtyCond = aTmp[2] ;
	this.bPrivilegeCond = aTmp[3];
	this.sRemiseType = aTmp[4] ;
	this.fRemise = aTmp[5] ; //Taux ou Montant;
	this.sRemiseCadeauRadicalID = aTmp[6] ;
	this.sRemiseCadeauTaille = aTmp[7] ;
	
	this.sRefVente = ';' + sRefVente + ';';

	this.isRefVenteDiscounted = function (sRefVenteId) { return (this.sRefVente.indexOf(';' + sRefVenteId + ';')>=0) } ;
	this.isRadicalGift = function (sRadicalId) { return (this.sRemiseCadeauRadicalID==sRadicalId) } ;

	this.oRadicalGift=null;
}

// ------------------------------------------------------ Struct Article

var aArticle=new Array();

var iCurrArticle=0;
function setIdx(iArt) {
	iCurrArticle=iArt;
}
var iCurrGabarit=null;
function setGabarit(iGab){
	iCurrGabarit=iGab;
}

var sCurrCatId=null;
function setCatalogId(sCatId){
	sCurrCatId=sCatId;
}

var sCurrImgVignArt=null;
function setImgVignArt(sUrl){
	sCurrImgVignArt=sUrl;
}

var iCurrArtId=null;
function setArtId(iId){
	iCurrArtId=iId;
}

// Article
function Article (sNiveauOper) {
	this.iArticle=iCurrArticle;
	this.iArtId=iCurrArtId;
	this.iGabaritPrix=iCurrGabarit;
	this.sImgVignArt=sCurrImgVignArt ;
	this.sNiveauOper=sNiveauOper;
	this.aRadical= new Array();
	this.oRefVenteDepuis=null;
}

function rs(sNiveauOper){
	aArticle[iCurrArticle] = new Article(sNiveauOper);
}

// Radical
var iCurrRadical=null;
	
function Radical (sRadicalId, sCouleur) {
	this.iArticle = iCurrArticle;
	this.iRadical = iCurrRadical;
	
	this.sRadicalId = sRadicalId;
	this.sCouleur = sCouleur;
	
	this.aRefVente = new Array();
	
	this.bIsGift=false;	
	if (oCurrPromo!=null) {
	    this.bIsGift = oCurrPromo.isRadicalGift (sRadicalId) ;
	    oCurrPromo.oRadicalGift=this;
	}
}

function r(sRadicalId, sCouleur){
	iCurrRadical = aArticle[iCurrArticle].aRadical.length;
	aArticle[iCurrArticle].aRadical[iCurrRadical] = new Radical(sRadicalId, sCouleur);		
}

// RefVente
var iCurrRefVente = null;

function RefVente(sRefVenteId, fPrix, fPrixBarre, fTauxDecote, fMontantDecote, sEtatDispo, sTailleCode, sTailleLib){
	this.iArticle = iCurrArticle;
	this.iRadical = iCurrRadical;
	this.iRefVente = iCurrRefVente;

	this.sRefVenteId=sRefVenteId ;
	this.fPrix = fPrix;

	this.fPrixBarre = fPrixBarre; 
	this.fTauxDecote = fTauxDecote; 
	this.fMontantDecote = fMontantDecote;
	this.sEtatDispo = sEtatDispo; 
	this.sTailleLib = sTailleLib; 
	this.sTailleCode = sTailleCode;

	this.bHasDiscount = false; // Check dynamiquement si la refvente est en promo ?
	if (oCurrPromo!=null) this.bHasDiscount = oCurrPromo.isRefVenteDiscounted(sRefVenteId) ;
}

var oTmpRefVente = null;
function rv(sRefVenteId, fPrix, fPrixBarre, fTauxDecote, fMontantDecote, sEtatDispo, sTailleLib, sTailleCode) {
	iCurrRefVente = aArticle[iCurrArticle].aRadical[iCurrRadical].aRefVente.length;
	oTmpRefVente = new RefVente(sRefVenteId, fPrix, fPrixBarre, fTauxDecote, fMontantDecote, sEtatDispo, sTailleLib, sTailleCode);
}		

// Catalogue principal, pro, non pro ....
// Afin de ne pas compliquer le rest du calcul on ne garde que les ref ventes du catalogue en cours 
function c(sCatId) {	
	// Ok, la ref vente existe bien pour le catalogue en cours de consutlation !
	if (sCurrCatId==sCatId) {
		aArticle[iCurrArticle].aRadical[iCurrRadical].aRefVente[iCurrRefVente] = oTmpRefVente ;	
		// on met a jour l'info PrixDepuis par simplicite
		if (aArticle[iCurrArticle].oRefVenteDepuis==null || aArticle[iCurrArticle].oRefVenteDepuis.fPrix > oTmpRefVente.fPrix) {
			aArticle[iCurrArticle].oRefVenteDepuis = oTmpRefVente;
		} 
	}
}

// ------------------------------------------------------------------------------------- fonction affichage

function displayAllCouleur(){
	for (iArt=1; iArt < aArticle.length; iArt++) displayCouleur(iArt);
}

function displayCouleur(iArt){
	oArt = aArticle[iArt];
	if (oArt.aRadical.length > 1) {
		// Si N couleur => Affichage du selectBox avec aucune couleur sélectionnée.
		sHtml = '<select class="dd" id="selCol_'+ iArt + '" onChange="changeCouleur('+ iArt +')">';
		for (i=0;i<oArt.aRadical.length;i++){
			oRad = oArt.aRadical[i];
			sHtml = sHtml + '<option value="'+ i +'">'+ oRad.sCouleur +'</option>';
		}
		//sHtml = sHtml +	'<option value="-1" selected>Couleur</option></select>';
	} else {
		// Si une seule couleur => Affichage du texte de la couleur sans selectBOX + affichage des tailles et radical
		oRad = oArt.aRadical[0];
		sHtml = oArt.aRadical[0].sCouleur + '<input type="hidden" id="selCol_'+ iArt +'" name="selCol_'+ iArt +'" value="0">';
		oDivRadical = document.getElementById("radical_" + iArt);
		if (oRad==-1) {
			oDivRadical.innerHTML = '';
		} else {
			oDivRadical.innerHTML = oRad.sRadicalId ;
		}
	}
	document.getElementById('divCol_'+iArt).innerHTML = sHtml ;
	displayTaille(iArt);
	displayQuantity(iArt);
}

// ------------------------------------------------------------------------------------- 

function displayTaille(iArt){
	var iRad = getValueOfElement('selCol_' + iArt);
	oRefVenteDepuis=aArticle[iArt].oRefVenteDepuis;
	if (iRad==-1) {
		// si Aucune couleur de s?lectionner ? return -1 si existe pas
		
		
		sHtml = '<font style="font-weight:normal;color:black;font-size:10px;">Veuillez choisir un coloris</font>' ;
		//MEL
		//sHtml='';
		sLabel='Depuis';
	} else {
		oRad = aArticle[iArt].aRadical[iRad];
		sSelected=' selected';
		sHtml = '<select class="dd" id="selTaille_'+ iArt +'" onChange="changeTaille('+ iArt +')">' ;
		sLabel='Prix :';
		for (i=0; i<oRad.aRefVente.length ; i++) {
			oRefVente = oRad.aRefVente[i];
			if (oRefVente.sEtatDispo != 'E' && oRefVente.sEtatDispo != 'S') {
				sHtml = sHtml + '<option  class="noir" value="'+ i + '"' + sSelected +'>'+ oRefVente.sTailleLib + '</option>';
				if (sSelected!='') oRefVenteDepuis=oRefVente;
				sSelected='';
			} else {
				sHtml = sHtml + '<option value="-1" class="barre" disabled="disabled" style="color:#DE98BA;">'+ oRefVente.sTailleLib +' - Epuisé</option>';
			}
		}
		sHtml = sHtml + '</select>';
	}
	displayPrix(sLabel, oRefVenteDepuis);
	document.getElementById('divTaille_' + iArt).innerHTML = sHtml;	
}

//--------------
function displayQuantity(iArt) {
	sHtml="";
	oSel = document.getElementById('selQte_'+iArt);
	oSel.options.length=0;
	for (i=0; i<10; i++) oSel.options[i] = new Option(i+1,i+1) ; 
}

//  TProd Taille, Prix,  RefVente, articleId, Radical, Stock

function displayPrix (sLabel, oRefVente) {
	
	iArt = oRefVente.iArticle;
	oArt = aArticle[iArt];
	
	var sDecote="";
	if (oArt.iGabaritPrix == 1) {
		// - TauxDecote, Prix, PrixBarre 
		if (oRefVente.fTauxDecote>0) sDecote="- " + oRefVente.fTauxDecote + "%";
	} else if (oArt.iGabaritPrix == 2) {
		// - MontantDecote, Prix, PrixBarre
		if (oRefVente.fMontantDecote>0) sDecote="-" + oRefVente.fMontantDecote + " &euro;" ;
	} else if (oArt.iGabaritPrix == 3) {
		// Prix et PrixBarre 
	}
	
	if (sDecote==""){
		$("#decote_" + iArt).addClass("invisible");
		//document.getElementById("decote_" + iArt).style.display='none';		 
	}
	else{
		//document.getElementById("decote_" + iArt).style.display='';
		$("#decote_" + iArt).removeClass("invisible");
		document.getElementById("decote_" + iArt).innerHTML = sDecote ;
	}
	var sPrixBarre = '' ;
	if (oRefVente.fPrixBarre > 0) {
		sPrixBarre = Number(oRefVente.fPrixBarre).toFixed(2).replace('.', ',') + ' &euro;';	
		$("#prixBarre_" + iArt).removeClass("invisible");
		document.getElementById("prixBarre_" + iArt).innerHTML = sPrixBarre;
	}
	else {
		$("#prixBarre_" + iArt).addClass("invisible");
		//document.getElementById("prixBarre_" + iArt).style.display='none';
	}
	
	
	document.getElementById('depuis_' + iArt).innerHTML = sLabel;
	var aTmp = String(oRefVente.fPrix + '').split('.');
	sPrixEuro = aTmp[0];
	if (aTmp.length>1) {
		sPrixCent =  aTmp[1]+'00'.substr(1,2 - aTmp[1].length) ;
	} else {
		sPrixCent ='00';
	}
	document.getElementById("euro_" + iArt).innerHTML = sPrixEuro ;
	document.getElementById("cents_" + iArt).innerHTML = "," + sPrixCent + " &euro;";

	
	if (sLabel == 'Depuis') {
		document.getElementById("dispo_" + iArt).innerHTML = '';
	} else {
		var currSEtatDispo = oRefVente.sEtatDispo;
		if (currSEtatDispo=='E'){currSEtatDispo='Epuisé';}
		if (currSEtatDispo=='S'){currSEtatDispo='Epuisé';}
		if (currSEtatDispo == '') {
			currSEtatDispo = 'Disponible';
		}
		document.getElementById("dispo_" + iArt).innerHTML = currSEtatDispo;
	}
	
	$("#px" + iArt).html($("#euro_" + iArt).html() + $("#cents_" + iArt).html());
	if ($("#prixBarre_" + iArt).css("display") != 'none') {
		$("#pxb" + iArt).html($("#prixBarre_" + iArt).html());
		$("#pxb" + iArt).show();
	}
	else {
		$("#pxb" + iArt).hide();
	}
	if ($("#decote_" + iArt).css("display") != 'none') {
		$("#rm" + iArt).html($("#decote_" + iArt).html());
		$("#rm" + iArt).show();
	}
	else {
		$("#rm" + iArt).hide();
	}
}



function displayTicket(iNbArt,fMontantPanier,fRemise) {
		
	displayBlock('recapPrix', (iNbArt > 0));
	displayBlock('jajoute', (iNbArt > 0));
	displayBlock('cprivilege', (iNbArt > 0));
	displayBlock('termine', false);
	displayBlock('continue', false);

	//document.getElementById("Remise").innerHTML=fRemise ;
	
	if (iNbArt == 0){
		document.getElementById('recapStatus').innerHTML='Pas d\'article sélectionné';
		document.getElementById("recapPrix").innerHTML='&nbsp;' ;
		document.getElementById("recapRemise").innerHTML='';
	} else {
		sPluriel=((iNbArt>1)?'s':'');
		document.getElementById('recapStatus').innerHTML= iNbArt + ' article' + sPluriel + ' sélectionné' + sPluriel;
		document.getElementById("recapPrix").innerHTML=(fMontantPanier - fRemise).arrondi(2) + ' &euro;';
		document.getElementById("recapRemise").innerHTML=((fRemise>0)?"- "+fRemise.arrondi(2)+" &euro;":"");
	}
}

// *****************************************************************************************************
// Rattachement d'une promotion à une offre et une seule ! 
// Il y a qu'une seule Promotion remontée par page Offre
//
// le cadeau ne remonte pas dans la condition de produit 
//
// Tous les produits de la page offre sont forcément concernés (hors cadeau)
// On ne prend pas en compte la carte ni le contexte panier pour le calcul
// => pas de calcul des remise de type privilège
// Par contre pour la carte privilège on montre systématiquement l'image promo 
// meme si on ne calcul pas la remise 

// On simule le panier dans le périmètre de la sélection des produits de la page offre.
// 	=> Les limites quantités (en fonction du type de promo)
//		E = Qty = Nb articles différents concernés (hors cadeau ?)
//		L = Qty = Nb Ref vente différents concernées(hors cadeau ?)		    
// 	=> Les limites en montant

// Le taux de remise est exprimé en flotant  (pas besoin de diviser par 100)
// remise type P => Le prix pièce s'applique à tout les produits

// Nb déclenchement cadeau possible .

//----------------------------- RULES
// Description

function calculPanier() {

	var fMontantPanier=0 ;
	var NbProductSelected4Discount = 0
	var QtyMinSelected4Discount = 99999 ;
	var QtySumSelected4Discount = 0 ;
	var NbDeclenchement=0;
	var iNbArtSelected =0;

	for (iArt=1 ; iArt < aArticle.length ; iArt++) {
		iRad=getValueOfElement('selCol_'+iArt); 
		iRefVente=getValueOfElement('selTaille_'+iArt);
		iQuantity=getValueOfElement('selQte_'+iArt);
		
		if (iRad>=0 && iRefVente>=0 && iQuantity >0) {
			iNbArtSelected++;
			oRefVente = aArticle[iArt].aRadical[iRad].aRefVente[iRefVente];
			fMontantPanier += oRefVente.fPrix * iQuantity ;
			if (oRefVente.bHasDiscount) {
				QtySumSelected4Discount += iQuantity ;
				NbProductSelected4Discount++;
				if (QtyMinSelected4Discount > iQuantity ) QtyMinSelected4Discount = iQuantity ;
			}
		} else {
			// TODO : pas d'affichage si le produit est en promo ?
		}
	}
	displayTicket(iNbArtSelected,fMontantPanier,0) ;
	
	
	if (QtyMinSelected4Discount==99999) QtyMinSelected4Discount=0 ;

	// On ne calcule pas la promotion de type privilege
	if (oCurrPromo.bPromotionPrivilegeCond) return ;

	// Montant sur le montant total avant remise de la page offre.
	if (fMontantPanier < oCurrPromo.iAmountCond) return ;

	// Lot
	if (oCurrPromo.sPromotionType=='L') {
		// Lot : somme toute quantités confondues
		QtySelected = QtySumSelected4Discount ;
		NbDeclenchement = Math.floor(QtySumSelected4Discount / oCurrPromo.iQtyCond) ;
	} else if (oCurrPromo.sPromotionType=='E') {
		// Qté =  Nb produit diff?érent sélectionnés et concernés par la promo
		QtySelected = NbProductSelected4Discount ;
		NbDeclenchement = QtyMinSelected4Discount ; 
	} else {
		return ; // erreur ?
	}

	// Test condition quantité en fonction 
	if (QtySelected < oCurrPromo.iQtyCond) return false ;

	// Montant de la remise 
	var fRemise=0;

	if (oCurrPromo.sRemiseType=='V') {
		fRemise = oCurrPromo.fRemise * NbDeclenchement ;

	} else if (oCurrPromo.sRemiseType=='T') {
		for (iArt=1 ; iArt < aArticle.length ; iArt++) {
			iRad=getValueOfElement('selCol_'+iArt); 
			iRefVente=getValueOfElement('selTaille_'+iArt);
			iQuantity=getValueOfElement('selQte_'+iArt);
			if (iRad>=0 && iRefVente>=0) {
				oRefVente = aArticle[iArt].aRadical[iRad].aRefVente[iRefVente];
				if (oRefVente.bHasDiscount) {
					if (oCurrPromo.sPromotionType=='L') {
						fRemise += oRefVente.fPrix * oCurrPromo.fRemise * iQuantity;
					} else if (oCurrPromo.sPromotionType=='E') {
						fRemise += oRefVente.fPrix * oCurrPromo.fRemise * QtyMinSelected4Discount;
					}	
				}
			}
		}

	} else if (oCurrPromo.sRemiseType=='P') {
		for (iArt=1 ; iArt<aArticle.length ; iArt++) {
			iRad=getValueOfElement('selCol_'+iArt); 
			iRefVente=getValueOfElement('selTaille_'+iArt);
			iQuantity=getValueOfElement('selQte_'+iArt);
			if (iRad>=0 && iRefVente>=0) {
				oRefVente = aArticle[iArt].aRadical[iRad].aRefVente[iRefVente];
				if (oRefVente.bHasDiscount) {
					if (oCurrPromo.sPromotionType=='L') {
						fRemise += oCurrPromo.fRemise * iQuantity;
					} else if (oCurrPromo.sPromotionType=='E') {
						fRemise += oCurrPromo.fRemise * QtyMinSelected4Discount;
					}	
				}
			}
		}

	} else if (oCurrPromo.sRemiseType=='K') {
		iArt = oCurrPromo.oRadicalGift.iArticle ;

		//oRq=document.getElementById('r_'+i);
		iRad=getValueOfElement('selCol_'+iArt); 
		iRefVente=getValueOfElement('selTaille_'+iArt);
		iQuantity=getValueOfElement('selQte_'+iArt);
		
		if (iQuantity > 0 && iRad==oCurrPromo.iRadical) {
			fRemise = 	aArticle[iArt].aRadical[iRad].aRefVente[iRefVente].fPrix;
			//oRq.innerHTML = "1 article Offert" ;
		} else {
			//oRq.innerHTML = "Ajouter ce produit il vous sera offert" ;
		}
	}
	displayTicket(iNbArtSelected,fMontantPanier,fRemise) ;

}



function lightbox_choix (id_lb, my_urldata) {		
	Box.open({
		idBox: id_lb,
		url: my_urldata,
		showWaitProgress: true
	});	
}
/*********************************************************************************************
 * 			Mise en page
 */
	
	// ouverture popup pour icones
	function OuvrirFenetreIcones(url,nom){
	    param = "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,width=250,height=250,resizable=yes";
	    taille = window.open(url,nom,param)
	}
	
	// arrondir à exp chiffres après la virgule
	Number.prototype.arrondi = function(exp) {
    	var virgule = Math.round(Math.pow(10, exp));
    	return Math.round(this*virgule)/virgule;
	};
	
function f_showAllVisuels(params, selection) {
	/*
	Box.open({
		idBox: 'lb21',
		arg: '1&img_sel=' + selection + '&' + params,
		url: '/VAD/ShowAllVisuels'
	});
	*/
}	

function f_enableAV() {
	var allAV = $('img[avsrc], a[avsrc]');
	allAV.unbind('click');
	allAV.one(
			'click', 
			function() {
				Box.open({
					idBox: 'lb21',
					arg: '1&img_sel=' + $(this).attr('avsrc') + '&' + $(this).attr('avparam'),
					url: '/VAD/ShowAllVisuels'
				});
			}
		);
}


function f_showVisuel(obj) {
	$('#autre_zoom').html("<img src='" + $(obj).attr('src') + "' border='0'>");	
	$('#zoom').hide();
	$('#autre_zoom').show();
}

function f_hideVisuel() {
	$('#autre_zoom').hide();
	$('#zoom').show();
}


function f_totalOffre() {
	// sur une offre ?
	if ($("#euro_total").length == 1) {	
		var total = Number(0);
		var isError = false;
		
		for (i = 0 ; i < aArticle.length ; i++) {
			if (aArticle[i] != null) {	
				var inputSel = document.getElementById('sel_prod_' + i);
				if (inputSel.checked) {			
					artCouleur = $('#selCol_' + i).val();				
					if (Number(artCouleur) < 0) {
						inputSel.checked = false;
						$("#checkbox_" + i).removeClass("checked");
						isError = true;
					}
					else {
						artTaille = $('#selTaille_' + i).val();
						if (artTaille == null) {
							inputSel.checked = false;
							$("#checkbox_" + i).removeClass("checked");
							isError = true;
						}
						else {
							artQte = $('#selQte_' + i).val();
							refVente = aArticle[i].aRadical[artCouleur].aRefVente[artTaille];
							total = total + (Number(artQte) * Number(refVente.fPrix));
						}
					}
				}
			}
		}
		
		total = Number(total).toFixed(2);
		sTotal = total.replace('.', ',');
		document.getElementById("euro_total").innerHTML = sTotal ;
				
		if (isError) {
			alert('Veuillez compléter la selection de votre article');
		}
	}
}

function f_initEvtTotal(indice) {
	$('#sel_prod_' + indice).click(f_totalOffre);
	/*
	$('#selCol_' + indice).change(function() { f_totalOffre(); f_initEvtCol(indice); });
	$('#selTaille_' + indice).change(f_totalOffre);
	$('#selQte_' + indice).change(f_totalOffre);
	*/
}

function f_initEvtCol(indice) {
	/*
	$('#selTaille_' + indice).unbind('change', f_totalOffre);
	$('#selTaille_' + indice).change(function() { alert('o');f_totalOffre()});
	*/	
}

function f_addGlobal() {
	
	// recuperer les produits selectionnes
	var selProd = $(":checkbox[name='sel_prod']:checked");
	var urlParams = "";
	
	if (selProd.length == 0) {
		alert("Veuillez sélectionner les produits à ajouter au panier.");
	}
	
	urlParams += "typeAjout=1";
	for (i = 0 ; i < selProd.length ; i++) {
		var productIndex = selProd[i].value;
		var art = aArticle[productIndex];
		var rad = art.aRadical[$("#selCol_" + productIndex).val()];
		var refVente = rad.aRefVente[$("#selTaille_" + productIndex).val()];
		
		urlParams += "&niv_oper=" + encodeURIComponent(art.sNiveauOper);
		urlParams += "&radicalId=" + encodeURIComponent(rad.sRadicalId);
		urlParams += "&taille=" + encodeURIComponent(refVente.sTailleCode);
		urlParams += "&qte=" + encodeURIComponent($("#selQte_" + productIndex).val());		
		urlParams += "&lib_art=" + encodeURIComponent($(":hidden[name='lib_art_" + productIndex + "']").val());
	}
	
	urlParams += "&vis_offre=" + encodeURIComponent($(":hidden[name='vis_offre']").val());
	urlParams += "&" + $(":hidden[name='url_param_" + selProd[0].value + "']").val();
	if (urlParams.substring(urlParams.length - 1) == '&') {
		urlParams = urlParams.substring(0, urlParams.length - 2);
	}
		
	sUrl  = '/VAD/PanierXSelling?' +  urlParams;
	
	
	Ajax_Panier_nbCall++;
	Box.open({
		idBox: 'lb70',
		url: sUrl,
		showWaitProgress:true
	});	
}


function f_showMaxDecote() {
	var maxDecote = 0;
	var previousDecote = -999;
	var diffDecote = false;
	
	var rad, refVente;
	for (i = 0 ; i < aArticle.length ; i++) {
		if (aArticle[i] != null) {
			rad = aArticle[i].aRadical;
			for (j = 0 ; j < rad.length ; j++) {
				refVente = rad[j].aRefVente;
				for (k = 0 ; k < refVente.length ; k++) {					
					if (previousDecote == -999) {
						previousDecote = refVente[k].fTauxDecote;
					}
					if (maxDecote < refVente[k].fTauxDecote) {
						maxDecote = refVente[k].fTauxDecote;
					}
					if (previousDecote != refVente[k].fTauxDecote) {
						diffDecote = true;
					}
					previousDecote = refVente[k].fTauxDecote;
				}
			}
		}
	}
	
}

function f_showMaxDecoteProd() {
		
	var rad, refVente;
	for (i = 0 ; i < aArticle.length ; i++) {
		if (aArticle[i] != null) {
			
			var maxDecote = 0;
			var previousDecote = -999;
			var diffDecote = false;
			rad = aArticle[i].aRadical;
			
			for (j = 0 ; j < rad.length ; j++) {
				refVente = rad[j].aRefVente;
				for (k = 0 ; k < refVente.length ; k++) {					
					if (previousDecote == -999) {
						previousDecote = refVente[k].fTauxDecote;
					}
					if (maxDecote < refVente[k].fTauxDecote) {
						maxDecote = refVente[k].fTauxDecote;
					}
					if (previousDecote != refVente[k].fTauxDecote) {
						diffDecote = true;
					}
					previousDecote = refVente[k].fTauxDecote;
				}
			}
			
			if (maxDecote > 0) {
				if (diffDecote) {
					$("#mxrm" + i).html("Jusqu'à - " + maxDecote + " %");
				}
				else {
					$("#mxrm" + i).html("- " + maxDecote + " %");
				}
				$("#mxrm" + i).show();
			}
		}
	}
}

function addPanierSelProduit(idProduit, urlParams) {
	lightbox_choix('lb20', '/VAD/DetailProduitAjoutPanier?' + urlParams);
}

function getPValue(sUrl, sParameterName) {
	var rExp = new RegExp("[?&]" + sParameterName + "=([^&#]+)");
	var pMatch = rExp.exec(sUrl);
	var pValue = pMatch ? decodeURIComponent(pMatch[1]) : null;
	return pValue;
}


function backToList() {
	var urlList = $(".chemin a:last").attr("href");
	var currentUrl = document.location.search;
	
	var fhListerPos = getPValue(currentUrl, "fh_lister_pos");
	var fhViewSize = getPValue(currentUrl, "fh_view_size");
	
	if (fhListerPos != null && fhViewSize != null) {
		fhStartIndex = Math.floor(fhListerPos / fhViewSize) * fhViewSize;
		urlList += "&fh_view_size=" + fhViewSize;
		urlList += "&fh_start_index=" + fhStartIndex;
	}
	
	window.location = urlList;
}

function reverseSelection(iArt){
	//recherche la prix
	couleur=$("#id_"+iArt).attr("couleur");
	taille=$("#id_"+iArt).attr("taille");
	//selectionne la couleur
	oRadical=null;
	iRad=null;
	oArticle=aArticle[iArt];
	for(iRad=0; iRad<oArticle.aRadical.length && oRadical==null; iRad++ ){
		if(oArticle.aRadical[iRad].sCouleur==couleur){
			oRadical=oArticle.aRadical[iRad];
		}
	}
	if(oRadical!=null){
		$("#selCol_"+iArt).val(iRad-1);
		changeTaille(iArt);
	}
	//selectionne la taille
	oRefVente=null;
	iRef=null;
	for(iRef=0;iRef< oRadical.aRefVente.length && oRefVente==null;iRef++){
		if(oRadical.aRefVente[iRef].sTailleLib==taille){
			oRefVente=oRadical.aRefVente[iRef];
		}
	}
	if(oRefVente!=null){
		$("#selTaille_"+iArt).val(iRef-1);
		changeTaille(iArt);
	}
}

