var TAction = []
var reJ = null
// ====================================      VUES       ===============================
//var Vues  = new ClassVues();
var Zones = new ClassZones();

function ClassVues() {
	this.T = [];
	this.nb = 0;
}

ClassVues.prototype.Add = function(Nom) {
	var T
	if ((T=this.T[Nom]))  return T
	T = this.T[Nom] = new ClassVue(Nom)
	return T
}

// --------------------------      VUE      ----------------------------
function ClassVue(Nom) {
	this.Nom = Nom
}


// ====================================      ZONES       ===============================
function ClassZones() {
	this.all = {};
	this.nb = 0;
	this.ZoneDef = 'volet2'
	this.Memo = {CleClic:0, LibClic:'', ChemClic:'', ZoneAction:'', Vars:{} }
}

ClassZones.prototype.Add = function(Nom) {
	var Z
	if ((Z=this.all[Nom])) {
		Z.Div = LID(Nom)    // au cas où la div a été recréée
		return Z
	}
	Z = this.all[Nom] = new ClassZone(Nom)
	Z.onMake()
	return Z
}

ClassZones.prototype.PutProp = function(Nom, V) {
	var Z, nz
	for (nz in this.all) {
		Z = this.all[nz];
		Z[Nom] = V
	}
}

ClassZones.prototype.Contexte = function() {
	var Z, nz, H='', S, O
	for (nz in this.all) {
		if (nz.indexOf('Bug')>=0)  continue;
		//if (!LID(nz)) { delete(this.all[nz]);  continue; }  cmt car sinon dans le site capeb où menu1 n'E pas encore, la zone est supprimée
		Z = this.all[nz];  if (!Z)  continue;
		//if (S=JSONS(Z.Data))  H+=', "'+nz+'":'+S
		if (Z.Rempli)  H+=', "'+nz+'":{Cle:'+Z.Cle+'}'
	}
	H = H.substr(2);  //if (!H)  return ''
	O = new ObjClone(this.Memo);  O.Zones = eval('({'+H+'})')
	H = JSONS(O, {nonVide:1});		//alert(H)
	return H
}

ClassZones.prototype.toString = function() {
	var Z, nz, S=''
	for (nz in this.all) {
	  Z = this.all[nz]
		S+=Z.Nom+cr
	}
	return S// + crr + JSONS(this.all)
}

ClassZones.prototype.Clear = function() { delete(this.all) }

ClassZones.prototype.toString = function() {
	return JSW(this)
}


// --------------------------      ZONE      ----------------------------
function ClassZone(Nom) {
	this.Nom = Nom
	this.Div = LID(Nom);  PX(this.Div,'Zone',Nom)
	this.Cle = null
	this.noAction = 0;
	this.Data = {}
	this.Rempli = 0     // rempli actuellement
	this.ReceptH = 0    // rempli sur action
	this.Hist = []
}

ClassZone.prototype.Htm = function(H) {
	if (!this.Div)  return
	if (H==undefined)
		return this.Div.innerHTML;
	else{
		var idem=0, vide=0
		if (!H)  vide = 1;  if (this.Div.innerHTML==H)  idem = 1
	  var re = this.debChange(idem,vide)
		if (re==undefined || re==true) {
			if (H)  this.Div.innerHTML = H;  else  ClearDiv(this.Div)
		}
	  this.onChange(idem,vide)
	}
}

ClassZone.prototype.Clear = function(Opt) {
  var re = this.debClear();  if (re===false)  return
  if (this.Div) {
		if (Opt)  this.Htm('');  else  this.Div.innerHTML = '' //ClearDiv(this.Div)
	}
  this.onClear()
}

ClassZone.prototype.Memo = function(JS) {
	copyObj(this, JS)
}

ClassZone.prototype.onMake = function() {}
ClassZone.prototype.debChange = function() {}
ClassZone.prototype.onChange = function() {}
ClassZone.prototype.debClear = function() {}
ClassZone.prototype.onClear = function() {}

ClassZone.prototype.toString = function() {
	//var Z, nz, S='';  for (nz in this.all) { Z = this.all[nz];  S+=Z.Nom+cr };  return S
	return JSW(this)
}


