// -----------------------------   CADDIE  -------------------------------
var Cadd

function InitCad() {
	Cadd.Init()
	//Cadd.TxTVA = 0
	Cadd.Load()
	DispatchQuant()
}

function ActionPrd(O) {
	var OP, O, S, i
	Ob = ElemPAt(O,'Obj');  
	S = LX(Ob,'Obj');  //alert(S+crr+Lout(O)+crr+Lout(Ob))
	switch (S) {
		case 'Prod':  S = ElemPAt(Ob,'!CodPrd');  ExecAction('FicheProd:'+S);  break
		case 'AppelDia':  Diaporama(LX(O,"Code"), "");  break
		case 'AddCad':		AddCad(Ob,-99);  break
		case 'Plus':  AddCad(Ob,1);  break
		case 'Mns':  	AddCad(Ob,-1);  break
	}
}

App.Change = function(O) {
  //alert(Lout(O))
  if (LX(O,'Obj')=='Quant')  AddCad(O,-99);
}


function CaddClass () {
	this.Div
	this.nb			= 0
	this.all		= {}
	this.Prd		= null
	this.TxTVA	= 0.196
	this.TVA    = 0
	this.Port		= this.adPort = 0;  this.ModeCalcPort = ''
	this.Poids	= this.adPoids = 0;
	this.MaxQ		= 0
	this.typMnt = 'HT'
	this.TotAff = 'Total'
	this.colTot = 4
	this.txRem  = 0
	this.Promo	= 0
	this.TotQte = 0
	this.Total  = 0
	this.TotTTC = 0
	this.JSCad  = {}
	this.Client
	this.Init		= Init
	this.Add		= Add
	this.Calc		= Calc
	this.CalcQte = CalcQte
	this.Load		=  Load
	this.Sov		= Sov
	this.Clean	= Clean
	this.Clear	= Clear
	this.msgNoRef = "Ce produit n'a pas de référence"

	function Init () {
	  if (!this.ChainePort && LID("ChainePort"))  this.ChainePort = LVal("ChainePort")
		//alert("Init ChainePort=" + this.ChainePort)
	}

	function Add (Cle, CodPrd, Quant, Prix, Poids, Rem) {
		var Prd, S
		//if (isNaN(Cle))  id = this.all.length;  else  id = Cle
		Prd = this.all[Cle+''];  if (!Prd) { Prd = this[this.nb] = new Object;  this.all[Cle+''] = Prd;  this.nb++ }
		Prd.Cle = Cle;  Prd.CodPrd = CodPrd;  Prd.Quant = Quant;  Prd.Prix = Prix;  Prd.Poids = zed(Poids);  Prd.Rem = Rem
		//Prd.Poids = Round(Prd.Poids*Quant,2)
		S = Prix*Quant;  if (Rem)  S=S-S*(Rem/100)
		Prd.Total = zed(S,2)
		this.Prd = Prd
	}

	function CalcQte () {
	  var Tot=0, T, i
		T = this.all;  for (i in T)  Tot+=ze(T[i].Quant)
		this.TotQte = Tot
		return Tot
	}

	function Calc() {
	  var i, Tot=0, T=this.all
		this.TVA=0
		for (i in T)  Tot+=T[i].Total
		if (this.txRem) {
			this.Promo = Round(Tot * (this.txRem/100),2)
			Tot -= this.Promo
		}
		this.CalcPoids()
		this.CalcPort()

		if (this.typMnt=='HT') {
			this.Total	= Tot;  if (Tot)  this.Total+= this.Port
			this.TVA		= Round(this.Total * this.TxTVA,2)
			this.TotTTC = Round(this.Total + this.TVA,2)
		}else{
			this.TotTTC	= Tot;  if (Tot)  this.TotTTC+= this.Port
			this.Total	= Round(this.TotTTC/(1+this.TxTVA),2)
			this.TVA		= Round(this.TotTTC - this.Total,2)
		}
	}

	function Load () {
		var T, O, i, l, S
		S = GetCookie('Caddie');  if(!S)  return;  //alert('Load'+crr+S)
		O = eval('('+S+')');  if (!O || !O.Liste)  return
		for (i in O.Liste) {
			T = O.Liste[i];		//alert(JSO(T))
			if (T.Quant)  this.Add(T.Cle, T.CodPrd, T.Quant, T.Prix, T.Poids, T.Rem)
		}
		delete(O.Liste)
		for (i in O) { this[i] = O[i] }
		//this.txRem = O.txRem
		//this.Client = O.Client;     //alert('Load'+crr+JSW(this.Client))
			//alert(JSW(this.all)+crr+S+crr+JSO(this.all))
	}

	function Sov() {
		var Prd, Cl='', Li=''
		this.Clean()
		this.JSCad = { Liste:this.all, Total:this.Total, TVA:this.TVA, TotTTC:this.TotTTC, Port:this.Port, txRem:this.txRem, Promo:this.Promo, Poids:this.Poids, Client:this.Client }		//;alert(JSO(this.all)+crr+JSO(this.JSCad))
 		//alert('Sov'+crr+JSONS(this.JSCad))
		SetCookie("Caddie", JSONS(this.JSCad),null,'/')
	}

	function Clean() {
		var Cle, i, T
		T = this.all
		for (i in T) {
			if (!T[i].Quant) { delete(T[i]);  this.nb-- }
		}
		//alert(JSO(this.all))
	}

	function Clear() {
		var i, T, J
		T = this.all
		for (i in T)  T[i].Quant = 0
		this.nb = 0
		this.txRem = this.Promo = 0
		DispatchQuant()
		for (var i in T)  delete(this.all[i])
		if ((J=this.Client) && J.CodCom)  delete(J.CodCom)
	}
}

