var reJ = null
var Sess = {}

function ExecAction(Action, Opt) {
	var ActionT, Pgm, O, re, e, Param, i, Ad, S;		//alert(Action+crr+Param)
	if (!Opt)  Opt={}
	ActionT = JSONS(Action);     //alert(JSW(Action)+crr+ActionT)
	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.Url || '/kiwi/php/Ajax.php';   //alert(Ad)
	if (Bug) {
		PH('ActBug',Action);
		//PH('ActBug',Param,1)
	}
	XmlPost(Param, Ad, TraitResult, Opt)
}

function TraitResult(re, Opt, reJ) {
	var T, NomZ, H, S
	T = Decoupe(re);	if (Bug) { LID('DivBug').innerHTML='' }  //PH('ActBug',reJ,1);
		//PH('DivBug2',re)
	for (NomZ in T) {
		H = T[NomZ]
		if (NomZ=='DivHBug') {
			PH(NomZ,H)
		}else if (NomZ.substr(0,3)=='JS_') {
		  H = JSONS(H)
		}else{
			LID(NomZ).innerHTML = H
		}
		if (Bug && NomZ!='DivHBug') {
		  if (Bug==1)  H = H.substr(0,1800)
			LID('DivBug').innerHTML+= wDiv(NomZ,H,1)
		}
	}
	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]+')')
		}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
	TP = re.split('|||')
	if (TP && TP.length>2)  re = TP[2]
	C = re.substr(0,1)
	if (C=='{' || C=='[')  JS = eval('('+re+')')
  return JS
}


// ================================      EVENEMENTS       ===========================
function hashChanged() {
	if (App.ClienLien) { App.ClienLien=0;  return }
	var S, T, V
	//T = Tableau1D(location.hash,'=&');  if (T.Cle)  S='Win:,'+T.Cle;  else  return
	V = VarListe(location.hash, "Cle", "&", "=")
	if (!location.hash) { location.href=RootSite;  return }
	if (V)  S='Win:,'+V;  else{ return }
	//Zones.Memo.ZoneAction = T.ZA
	ExecAction(S)
	if (window.pageTracker)  try { pageTracker._trackPageview(); } catch(err) {}
}

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 Wash(OA) {
	var A, D, Action, V, S
	if (!OA || !window.onhashchange)  return
	if (OA.tagName=='A')  A = OA;  else  A = LTag(OA,'A',0)
	if (A && A.search.indexOf('Cle=')>=0) { V = VarListe(A.search, "Cle", "&", "=");  PX(A,'Action','Win:,'+V) }
 	if (A) {
		Action = LX(OA,'Action');
		D = ElemPAt(A,'ZoneAction')
		if ((S=A.search))
			S = S.substr(1);
		else if (Action) {
			if (Action.substr(0,5)=='Win:,')  S='Cle=' + Action.substr(5)
		}
		location.hash = S;  App.ClienLien = 1
	}
	//location.hash = A.search.substr(1)+'&ZA='+LX(D,'ZoneAction');  App.ClienLien = 1
}

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')
	setTimeout(function() {W.document.body.innerHTML = D.innerHTML}, 1200)
}