// ====================================      SS-PGM       ===============================
function ExecAct(Action, Li, fct, Param) {
	//ExecAct('LitSql:SELECT SUM(apQuant) AS Tot FROM flux WHERE CodPrd='+V+' AND QUARTER(apDat)=QUARTER(Now())', null, function(re, Param) {
	var re, e, Contexte, i, S;		//alert(Action+crr+Li)
	Contexte = Zones.Contexte();  Li += '&Contexte='+escape(Contexte);    //alert(Li)
	XmlPost2(Li, '/SIP/iview/appli.php?Action='+Action+'&BugH='+Bug+AjUrl, fct, Param)
}

function ExAction(Action, OA) {
	var S, D, A
	MemoAction(OA, Action)
	var Action = Zones.Memo.Action
//alert(JSW(Action))
	if (LX(OA,'TypClic')=='switch') {
		A = Action[0]
		if ((D=LID(A.Zone)) && D.innerHTML) { InverseDisplay(D);  return }
	}
	ExecAction(Action, LX(OA,'Param'))
}

function MemoAction(O, Action, Opt) {
	var OP, AO, ZA, Fo, M, Action, ActionT, S, V, T
	//if (LVal('Rech')=='a')  alert(aa)
	if (typeof Opt!=='object')  Opt={}
	if (!Action) { Action = LX(O,'Action');  if (!Action) return }		//;alert(Lout(O)+crr+Lout(ElemPAt(O,'ValCle')))
	if (typeof(Action)=="string") {
		ActionT = Action
		if (Action.substr(0,1)=='[') {
				//alert(Action)
			Action = eval('('+Action+')')
		}else{
		  Action = [{}];  AO = Action[0]
		  T = ActionT.split(':')
		  AO.Action = T[0]
		  if (T.length>1) {  AO.TypL = Isole(T[1],',',1);  if ((S=Isole(T[1],',',2)))  AO.ValCle = S; }
		  if (T.length>2) {  AO.Param = T[2] }
		}
	}
	AO = Action[0]
	if (App.Transfo_Action)  App.Transfo_Action(Action)

	M = Zones.Memo
	M.Action = Action
	M.ZoneAction = '' // sinon un ExecAction ne provenant pas d'un clic (O est vide) garde la valeur du dernier clic et cela influe dans Vue.PreparTree
	if (O) {
		if (App.Modul.Fo && (Fo=FoGen(O))) {  // Ajouter les données du formulaire parent dans l'Action (ex: clic sur Ajouter une fiche d'un ss-form inclut dans un form)
			AO.foAppel = Fo.Nom		//alert(JSW(AO))
			//AO.JSData = Fo.Table.ExportRecord();		//alert(JSW(AO))
		}
		OP = ElemPAt(O,'ValCle')
		M.CleClic = LX(OP,'ValCle');
		M.LibClic = Trim(Isole(LText(O),'\n',1))
		M.ChemClic = LX(OP,'Chemin')
		ZA = ElemPAt(O,'ZoneAction')
		if (ZA) {
			M.ZoneAction = LX(ZA,'ZoneAction')
			T = new Array()
			while((ZA=ElemPAt(ZA,'ZoneAction'))) {
			  S = LX(ZA,'ZoneAction')
				V = GetActionZone(S)
				T.push(V);			//alert(JSONS(T)+crr+JSONS(V)+crr+Lout(ZA))
				ZA = ZA.parentNode
			}
			M.ZonesParent = T
		}
		Zones.Param = LX(O,'Param')
		if ((S=ElemPAt(O,'!ZoneDef')))  M.ZoneDef = S;  else M.ZoneDef=null
	}
}

