var TAction = []

// ====================================      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.T = [];
	this.nb = 0;
	this.ZoneDef = 'volet2'
	this.Memo = {CleClic:0, LibClic:'', ZoneAction:''}
}

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

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

ClassZones.prototype.Contexte = function() {
	var Z, nz, H='', S, O
	for (nz in this.T) {
		if (nz.indexOf('Bug')>=0)  continue;
		if (!LID(nz)) {
      //alert(nz)
			delete(this.T[nz])
			continue;
		}
		Z = this.T[nz];  if (!Z)  continue;
    if (S=JSON(Z.Data))  H+=', "'+nz+'":'+S
	}
	H = H.substr(2);  if (!H)  return ''
	O = new ObjClone(this.Memo);  O.Zones = eval('({'+H+'})')
	H = JSON(O, {nonVide:1});		//alert(H)
	return H
}

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

ClassZones.prototype.Clear = function() {
	//for (var nz in this.T)  this.T[nz] = null
	//var L, i;  L = this.T.length;  for (i=L; i>=0; i--)  this.T.splice(i,1)
	this.T = null
}


// --------------------------      ZONE      ----------------------------
function ClassZone(Nom) {
	this.Nom = Nom
	this.noAction = 0;
	this.Data = null
	this.ReceptH = 0
	this.Hist = []
}

ClassZone.prototype.AffHist = function() {
	var Z, nz, S=''
	for (nz in this.T) {
	  Z = this.T[nz]
		S+=Z.Nom+cr
	}
	return S
}


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

