// JavaScript Document

<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->

	function JWM_swapImage() {
	  a = JWM_swapImage.arguments;
	  document.JWM_tmp = new Array;
	  j=0;
	  for (i=0;i<(a.length-1);i+=2) {
	    x = document.getElementById(a[i]);
	    document.JWM_tmp[j++] = x;
      if(!x.oSrc) x.oSrc=x.src;
      x.src = a[i+1];
	  }
	}

	function JWM_swapImgRestore() {
    a = document.JWM_tmp;
    for (i=0;a&&i<a.length&&a[i].oSrc;i++)
      a[i].src = a[i].oSrc;
	}


//aggiunge il parametro specificato alla querystring
function addParameter(nome,valore) {
  for (var l=0; l<document.links.length; l++) {
    var abslink = document.links[l].href.indexOf("http://");
    var intlink = document.links[l].href.indexOf("#");
    var jslink = document.links[l].href.indexOf("javascript:");  //forse va trattato!!!!
    var jwmlink = document.links[l].href.indexOf("http://");   //forse è meglio aggiungere il nome dell'applicazione
    var isjsp = (document.links[l].href.indexOf(".jsp")!=-1)?true:false;
    var separatore = (document.links[l].href.indexOf("?")!=-1)?"&":"?";

    if (intlink==-1 && jslink==-1 && jwmlink!=-1 && isjsp)
      document.links[l].href += separatore+nome+"="+valore;
  }
}

//ritorna undefined se il parametro non è definito
function getParameter(nome) {

   var url = window.location.href;
   var paramsStart = url.indexOf("?");
   if (paramsStart != -1) {
      var paramString = url.substr(paramsStart + 1);
      var tokenStart = paramString.indexOf(nome);

      if (tokenStart != -1) {
         paramToEnd = paramString.substr(tokenStart + nome.length + 1);
         var delimiterPos = paramToEnd.indexOf("&");

         if (delimiterPos == -1) {
            return paramToEnd;
         } else {
            return paramToEnd.substr(0, delimiterPos);
         }
      }
   }
}

function getParameters() {
   var params = new Array();
   var url = window.location.href;
   var paramsStart = url.indexOf("?");
   var hasMoreParams = true;

   if (paramsStart != -1) {
     var paramString = url.substr(paramsStart + 1);
     var params = paramString.split("&");
     for (var i = 0 ; i < params.length ; i++) {
       var pairArray = params[i].split("=");
       if (pairArray.length == 2) {
         params[pairArray[0]] = pairArray[1];
       }
     }
     return params;
   }
}


function getAttributo(el,attr,isselect) {
  retval = document.getElementById(el)

  if (retval) {
    if (isselect)
      return eval("retval.options[retval.selectedIndex]."+attr)
    else
      return eval("retval."+attr)
  }
}

function vai(desturl,repl) {
  if (desturl && desturl!="") {
    if (repl)
      document.location.replace(desturl)
    else
      document.location = desturl
  }
}


function cerca(form,id,error) {
    if (trimString(document.getElementById(id).value) == '' ) {
       alert(error)
    } else {
     document.forms[form].submit();
    }
}

function trimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}
//stampa la data in formato gg/mm/aaaa
//o mm/gg/aaaa se passiamo 'eng'
function printUltimaModifica(lng) {
  var mydate = new Date(document.lastModified);
  var g = mydate.getDate();
  var gg = new String(g);
  gg = gg.length==1?"0"+g:g;
  var m = mydate.getMonth();
  var mm = new String(m);
  mm = mm.length==1?"0"+(m+1):(m+1);
  var aaaa = mydate.getFullYear();
  if (lng=="eng")
    document.write(mm+ "/" + gg + "/" + aaaa)
  else
    document.write(gg+ "/" + mm + "/" + aaaa)

}

//funzione per "nascondere" un layer
function hideLayers() {
  //compatibilità: Netscape 6.x, Explorer 5 e 6, Opera 5 e 6, Mozilla
  a = hideLayers.arguments;
  for (i=0;i<a.length;i++) {
    if (document.getElementById(a[i])) {
      //document.getElementById(a[i]).style.visibility="hidden";
      document.getElementById(a[i]).style.display="none";
    }
  }
}