function ExecAction(Action, Param, Opt) {
	var ActionT='', T, T2, O, re, e, Contexte, i, S;		//alert(Action+crr+Param)
	if (Param==undefined)  Param=''

	if (typeof Opt!=='object')  Opt={}
	if (App.debAction)  App.debAction()
	
	if (typeof(Action)=='string') {
		MemoAction(null, Action, Opt)
		Action = Zones.Memo.Action
	}
	if (Action) {
		ActionT = JSONS(Action);     //alert(ActionT)
	  O = Action[0]
		if (O.Action=='JS') { Eval(O.JS,1); return 1 }
		if (O.paramWin) {
			S = O.paramWin.Nom
			if (!(re=Wins.all[S]))  re = WinMake(O);
			//setTimeout('Wins.all["'+S+'"].Show()',1000)
			re.Show(O)
		}
		if (O.TypAct && O.TypAct=='Win') {
		  var Ad = O.Nom;  if (Ad.indexOf('.')<0)  Ad = 'iview/'+Ad+'.php?'
			//Action.splice(0,1);
			O.TypAct='Dialog';  ActionT = JSONS(Action);
		  Ad+='Action='+ActionT+AjUrl
			window.open(Ad, 'Dialog', "width=850,height=600,titlebar=1,toolbar=1,directories=1,menubar=1,scrollbars=1,resizable=1,status=1");
			return
		}
	}
		//alert(JSONS(Action))
	reJ = App.Retour = null
	Contexte = Zones.Contexte();    //alert(Contexte)
	//Contexte = Replace(Contexte,'&','&amp;');  
	Param += '&Contexte='+escape(Contexte);    //alert(ActionT+crr+Param)
	Zones.Memo.Action = Action
	S = '/SIP/iview/appli.php?Action='+ActionT+'&BugH='+Bug+AjUrl
	if (!App.F2 && Opt.Asynch!==0) {
		var Pgm = Opt.CallBack;  if (!Pgm)  Pgm = TraitResult
		XmlPost2(Param, S, Pgm, Opt)
	}else{
		re = XmlPost(Param, S);  //encodeURIComponent
		TraitResult(re, Opt)
		return (re)
	}
}

function TraitResult(re, Opt) {
	var A, O, Z, e, S, JSS, i, NomZ, po;
	if (!Opt)  Opt={}
	delete(Apg);  Apg = {}
	var Memo = Zones.Memo
	var Action = Memo.Action;   if (!Action)  Action = [{}];		//alert(JSONS(Action))
	Memo.ActionZone = A = Action[0]     // ActionZone = dernière action influant sur une zone (LitSql non pris en compte)
	if (!re) {
		re = LText('DivPrim');  PText('DivPrim','')
	}//else  PText('DivPrim',re)

	if (re.substr(0,1)=='{') {
		JSS = Isole(re,'|||',1);  re = re.substr(JSS.length);		//alert(JSS+crr+re)
		reJ = App.Retour = eval('('+JSS+')')
		if ((S=reJ.Sess))  Memo.Sess = S
		if (reJ.RP) { Memo.oldNiv = Memo.Niv;  Memo.Niv = reJ.RP.Niv; }		//alert(Memo.Niv+cr+Memo.oldNiv+crr+re)
		if (reJ.Zones) {
			//alert(JSW(reJ.Zones))
			for (NomZ in reJ.Zones) {
				if (!(O=reJ.Zones[NomZ]))  continue;
				Z = Zones.Add(NomZ);  po =reJ[NomZ];  if (po) { Z.reJ = po;  Z.Action = new Array();  Z.Action.push(po.Action) }
				if (!O.ReceptH)  continue
				if (!re)  Z.onChange()
				O.Rempli = 1
				Z.Memo(O);  Z.Cle = O.Cle;  Z.Info = O.Info;
				//if (NomZ==A.Zone) { Z.Action = new Array();  Z.Action.push(A) }
				//alert(JSW(Z)+crr+JSW(O))
			}
			//alert(JSW(Zones.all))
		}
		//Action = Zones.Memo.Action = App.Retour.Acts
	}
	if (App.finTraitMemo) { App.finTraitMemo();  App.finTraitMemo = null }
	if (App.finTraitMemo2)  App.finTraitMemo2();
		//alert(Zones.all.menu1+crr+Zones.all.ssmenu+crr+Zones.all.volet2)
	if (WinBug)  WinBug.LID('TitBug').innerHTML = '<b>Action</b>='+JSONS(Action)+'<br><b>Contexte=</b>'+Zones.Contexte()+cr+'<br>' + '<b>reJ=</b>' + JSS
	if (re.substr(0,1)!='|') { if (WinBug && re)  WinBug.LID('DivHBug').innerHTML = re;  return }

	if (A.ValCle)  App.LastActWithCle = Action  // Pour les retours
	//S = Isole(re,'|||',2);			//alert(S+crr+re)
	if (!Opt.noPush && (Action && Action[0] && !Action[0].PosJeu)) {
		S = JSONS(LastAction());  e = JSONS(Memo);  //alert(JSONS(Action)+crr+S+crr+e);  if (S!=e)  alert('Memo'+crr+JSONS(Memo))
		if (S!=e)  TAction.push(new ObjClone(Memo))
	}
	if (Bug==3 && WinBug)  PText(WinBug.LID('DivBug'),re+crr)
	e = TraitRetour(re);  if (!e)  return

	Distrib(re, Opt);			//alert(Zones)
	if (A.finAction)  eval(A.finAction)
	FinAction(Opt)
	if (App.finAction) { App.finAction();  App.finAction = null; }
	if (App.finAction2) setTimeout("App.finAction2()",100)
	if (Action) {
		for (i=0; i<Action.length; i++) {
			O = Action[i];  S = O.Action;  if (Apg['fin_'+S]) { Apg['fin_'+S]();  Apg['fin_'+S] = null }
			S = O.NomR;  if (Apg['fin_'+S]) { Apg['fin_'+S]();  Apg['fin_'+S] = null }
		}
	}
	if (App.Affich) { App.Affich();  App.Affich = null }
	if (Opt.Suite)  Opt.Suite(Opt)
		//alert('ExecAction'+crr+TAction.length+crr+AffAction())
}

