﻿// 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

function openSendFriend(iArt) {
	var oImg, sImgUrl;
	oImg = document.getElementById('img_' + iArt);
	if (oImg) {
	    sImgUrl = escape(oImg.src) ;
	} else {
	    sImgUrl = "";
	}
	
	a=window.open("/VAD/PopupSendFriend", "_blank", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=590,height=350") ;
	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("http://www.quelle.fr/portal/portal/SiteInternet/Contenu/AideEnLigne/aide.html","aide","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,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;
	//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;
	//calculPanier();
}

function changeQte(iArticle) {
	//calculPanier();
}

// 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,
					getInnerHTMLOfElement('int_art_'+oArticle.iArticle),
					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 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=affine style="line-height:20px;" 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">Veuillez choisir une couleur</font>' ;
		sLabel='Depuis';
	} else {
		oRad = aArticle[iArt].aRadical[iRad];
		sSelected=' selected';
		sHtml = '<select id="selTaille_'+ iArt +'" class=affine  style="margin-top:-2px;" 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<3; 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==""){
		document.getElementById("decote_" + iArt).style.display='none';
	}
	else{
		document.getElementById("decote_" + iArt).style.display='';
		document.getElementById("decote_" + iArt).innerHTML = sDecote ;
	}
	var sPrixBarre = '' ;
	if (oRefVente.fPrixBarre > 0) {
		sPrixBarre = oRefVente.fPrixBarre + ' &euro;';		
	}
	
	document.getElementById("prixBarre_" + iArt).innerHTML = sPrixBarre  ;
	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 = "<sup>&euro;" + sPrixCent + "</sup>";

	if (sLabel == 'Depuis') {
		document.getElementById("dispo_" + iArt).innerHTML = '';
	} else {
		var currSEtatDispo = oRefVente.sEtatDispo;
		if (currSEtatDispo=='E'){currSEtatDispo='Epuisé';}
		if (currSEtatDispo=='S'){currSEtatDispo='Epuisé';}
		document.getElementById("dispo_" + iArt).innerHTML = currSEtatDispo;
	}
}



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) ;

}

/*********************************************************************************************
 * 			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;
	};
	