CaddClass.prototype.CalcPoids = function() {
	var i, Poids=0, T
	T = this.all;  for (i in T)  Poids+= T[i].Poids * T[i].Quant
	this.Poids = Round(Poids,2) + this.adPoids
}

CaddClass.prototype.CalcPort = function() {
	switch(this.ModeCalcPort) {
		case 'Poids':  this.CalcPortPoids();  break;
		case 'Mnt':    this.CalcPortMnt();  break;
	}
}

CaddClass.prototype.CalcPortPoids = function() {
	if (this.ModeCalcPort!='Poids')  return
	var Li, TLi, Port=0, TPor, Pds, i, Poids
	Poids = this.Poids
	if (Poids>0 && this.ChainePort) {
		TPor = this.ChainePort.split(cr)
		for (i=0; i<TPor.length; i++) {
			Li = Trim(TPor[i]);	 if (!Li)  continue;  //alert(Li)
			TLi = Li.split('=')
			if (TLi.length<2) { Port = zed(TLi[0]);  break }
			Pds = zed(TLi[0])
			//alert("Pds=" + Pds + ", Poids=" + this.Poids + " " + (Pds>=this.Poids) + ", €=" + TLi[1])
			if (Pds>=Poids) { Port = zed(TLi[1]);  break }
		}
	}
	//alert(Port)
	this.Port = Port + this.adPort
}

CaddClass.prototype.CalcPortMnt = function() {
	if (this.ModeCalcPort!='Total')  return
}

CaddClass.prototype.Cmde = function() {}

CaddClass.prototype.ChangeRef = function(Ctl) { 	// Si un produit est catégorisé par une CB, la avelur de cette CB s'ajoute à la Ref et le produit peut être commandé par AddCad
	var Ref, S, D, R, oi
	D = ElemPAt(Ctl,'debRef');  S = LX(D,'debRef');  Ref = S+'-T'+Ctl.value;
	PX(D,'ValCle',Ref);
	oi = $('INPUT',D).get(0)
	DelClass(D,'P_*');
	if ((R=this.all[Ref])) {
		AddClass(D,'P_'+Ref)
		oi.value = R.Quant
	}else
		oi.value = ''
}



// --------------------   SS PGM   ---------------------
function AddCad(Ob, Q) {
	var TR, oi, Quant, T, Rem=0
	TR = ElemPAt(Ob,'ValCle');  if (!TR) { alert(Cadd.msgNoRef);  return }
	Cadd.TR = TR
	oi = LTags(TR,'INPUT',0);  if (Q==-99) { if (oi) Quant=ze(oi.value) || 1; }  else if (oi) Quant = ze(oi.value) + Q
	if (Quant<0)  Quant = 0
	if (Quant>Cadd.MaxQ) {
		Quant = Cadd.MaxQ;  alert(Cadd.MsgMaxQ)
	}
	var Stock = ze(LX(TR,'Stock'))
	if (Stock>0 && Quant>Stock) { alert("Cet article n'est actuellement disponible qu'en " + Stock + " exemplaire(s)");  Quant=Stock }
	if (oi) oi.value= Quant;   //alert(Lout(TR) +crr+ LX(TR,"Prix"))
	var Prix	= zed(LX(TR,'Promo'));  if (!Prix)  Prix = zed(LX(TR,"Prix"));  if (!Prix)  return
	var Poids = zed(LX(TR,'Poids'))
	var Rems	= LX(TR,'MemoPrd')
	if (Rems) {
		T = Tableau2D(Rems,'=\r\n');	//alert(JSONS(T)+crr+Rems)
		Rem	= RechArray(T,Quant,0,1,'>');  if (Rem<0)  Rem=0  //alert(Rem)
	}
	Cadd.Add(LX(TR,'ValCle'), LX(TR,'CodPrd'), Quant, Prix, Poids, Rem)
	InfoCad()
	if (Cadd.FinMajCad)  Cadd.FinMajCad()
	Cadd.Sov()  		// FinMajCad fait habituellement un DispatchQuant. Sov supprime les fiches à 0. Donc Sov doit être après FinMajCad
	//alert(JSO(Cadd.all))
	return 1
}