function Distrib(H, Opt) {
	var TP, T, TD, Z, P, H, Zone, NomZ, NomZR, O, LiAct, i, S, Chaine, D, Act, volet2_fait, $Win;
	//PVal('volet2',H);  return
	TP = H.split('|||')
	if (WinBug)  PText(WinBug.LID('DivBug'),'')
	Zones.PutProp('ReceptH',0)

	for (i=1; i<TP.length-1; i+=2) {
		LiAct = TP[i];  T = null
		H = TP[i+1];
		if (LiAct.indexOf('=')>0) { TD = Tableau1D(LiAct, '=|');  NomZ = TD['Zone']; }
		NomZR = Isole(NomZ,'~',1)
			//alert(LiAct+crr+NomZ+crr+JSON_Encode(TD)+crr+H)
		if (NomZR=='volet2')  volet2_fait=1;
		if (NomZ=='JS') {
			eval('('+H+')');
			continue
		}
		//alert(LiAct+crr+H)
		if (H.substr(0,2)=='||') {
			T = H.substr(2).split('||');
			T[0] = Trim(T[0])
			H = T[1]
		}
		H = Trim(H)
		Zone = LID(NomZ);  $Win = null
		if (!Zone) {
			//alert(NomZ+crr+window[NomZ])
			if (($Win=window[NomZ])) {
				$Win.html(H);
				if ($Win.length)  Zone = $Win[0];  //if ((S=$Win.JS.height))  Zone.style.height = S + 'px'
				Zones.all[NomZ].onChange()
			}else{
				Zone = D = document.createElement('div');  PX(D,'id',NomZ);  D.className='divAjout'
				document.body.appendChild(D)
			}
		}
		if (Zone) {
			TD.Cle = getContextZone(NomZ, 'Cle');     //alert(Lout(Zone)+crr+H)
			Z = Zones.Add(NomZ);  Z.Data = TD
			if (H || (reJ.Zones[NomZ] && reJ.Zones[NomZ].HideVide=='!')) { // ! permet de vider le volet si rien en réception (cas de volet (fiche Iris Zone) déclaré mais sans contenu)
				if (NomZ.indexOf('~')>=0) { D = Zone.parentNode;  RX(D,'ValCle');  RX(D,'TypDoc'); }

				// --------------  PUT  -----------------
				if (NomZ=='DivHBug') {
					if (WinBug)  WinBug.LID(NomZ).innerHTML = H
				}else if (!$Win) {
				  //Z.debChange();  PVal(Zone, H, 1);  Z.onChange();
				  Z.Htm(H);       //alert(NomZ+crr+H)
					if ((D=LID('debScript'))) {
						S = Z.onChange.toString();
						Chaine = D.innerHTML;  RX('debScript','id');		//alert(LID('debScript')+crr+Chaine)
						eval(Chaine);
						if (S.length<18)  Z.onChange()   // le onChange étant dans Chaine, il ne peut pas être lancé par Z.Htm(H)
					}
					O = ElemAt(Zone, 'ValCle', null, 0);
					if (O) { S = LX(O,'clp');  PX(Zone,'ValCle',S,1);  PX(Zone,'cl1',S,1);  PX(Zone,'Table',LX(O,'Table'),1); }
				}
				// ------------  FIN PUT  ---------------

				//if (NomZ.indexOf('~')>=0)  alert(NomZ+crr+Lout(Zone))
				if (H)  AdaptZone(Zone)
			}
			Z = Zones.all[NomZ];  Z.SiH = (H) ? 1:0;  Z.ReceptH = 1
			S = Zone.className
			if (S!='divAjout' && !hasClass(Zone,'hideGen') && S.indexOf('yui-tabpanel')<0 && LX(Zone.parentNode,'id')!='DivBugs') {
				Aff(Zone);  //if ((S=LX(Zone,'conteneur')))  Aff(S)
			}
			Act = GetActionZone(NomZ);
			if (!LX(Zone,'ZoneAction'))  PX(Zone,'ZoneAction',NomZ)
			if (App['Zone'+NomZ])  App['Zone'+NomZ]()     		//--> appel de Zonevolet2()
			//if ((O=LID(NomZ+'_Act')))  TD.Act = eval('('+LText(O)+')')
		}
		if (T && T[0]) { eval(T[0])	}
		if (Bug && NomZ!='DivHBug') {
		  S = TP[i+1];  if (Bug==1)  S = S.substr(0,1800)
			if (WinBug) WinBug.LID('DivBug').innerHTML+= wDiv(NomZ,S,1)
		}
		if (H && (Chaine=LVal('ScriptSuppl_'+NomZ))) {
			//PText('ScriptSuppl_'+NomZ,'');
			eval(Chaine);
		}
		if (Act && Act.ParamIn)  ExecParam(Act.ParamIn)  // à placer après de script T.InitChamps (Init)
		if (window['finAction_'+NomZ])  window['finAction_'+NomZ]()     //--> appel de finAction_volet2()
	}
	//if (!volet2_fait && !Opt.noPush)  PVal('volet2', '');   // ::> mise en cmt car effacement d'une div inclue dans volet2 dans Sicos>Stats>Ok
	if ((Z=LID('DivScript'))) {
		Chaine = Z.innerHTML;  RX('DivScript','id');		//alert(LID('DivScript')+crr+Chaine)
		eval(Chaine);
	}
}

