// -------------------  CAL CLASS  Liste des retraites sélectionnées ------------------
CalClass = function(id, DivP) {
	this.id = id
	this.DivP = LID(DivP)
	this.T = {}
	this.CodCal = null     // CodCal sélectionné (dans l'hypothèse ou une seule retraite peut être sélectionnée à la fois)
	this.nb = 0
}

CalClass.prototype.Aff = function() {
	var D, i=0, Cle, H='', R, S
	D = LID('ulCal');  PVal(D,'')
	for (Cle in this.T) {
		R = this.T[Cle]
		H+='<li><b>' + R.Titre + '</b><br>'+R.Dat+'</li>'
	}
	if (!H) {	}
	D.innerHTML = H
	Aff(this.DivP)
}

CalClass.prototype.CheckAff = function() {
	var Os, O, i, V
	if (!(Os=LTags(LID(this.id),'INPUT')))  return
//alert(JSW(this.T))
	for (i=0; i<Os.length; i++) {
		O = Os[i];  V = LX(O,'ValCle');  if (this.T[V])  O.checked = true
	}
}

CalClass.prototype.Sel = function(O) {
	var TR, V
	if (O.checked && this.nb==1) { alert("Vous ne pouvez sélectionner qu'une retraite à la fois");  O.checked=false;  return }
	TR = ElemPTag(O,'TR');  V = LX(TR,'ValCle');  //alert(Lout(TR))
	if (!V) { alert("Aucune clé");  O.checked=false;  return }
	if (O.checked) {
		this.T[V] = { Titre:LX(TR,'LibAct'), An:LX(TR,'An'), Sit:LX(TR,'SitAct'), Dat:LX(TR,'Dat') };  this.nb++
		this.CodCal = V;		//alert(JSW(this.T))
	}else{
		delete(this.T[V]);  this.nb--;  this.CodCal = 0
	}
}

CalClass.prototype.goInscript = function() {
	if (!this.nb) { alert("Vous devez au préalable sélectionner la retraite\à laquelle vous voulez vous inscrire.\n\n(case à cocher en bout de ligne)");  return }
	this.Aff()
	ExecAction([{Action:"LoadPHP",TypL:"site/Inscript1.php",Zone:"DivLibre1",ValCle:17}])
}

CalClass.prototype.Sov = function() {
	var H='', Co, Cle, T=[], S='', nok, R, RCal, O
	if (!App.CaddU)  nok=1;  else if (!App.CaddU.nbSel)  nok=1;  if (nok) { alert("La liste est vide"); return }
  if (!confirm("Désirez-vous inscrire ces "+App.CaddU.nbSel+" personne(s)"))  return
	for (Cle in this.T) {
	  RCal = this.T[Cle]
		for (Co in App.CaddU.Sels) {
		  R = App.CaddU.T[Co]
		  if (R && R.Arriv)  O = R.Arriv;
			T.push([Cle, Co, 'coIns', RCal.Sit, RCal.An, O])
		}
	}
	//for (Cle in this.T)  for (Co in App.CaddU.Sels)  H+=",("+ Cle + "," + Co + ",'coIns')";  if (!H)  return;  H = "INSERT INTO cal_l (CodCal, LCal, TypLCal) VALUES " + H.substr(1);  ExecAction([{Action:'ExecSql', Sql:H }],'',{CallBack:function(re){
	ExecAction([{Action:'SovInscript', First:App.CaddU.First, TV:T }],'',{CallBack:function(re) {
	  var R, JS, Sql
		if (!re) { alert('Exécution anormale');  return }
		wh(re,'DivOther');  //alert(re)
		re = JSOND(re)
		if (re.nb!=re.retour)  S = "  - " + (re.nb-re.retour) + ' personne(s) étai(ent) déjà inscrite(s) '
		R = App.CaddU.Rfirst;  JS = { CodCo:App.CaddU.First, Civil:R.Civil, Nom:R.Nom, Prenom:R.Prenom, eMail:R.eMail }
		Sql = "SELECT RefLCal FROM cal_l WHERE CodCal=" + Cal.CodCal + " AND LCal=" + App.CaddU.First + " AND TypLCal='coIns'"
		ExecAction([{Action:"Win", ValCle:123, SqlData:Sql, JSData:JSONS(JS)}])
		Zones.all.DivLibre.Clear();
		alert("INSCRIPTION EFFECTUEE CORRECTEMENT\n\n  - " + re.retour + " inscription(s) effectuée(s) sur " + re.nb + " demandée(s)\n\n" + S)
	}})
}

CalClass.prototype.Clear = function() { this.T = {};  this.nb = 0;  Hide(Cal.DivP); }


// -------------------  Caddie Utilisateur  --------------
CaddUClass = function(id) {
  this.Div = LID(id);  //this.Div.innerHTML=''
	this.T  = {}  // Cod, NomPr, Arriv
	this.Rels = {}  // Rels = [CodCo] de { rel:'enfan', CodCol:contact_l.CodCol },  ex [{"33":{"Cle":5,"rel":"enfan","Avec":"17"},"34":{"Cle":6,"rel":"enfan","Avec":"17"}}]
	this.Sels = null
	this.nb = 0
	this.nbSel = 0
	this.Cod	= null
	this.First = null
	this.Rfirst = null
}

