var reJ = null
var Sess = {}, Profil=30

function ExecAction(Action, Opt) {
	var ActionT, Pgm, O, re, e, Param, i, Ad, S;		//alert(Action+crr+Param)
	if (TypOf(Opt)=='Function')  Opt = {Callback:Opt};  else if (!Opt)  Opt={}
	ActionT = JSONS(Action);     //alert(JSW(Action)+crr+ActionT)
	if (isObj(Action)) { if (ActionT.substr(0,1)=='[')  S = Action[0].Action;  else  S = Action.Action;  Opt.NomAction = S }
	Param = 'Action='+encodeURIComponent(ActionT);  if (Sess.Co)  Param += '&Sess='+JSONS(Sess)
	Param+= '&Bug='+Bug+AjUrl
	if ((Ad=Opt.phpTrait))  delete(Opt.phpTrait);  else  Ad = (App.Ajax && App.Ajax.Url) || '/kiwi/php/Ajax.php';   //alert(Ad)
	PHB(Action,'Action','Act');  //PHB(Param,'Param','Act',1)
	XmlPost(Param, Ad, TraitResult, Opt)
}

function TraitResult(re, Opt, reJ) {
	var T, NomZ, H, S, Vu;		//alert(re)
	T = Decoupe(re);	PHB(re,'TraitResult','Contenu');  PHB(reJ,'TraitResult.reJ','Act')
	if (!reJ && re.indexOf('error')>=0) { alert("Erreur de chargement\n\n"+re);  return }
	for (NomZ in T) {
		H = T[NomZ];		//alert(NomZ+crr+H)
		if (NomZ=='DivHBug') {
			PHB(H,Opt.NomAction,'DivH')
		}else if (NomZ.substr(0,3)=='JS_') {
		  H = JSONS(H)
		}else{
			if (window.getVueP && (Vu=getVueP(NomZ)))  Vu.Htm(H,NomZ);  else  LID(NomZ).innerHTML = H
		}
		if (Bug && NomZ!='DivHBug') {
		  if (Bug==1)  H = H.substr(0,1800)
			PHB(wDiv(NomZ,H,1), 'TraitResult3', 'Contenu', 1)
		}
	}
	if (!reJ)  reJ = {}
	if (Opt.memoRe)  Opt.memoRe = reJ.re
	if ((fct=Opt.Callback)) {
		Opt.Callback=null;
		//fct(re, Opt, reJ)
		if (Opt.This)  fct.call(Opt.This, re, Opt, reJ);  else  fct(re, Opt, reJ)
	}
}

function Decoupe(Div) {
	// Découpe la réponse dans un tableau
	var JS, S, T={}, TP, TN, re, i, Nom, NomO
	if (isObj(Div))  re = Div.innerHTML;  else  re = Div

	TP = re.split('|||')
	for (i=1; i<TP.length-1; i+=2) {
		TN = TP[i].split('=');  NomO = TN[0];  Nom = TN[1];		//alert(S+crr+Nom+crr+TP[i+1])
		if (NomO=='JS') {
			T['JS_'+Nom] = eval('('+TP[i+1]+')')
			//T['JS_'+Nom] = JSOND(TP[i+1])
		}else{
			T[Nom] = TP[i+1];
		}
	}
  return T
}

function DecoupeJS(Div) {
	// Découpe la partie JSON
	var JS, TP, re, C
	if (isObj(Div))  re = Div.innerHTML;  else  re = Div
	re = Trim(re)
	TP = re.split('|||')
	if (TP && TP.length>2)  re = TP[2]
	C = re.substr(0,1)
	if (C=='{' || C=='[')  JS = eval('('+re+')')
  return JS
}