function Decoupe(Div) {
	// Découpe la réponse dans un tableau
	var T = {}, TP, Nom, S, i, re
	if (typeof(Div)=='string' && Div.length>10)  re = Div;  else if ((Div=LID(Div)))  re = Div.innerHTML
	if (re.substr(0,1)=='{') {
		S = Isole(re,'|||',1);  re = re.substr(S.length);		//alert(S+crr+re)
		T.JS = eval('('+S+')')
	}

	TP = re.split('|||')
	for (i=1; i<TP.length-1; i+=2) {
		S = TP[i];  Nom = Isole(S,'=',2);		//alert(S+crr+Nom+crr+TP[i+1])
		T[Nom] = TP[i+1];
	}
  return T
}

function DecoupeJS(Div) {
	// Découpe la partie JSON
	var JS, S, re, C
	if (!Div)  return
	if (typeof(Div)=='string' && Div.length>10)  re = Div;  else if ((Div=LID(Div)))  re = Div.innerHTML
	C = re.substr(0,1)
	if (C=='{' || C=='[') {
		S = Isole(re,'|||',1);  re = re.substr(S.length)
		JS = eval('('+S+')')
	}
  return JS
}

function AdaptZone(Zone) {
	var Os, O, X, i, S, Z, D
	Z = Isole(Zone.id,'~',1)
	
	if (window.YAHOO) {
	  Os = $G('div[MoveDiv]',Zone)
	  for (i in Os) {
			O = Os[i];
			if ((D=LID(LX(O,'MoveDiv'))))  D.insertBefore(O);			//alert(Lout(O)+crr+Lout(D))
		}
		if (YAHOO.util.DD && ElemPAt(Z,'drag')) {
			Os = ElemAt(Z,'ValCle');		//alert(Z+cr+Os.length)
			for (i in Os) {
				O = Os[i]
				X = new YAHOO.util.DD(O);
				X.clickValidator = clickValidator
				X.onDragDrop = onDragDrop
				X.onInvalidDrop = function(e) { Dom.setXY(this.getEl(), this.xy); }
			}
		}
	}
	//if (ElemPAt(Z,'drop')) {  Os = ElemAt(Z,"ValCle");  for (i in Os) { new YAHOO.util.DDTarget(Os[i]); } }
	O = ElemPAt(Zone,'objet')
	if ((S=LX(O,'objet'))) {
		O = eval(S);  if (O.PreparCod)  O.PreparCod()
	}
}

