/* Utility.js
 * Prima creazione:  9/12/2004
 *
 * 4 sezioni:
 * - elenchi editabili (prefisso ee..)
 * - master/detail     (prefisso md..)
 * - lookup
 * - utility varie
 */

/*******************/
/* ELENCHI (NUOVI) */
/*******************/

function rigaOnClick(nome, inRigaCorrente) {
//  var nomeForm = "form" + nome;
  var nomeRigaCorrente = "rigaCorrente" + nome;
  var nomeTabella = "tabella" + nome;

  var tabella = document.getElementById(nomeTabella);
  tabella.rows[document.getElementById(nomeRigaCorrente).value*1.0 + 1].className = "nNonEvidenziato";

  document.getElementById(nomeRigaCorrente).value = inRigaCorrente;
  tabella.rows[inRigaCorrente*1.0+1].className = "nEvidenziato";
}

function postOnClick(nome,inUrl /* , inConferma, inCheck */) {
  var nomeForm = "form" + nome;

/*
  if (inConferma != null) {
   if (! confirm(inConferma)) return null;
  }

  if (inCheck != null) {
   if (!inCheck()) return
  }
*/

  document.getElementById(nomeForm).action = inUrl;
  document.getElementById(nomeForm).submit();
}

/*********************/
/* ELENCHI EDITABILI */
/*********************/

 function eeRigaOnClick(inRigaCorrente) {
  var tabella;

  tabella = document.getElementById("eeTable");
  tabella.rows[document.eeForm.rigaCorrente.value*1.0 + 1].className = "nNonEvidenziato";

  document.eeForm.rigaCorrente.value = inRigaCorrente;

  tabella.rows[inRigaCorrente*1.0+1].className = "nEvidenziato";
 }

 function eeSubmitOnClick(url, messaggio) {
  if (messaggio == null) messaggio = "";

  if (messaggio != "") {
   if (confirm(messaggio)) {
    document.eeForm.action = url;
    document.eeForm.submit();
   }
  } else {
   document.eeForm.action = url;
   document.eeForm.submit();
  }
 }

/***********************/
/* MASTER/DETAIL UTILS */
/***********************/

 function mdRigaOnClick(inRigaCorrente) {
  var tabella;

  tabella = document.getElementById("mdElenco");
  tabella.rows[document.mdForm.rigaCorrente.value*1.0 + 1].className = "nNonEvidenziato";

  document.mdForm.rigaCorrente.value = inRigaCorrente;
  tabella.rows[inRigaCorrente*1.0+1].className = "nEvidenziato";
 }

 function mdPostOnClick(inUrl, inConferma, inCheck) {

  if (inConferma != null) {
   if (! confirm(inConferma)) return null;
  }

  if (inCheck != null) {
   if (!inCheck()) return
  }

  document.mdForm.action = inUrl;
  document.mdForm.submit();
 }


 function mdGetOnClick(inUrl, inConferma, inCheck) {
  //alert ("mdGetOnClick: " + inUrl);
  if (inConferma != null) {
   if (! confirm(inConferma)) return null;
  }

  if (inCheck != null) {
   inCheck();
  }

  document.location.href = inUrl;
 }

function mdOpenOnClick(inUrl, inTitolo, inW, inH) {
 var w = (inW == null ? 600 : inW);
 var h = (inH == null ? 500 : inH);
 var winl = (screen.width - w) / 2;
 var wint = (screen.height - h) / 2;

 w = window.open(inUrl, inTitolo, "width=" + w + ", height=" + h + ", top=" + wint + ",left=" + winl + "location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=yes, modal=Yes");
 if (document.all) w.focus();
}

 function mdPostOnClick1(inUrl, inConferma, inCheck) {

  
  if (inConferma != null) {
   if (! confirm(inConferma)) return null;
  }

  if (inCheck != null) {
   if (!inCheck()) return
  }
  document.elenco.action = inUrl;
  document.elenco.submit();
 }
/****************/
/* LOOKUP UTILS */
/****************/