CaddUClass.prototype.Aff = function() {
	var D, i, H='', R, S, Aj, T = this.T
	D = this.Div;  PVal(D,'')
	for (i in T) {
	  Aj=S='';
		R = T[i];		//alert(JSW(R))
		if (i==this.First) { Aj=' style="font-weight:bold; color:yellow"'; }
		if (this.Sels[i])  S=' checked';
		H+='<li CodCo='+i+Aj+'><input type=checkbox'+S+'>&nbsp;' + R.NomPr + '</li>'
	}
	H+='<li><img src="/biblio/img/ext/add.gif">&nbsp; Ajouter un participant</li>'
	if (H) {
		D.innerHTML = H;    //alert(H)
		//Aff(Cal.DivP)
	}
}

CaddUClass.prototype.Add = function(Cod, NomPr, Arriv, Auto) {
	var R, i, S, O
	this.Cod = Cod;		//alert(Cod+crr+NomPr)
	if (!(R=this.T[Cod])) { R = this.T[Cod] = {};  this.nb++; }
	R.NomPr = NomPr
	if (Arriv) { O = JSOND(Arriv);  R.Arriv = O.Arriv;  R.Arriv.CodCal = Cal.CodCal; }
	if (!this.First) {  // Premier partis. = prescripteur
		R.eMail = LVal('eMail')
		this.First = Cod;  this.Rfirst = R;  this.Sels = {};  this.Sels[Cod]=1;  this.nbSel++
		if (App.Data.Particips) {		// Chargement de la liste de partis. précédente
			this.Rels = App.Data.Particips
			S='';  for (i in this.Rels)  S=S+','+i;		//alert(S+crr+JSW(this.Rels))
			ExecAction([{Action:'LitSql', Sql:'SELECT CodCo, Nom, Prenom, JSCo FROM contact WHERE CodCo IN ('+S.substr(1)+')'}],'',{CallBack:function(re) {
				var S, O, Arriv
				if (!re)  return;  var Co
				re = JSOND(re);   //alert(JSW(re))
				for (i in re) {
					Co = re[i];  Arriv = null;  //alert(JSW(Co))
					if ((S=Co.JSCo)) { O = JSOND(S);  Arriv = O; }
					App.CaddU.Add(Co.CodCo, Co.Prenom+' '+Co.Nom, Arriv, 1)
				}
				App.CaddU.Aff()
				//alert(App.CaddU.nb+crr+JSW(App.CaddU))
			}})
		}
	}
	if (!Auto && !this.Sels[Cod]) { this.Sels[Cod]=1;  this.nbSel++ }
  return R
}

CaddUClass.prototype.Sov = function(Cod, NomPr, Arriv, rel, Avec) {
	var R, i, S, CodCol, Sql, SqW
	R = this.Add(Cod, NomPr, Arriv)
	if (Cod==this.First) {
		R.Civil = LVal('Civil');  R.Nom = LVal('Nom');  R.Prenom = LVal('Prenom');
		return
	}

	if (rel=='enfan' || rel=='conjo') {
		Sql = "UPSERT contact_l SET Col="+Avec+", ColP="+Avec+", ColT='famil', ColTyp='paren' WHERE Col="+Avec+" AND ColP="+Avec;
		ExecAction([{Action:"ExecSql", Sql:Sql}])
	}

	R = this.Rels[Cod];  if (R)  CodCol = R.Cle
	S =  "SET Col="+Cod+", ColP="+Avec+", ColT='famil', ColTyp='"+rel+"' "
	if (CodCol) { Sql= "UPDATE";  SqW= "WHERE CodCol="+CodCol; }else{ Sql= "UPSERT";  SqW = "WHERE Col="+Cod+" AND ColP="+Avec }
	if (rel=='accom') { Sql = "DELETE FROM";  S='' }
	Sql= Sql + ' contact_l ' + S + SqW;			//alert(Cod+crr+Sql+crr+JSW(R))
	ExecAction([{Action:"ExecSql", Sql:Sql}],'',{CallBack:function(re) {
		if (re) {
		  wh(re,'DivOther')
			re = JSOND(re)
			S = re.CleInsert;  if (S<0)  S = CodCol
			if (rel=='accom')  App.CaddU.Rels[Cod] = { rel:rel, Avec:Avec };  else  App.CaddU.Rels[Cod] = { Cle:S, rel:rel, Avec:Avec }
			var O = { DatMaj:FormatDate(new Date(),5), Participants:App.CaddU.Rels }
			Sql = "UPDATE contact SET Particips='"+Replace(JSONS(O),'"','\"')+"' WHERE CodCo=" + App.CaddU.First
			ExecAction([{Action:'ExecSql', Sql:Sql}],'',{CallBack:function(re){
			  wh(re,'DivOther')
				if (re) {
					re = JSOND(re)
				}
			}})
		}
	}})
}

CaddUClass.prototype.boucle_Oui = function() { ExecAction([{Action:"LoadPHP", TypL:"site/Inscript2.php", CodCal:Cal.CodCal, Zone:"DivLibre1"}]) }
CaddUClass.prototype.boucle_Non = function() { Cal.Sov() }

CaddUClass.prototype.Clear = function() { this.T = {};  this.Rels = {};  this.Sels = null;  this.nb = 0;  this.nbSel = 0;  this.Div.innerHTML='' }

