// 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


//======================= GESTIONE LINGUA
/*
var lingua = "ita";
function setLingua(lng) {
  lingua = lng;
}
function getLingua() {
  getLingua = lingua;
}


function isPopup() {
  if (window.opener) return true
  return false
}
*/

//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 email_reg_exp = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$");
  var val = campo.value
//  var ok = val.match(email_reg_exp);
  var ok = val.search(email_reg_exp);
  if((ok == -1) && (val != "")) {
    alert(labelScriptEmail);
//    campo.value = "";
    campo.focus();
    if(campo.select) campo.select();
  };
};

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})$/;
//    date_reg_exp = new RegExp("/^((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})$/;
//    date_reg_exp = new RegExp("/^((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})$/;
//    date_reg_exp = new RegExp("/^((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) {
    alert(labelScriptData);
    campo.value = "";
    //campo.focus();
  };
};

function resetForm(idform){
  var myform = document.forms[0];
  if(idform != undefined) myform = document.forms[idform];
  myform.reset();
};

function checkForm(lingua,idform) {
  //I CAMPI OBBLIGATORI DEVONO AVERE id = "required" + n !!!
  var regexp = /^(required_)[0-9]*$/gi;
//  var regexp = new 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;
    tipocampo = campo.type;
    if(campo) {
      if(idcampo.indexOf("conferma_") != -1) {
        var strtmp = idcampo.substr(idcampo.indexOf("conferma_") + 9);
        var camporif = document.getElementById(strtmp);
        if(camporif.value != campo.value) {
          alert(labelScriptForm1);
          campo.focus();
          if(campo.select) campo.select();
          error = 2;
          break;
        };
      };
      if((tipocampo != "hidden") && (idcampo.search(regexp) != -1)) {
        switch (tipocampo) {
          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 "select-one":
          case "select-multiple": {
            if((campo.selectedIndex == -1) || (campo.options[campo.selectedIndex].value == "")) {
              error = 1;
              campimancanti += (campo.name + "\n");
            };
            break;
          }
          case "text":
          case "textarea":
          case "file": {
            if(campo.value == "") {
              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
    alert(labelScriptForm2);
  }
  else if (error == 2) {
    //campo conferma errato
  }
  else {
    myform.submit();
  };
};

function isNumber(campo,lingua) {
  if(isNaN(campo.value) && (campo.value != "")) {
    alert(labelScriptNumber);
    campo.value = "";
    campo.focus();
  };
};

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 (compatibilit? Netscape 6.x, Explorer 5 e 6, Opera 5 e 6, Mozilla)
function hideLayers() {
  a = hideLayers.arguments;
  for(var i = 0; i < a.length; i++) {
    if(document.getElementById(a[i])) document.getElementById(a[i]).style.display = "none";
  };
};

// rende visibile un layer (compatibilit? Netscape 6.x, Explorer 5 e 6, Opera 5 e 6, Mozilla)
function showLayers() {
  a = showLayers.arguments;
  for(var i = 0; i < a.length; i++) {
    if(document.getElementById(a[i])) document.getElementById(a[i]).style.display = "block";
  };
};

//funzione per "nascondere" un layer (compatibilit? Netscape 6.x, Explorer 5 e 6, Opera 5 e 6, Mozilla)
function hideSplashMenu() {
  a = hideSplashMenu.arguments;
  for(var i = 0; i < a.length; i++) {
    if(document.getElementById(a[i])) document.getElementById(a[i]).style.visibility = "hidden";
  };
};

//rende visibile un layer (compatibilit? Netscape 6.x, Explorer 5 e 6, Opera 5 e 6, Mozilla)
function showSplashMenu() {
  a = showSplashMenu.arguments;
  for(var i = 0; i < a.length; i++) {
    if(document.getElementById(a[i])) document.getElementById(a[i]).style.visibility = "visible";
  };
};

//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, linguasito) {
  if(confirm("Remember this choice?")) {
    createCookie("idProgetto", lingua, 365);
    createCookie("idPaeseLingua", paese, 365);
    createCookie("idLinguaSito", linguasito, 365);
  };
  document.location.href = "ChangeProject.jsp?idProgetto=" + lingua + "&idPaeseLingua=" + paese + "&idLinguaSito=" +linguasito;
}

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";
}
