// 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];}
}
//-->


//rilevo SO e Browser
SOWIN = (navigator.userAgent.toLowerCase().indexOf("win") > -1) ? 1 : 0;
SOMAC = (navigator.userAgent.toLowerCase().indexOf("mac") > -1) ? 1 : 0;
SOLIN = (navigator.userAgent.toLowerCase().indexOf("linux") > -1) ? 1 : 0;
SOALT = (!SOWIN && !SOMAC && !SOLIN) ? 1 : 0;
OP = ((ind1 = navigator.userAgent.indexOf("Opera")) > -1) ? 1 : 0;
punto = (OP) ? navigator.userAgent.indexOf(".",ind1):0;
OP5 = (OP && parseInt(navigator.userAgent.substr(punto-1)) == 5) ? 1 : 0;
OP6 = (OP && parseInt(navigator.userAgent.substr(punto-1)) == 6) ? 1 : 0;
IE = ((ind2 = navigator.appVersion.indexOf("MSIE")) > -1 && !OP) ? 1 : 0;
IE4 = (IE && parseInt(navigator.appVersion.substr(ind2+5)) == 4) ? 1 : 0;
IE5 = (IE && parseInt(navigator.appVersion.substr(ind2+5)) == 5) ? 1 : 0;
IE6 = (IE && parseInt(navigator.appVersion.substr(ind2+5)) == 6) ? 1 : 0;
NN = (navigator.appName.indexOf("Netscape")>-1) ? 1 : 0;
NN4 = (NN && parseInt(navigator.appVersion)==4) ? 1 : 0;
NN6 = (NN && parseInt(navigator.appVersion)>4) ? 1 : 0;
GE = (navigator.userAgent.toLowerCase().indexOf("gecko")>-1) ? 1 : 0;
//riconosce mozilla, NN6 e tutti i browser basati sul gecko layout engine


//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;
   }
}


/* ========================= GESTIONE COOKIE ========================= */
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    //date.setTime(date.getTime()+(min*60*1000));
    var expires = '; expires='+date.toGMTString();
  } else {
    expires = '';
  }
  document.cookie = name+'='+value+expires+'; path=/';
}

function readCookie(name) {
  var labelName = name + '=';
  var st = document.cookie.split(';');
  for (var i=0;i<st.length;i++) {
    var token = st[i];
    while (token.charAt(0)==' ') token = token.substring(1,token.length);
    if (token.indexOf(labelName) == 0) return token.substring(labelName.length,token.length);
  }
}

function deleteCookie(name) {
  var expDate = new Date();
  expYear = expDate.getYear();
  expDate.setYear(expYear);
  var expires = '; expires='+expDate.toGMTString();
  document.cookie = name+'=deleted'+expires+'; path=/';
}
/* =========================/ GESTIONE COOKIE ========================= */


/* ======================================================= */
function checkEmail(campo,lingua) {
  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 != "" ) {

    if (lingua=="ita")
      alert("Email non valida");
    else if (lingua=="eng")
      alert("Non-valid email");
    else
      alert("Email non valida");

    campo.value = "";
    campo.focus();
  }
}
/* ======================================================= */

/* ======================================================= */
function checkDate(campo,lingua) {

  var date_reg_exp

  if (lingua=="ita") {  //gg/mm/aaaa
    date_reg_exp = /^((3[01])|([12][0-9])|(0[1-9]))[\/]((1[0-2])|(0[1-9]))[\/]((19|20)\d{2})$/;
  } else if (lingua=="eng") {  //mm/dd/yyyy
    date_reg_exp = /^((1[0-2])|(0[1-9]))[\/]((3[01])|([12][0-9])|(0[1-9]))[\/]((19|20)\d{2})$/;
  } else {
    date_reg_exp = /^((1[0-2])|(0[1-9]))[\/]((3[01])|([12][0-9])|(0[1-9]))[\/]((19|20)\d{2})$/;
  }

  var error = 0;

  if (campo.value!="") {

    var giorno
    var mese
    if (lingua=="ita") {
      giorno = campo.value.substr(0, 2);
      mese = campo.value.substr(3, 2) - 1; //in js i mesi partono da 0
    } else if (lingua=="eng") {
      mese = campo.value.substr(0,2) - 1;
      giorno = campo.value.substr(3, 2);
    } else {
      giorno = campo.value.substr(0, 2);
      mese = campo.value.substr(3, 2) - 1;
    }
    var anno = campo.value.substr(6, 4);

    var ok = campo.value.search(date_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 ha novembre con april giugno e settembre di ventotto ce n'è uno tutti gli altri ne han trentuno
    }
  }

  if (error==1) {

    if (lingua=="ita")
      alert("Formato data non valido (gg/mm/aaaa)");
    else if (lingua=="eng")
      alert("Non-valid date format (mm/dd/yyyy)");
    else
      alert("Formato data non valido (gg/mm/aaaa)");

    campo.value = "";
  }

}
/* ======================================================= */

