// Funzioni referenziate
this.richiediLogin = richiediLogin;
this.ctrlPost = ctrlPost;
this.post = post;
this.setAnnullo = setAnnullo;
this.impostaResp = impostaResp;
this.cercaResp = cercaResp;
this.chiamaFunzione = chiamaFunzione;
this.richiamaFunzione = richiamaFunzione;
this.postato = postato;
this.annullo = annullo;
this.act = act;
this.res = res;
this.par = par;
this.timeID = timeID;

var postato = true;
var annullo = false;
var act;
var res;
var par;
var timeID;

function chiamaFunzione (action, resp, param)
{
 if (!postato) {
  if (navigator.appName != "Netscape") {
   return false;
  }
  return;
 }
 if (parent.frames[2]&&
     parent.frames[2].getPost &&
     parent.frames[2].getPost() == false) {
  alert ("Chiudere la finestra secondaria");
  if (navigator.appName != "Netscape") {
   return false;
  }
  return;
 }
 act = action;
 res = new Array ();
 par = new Array ();
 var i = 0;
 while (i < resp.length) {
  par [par.length] = param [i];
  res [res.length] = resp [i++];
 }
 var parametri = cercaResp (resp, param);
 if (parametri == "nonTrovato") {
  richiediLogin ();
  return;
 }
 timeID=setInterval ("ctrlPost()",1);
}

function richiediLogin () {
 postato = false;
 loginWnd=window.open("login.jsp","AreaRiservata","toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=0,status=0,width=300,height=200,left="+((screen.width/2)-150)+",top="+((screen.height/2)-100));
 loginWnd.focus();
 timeID=setInterval ("ctrlPost()",100);
}

function ctrlPost () {
 if (!postato) {
	return;
 }
 clearInterval(timeID);
 parametri = cercaResp (res, par);
 if (parametri == "nonTrovato") {
  if (annullo) {
   annullo=false;
   return;
  }
  alert ("Utente non abilitato alla funzione");
  return;
 }
 return richiamaFunzione (parametri);
}

function richiamaFunzione (parametri)
{
 if (act.indexOf ("dispatcher") >= 0)
 {
  if (parametri != "") parametri += "&";
  parametri +=  "browser="+navigator.appName+
                "&browserLevel="+navigator.appVersion+
                "&applId="+applId+
                "&sessionId="+sessionId;
  if (isHomePage) {
   parametri += "&attiv=daindex";
  }
  parametri='parametriAttivazione='+CodificaHex(parametri)+
                "&ricmenu=si";
  if ( existsObject ('userlog')  )
  {
     parametri+="&userlog="+userlog.user;
  }
//  +'"';
  if (act.indexOf ("?") < 0) {
   act += "?" + parametri;
  }
  else {
   act += "&" + parametri;
  }
 }

 if (act.indexOf("window=") == 0)
 {
     act = act.substring(7);
     var virgola = act.indexOf(",");
     var finestra = act.substring(0, virgola);
     act = act.substring (virgola + 1);
     openFinestra(act,finestra,0);
 }
 else
 {
     if (isHomePage)
     {
       if (act.indexOf("makeArea") >= 0) 
       {
        apriFinestraEngine("framesWI.jsp?sessionId="+sessionId
             +"&action="+CodificaHex(act)
                          , "MainTecnet", 0);         
       } else {
        apriFinestraEngine("frames.jsp?sessionId="+sessionId
                                      +"&action="+CodificaHex(act)
                          , "MainTecnet", 0);
       }
     }
     else
     {
        if (act.indexOf ("dispatcher") >= 0)
        {
           top.mainframe.location.href=act;
        }
     }
 }
 if (navigator.appName != "Netscape")
 {
    return false;
 }
}

function existsObject ( name)
{
   for (var i=0;i<document.applets.length;i++ )
   {
       if ( document.applets[i].id == name ) return true;
   }
   return false;
}


function impostaResp (resp) {
 var s = new String (resp.valueOf());
 respCorr = new Array();
 var i;
 while ((i = s.indexOf (",")) >= 0) {
  respCorr [respCorr.length] = s.substring (0, i);
  s = s.substring (i + 1);
 }
 respCorr [respCorr.length] = s;
 post();
}


function post () {
 postato = true;
}

function setAnnullo () {
 annullo = true;
}

function cercaResp (resp, param)
{
 var parx = "";
 var trovato = 0;
 var i = 0;
 while (i < resp.length) {
  var j = 0;
  while (j < respCorr.length) {
   if (resp [i] == respCorr [j]) {
    trovato = 1;
    parx = param [i] == "" ? parx
                           : parx == "" ? param [i]
                                        : parx < param [i] ? param [i]
                                                           : parx;
   }
   ++j;
  }
  ++i;
 }
 if (trovato == 0) {
  return "nonTrovato";
 }
 return parx;
}