function MemoAction(O, Action, noPush) {
	var OP, Opt, O, AO, M, Action, ActionT, S, V, T
	//if (LVal('Rech')=='a')  alert(aa)
	if (typeof noPush==='object') { Opt = noPush;  noPush = Opt.noPush }  else  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 (AO.Action=='LitSql')  noPush = -1;  //if (noPush<0)  return

	M = Zones.Memo
	M.Action = Action
	if (O) {
		M.CleClic = ElemPAt(O,'!ValCle');
		M.LibClic = Trim(Isole(LText(O),'\n',1))
		AO = ElemPAt(O,'ZoneAction')
		if (AO) {
			M.ZoneAction = LX(AO,'ZoneAction')
			//alert(Lout(AO))
			T = new Array()
			//if (M.ZoneAction=='DivMatProd')  alert(aa)
			while((AO=ElemPAt(AO,'ZoneAction'))) {
			  S = LX(AO,'ZoneAction')
				V = GetActionZone(S)
				T.push(V);			//alert(JSON(T)+crr+JSON(V)+crr+Lout(AO))
				AO = AO.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, noPush) {
	var ActionT='', T, T2, O, Opt, re, e, Contexte, i, S;		//alert(Action+crr+Param)

	if (typeof noPush==='object') { Opt = noPush;  noPush = Opt.noPush }  else  Opt={}
	if (App.debAction)  App.debAction()
	
	if (typeof(Action)=="string") {
		MemoAction(null, Action, noPush)
		Action = Zones.Memo.Action
	}
  if (Action) {
		ActionT = JSON(Action);     //alert(ActionT)
	  O = Action[0]
		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 = JSON(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(JSON(Action))
	App.Retour = null
	Contexte = Zones.Contexte();
	//Contexte = Replace(Contexte,'&','&amp;');  
	Param += '&Contexte='+escape(Contexte);    //alert(Param)
	Zones.Memo.Action = Action
	if (!App.F2 && Opt.Asynch!==0) {
	  App.OptTrait = Opt
		XmlPost2(Param, '/SIP/iview/appli.php?Action='+ActionT+'&BugH='+Bug+AjUrl, TraitResult)
	}else{
		re = XmlPost(Param, '/SIP/iview/appli.php?Action='+ActionT+'&BugH='+Bug+AjUrl);  //encodeURIComponent
		TraitResult(re, noPush)
		return (re)
	}
    //LID('DivPrim').innerText = re
}

function TraitResult(re, noPush) {
  var T, T2, Opt, O, e, S, JS, i;
	if (typeof noPush==='object') { Opt = noPush;  noPush = Opt.noPush }  else  Opt = App.OptTrait;
	if (!Opt)  Opt={}
	App.OptTrait = null
  var Action = Zones.Memo.Action;   if (!Action)  Action = [{}];		//alert(JSON(Action))
  if (!re) { re = LText('DivPrim');  PText('DivPrim','') }
	if (re.substr(0,1)=='{') {
		JS = Isole(re,'|||',1);  re = re.substr(JS.length);		//alert(JS+crr+re)
		App.Retour = eval('('+JS+')')
		if ((S=App.Retour.Sess))  Zones.Memo.Sess = S
		//Action = Zones.Memo.Action = App.Retour.Acts
	}
	if (Bug)  LID('TitBug').innerHTML = '<b>Action</b>='+JSON(Action)+'<br><b>Contexte=</b>'+Zones.Contexte()+cr+'<br>' + '<b>JS=</b>' + JS
	//if (Bug)  PText('TitBug',S)
	if (re.substr(0,1)!='|') { if (Bug)  LID('DivHBug').innerHTML = re;  return }

	Zones.Memo.ActionZone = O = Action[0]     // ActionZone = dernière action influant sur une zone (LitSql non pris en compte)
	if (O.ValCle)  App.LastActWithCle = Action  // Pour les retours
	//S = Isole(re,'|||',2);			//alert(S+crr+re)
	if (!noPush && (Action && Action[0] && !Action[0].PosJeu)) {
	  S = JSON(LastAction());  e = JSON(Zones.Memo);  //alert(JSON(Action)+crr+S+crr+e);  if (S!=e)  alert('Memo'+crr+JSON(Zones.Memo))
	  if (S!=e)  TAction.push(new ObjClone(Zones.Memo))
	}
	if (Bug==3)  PText('DivBug',re+crr)
  e = TraitRetour(re);  if (!e)  return

	Distrib(re);			//alert(Zones.Aff())
	FinAction()
	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 (App['fin_'+S]) { App['fin_'+S]();  App['fin_'+S] = null }
		  S = O.NomR;  if (App['fin_'+S]) { App['fin_'+S]();  App['fin_'+S] = null }
		}
	}
	if (App.Affich) { App.Affich();  App.Affich = null }
	if (Opt.Suite)  Opt.Suite()
		//alert('ExecAction'+crr+TAction.length+crr+AffAction())
}

function Distrib(H) {
	var TP, T, TD, Z, P, H, Zone, NomZ, O, LiAct, i, S, Chaine, D, Act;
	TP = H.split('|||')
	if (Bug)  PText('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']; }
			//alert(LiAct+crr+NomZ+crr+JSON_Encode(TD)+crr+H)
		if (NomZ=='JS') {
			eval('('+H+')');
			continue
		}
		if (H.substr(0,2)=='||') {
			T = H.substr(2).split('||');
			T[0] = Trim(T[0])
			H = T[1]
		}
		Zone = LID(NomZ);  //if (!Zone) { NomZ = Zones.ZoneDef;  Zone = LID(NomZ); }
		if (Zone) {
			if (App.Retour) {
				if (App.Retour.Zones && (P=App.Retour.Zones[NomZ])) { TD.Cle = P.Cle }
			}
			Z = Zones.Add(NomZ);  Z.Data = TD

			PVal(Zone, H, true);			//alert(NomZ+crr+H)
			//D = document.createElement('div');  D.innerHTML = H;  LID(Zone).appendChild(D)

			Zones.T[NomZ].ReceptH = 1
			Aff(Zone)
			Act = GetActionZone(NomZ);
			if (!LX(Zone,'ZoneAction'))  PX(Zone,'ZoneAction',NomZ)
			if (App['Zone'+NomZ])  App['Zone'+NomZ]()
			//if ((O=LID(NomZ+'_Act')))  TD.Act = eval('('+LText(O)+')')
		}
		if (T && T[0]) {
			eval(T[0])
			//alert(T[0])
		}
		if (Bug && NomZ!='DivHBug') {
		  S = TP[i+1];  if (Bug==1)  S = S.substr(0,1000)
			PText('DivBug',LText('DivBug') + '\n\n'+LiAct+'\n===========\n'+S)
		}
		if (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 (Z=LID('DivScript')) {
		Chaine = Z.innerHTML;  //PVal('DivScript','');
		//alert(Chaine)
		eval(Chaine);
	}
}

function FinAction() {
	var T, H, S
	Hide_Div_Vide()
	if (window['RePosWin'])  RePosWin()
	return
  if (LID('MoveT')) {
	  if (App.Retour.volet2 && (T=App.Retour.volet2.Table)) {
	    if (T.LimitJeu) {
		    var Act = LastAction()
	      var Lim = T.LimitJeu
	      S = JSON(Act)
		    H=''
		    if (T.PosJeu>1)  H+= '<img xObj=JeuPrec Action=[{"Action":"'+Act.Action+'","Action":"","Action":"","Action":""}] src="/biblio/img/fl/flvG.gif" style="cursor:pointer">'
		    if (T.PosJeu+Lim<T.NbTot)  H+= '<img Obj=JeuSuiv src="/biblio/img/fl/flvD.gif" style="cursor:pointer">'
		    LID('MoveT').innerHTML = H
			}
		}
	}
}

function Hide_Div_Vide() {
	var TZ, Zone, Z, ZoneP, T, S, ZA
	if (!App.Retour)  return
	TZ = App.Retour.TZ;
	ZA = Zones.Memo.ZoneAction;		//alert(JSON(TZ))
	for (Zone in TZ) {
		Z = TZ[Zone];  if (!Z)  continue;
		if (Zones.T[Zone] && Zones.T[Zone].ReceptH)  continue;  // si reçu du code
		S = Z.HideVide;  if (!S)  continue;			//alert('Zone='+Zone+crr+'MA='+S+cr+'ZA='+ZA)
		//if (ZA && S!=ZA)  continue;
		if (!ZA || (S!='*' && S!=ZA))  continue;
		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);
			wh("HIDE("+ZoneP+")",'TitBug')
		}
	}
}

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

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

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 Retour(n) {
	var O, M, S, i
	if (!n)  n=2;		//alert(n+','+TAction.length+crr+AffAction())
	if (TAction.length<n)  ExecAction('')
  if (TAction.length<n)  return;
	for (i=0; i<n; i++) {
		O = TAction.pop();    //alert(TAction.length+crr+S)
	}
// 	while (TAction.length) {
// 	  S = O.Action[0].Action;  //alert(S+crr+JSON(O))
// 	  if (S=='Win' || S=='Liste')  break;  else  O = TAction.pop();
// 	}
	//alert(JSON(O)+crr+AffAction())
	if (!O)  return
	M = Zones.Memo;  M.CleClic = O.CleClic;  M.LibClic = O.LibClic;  M.ZoneAction = O.ZoneAction
  //alert(JSON(O.Action))
	ExecAction(O.Action)
}

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 (Bug && Msg2)  PVal('DivOther',Msg2,1);
	if (Msg) { PVal(Zones.ZoneDef,Msg,1);  return false }  else  return true;
}

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

// ====================================      DIVERS       ===============================

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
}