function DispatchQuant() {
	// affiche les quantités dans les vignettes
  var TR, D, i, oi, Quant, Prd, T
	T = Cadd.all;     //alert(JSW(T))
	$('input.inputQ').each(function() { this.value = '' })
	for (i in T) {
		Prd = T[i];		//alert(JSW(Prd))
		$('.P_'+Prd.Cle).each(function() {
			if ((oi=$('INPUT',this).get(0)))  oi.value = Prd.Quant
			if ((oi=$('.mnt',this).get(0)))  oi.innerHTML = FormatN(Prd.Total,2, ", &nbsp;&euro;")
		})
	}
	InfoCad()
}

function MajQuant(O) { AddCad(O,0) }    //if (ze(oi.value))

function InfoCad() {
	// affiche les totaux du caddie dans la page
	var T, TR, Q, Opt
	Q = Cadd.CalcQte();  TR = Cadd.TR
	Opt = ", &nbsp;&euro;"
	if (ElemPClass(TR,'DivCad')) {
		if (TR) {
			var Rems	= LX(TR,"MemoPrd")
			//alert(Rems+crr+Lout(TR))
		  if (Rems)  PVal(TR.cells[Cadd.colTot-1], Cadd.Prd.Rem+'%', true)
			PVal(TR.cells[Cadd.colTot], FormatN(Cadd.Prd.Total,2, Opt), true)
		}
	}
	Cadd.Calc()
	PVal("CadQuant", Q)
	PVal("CadTotal", FormatN(Cadd[Cadd.TotAff],2,Opt), true)
	PVal("CadTotHT", FormatN(Cadd.Total,2,Opt), true)
	PVal("CadTVA", FormatN(Cadd.TVA,2, Opt), true)
	PVal("CadTotTTC", FormatN(Cadd.TotTTC,2, Opt), true)
	PVal("CadPort", (Cadd.Port)?FormatN(Cadd.Port,2, Opt):'OFFERT', true)
	PVal("Poids", Cadd.Poids)
	PVal("CadPromo", '-&nbsp;'+FormatN(Cadd.Promo,2, Opt), true)
	PVal("Total", FormatN(Cadd.Total,2), true)
	if (LID('CmtPds'))  LID('CmtPds').title =  Cadd.Poids
	if (Q)  Aff('MiniCad');  else  Hide('MiniCad')
}

function ClearCad() {
	Cadd.Clear()
	DelCookie('Caddie','/')
	DispatchQuant()
	InfoCad()
}

function AffCmde() {
	Aff("IFM");  Hide("TabInt");  Hide("BoxCmd");  Hide("DivCadQ");  Hide("DivCadT");
	Cadd.TR = null
	IFM.location = RootSite + "/Caddie.php"
}

function MajPromo(O) {
	var Sql, V
	V = LVal(O);  if (!V)  return
	if (Cadd.txRem) { alert("Vous bénéficiez déjà d'une remise");  return }
  Sql = "SELECT DatDoc, AdrWeb FROM base WHERE Titre LIKE '"+V+"'"
  XmlPost2("Pgm=ListBox&Sql=" + escape(Sql)+AjUrl,'',ExecPromo)
}

function ExecPromo(re) {
  var S, D, Rem
  if (!re) { alert('Code promo non trouvé');  return }
  S = Isole(re,',',1);  Rem = Isole(re,',',2);  if (!Rem)  return
  D = DateJS(S);  if (D<new Date()) { alert('Code promo expiré');  return };		//alert(S+crr+D)
  Cadd.txRem = Rem
  Cadd.Calc()
  Cadd.Sov()
  ExecAction('Caddie')
  alert("Ce code promotion vous donne droit à une remise de " + Cadd.Promo + ' €')
}