// Apre una lookup. Parametri: NomeLookup, CampoPagina, CampoLookup, sbianca, CampoPagina, CampoLookup, , sbianca, ...
function openLookup () {
 var w = 600;
 var h = 500;
 var winl = (screen.width - w) / 2;
 var wint = (screen.height - h) / 2;

 var i, j;
 var url = "../servlet/AdapterHTTP?page=" + arguments[0] + "&modalita=Cerca";

 for (i=1; i<arguments.length; i+=3) {
  //alert(arguments[i]);

  if (arguments[i+2].toLowerCase() == "prima") {
   document.getElementById (arguments[i]).value = "";
   document.getElementById (arguments[i]).selectedIndex = 0;
  }

  if (document.getElementsByName (arguments[i]).length > 1) {
   // radio
   url += "&fl" + arguments[i+1] + "="
   for (j=0; j<document.getElementsByName (arguments[i]).length; j++) {
    if (document.getElementsByName (arguments[i])[j].checked == true) {
     url += document.getElementsByName (arguments[i])[j].value;
    }
   }

   url += "&nome" + arguments[i+1] + "=" + arguments[i];
  } else if (document.getElementById (arguments[i]).value != null) {
   // input
   url += "&fl" + arguments[i+1] + "=" + document.getElementById (arguments[i]).value + "&nome" + arguments[i+1] + "=" + arguments[i];
  }

  if (document.getElementsByName (arguments[i]).length == 1)
   if (arguments[i+2].toLowerCase("dopo") == "dopo") {
    document.getElementById (arguments[i]).value = "";
    document.getElementById (arguments[i]).selectedIndex = 0;
   }
 }

  //window.showModalDialog(url, null, "dialogHeight: " + h + "px; dialogWidth: " + w + "px; edge: Raised; center: Yes; help: No; resizable: No; status: No; scroll: No");
 w = window.open(url, "Lookup", "width=" + w + ", height=" + h + ", top=" + wint + ",left=" + winl + "location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=yes, modal=Yes");
 if (document.all) w.focus();
}

// Permette di popolare i campi della form che ha invocato la lookup
function lookupOnClick() {
  var riga = arguments[0];

  for (var i=1; i < arguments.length; i++) {
    var nomeControllo = (document.getElementById("nome" + arguments[i])).value;

    if (nomeControllo != "" && (p = window.opener.document.getElementById (nomeControllo))) {
      p.value = delBlank(riga.cells[i].innerHTML);
      //alert("arg. " + i + ": " + nomeControllo + "=" + p.value);
    }
  }
}

/********************/
/* RELOAD ON CHANGE */
/********************/
function codFieldOnChange(navString, obj){
  var objForm = document.getElementById("elenco");
  //alert(objForm.action);
  //objForm.action = '../servlet/AdapterHTTP?action_name=<%=Actions.INSERIMENTO_EROGAZIONI_ACTION%>&<%=ReloadOnFieldChangeNavigator.getNavString()%>';
  objForm.action = objForm.action + '&' + navString + obj.name;
  objForm.submit();
}


/***************/
/* UTILS VARIE */
/***************/

// Pulisce i campi passati
function sbianca () {
 for (i=0; i<arguments.length; i++) {
  document.getElementById (arguments[i]).value = "";
  document.getElementById (arguments[i]).selectedIndex = 0;
 }
}

function delBlank (inStringa) {
 for (;inStringa.toUpperCase().indexOf("&NBSP;") == 0;) inStringa = inStringa.substr(6);

 return inStringa;
}

/* Abilita/disabilita i campi passati in base al flag passato come primo argomento
   N.B.: la disabilitazione di una text o di una select comporta anche il suo
   sbiancamento
 */
function abilita() {
  if (arguments.length < 2) return; // chiamata male
  flag = arguments[0];
  for (i=1; i<arguments.length; i++) {
    obj = document.getElementById (arguments[i]);
    if (obj.type != 'hidden')
      obj.disabled = !flag;
    if (!flag && obj.type != 'button') {
      obj.value = "";
      obj.selectedIndex = 0;
    }
  }
}

// Avvia una stampapdf da processo
function stampaHourGlass (url) {
 var w = 600;
 var h = 400;
 var winl = (screen.width - w) / 2;
 var wint = (screen.height - h) / 2; 
 
 re = /[&]/g
 url = url.replace (re, "%26");
 
 w = window.open("../servlet/AdapterHTTP?action_name=hourGlassAction&url=" + url, "Stampa", "width=" + w + ", height=" + h + ", top=" + wint + ",left=" + winl + "location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=yes, modal=Yes");
 
 try {
  w.focus();
 } catch (e) {
 } 
}


function hide() {
 document.getElementById ("colonnaLeft").style.display = "none";
 document.getElementById ("colonnaLeft2").style.display = "";
 document.getElementById ("colonnaPrincipale").style.width = "95%";
 document.getElementById ("menuVisibile").value = "N";
}

function show() {
 document.getElementById ("colonnaLeft").style.display = "";
 document.getElementById ("colonnaLeft2").style.display = "none";
 document.getElementById ("colonnaPrincipale").style.width = "76%";   
 document.getElementById ("menuVisibile").value = "S";
}

function initMenu () {
 if (document.getElementById("showButton"))
  document.getElementById("showButton").style.display = "";
  
 if (document.getElementById ("menuVisibile")) {
  if (document.getElementById ("menuVisibile").value == "S") {
   show();
  } else {
   hide();
  }
 }
 
}

function initForLookupPage () {
	 document.getElementById ("colonnaPrincipale").style.width = "95%";
 } 
 


