//controllo e-mail
function controlloemail(campo){
  var e_mail = campo.value,at=false,dot=false,e_mail_error=false;
  if (e_mail != "" && e_mail.length > 4 ){
    for (i=0; i < e_mail.length; i++){
      if (e_mail.charAt(i) == "@")
        for (at=i+1; at < e_mail.length; at++)
          if (e_mail.charAt(at) == "@"){
            e_mail_error=true;
          }
      if (((e_mail.charAt(i) == ".") && ((e_mail.charAt(i+1) == ".")||(e_mail.charAt(i+1) == "@")))||((e_mail.charAt(i) == "@") && (e_mail.charAt(i+1) == "."))){
        e_mail_error=true;
      }
      if (e_mail.charAt(i) == "@")
        at=true;
      if (e_mail.charAt(i) == ".")
        dot=true;
    }
    if ((e_mail.charAt(0) == ".") || (e_mail.charAt(e_mail.length) == ".") || (at == false) || (dot == false)){
      e_mail_error=true;
    }
  }else{
    e_mail_error=true;
  }
  if (e_mail_error==true){
    alert("inserire un inirizzo e-mail valido")
	campo.focus();
    return false
  }
  return true
}
//controllo codice fiscale/partita iva
function controllocfpiva(campo,errormex,tipo){
  var ritorno = true
  //tipo: 1 = cf, 2 = piva, 3= combo
  campo.value = campo.value.toUpperCase()
  if (campo.value != ""){
    switch (tipo){
	  case 1: if (campo.value.length==16){
	            ritorno = controllocf(String(campo.value))
			  }else{
	            ritorno = "Il codice fiscale deve essere formato da 16 caratteri alfanumerici"
			  }
	  case 2: if (campo.value.length==11){
	            ritorno = controllopiva(String(campo.value))
			  }else{
	            ritorno = "La parita IVA deve essere formata da 11 caratteri numerici"
			  }
	  case 3:
        switch (campo.value.length){
          case 11: ritorno = controllopiva(String(campo.value))
            break;
          case 16: ritorno = controllocf(String(campo.value))
            break;
          default: ritorno = false
	    }
	}
  }
  if (!ritorno){
    alert(errormex);
    campo.focus();
  }
  return ritorno
}
function controllopiva(valore){
  var piva_error=false
  if (!ChkNumberStr (valore,"")){
    piva_error = true
  }else{
    var xchk1=0,xchk2=0,xchk3=0,i,xchk4,t,r,u;
    for(i=0;i<9;i=i+2){
      xchk1+=parseInt(valore.charAt(i))
    }
    for(i=1;i<10;i=i+2){
      xchk3=2*parseInt(valore.charAt(i))
      if(xchk3<10){
        xchk2=xchk2+xchk3
      }else{
        xchk3=xchk3-9
        xchk2+=xchk3
      }
    }
    t=xchk1+xchk2
    xchk4=t % 10
    r = (xchk4==0?0:10-xchk4)
    u = parseInt(valore.charAt(10))
    if(r!=u) piva_error = true
  }
  return (!piva_error)
}
function controllocf(valore){
  var cf_error=0
  if (!ChkNumberStr (valore,"")){
    cf_error = 1
  }else{
    var dispari= new Array(1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23)
    var cont = 0;
    var carattere_controllo=valore.charCodeAt(15);
    for (i=0; i<15; i++){
      if (i % 2 ==0){
        if(valore.charCodeAt(i)<58){
          cont += dispari[valore.charCodeAt(i) - 48]
        }else{
          cont += dispari[valore.charCodeAt(i) - 65]
        }
      }else{
        if(valore.charCodeAt(i)<58){
          cont += valore.charCodeAt(i) - 48
        }else{
          cont += valore.charCodeAt(i) - 65
        }
      }
    }

    if (carattere_controllo<58){
      carattere_controllo=carattere_controllo-48
    }else{
      carattere_controllo=carattere_controllo-65
    }
    if ((cont % 26)!=carattere_controllo){cf_error=1}
  }
  if (cf_error == 1){return false}
  return true
}
//controllo data
function controllodata(giorno,mese,anno){
  if (giorno.value!=""||mese.value!=""||anno.value!=""){
    var prsGiorno = parseFloat(giorno.value);
    var prsMese = parseFloat(mese.value);
    var prsAnno = parseFloat(anno.value);
    if (!(prsMese == 2 || prsMese == 4 || prsMese == 6 || prsMese == 9 || prsMese == 11)){
      if (!(prsGiorno >= 1 && prsGiorno <= 31) || !ChkNumberStr(giorno.value,"N")) {
        alert("Inserire un valore da 1 a 31 per il giorno");
        giorno.focus();
        return false;
      }
    }else{
      if (prsMese != 2 && (!(prsGiorno >= 1 && prsGiorno <= 30)|| !ChkNumberStr(giorno.value,"N"))) {
        alert("Inserire un valore da 1 a 30 per il giorno");
        giorno.focus();
        return false;
      }
      var bisestile = 0;
      if (prsMese == 2){
        for(i=1900; i < 2100;i=i+4){
          if(i==prsAnno){
            bisestile = 1;
          }
        }
        if (bisestile==0&& !(prsGiorno >= 1 && prsGiorno <= 28)|| !ChkNumberStr(giorno.value,"N")){
          alert("Inserire un valore da 1 a 28 per il giorno");
          giorno.focus();
          return false;
        }
        if (bisestile==1&& !(prsGiorno >= 1 && prsGiorno <= 29)|| !ChkNumberStr(giorno.value,"N")){
          alert("Inserire un valore da 1 a 29 per il giorno");
          giorno.focus();
          return false;
        }
      }
    }
    if (!(prsMese >= 1 && prsMese <= 12)|| !ChkNumberStr(mese.value,"N")){
      alert("Inserire un valore da 1 a 12 per il mese");
      mese.focus();
      return false;
    }
    if (!prsAnno >= 1900 || !ChkNumberStr(anno.value,"N")){
      alert("Inserire un valore corretto per l'anno");
      anno.focus();
      return false;
    }
  }
  return true
}
function controllo_data(stringa){
	var espressione = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
	if (!espressione.test(stringa))
	{
	    return false;
	}else{
		anno = parseInt(stringa.substr(6),10);
		mese = parseInt(stringa.substr(3, 2),10);
		giorno = parseInt(stringa.substr(0, 2),10);
		var data=new Date(anno, mese-1, giorno);
		if(data.getFullYear()==anno && data.getMonth()+1==mese && data.getDate()==giorno){
			return true;
		}else{
			return false;
		}
	}
}
//controllo cap
function ControllaCap (cap){
  if (!cap.value=="" && (!ChkNumberStr(cap.value,"N") || cap.value.length!=5)){
    alert("Inserire un valore corretto per il CAP");
    cap.focus();
    return false;
  }
  return true;
}
function ChkNumberStr (strToChk, typetochk)
{
  if (typetochk=="N")
   	 var checkOK = "0123456789";
  else
     var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
  var checkStr = String(strToChk);
  var allValid = true;
  var decPoints = 0;
  var allNum = "";

  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length){
      return(false);
      break;
    }
    allNum += ch;
  }
  return(true);
}
function ChkNumberStrTarga (strToChk, typetochk)
{
  if (typetochk=="N")
   	 var checkOK = "0123456789";
  else
     var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  var checkStr = String(strToChk);
  var allValid = true;
  var decPoints = 0;
  var allNum = "";

  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length){
      return(false);
      break;
    }
    allNum += ch;
  }
  return(true);
}
function FinePagina(){
  var w = window.screen.width;
  var h = window.screen.height;
  window.scrollTo(w * h, w * h)
}
function BloccoCaratteriIndesiderati(evento, mod){
	/*Modalità:
	  1 = solo numeri
	  2 = solo alfanumerici (no spazi)
	  3 = non accetta 'e "
		4 = non accetta backspace*/
	switch (mod){
		case 1: if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;
						break;
		case 2: if ((event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97)) event.returnValue = false;
						break;
		case 3: if (event.keyCode==34 || event.keyCode==39) event.returnValue = false;
						break;
		case 4: if (event.keyCode==8) event.returnValue = false;
						break;
	}
	return false
}
function fnzDisabilita(index,campo){
  campo.disabled = index != 1
  if (campo.disabled) campo.value = ""
}
function fnzDis(index) {
  return (index != 1);
}
function HideDiv(){
	/*FUNZIONA PER I DIV WARNING*/
	var divs = document.getElementsByTagName('div');
	for (i in divs){
		var str = String(divs[i].id).substring(0,7);
		if (str=="Warning"){
			MM_showHideLayers(divs[i].id,'','hide')
		}
	}
}
function ChkWarn(str){
	/*FUNZIONA PER I DIV WARNING*/
	var args = str.split("/");
	for (var i=0;i<args.length-1;i++){
		MM_showHideLayers('Warning'+args[i]+'','','show')
	}
}
function GoHome(pos){
	document.frmLayout.action=pos+"default.asp";
	document.frmLayout.onClick="";
	document.frmLayout.target="";
	document.frmLayout.submit();
}
function fnzSrch(pg,pos){
	document.frmLayout.action=pos+"Utilities/ricerca.asp?pg="+pg;
	document.frmLayout.onClick="";
	document.frmLayout.target="";
	document.frmLayout.submit();
}
function Map(pos){
	document.frmLayout.action=pos+"Utilities/Mappa-Sito.asp";
	document.frmLayout.onClick="";
	document.frmLayout.target="";
	document.frmLayout.submit();
}
function Contact(pos){
	document.frmLayout.action=pos+"Contatti/Modulo_Contatti.asp";
	document.frmLayout.onClick="";
	document.frmLayout.target="";
	document.frmLayout.submit();
}
function MyAccount(pos){
	document.frmLayout.action=pos+"PersonalAccount/Personal-Account.asp";
	document.frmLayout.onClick="";
	document.frmLayout.target="";
	document.frmLayout.submit();
}
function GetPwd(pos){
	document.frmLayout.action=pos+"PersonalAccount/richiesta-password.asp";
	document.frmLayout.onClick="";
	document.frmLayout.target="";
	document.frmLayout.submit();
}
function RegIntro(pos){
	document.frmLayout.action=pos+"Registrazione/Intro-Registrazione.asp";
	document.frmLayout.onClick="";
	document.frmLayout.target="";
	document.frmLayout.submit();
}
function submit_form (form,link_form)
{
    form.action=link_form;
    form.submit();
}