function FinAction(Opt) {
	var S
	if (!Opt.noPush) {
		Hide_Div_Vide()
		if (window.RePosWin)  RePosWin()
	}
}

function Hide_Div_Vide() {
	var TZ, Zone, Z, ZA, ZoneP, T, S, c, cc
	if (!reJ)  return
	TZ = reJ.Zones;
	var Memo = Zones.Memo
	ZA = Memo.ZoneAction;		//alert(JSONS(TZ))
	for (Zone in TZ) {
		Z = TZ[Zone];  if (!Z)  continue;
		if (Zones.all[Zone] && Zones.all[Zone].ReceptH)  continue;  // si reçu du code
		cc = Z.HideVide;  if (!cc)  continue;  c = cc.substr(0,1);  	//alert('Zone='+Zone+crr+'c='+c+','+cc+cr+'ZA='+ZA+crr+Memo.Niv+','+Memo.oldNiv)
		if (c=='<') { cc = ze(cc.substr(1));  if (Memo.Niv>=cc)  continue; }  //Memo.Niv>Memo.oldNiv ||
 		else if (c!='!' && (!ZA || (c!='*' && cc!=ZA)))  continue;
		if (App.ZA && App.menuClic && App.menuClic.indexOf(','+App.ZA+',')>=0) {
			PVal(Zone, '')   //Si la zone n'a pas reçu et si la provenance de l'action est correcte (HideVide), vider la zone. Ainsi un menu1 sans fils doit vider le volet1
			if ((ZoneP=VoletVide(Zone))) {
				Hide(ZoneP);
				if (WinBug)  wh("HIDE("+ZoneP+")",WinBug.LID('TitBug'))
			}
		}
	}
}

function GetActionZone(Zone) {
	var S
  //alert(Lout((Zone+'_Act')))
	S=LText(Zone+'_Act');
	if (!S)  S=LText('LastAct');
	if (S) {
	  if (S.substr(0,1)=='[' && S.substr(S.length-1,1)==']') {
			return eval('('+S+')')
		}//else  alert('mauvais retour'+crr+S)
	}
}

function getContextZone(NomZ, Param) {
	var O
	//if (Param=='Cle')  alert(JSONS(reJ.Zones))
	if (reJ && reJ.Zones)  O = reJ.Zones[NomZ];  if (!O)  return
	//alert(NomZ+'='+JSONS(O)+crr+JSONS(reJ))
	if (Param)  return O[Param];  else  return O
}

function AffAction() {
	var S='', i
	//if (TAction.length>2)  alert(aa)
	for (i=0; i<TAction.length; i++) {
		S+=JSONS(TAction[i])+crr;
	}
	return S+crr+Zones
}

function LastAction() {
	return TAction[TAction.length-1];
}

function Retour(n, Opt) {
	var O, M, S, i
	if (!n)  n=2;		//alert(n+','+TAction.length+crr+AffAction())
	if (!Opt)  Opt={}
	if (TAction.length<n)  ExecAction('')
  if (TAction.length<n)  return;
	for (i=0; i<n; i++) {
		O = TAction.pop();
	}
	if (!O)  return
	M = Zones.Memo;  M.CleClic = O.CleClic;  M.LibClic = O.LibClic;  M.ZoneAction = O.ZoneAction
	  //alert(TAction.length+crr+JSW(O.Action)+crr+AffAction())
	ExecAction(O.Action,'',Opt.ExecAction)
}

function ExecParam(Param) {
	var NomV, S, V
	for (NomV in Param) {
		V = Param[NomV]
		PVal(NomV,V)
	}
}

function PutParam(Param, TR) {
	var NomCh, NomV, V
	for (NomV in Param) {
		NomCh = Param[NomV]
		V = LX(TR,NomCh);  if (!V)  V = LVal(NomCh)
		Param[NomV] = V
		//Param[NomV] = escape(V)
	}
}

function TraitRetour(re, tjrs) {
	var T, Msg, Msg2, S
	if (re.substr(0,1)=='{') {
		S = Isole(re,'|||',1);  Msg2 = re.substr(S.length);		//alert(S+crr+re)
		T = eval('('+S+')')
		Msg = T.MsgErr;  
	}else if (re.substr(0,1)!='|')
		Msg = re
	else{
		T = re.split('|')
		switch(T[1]) {
			case 'Msg':  Msg = T[2];  break
			default:  if (tjrs) Msg = T[2];  break
		}
	}
	if (WinBug && Msg2)  PVal(WinBug.LID('DivOther'),Msg2,1);
	if (Msg) { PVal(Zones.ZoneDef,Msg,1);  return false }  else  return true;
}

