/*
 * JAVASCRIPT DEL LAYOUT
 *
 * author: Fabio Guagnelli, Capgemini Italia s.p.a,
 *         fabio.guagnelli@capgemini.com
 * version: 1.0 
 *
 */

// ----------------------------------------------
// FUNZIONE CHE PERMETTE LA STAMPA
// ----------------------------------------------
function PopupStampa(urlStampa) {
  if (urlStampa.indexOf("?") > -1 ) {
    urlStampa += "&";
  } else {
    urlStampa += "?";
  }
  window.open(urlStampa + "isStampa=true", "", "top=10, left=10, width=800, height=600, status=no, menubar=yes, toolbar=no, scrollbars=yes, resizable=yes");
}

// ====================================================================================

// ----------------------------------------------
// FUNZIONE CHE SOTTOMETTE LA FORM DI RICERCA SU ONSUBMIT
// ----------------------------------------------
function submitSearchForm()
{
  if(checkSearchForm())
    document.CDP_FORM_CERCA.submit();
}

function checkSearchForm()
{
  //ctrl = document.CDP_FORM_CERCA.dataRel;
  //if (ctrl.value!="")
  //{
  //  if (!isDate(ctrl))
  //  {
  //    alert("La data del documento [Dal] deve essere\nnella forma gg/mm/aaaa (giorno/mese/anno)");
  //    ctrl.select();
  //    ctrl.focus();
  //    return;
  //  }
  //}
  //ctrl = document.CDP_FORM_CERCA.dataRel2;
  //if (ctrl.value!="")
  //{
  //  if (!isDate(ctrl))
  //  {
  //    alert("La data del documento [Al] deve essere\nnella forma gg/mm/aaaa (giorno/mese/anno)");
  //    ctrl.select();
  //    ctrl.focus();
  //    return;
  //  }
  //}
  
  ctrl = document.CDP_FORM_CERCA.ssUserText;
  
  /*if (ctrl.value.length<=2)
  {
    alert("Inserire una parola da ricercare di almeno tre caratteri.");
    ctrl.select();
    ctrl.focus();
    return;
  }*/

  
  var goOn = true;
  
  re = /^\s+|\s+$/g;
  var userText = ctrl.value.replace(re, '');
  /*
  if (ctrl.value=="   ")
    return true;
  */
  
  // FUNZIONA
  //re = /^([a-z]+|[A-Z]+|\d+|\s+|à+|è+|ì+|ò+|ù+)*$/;
  re = /^(\w+|\s+|à+|è+|ì+|ò+|ù+|\')*$/;
  //if(!re.test(userText)){

  //re = /.*(\<+|\>+|\/+|\'+|\-+|\\).*/;

  if(!re.test(userText)){
    goOn = false;
    alert('Inserire parole con lettere e/o numeri');
    ctrl.select();
    ctrl.focus();
  }
   
  /*
  re = /^([^\s]{3,}\s*)+$/;
  if(userText.match(re)==null) {
    goOn = false;
    alert('Inserire parole di almeno tre caratteri.');
    ctrl.select();
    ctrl.focus();
  }
  */
  
  if(userText=="")
  {
    goOn = false;
    alert('Inserire una parola da ricercare.');
    ctrl.select();
    ctrl.focus();  
  }
  
  
  return goOn;
}

// ====================================================================================

// ----------------------------------------------------
// FUNZIONE CHE SOTTOMETTE LA FORM DI RICERCA NORMATIVA SU ONSUBMIT
// modificata 04-09-2008
// ----------------------------------------------------
function submitSearchNormativaForm()
{
  if(checkSearchNormativaForm())
  {
    // CONTROLLO FORMATA DATA
    ctrlData = document.CDP_FORM_CERCA_NORMATIVA.dataDoc;
    if (ctrlData.value!="")
    { 
      if (!isDate(ctrlData))
      {
        alert("La data deve essere nel formato gg/mm/aaaa");
        ctrlData.select();
        ctrlData.focus();
        return;
      }
    }
    
    document.CDP_FORM_CERCA_NORMATIVA.tn.value=document.CDP_FORM_CERCA_NORMATIVA.tipoNorm.value;
    document.CDP_FORM_CERCA_NORMATIVA.submit();
  }
}

function checkSearchNormativaForm()
{
  ctrl = document.CDP_FORM_CERCA_NORMATIVA.ssUserText;
  var goOn = true;
  
  re = /^\s+|\s+$/g;
  var userText = ctrl.value.replace(re, '');
  
  /* CAMPO NON OBBLIGATORIO
  if (ctrl.value=="   ")
    return true;
    
  re = /^([^\s]{3,}\s*)+$/;
  if(userText.match(re)==null) {
    goOn = false;
    alert('Inserire parole di almeno tre caratteri.');
    ctrl.select();
    ctrl.focus();
  }
  */
  
    // FUNZIONA
  //re = /^([a-z]+|[A-Z]+|\d+|\s+|à+|è+|ì+|ò+|ù+)*$/;
  re = /^(\w+|\s+|à+|è+|ì+|ò+|ù+|\')*$/;
  //if(!re.test(userText)){

  //re = /.*(\<+|\>+|\/+|\'+|\-+|\\).*/;

  if(!re.test(userText)){
    goOn = false;
    alert('Inserire parole con lettere e/o numeri');
    ctrl.select();
    ctrl.focus();
  }
  
  document.CDP_FORM_CERCA_NORMATIVA.tn.value=document.CDP_FORM_CERCA_NORMATIVA.tipoNorm.value;
  
  // CONTROLLO FORMATA DATA
  /*ctrlData = document.CDP_FORM_CERCA_NORMATIVA.dataDoc;
    if (ctrlData.value!="")
    { 
      if (!isDate(ctrlData))
      {
        alert("La data deve essere nel formato gg/mm/aaaa");
        ctrlData.select();
        ctrlData.focus();
        goOn=false;
      }
    }
  */
  
   // CONTROLLO FORMATA DATA
   ctrlDataMese = document.CDP_FORM_CERCA_NORMATIVA.dataMeseDoc;
   ctrlDataAnno = document.CDP_FORM_CERCA_NORMATIVA.dataAnnoDoc;  
   if (ctrlDataMese.value!="00" && ctrlDataAnno.value=="")
   {    
        goOn=false;
        alert("Selezionare mese e anno");
    }


    
   if(isNaN(ctrlDataAnno.value)){
     
     alert("Inserire un valore numerico");
        ctrlDataAnno.value=""; 
        ctrlDataAnno.select();
        ctrlDataAnno.focus();
        goOn=false;
    }
    else  if(trim(ctrlDataAnno.value)!="" && trim(ctrlDataAnno.value).length<4){
    alert("Inserire un numero di 4 cifre"); 
        ctrlDataAnno.select();
        ctrlDataAnno.focus();
        goOn=false;
   }
   

  return goOn;
}

// ====================================================================================

// ----------------------------------------------------
// FUNZIONE CHE SOTTOMETTE LA FORM DI RICERCA MODULI SU ONSUBMIT
// modificata 04-09-2008
// ----------------------------------------------------
/*function submitSearchModuliForm()
{
  if(checkSearchModuliForm())
  {
    // CONTROLLO FORMATA DATA
    ctrlData = document.CDP_FORM_CERCA_MODULI.dataDoc;
    if (ctrlData.value!="")
    { 
      if (!isDate(ctrlData))
      {
        alert("La data deve essere nel formato gg/mm/aaaa");
        ctrlData.select();
        ctrlData.focus();
        return;
      }
    }
    
    document.CDP_FORM_CERCA_MODULI.td.value=document.CDP_FORM_CERCA_MODULI.tipoDoc.value;
    document.CDP_FORM_CERCA_MODULI.submit();
  }
}
*/

function checkSearchModuliForm()
{
  ctrl = document.CDP_FORM_CERCA_MODULI.ssUserText;
  var goOn = true;
  
  re = /^\s+|\s+$/g;
  var userText = ctrl.value.replace(re, '');
  
  /* CAMPO NON OBBLIGATORIO
  if (ctrl.value=="   ")
    return true;
    
  re = /^([^\s]{3,}\s*)+$/;
  if(userText.match(re)==null) {
    goOn = false;
    alert('Inserire parole di almeno tre caratteri.');
    ctrl.select();
    ctrl.focus();
  }
  */
  
    // FUNZIONA
  //re = /^([a-z]+|[A-Z]+|\d+|\s+|à+|è+|ì+|ò+|ù+)*$/;
  re = /^(\w+|\s+|à+|è+|ì+|ò+|ù+|\')*$/;
  //if(!re.test(userText)){

  //re = /.*(\<+|\>+|\/+|\'+|\-+|\\).*/;

  if(!re.test(userText)){
    goOn = false;
    alert('Inserire parole con lettere e/o numeri');
    ctrl.select();
    ctrl.focus();
  }
  document.CDP_FORM_CERCA_MODULI.td.value=document.CDP_FORM_CERCA_MODULI.tipoDoc.value;
  
  // CONTROLLO FORMATA DATA
 /* ctrlData = document.CDP_FORM_CERCA_MODULI.dataDoc;
    if (ctrlData.value!="")
      { 
        if (!isDate(ctrlData))
        {
          alert("La data deve essere nel formato gg/mm/aaaa");
          ctrlData.select();
          ctrlData.focus();
          goOn=false;
        }
    }
  */
  return goOn;
}

function checkTipoMod()
{
  if (document.CDP_FORM_CERCA_MODULI.tipoDoc.value=='mod')
  {
    document.CDP_FORM_CERCA_MODULI.tipoMod.disabled=false;
  }
  else
  {
    document.CDP_FORM_CERCA_MODULI.tipoMod.selectedIndex=0;
    document.CDP_FORM_CERCA_MODULI.tipoMod.disabled=true;
  }
}

// ====================================================================================

// ----------------------------------------------
// FUNZIONE CHE MODIFICA LA DIMENSIONE DEL TESTO
// ----------------------------------------------
function changeTextSize(dimText)
{
  document.formChangeTextSize.textSize.value=dimText;
  document.formChangeTextSize.submit();
}

// ====================================================================================

// ----------------------------------------------
// FUNZIONI PER MENU
// ----------------------------------------------
function cm(suffix)
{ 
  var voce;
  voce=document.getElementById("v_"+suffix);

  if(voce.style.display=="")
  {
    voce.style.display="block";
  }

  var m;
  m=document.getElementById("ml_"+suffix);

  if (m)
  {
    if(m.style.display=="none")
    {
      m.style.display="block";
      var l;
      l=document.getElementById("l_"+suffix);

      if(l)
      {
        l.className="link_selected"
      }

      // devo chiudere tutti i menu di questo livello ad eccezione di questo.
      close_all(suffix);
    }
    else
    {
      m.style.display="none";
    }
  }
  else
  {
    continua=false;
  }
}

function close_all(suffix)
{
  var base_livello;
  base_livello="";
  var arr_suffix = new Array();
  arr_suffix = suffix.split("_");

  for (j=0;j<arr_suffix.length-1;j++)
  {
    base_livello=base_livello+arr_suffix[j]+"_"
  };

  var continua;
  continua=true;
  var i;
  i=0;

  do
  {
    i++;
    if(base_livello+i!=suffix)
    {
      var l2;
      l2=document.getElementById("l_"+base_livello+i);
      var m2;
      m2=document.getElementById("ml_"+base_livello+i);

      if(l2)
      {
        if(m2)
        {
          ciclo_chiusura(base_livello+i);
          m2.style.display="none";
          var l;
          l=document.getElementById("l_"+base_livello+i);

          if(l)
          {
            l.className="";
          }
        }
      }
      else
      {
        continua=false;
      }
    }
  }
  while(continua==true);
}

function ciclo_chiusura(suffix)
{
  var continua;
  continua=true;
  var i;
  i=0;

  do
  {
    i++;
    var l2;
    l2=document.getElementById("l_"+suffix+"_"+i);
    var m2;
    m2=document.getElementById("ml_"+suffix+"_"+i);

    if(l2)
    {
      if(m2)
      {
        ciclo_chiusura(suffix+"_"+i);
        m2.style.display="none";
        var l;
        l=document.getElementById("l_"+suffix+"_"+i);

        if(l)
        {
          l.className="";
        }
      }
    }
    else
    {
      continua=false;
    }
  }
  while(continua==true);
}

function apri_menu(cat1, cat2, cat3, cat4, cat5)
{
  var c1;var c2;var c3;var c4;var c5;
  c1=document.getElementById("ml_"+cat1);
  c2=document.getElementById("ml_"+cat1+"_"+cat2);
  c3=document.getElementById("ml_"+cat1+"_"+cat2+"_"+cat3);
  c4=document.getElementById("ml_"+cat1+"_"+cat2+"_"+cat3+"_"+cat4);
  c5=document.getElementById("ml_"+cat1+"_"+cat2+"_"+cat3+"_"+cat4+"_"+cat5);
  var l1;var l2;var l3;var l4;var l5;
  l1=document.getElementById("l_"+cat1);
  l2=document.getElementById("l_"+cat1+"_"+cat2);
  l3=document.getElementById("l_"+cat1+"_"+cat2+"_"+cat3);
  l4=document.getElementById("l_"+cat1+"_"+cat2+"_"+cat3+"_"+cat4);
  l5=document.getElementById("l_"+cat1+"_"+cat2+"_"+cat3+"_"+cat4+"_"+cat5);
  if(c1) c1.style.display="block";
  if(c2) c2.style.display="block";
  if(c3) c3.style.display="block";
  if(c4) c4.style.display="block";
  if(c5) c5.style.display="block";
  if(l1) l1.className="link_selected";
  if(l2) l2.className="link_selected";
  if(l3) l3.className="link_selected";
  if(l4) l4.className="link_selected";
  if(l5) l5.className="link_selected";
  
  // EVENTUALE APERTURA **** AREA UTENTI ****
  if (idAreaUtenti != '')
  {
    if (idAreaUtenti!=cat1)
    {
      cm(idAreaUtenti);
    }
    
    //alert("id area utenti:"+idAreaUtenti+"\ncat1:"+cat1);
  }
}

function renderMenu(indice,livPrec,contaLiv_1,contaLiv_2,contaLiv_3,contaLiv_4,contaLiv_5)
{ 
  if (indice<nomiPagina.length)
  {
    // ESCLUDO IL LIVELLO 1 (ROOT)
    //
    if (livelliPagina[indice]>1)
    {
      // CALCOLO LIVELLO ATTUALE E PRECEDENTE
      //
      livCorr=livelliPagina[indice];
      diff=livPrec-livCorr;
      lastLiv=livCorr;

      // PASSAGGIO AD UN LIVELLO PIU ALTO
      // AZZERO I CONTATORI
      //
      if (livCorr>livPrec)
      {
        // AGGIORNAMENTO CONTATORI
        //
        if (livelliPagina[indice]==2) contaLiv_1=1;
        if (livelliPagina[indice]==3) contaLiv_2=1;
        if (livelliPagina[indice]==4) contaLiv_3=1;
        if (livelliPagina[indice]==5) contaLiv_4=1;
        if (livelliPagina[indice]==6) contaLiv_5=1;
      }
      else
      {
        // AGGIORNAMENTO CONTATORI
        //
        if (livelliPagina[indice]==2) contaLiv_1=contaLiv_1+1;
        if (livelliPagina[indice]==3) contaLiv_2=contaLiv_2+1;
        if (livelliPagina[indice]==4) contaLiv_3=contaLiv_3+1;
        if (livelliPagina[indice]==5) contaLiv_4=contaLiv_4+1;
        if (livelliPagina[indice]==6) contaLiv_5=contaLiv_5+1;
      }

      // PASSAGGIO AD UN LIVELLO PIU ALTO
      // CHIUDO IL TAG LI ed UI PRECEDENTI
      //
      if (diff>=0)
      {
        if (diff==0) document.write('</LI>');
        if (diff==1) document.write('</LI></UL></LI>');
        if (diff==2) document.write('</LI></UL></LI></UL></LI>');
        if (diff==3) document.write('</LI></UL></LI></UL></LI></UL></LI>');
        if (diff==4) document.write('</LI></UL></LI></UL></LI></UL></LI></UL></LI>');
        if (diff==4) document.write('</LI></UL></LI></UL></LI></UL></LI></UL></LI></UL></LI>');
      }
      
      // COSTRUZIONE ID UL
      //
      if (livCorr==2) idUL="";
      if (livCorr==3) idUL=""+contaLiv_1;
      if (livCorr==4) idUL=""+contaLiv_1+"_"+contaLiv_2;
      if (livCorr==5) idUL=""+contaLiv_1+"_"+contaLiv_2+"_"+contaLiv_3;
      if (livCorr==6) idUL=""+contaLiv_1+"_"+contaLiv_2+"_"+contaLiv_3+"_"+contaLiv_4;
      
      var livTemp = parseInt(livPrec,10)+1;
      
      // APERTURA TAG UL
      //
      if ( livCorr==livTemp && parseInt(livPrec,10)!=1 )
      {
        document.write('<UL class="menu" id="ml_'+idUL+'" style="DISPLAY: none">');
      }
      
      // COSTRUZIONE ID LI
      //
      if (livCorr==2) idLI=""+contaLiv_1;
      if (livCorr==3) idLI=""+contaLiv_1+"_"+contaLiv_2;
      if (livCorr==4) idLI=""+contaLiv_1+"_"+contaLiv_2+"_"+contaLiv_3;
      if (livCorr==5) idLI=""+contaLiv_1+"_"+contaLiv_2+"_"+contaLiv_3+"_"+contaLiv_4;
      if (livCorr==6) idLI=""+contaLiv_1+"_"+contaLiv_2+"_"+contaLiv_3+"_"+contaLiv_4+"_"+contaLiv_5;
      
      // CALCOLO DISTANZA IN BASE AL LIVELLO
      //
      dist = "0";
      if (livCorr==2) dist="2";
      if (livCorr==3) dist="12";
      if (livCorr==4) dist="22";
      if (livCorr==5) dist="32";
      if (livCorr==6) dist="42";
      
      // DETERMINAZIONE SE PAGINA CORRENTE
      //      
      if (selPagina[indice]=='1')
      {
        selLiv_1=contaLiv_1;
        selLiv_2=contaLiv_2;
        selLiv_3=contaLiv_3;
        selLiv_4=contaLiv_4;
        selLiv_5=contaLiv_5;
        
        if (livelliPagina[indice]==2)
        {
          selLiv_2=0;
          selLiv_3=0;
          selLiv_4=0;
          selLiv_5=0;
        }        
        if (livelliPagina[indice]==3)
        {
          selLiv_3=0;
          selLiv_4=0;
          selLiv_5=0;
        }   
        if (livelliPagina[indice]==4)
        {
          selLiv_4=0;
          selLiv_5=0;
        }
        if (livelliPagina[indice]==5)
        {
          selLiv_5=0;
        }
      }
      
      var nomeImg = "cdp_spacer.gif";
      var classImg = "";
      var widthImg = "0";
      
      // INDIVIDUAZIONE STILE LINK MENU' E SFONDO
      //
      var styleLink = "CDP_LinkMenuLiv3";
      var nomeImgRiga = "cdp_menubgliv3.jpg";
      
      if (livelliPagina[indice]==2) // Livello 2
      {
        styleLink = "CDP_LinkMenuLiv1";
        nomeImgRiga = "cdp_menubgliv1.jpg";
      } 
      
      if (livelliPagina[indice]==3) // Livello 3
      {
        styleLink = "CDP_LinkMenuLiv2";
        nomeImgRiga = "cdp_menubgliv2.jpg";
      }
      
      // DETERMINAZIONE SE METTERE IL BULLET
      //
      if (livelliPagina[indice]==3)
      {
        widthImg = "8";
        
        if (haveChildrens[indice]==true)
        {
          nomeImg = "cdp_menubullet_giu.gif";
          classImg = "image";
        }
      }
       
      if (livelliPagina[indice]==4)
      {
        widthImg = "8";
        
        if (haveChildrens[indice]==true)
        {
          nomeImg = "cdp_menubullet_giu.gif";
          classImg = "image";
        }
      }
      
      
      // IMMAGINE SEPARATORE VOCI E DETERMINAZIONE SE METTERLO
      //
      var nomeImgSeparatore = "cdp_menuseparatore.jpg";
      var mettiSeparatore = false;
      
      if (livelliPagina[indice]<=3)
        mettiSeparatore = true;      
      if (livelliPagina[indice]==4)
        if ( (indice+1)<nomiPagina.length )
          if (livelliPagina[indice+1]<livCorr )
            mettiSeparatore = true;
      
           
      // VERIFICO SE ESISTE **** AREA UTENTI ****
      //if (nomiPagina[indice]=='Area Utenti') 
      if (nomiPagina[indice].toLowerCase()=='aree utenti') 
      {      
        idAreaUtenti = idLI;
      }
      
      
      // APERTURA TAG LI
      //
      document.write('<LI id="v_'+idLI+'" style="display: inline;">');       
      document.write('  <TABLE width="100%" cellspacing="0" cellpadding="0" border="0">');
      document.write('    <TR>');      
      document.write('      <TD align="left" valign="middle" style="background: url(\''+imagesPath+'/'+nomeImgRiga+'\');" >');            
      document.write('        <TABLE width="100%" cellspacing="0" cellpadding="0" border="0">');
      document.write('          <TR>');
      document.write('            <TD align="left" valign="middle" width="2" height="18" nowrap><IMG alt="" src="'+imagesPath+'/cdp_spacer.gif" width="2" height="1" border="0"></TD>');            
      
      if (livelliPagina[indice]==4) {
      document.write('            <TD align="left" valign="top" nowrap>&nbsp;&nbsp;</TD>');
      } 
      document.write('            <TD align="left" valign="top">');
      document.write('              <IMG onclick="cm(\''+idLI+'\')" alt="" src="'+imagesPath+'/'+nomeImg+'" width="'+widthImg+'" height="18" border="0" class="'+classImg+'"></TD>');      
               
      if (livelliPagina[indice]>=5) {
      document.write('            <TD align="left" valign="top" nowrap>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>');
      } 
      
      document.write('            <TD width="100%" align="left" valign="middle">');
      
      if (livelliPagina[indice]>=5) {
      document.write('              <A id="l'+idLI+'" class="'+styleLink+'" href="'+urlPagina[indice]+'"><i> '+nomiPagina[indice]+'</i></A></TD>');
      } else {
      document.write('              <A id="l'+idLI+'" class="'+styleLink+'" href="'+urlPagina[indice]+'"> '+nomiPagina[indice]+'</A></TD>');
      }
      
      document.write('          </TR>');
      document.write('        </TABLE>');
      document.write('      </TD>');
      document.write('    </TR>');
      
      if (mettiSeparatore==true) {
      document.write('    <TR>');
      document.write('      <TD align="left" height="2" valign="middle" style="background: url(\''+imagesPath+'/'+nomeImgSeparatore+'\');" >');
      document.write('        <IMG src="'+imagesPath+'/cdp_spacer.gif" width="191" height="2" border="0"></TD>');
      document.write('    </TR>');
      } 
      
      if (mettiSeparatore==false) {
      document.write('    <TR>');
      document.write('      <TD align="left" height="1" valign="middle" style="background: url(\''+imagesPath+'/cdp_menuseparatore3.jpg\');" >');
      document.write('        <IMG src="'+imagesPath+'/cdp_spacer.gif" width="191" height="1" border="0"></TD>');
      document.write('    </TR>');
      }       
      
      document.write('  </TABLE>');
    }
    
    //document.write('        <IMG src="'+imagesPath+'/'+nomeImgSeparatore+'" width="191" height="2" border="0"></TD>');
    
    // CHIAMATA RICORSIVA
    //
    renderMenu(indice+1,livelliPagina[indice],contaLiv_1,contaLiv_2,contaLiv_3,contaLiv_4,contaLiv_5);
  }
  else
  {
    // CONDIZIONE DI USCITA
    return;
  }

  // ULTIME CHIUSURE TAG
  //
  if (indice==nomiPagina.length-1)
  {
    if (lastLiv==2) document.write('</LI>');
    if (lastLiv==3) document.write('</LI></UL></LI>');
    if (lastLiv==4) document.write('</LI></UL></LI></UL></LI>');
    if (lastLiv==5) document.write('</LI></UL></LI></UL></LI></UL></LI>');
    if (lastLiv==6) document.write('</LI></UL></LI></UL></LI></UL></LI></UL></LI>');
  }
}

function callRenderMenu()
{ 
  if (nomiPagina.length>1)
    renderMenu(0, livelliPagina[0], 0, 0, 0, 0, 0);
}  

// VARIABILI PER MENU
//
var selLiv_1=0;
var selLiv_2=0;
var selLiv_3=0;
var selLiv_4=0;
var selLiv_5=0;
var lastLiv = 0;
var idLI = "";
var idUL = "";
var idAreaUtenti = "";

var portalContextPath = '';
var contaPagine = 0;
var nomiPagina = new Array(0);
var urlPagina = new Array(0);
var livelliPagina = new Array(0);
var selPagina = new Array(0);
var haveChildrens = new Array(0);

// ====================================================================================












// --------------------------------------------------------------------------
//  FUNZIONI CHE CONTROLLA LA CORRETTEZZA DI UN CAMPO DATA
// --------------------------------------------------------------------------
function trim(s) 
{
  for(index=0; index<s.length;)
  {
    if(s.charAt(index)==" ")
      s = s.substring(index+1, s.length);
    else
      break;
  }

  for(index=s.length-1; index>=0; index=s.length-1)
  {
    if(s.charAt(index)==" ")
      s = s.substring(0, index);
    else
      break;
  }

  return s;
}


function isDate(ctrl) 
{
  s = ctrl.value;
  msg = '';

  if(trim(s).length!=10) 
  {
    msg = 'invalid ' + ctrl.name;
  }

  if(isNaN(s.substr(6, 4)) && msg=='')
  {
    msg = ctrl.name + '\ninvalid year';
  }

  if(s.substr(5, 1)!='/' && msg=='') 
  {
    msg = ctrl.name + '\ninvalid date separator';
  }
    
  if(isNaN(s.substr(3, 2)) || !isValidMonth(s.substr(3, 2)) && msg=='')
  {
    msg = ctrl.name + '\ninvalid month';
  }

  if(s.substr(2, 1)!='/' && msg=='') 
  {
    msg = ctrl.name + '\ninvalid date separator';
  }

  if(isNaN(s.substr(0, 2)) || !isValidDay(s.substr(0, 2), s.substr(3, 2), s.substr(6, 4)) && msg=='')
  {
    msg = ctrl.name + '\ninvalid day';
  }
  
  return (msg!='')?false:true;
}

function isValidMonth(month) 
{
  mm = parseInt(stripLeadZero(month));
  retVal = true;
  
  if((mm<1) || (mm>12))
  {
    retVal = false;
  }
  
  return retVal;
}

function isValidDay(day, month, year)
{
  dd = parseInt(stripLeadZero(day));
  mm = parseInt(stripLeadZero(month));
  yyyy = parseInt(stripLeadZero(year));

  retVal = false;

  if(mm==4 || mm==6 || mm==9 || mm==11)
  {
    if(dd>0 && dd<31)
      retVal = true;
  }
  else if(mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)
  {
    if(dd>0 && dd<32)
    {
      retVal = true;
    }
  }
  else if(mm==2)
  {
    if(yyyy%4==0)
    {
      if(dd>0 && dd<30)
        retVal = true;
    }
    else
    {
      if(dd>0 && dd<29)
        retVal = true;
    }
  }

  return retVal;
}

function isNotAfter(start, end) 
{
  before = false;
    
  if(start!='' && end!='')
  {
    ddS = parseInt(stripLeadZero(start.substr(0, 2)));
    mmS = parseInt(stripLeadZero(start.substr(3, 2)));
    yyyyS = parseInt(stripLeadZero(start.substr(6, 4)));
    
    ddE = parseInt(stripLeadZero(end.substr(0, 2)));
    mmE = parseInt(stripLeadZero(end.substr(3, 2)));
    yyyyE = parseInt(stripLeadZero(end.substr(6, 4)));
    
    if(yyyyS<yyyyE)
    {
      before = true;
    } 
    else if(yyyyS==yyyyE)
    {
      if(mmS<mmE)
      {
        before = true;
      }
      else if(mmS==mmE) 
      {
        if(ddS<=ddE)
        {
          /* ddS>ddE */
          before = true;
        }
        else
        {
          before = false;
        }
      }
      else
      {
        /* mmS>mmE */
        before = false;
      }
    }
    else
    {
      /* yyyyS<yyyyE */
      before = false;
    }
  
  }
  else 
  {
    before = true;
  }
  
  return before;
}

function stripLeadZero(s)
{
  retVal = s;
  
  if(s.substr(0,1)==0)
    retVal = s.substr(1,1);
  
  return retVal;
}

// ====================================================================================