/* ======================================================= */
function checkForm(lingua,idform) {
  //I CAMPI OBBLIGATORI DEVONO AVERE id="required"+n !!!

  var regexp = /^(required)[0-9]*$/gi
  var error = 0;

  var myform = document.forms[0];
  if (idform!=undefined)
    myform = document.forms[idform];
  var campi = myform.elements;
  var ncampi = myform.length;
  var campimancanti = "\n";

  for (i=0;i<ncampi;i++) {
    campo = campi[i];
    idcampo = campo.id;

    if (campo) {
      if (campo.type!="hidden" && idcampo.search(regexp)!=-1) {

        switch (campo.type) {
          case "select-one":
            if (campo.selectedIndex == -1 || campo.options[campo.selectedIndex].value=="") {
              error = 1;
              campimancanti += campo.name + "\n";
            }
            break;
          case "select-multiple":
            if (campo.selectedIndex == -1 || campo.options[campo.selectedIndex].value=="") {
              error = 1;
              campimancanti += campo.name + "\n";
            }
            break;
          case "file":
            if (campo.value=="") {
              error = 1;
              campimancanti += campo.name + "\n";
            }
            break;
          case "text":
            if (campo.value=="") {
              error = 1;
              campimancanti += campo.name + "\n";
            }
            break;
          case "textarea":
            if (campo.value=="") {
              error = 1;
              campimancanti += campo.name + "\n";
            }
            break;
          case "checkbox":
            if (myform.elements[campo.name].length) {
              var atleastone = false;
              for (j=0;j<myform.elements[campo.name].length;j++) {
                if (myform.elements[campo.name][j].checked)
                  atleastone = true;
              }
              if (!atleastone) {
                error = 1;
                campimancanti += campo.name + "\n";
              }
            } else {
              if (!myform.elements[campo.name].checked) {
                error = 1;
                campimancanti += campo.name + "\n";
              }
            }
            break;
          case "radio":
            var atleastone = false;
            for (j=0;j<myform.elements[campo.name].length;j++) {
              if (myform.elements[campo.name][j].checked)
                atleastone = true;
            }
            if (!atleastone) {
              error = 1;
              campimancanti += campo.name + "\n";
            }
            break;
          default:
        }

      }
    }

  }

  if (error==1) {
    //in campimancanti ho l'elenco di tutti i campi obbligatori non compilati
    if (lingua=="ita")
      alert("Compilare tutti i campi obbligatori !!");
    else if (lingua=="eng")
      alert("fill all required fields !!");
    else
      alert("Compilare tutti i campi obbligatori !!");

  } else {
    myform.submit();
  }
}
/* ======================================================= */


function isNumber(campo,lingua) {
  if (isNaN(campo.value) && campo.value!="") {
    if (lingua=="ita")
      alert("valore numerico !!");
    else if (lingua=="eng")
      alert("numeric value !!");
    else
      alert("valore numerico !!");

    campo.value = "";
  }
}


	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;
	}

//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";
    }
  }
}




//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'
}


function goHome(lingua,paese) {
  if (confirm('Remember this choice?')) {
    createCookie('idPaeseLingua',paese,365);
  }

  document.location.href = "ChangeProject.jsp?idProgetto="+lingua+"&idPaeseLingua="+paese;
}

function goLogin() {
  //da login della splash vado in Login.jsp con lingua inglese e paese italia
  document.location.href = "ChangeProject.jsp?idProgetto=2&idPaeseLingua=1&page=login";
}