App.debAction = function() {
	//App.finSubmit = null
}


// ====================================      prototype       ===============================
TableClass.prototype.SovBack = function(re, Param) {
	if (WinBug)  WinBug.LID('DivOther').innerHTML = wDiv('Sov', re) + wDiv('App.Retour', JSW(reJ), 1)
	TraitResult(re);
	//if (window.FinSubmit)  window.FinSubmit()
	if (reJ && reJ.backVal) {
		Retour()
	}
	//e = TraitRetour(re);  if (!e)  return;  Retour()
}

TableClass.prototype.SupprBack = function(re, Param) {
	TraitResult(re)
	if (reJ.backVal) {
		Retour()
	}
}


// ====================================      ValidIdent       ===============================
function ValidIdent_Class(id) {
	this.F		= LID(id)
	this.ci		= this.F.elements['Ident']
	this.cm		= this.F.elements['mp']
	this.btn	= this.F.elements['btnIdent']
	this.mode = 'ajax'
}

ValidIdent_Class.prototype.Submit = function() {
	var S, ci, cm
	ci = this.ci;  cm = this.cm
	if (ci.value=='Identifiant') { ci.value='';  this.F.action = MajListe(this.F.action,'Tree','','&','=',1,'') }
	if (cm.value=='Mot de passe')  cm.value=''
	if (this.mode=='post') {
		this.F.submit();  return
	}else{
	  this.F.action = ''
		XmlPost2('&Ident=' + ci.value + '&mp=' + cm.value, '/SIP/iview/appli.php?Action=VerifIdent' + AjUrl, this.Aff, this)
		ci.value='Identifiant';  cm.value='Mot de passe'
	}
}

ValidIdent_Class.prototype.Aff = function(re, VO) {
	var S
	PVal('MsgErr','')
	if (re) {   // re valide lorsque l'appel vient d'un XmlPost2
		S = Isole(re,'|',2);
		if (Isole(re,'|',1)=="Type=ERR") {
			PVal('MsgErr',S)
		}else{
	  	S = JSOND(S);  reJ = App.Retour = App.Retour || {};  reJ.Sess = S || {};    //alert(JSW(S))
		}
	}else
	  VO = this
	if (reJ) {
		var Sess = reJ.Sess;   //alert(JSW(Sess))
		var O = VO.btn
		if (Sess && (S=Sess.Ident)) {
		  Hide('DivChId');  Aff('DivNom');  PH('DivNom', 'Bienvenue '+' '+Sess.Civil+'&nbsp;'+Sess.Nom)
 			O.value = 'x';  O.title = 'Déconnexion'
		}else{
			Aff('DivChId');  Hide('DivNom');  O.value = 'OK';  O.title = 'Valider identifiant'
			//setTimeout("Focus('Ident')",1000)
		}
	}
}


// ====================================      DIVERS       ===============================
function Gokey(Cle) {
	ExecAction("Win:SIT," + Cle)
}

function ReIdent(O) {
	var A,B,TR
	if (typeof(O)=="string") {
		A = Isole(O,',',1);  B =  Isole(O,',',2)
	}else if (!isNaN(O)) {
    if (window.ReIdentNum) { ReIdentNum(O);  return }
	}else{
		TR = O.parentNode;  A = LX(TR,'Ident');  B = LX(TR,'mpCo')
	}
	PVal('Ident',A);  PVal('mp',B)
	ValidIdent()
}

function NomAction() {
	var V = Zones.Memo.Action[0].NomR;  if (!V)  V = Zones.Memo.Action[0].TypL
	return V
}

function Fiche(e, Cle, Act) {
	var O, TR, F, Fo, A, P
	O = EventCtrl(e)
	TR = ElemPAt(O,'ValCle');  if (!TR)  return
	if (!Cle)  Cle = LX(TR,'cl1');  if (!Cle)  return
	if (Act=='Form') {
		F = LTags(TR,'FORM',0);  Fo = eval(LX(F,'ojs'))
		if (Fo && (A=VerifChamps(Fo.Ctrls))) { alert(A);  return }
	}
	P = TR.parentNode
	A = [{Action:'Win', ValCle:Cle, ZoneForce:LX(P,'id'), ajoutNiv:LX(TR,'NivLst')}];		//alert(JSW(A)+crr+Lout(P))
	ExecAction(A, TR, {noPush:1})
}