//rende visibile un layer
function showLayers() {
  //compatibilità: Netscape 6.x, Explorer 5 e 6, Opera 5 e 6, Mozilla
  a = showLayers.arguments;
  for (i=0;i<a.length;i++) {
    if (document.getElementById(a[i])) {
      //document.getElementById(a[i]).style.visibility="visible";
      document.getElementById(a[i]).style.display="block";
    }
  }
}

//controlla che il testo inserito nel campo sia un numero
function isNumber(idcampo) {
  campo = document.getElementById(idcampo);
  if (isNaN(campo.value) && campo.value!="") {
    alert("valore numerico !!");
    campo.value = 0
  }
}

//controlla che il testo inserito sia un indirizzo email formalmente valido
function isEmail(idcampo) {
  campo = document.getElementById(idcampo);
  var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
  var val = campo.value
  var ok = val.search(email_reg_exp);
  if( ok == -1 && val != "" ) {
    alert("Email non valida");
    campo.value = "";
    campo.focus();
    return false;
  } else return true;
}

//controlla che il il testo inserito sia nel formato gg/mm/aaaa
function checkItaDate(idcampo) {
  campo = document.getElementById(idcampo);

  //regular expression per il formato della data (gg/mm/aaaa)
  var dateIta_reg_exp = /^((3[01])|([12][0-9])|(0[1-9]))[\/]((1[0-2])|(0[1-9]))[\/]((19|20)\d{2})$/;

  var val= campo.value;
  var error= 0;
  if(val!="") {
    var giorno= val.substr(0, 2);
    var mese= val.substr(3, 2) - 1; //in js i mesi partono da 0
    var anno= val.substr(6, 4);
    var ok= val.search(dateIta_reg_exp);
    if(ok==-1 ) {
      error= 1;
    } else {
      //controllo sui mesi che non hanno 31 giorni e sull'anno bisestile
      var mese_tmp = mese.toString();
      if( mese_tmp.substr(0, 1) == 0 ) { mese_tmp = mese_tmp.substr(1,1) } //tolgo l'eventuale 0 davanti al mese
      gm= new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
      if ( ( ( anno % 4 == 0 ) && ( anno % 100 != 0 ) ) || ( anno % 400 == 0 ) ) { gm[1] = 29; } //anno bisestile
      if (giorno>gm[mese_tmp]) { error = 1; } //30 giorni a novembre con april......
    }
  }
  if( error == 1 ) {
    alert("Data non valida");
    campo.value= "";
  }

//simo per gestione apri chiudi form si ricerca
function nobox(nome) {
document.getElementById(nome).style.display='none'
}
function box(nome) {
document.getElementById(nome).style.display='block'
}
}
//MAPPA
function updateLink(link) {
window.location.href = link;
}

//GESTIONE MENU A TENDINA
<!--
window.onload=montre;
function montre(id) {
var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) {
		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	}
if (d) {d.style.display='block';}
}
//-->

//Eliminazione allegati area riservata
function eliminaAllegato(id,idSezione,idSezioneRif) {
  if(window.confirm('Sei sicuro di voler cancellare il file ?')){
     window.location.href='EliminaAllegati.jsp?idAllegatoAr=' +id +'&idSezione='+ idSezione+ '&idSezioneRif=' +idSezioneRif;
  }
}

//(funzione da inserire nel onkeyup del campo ricerca)
//in base alla parola inserita nel campo,
//lo script cerca il valore all'interno del campo select
function searchCombo(idricerca, idcombo) {
  ricerca = document.getElementById(idricerca);
  combo = document.getElementById(idcombo);

  trovato = -1;
  entry = ricerca.value;
  entryLength = entry.length;
  if (entryLength>0)
    for (i=0;i<combo.options.length;i++)
      trovato==-1&&combo.options[i].text.toLowerCase().substring(0, entryLength)==entry.toLowerCase()?trovato=i:null;
  else
    trovato=0;
  combo.selectedIndex=trovato;
}