// ================================      CONNEXION       ===========================
function ConnexClass() {
	var stk, sts, CookSess = 'Sess_'+Proj,  CookTps = 1
	if ((!ie && navV<8)) { stk = window.localStorage;  sts = window.sessionStorage; }
	return {
		Init:function () {  // Lit la session et charge Sess & Profil
			var S, J
			if (App.Sess) {
				S = JSONS(App.Sess)
			}else{
				if (stk)
					S = stk[CookSess] || sts[CookSess]
				else
					S = GetCookie(CookSess)
			}
			if (S) {
				J = JSOND(S);		//alert(JSW(J))
				if (J && J.Date) {
					Sess.Co = J
					Profil = Sess.Co.Profil;		//alert(JSW(Sess))
					if (window.onProfil)  window.onProfil()
				}
			}
			if (Sess.Co) { this.Msg();  return 1 }
		},
		Load:function (reJ, memoMp) { // Charge identité depuis formulaire
			var JA, S;      //alert(memoMp+crr+JSW(reJ))
			if (!reJ.Sess) { alert("Non trouvé");  return };  if ((S=reJ.Sess.MsgErr)) { alert(S);  return }
			Sess.Co = reJ.Sess.Co
			//if ((JA=reJ.Sess.Co.Descript)) { reJ.Sess.Co.Droit = JSOND(JA);  delete(reJ.Sess.Co.Descript) }
			Sess.Co.Date = new Date;  S = JSONS(Sess.Co)
			if (stk) {
				sts[CookSess] = S
				if (memoMp)  stk[CookSess] = S;  else if (stk[CookSess])  stk.removeItem(CookSess);
			}
			SetCookie(CookSess, S, ((memoMp) ? CookTps:null),'/')
			location.reload()
		},
		Msg:function() {},
		Connex:function(Id,mp) { LoadDemar({Ident:Id, mp:mp}) },
		Deconnex:function() {
			if (stk && stk[CookSess])  stk.removeItem(CookSess);  if (sts && sts[CookSess])  sts.removeItem(CookSess);
			DelCookie(CookSess,'/')
			location.reload()
		}
	}
}

var CX = ConnexClass()


// ================================      EVENEMENTS       ===========================
function CBData(Ctl, Nom, Opt) {  // Un SELECT avec setCB='=App.CB.CodRub' va exécuter la 1°x son Sql et la 2°x ses données
	var S, CB = App.CB[Nom]
	if (CB.Sql) {
		ExecAction({Action:'LitSql',Sql:CB.Sql}, function(re,S,reJ){	//alert(JSW(reJ)+crr+re)
			if (!reJ.retour) { alert("Erreur de requête");  return }
			App.CB[Nom] = reJ.Data
			setComboBox(Ctl, reJ.Data, Opt)
		})
	}else{
		setComboBox(Ctl, CB, Opt)
	}
}

bodyClic = function(e, This) {
	var OP, O, OA, OC, TA, TR, S, V, Action, ActMaj, A

	if (!This)  This = this
	OC = (e.tagName) ? e : e.target
	OP = This.Obj;  O = This.Sel;  if (!O)  O = OC;			alert('bodyClic'+crr+Lout(e.target)+crr+Lout(O)+crr+Lout(this.Sel));

 	if (ElemPClass(O,'EncartPrd') || ElemPClass(O,'DivCad')) { ActionPrd(O);  AnnulEvent(e);  return true; }

// 	OA = ElemPAt(OC,'Action');  if (OA && OA.tagName=='FORM')  OA = null
// 	if (!OA)  OA = ElemPAt(O,'ActMaj');
// 	Action = LX(OA,'Action');  ActMaj = LX(OA,'ActMaj')
// 	Wash(OA)
// 	TR = ElemPAt(O,'ValCle');  TA = ElemPAt(O,'Table');  if (!TR)  TR = TA  // au - pour IcoMaj Ajout
//
// 	if ((S=LX(O,'Obj'))) { if (window[S+'_clic']) { window[S+'_clic'](O);  return true } }
//   if (This.ClicMe)  if (!(V=This.ClicMe(O,TR,TA)))  return
// 	if (!OA)  return
//
// 	//alert(Action+crr+Lout(OA))
// 	ExAction(Action, OA)
// 	AnnulEvent(e)
	return true
}

function ImprimBody(Div) {
	var W, D
	D = LID(Div) || LID('volet2')
	W = window.open('page.php?css='+RootSite+'/admin/courrier.css', 'Imprim', 'toolbar=1,menubar=1,scrollbars=yes,resizable=yes,status=yes,width=760,height=600')
	W.onload = function() {
		var V2 = W.document.getElementById('volet2') || W.document.body
		V2.innerHTML = D.innerHTML
	}
	//setTimeout(function() {W.document.body.innerHTML = D.innerHTML}, 1200)
}

function onLoadIFC() { if (window.finIFC)  window.finIFC() }