GoAction = function(Act, O, This) {
	var S
	if (This && (S=This.ZoneAction)) {
		if (typeof(Act)=='string' && Act.substr(0,1)=='[') { Act = JSOND(Act);  Act.Zone = S }
	}
	ExAction(Act, O)
}

MajFiche = function(O, Aj) {
	var Cle, Ad, width, V, S, li, OA
	TR = ElemPAt(O,'ValCle');  TA = ElemPAt(O,'Table')
	V = LX(TR,'ValCle');  if ((S=LX(TR,'NomC')))  Zones.Memo.Vars[S] = V
	if ((S=LX(TA,'Form'))) {
		S+='?ValCle='+V
		OutPopup(S,LX(TA,'parWin'))
  	return
	}
	//alert(Lout(O)+crr+Lout(TR)+crr+Lout(OA))
	OA = ElemPAt(O,'Action');  if (OA && OA.tagName=='FORM')  OA = null
	li = LX(OA,'Action');  li = Replace(li,'$ValCle',V)
	if (li) {
		if (li.substr(0,1)=='[') { V = JSOND(li);  S = V[0].Action }  else  S = Isole(li,':',1)   // action texte du type Win:,8
		if (S!='Win') { ExAction(li, OA);  return }
	}

	var NomT, cl1
	if (!(NomT=LX(TA,'VueMaj')))  NomT = LX(TA,'Table')
	cl1 = LX(TR,'cl1')
	if (NomT && NomT=='base')
		Cle = 'Cle=' + LX(O,'ValCle');
	else if (NomT.substr(0,2)=='l_') {
		if (cl1 && !Aj)  Cle = 'cl1=' + cl1;  else  Cle = 'clp=' + cl1;
	}
	if (Cle) {
		switch(NomT) {
			case 'l_contact':  Ad = '/SIP/iview/form/contact.php?';  width='width=755,height=500';  break
			default:  Ad = '/SIP/Fich.php?';  width='width=760,height=600';
		}
		OutPopup(Ad + Cle + '&id=' + LX(TR,'id'), width)
	}else{
	  ExAction("Form:"+NomT+','+V, TR)
	}
}


// ================================      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 (!Zones.Memo.ZoneAction)  Zones.Memo.ZoneAction = 'menu1'
	if (window.pageTracker)  try { pageTracker._trackPageview(); } catch(err) {}
}

bodyClic = function(e, This) {
	var OP, O, OA, OC, TA, TR, S, V, Action, ActMaj, A, ok

	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));
	App.ZA = ElemPAt(OC,'!ZoneAction')

 	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')
	//if (O!=OA && O.tagName=='A' && O.search.indexOf('Cle=')>=0) { V = VarListe(O.search, "Cle", "&", "=");  PX(O,'Action','Win:,'+V) }
	Wash(OA)
// 	if (!OA && O.tagName=='A' && O.search.indexOf('Cle=')>=0) { V = VarListe(O.search, "Cle", "&", "=");  PX(O,'Action','Win:,'+V) }
// 	if (OA)  if (OA.tagName=='A')  A=OA;  else  A = LTag(OA,'A',0)
// 	if (A && window.onhashchange) {  //A.href &&
// 		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
// 	}
	TR = ElemPAt(O,'ValCle');  TA = ElemPAt(O,'Table');  if (!TR)  TR = TA  // au - pour IcoMaj Ajout

	ok = 0
	if ((S=LX(O,'Obj'))) { if (window[S+'_clic']) { window[S+'_clic'](O);  return true } }
	if (S) {
		switch(S) {
			case 'IcoMaj':
			case 'IcoAj':  MajFiche(O);  ok=1;  break
		}
	}
	if (!ok) {
	  if (This.ClicMe)  if (!(V=This.ClicMe(O,TR,TA)))  return
		if (!OA)  return
		if (ActMaj) {  // ajout de params action dans Action
			var JS, Z
			JS = JSOND(ActMaj)
			Z = ElemPAt(OA,'!Zone')
			if (Z && JS && (Action=Zones.all[Z].Action)) { O = Action[0];  copyObj(O,JS) }		//alert(JSW(JS)+crr+JSW(O))
		}
		//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)
}
