//::::: verificaVerioneBrowser ::::: INIZIO
function verificaVersioneBrowser(){
   browserName = navigator.appName;
   browserVer = parseInt(navigator.appVersion);
   if(browserName == "Netscape" && browserVer >= 4.7 || browserName == "Microsoft Internet Explorer" && browserVer >= 4){
      version = 1;
	}
   else if(browserName == "Netscape" && browserVer >= 3){
      version = 2;
   }
   else{
      version = 3;
   }
	return version
}
//::::: verificaVerioneBrowser ::::: FINE

//::::: impostaCSS ::::: INIZIO
function impostaCSS(js_theme){
   var version = verificaVersioneBrowser();


   if(version == 1){
      document.write("<link href=/shop/themes/"+js_theme+"/style.css rel=stylesheet type=text/css>");
      document.write("<link href=/shop/themes/"+js_theme+"/treecat/cooltree.css rel=stylesheet type=text/css>")
   }
   else if(version == 2){
      document.write("<link href=/shop/themes/"+js_theme+"/NS_style.css rel=stylesheet type=text/css>");
      document.write("<link href=/shop/themes/"+js_theme+"/treecat/NS_cooltree.css rel=stylesheet type=text/css>")

   }
   else {
      document.write("<link href=/shop/themes/"+js_theme+"/style.css rel=stylesheet type=text/css>");
      document.write("<link href=/shop/themes/"+js_theme+"/treecat/cooltree.css rel=stylesheet type=text/css>")
   }
}
//::::: impostaCSS ::::: FINE

//::::: MM_function ::::: INIZIO
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_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
//::::: MM_function ::::: FINE


//::::: Verifica Modulo Generico ::::: INIZIO
function verificaFormGenerico(msgPlease,msgSorry,msgIncorrect,nomeForm,campi,valori,chiavi){

   arrayCampi = campi.split("|");
   arrayValori = valori.split("|");
   arrayChiavi = chiavi.split("|");

   numCampi = arrayCampi.length;

   for(i=0;i<numCampi;i++){
      if(document.forms[nomeForm].elements[arrayCampi[i]].value.length==0){
         alert(msgPlease+" " + arrayChiavi[i] + ".");
         document.forms[nomeForm].elements[arrayCampi[i]].focus();
         return false;
      }
      else if(document.forms[nomeForm].elements[arrayCampi[i]].value.indexOf("@")<1 && arrayValori[i]=="mail"){
         alert(msgSorry+" " + arrayChiavi[i] + " "+msgIncorrect);
         document.forms[nomeForm].elements[arrayCampi[i]].focus();
         return false;
      }
      else if(isNaN(document.forms[nomeForm].elements[arrayCampi[i]].value) && arrayValori[i]=="num"){
         alert(msgSorry+" " + arrayChiavi[i] + " "+msgIncorrect);
         document.forms[nomeForm].elements[arrayCampi[i]].focus();
         return false;
      }
      else if(document.forms[nomeForm].elements[arrayCampi[i]].value==arrayValori[i]){
         alert(msgSorry+" " + arrayChiavi[i] +" "+msgIncorrect);
         document.forms[nomeForm].elements[arrayCampi[i]].focus();
         return false;
      }
   }
}
//::::: Verifica Modulo Modifica Generico ::::: FINE



//::::: field toUppercase ::::: INIZIO
function fieldUpperCase(myfield)
{
if (myfield.inchange)return;
myfield.inchange=true;
myfield.value=myfield.value.toUpperCase();
myfield.inchange=false;
}
//::::: field toUppercase ::::: FINE

function checkDate(day, month, year)
{
   var valid = "0123456789";

   var temp;

   for (j=0; j< day.length; j++)
   {
      temp = "" + day.substring(j, j+1);
      if (valid.indexOf(temp) == "-1")
      {
         //alert('Inserire solo carattere numerici nei campi GG e MM');
         return false;
      }
   }
   for (j=0; j< month.length; j++)
   {
      temp = "" + month.substring(j, j+1);
      if (valid.indexOf(temp) == "-1")
      {
         //alert('Inserire solo carattere numerici nei campi GG e MM');
         return false;
      }
   }
   for (j=0; j< year.length; j++)
   {
     temp = "" + year.substring(j, j+1);
     if (valid.indexOf(temp) == "-1")
      {
         //alert('Inserire solo carattere numerici nei campi GG e MM');
         return false;
      }
   }


   if (day < 1 || day > 31 || month <1 || month > 12)
   {
      //alert('Inserire valori plausibili nei campi GG e MM');
      return false;
   }

   if (month==4 || month==6 || month==9 || month==11)
   {
      if (day==31)
      {
         //alert('Il mese selezionato non ha 31 giorni');
         return false;
      }
   }

   if (month==2)
   {
      if (day>29)
      {
         //alert('Febbraio non ha mai un numero di giorni superiore a 29!');
         return false;
      }
      if (day==29 && ((year/4)!=parseInt(year/4)))
      {
         //alert('Quest\'anno Febbraio ha solo 28 giorni!');
         return false;
      }
   }

   return true;
}


function controllaCodFisc(campo){
   caratteri= new Array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
   pari= new Array (0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)
   dispari=new Array (1,0,5,7,9,13,15,17,19,21,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)
   cod=campo.value.toLowerCase();
   check=true;
   if (cod.length!=16){
      check=false
      }
     else
      {
      lettere=cod.substr(0,6)+cod.substr(8,1)+cod.substr(11,1)+cod.substr(15);
      numeri=cod.substr(6,2)+cod.substr(9,2)+cod.substr(12,3);
      for (i=0;i<10;i++){
         if (lettere.charCodeAt(i)<97 || lettere.charCodeAt(i)>122){
            check=false;}
      }
      for (i=0;i<8;i++){
         if (numeri.charCodeAt(i)<48 || numeri.charCodeAt(i)>57){
            check=false;
         }
      }
     }
   //checksum del codice fiscale
   test=cod.substr(15,1);
   var somma=0
   for (i=0;i<16;i=i+2){ //dispari
       carattere=cod.substr(i,1)
       for (k=0;k<36;k++){
          if (carattere==caratteri[k]){
             somma=somma+dispari[k]
             break
          }
       }
    }
    for (i=1;i<15;i=i+2){ //pari
       carattere=cod.substr(i,1)
       for (k=0;k<36;k++){
          if (carattere==caratteri[k]){
             somma=somma+pari[k]
             break
          }
       }
    }
   resto=somma % 26;
   var lettera=String.fromCharCode(97+resto);
   if (test != lettera){
      check=false;
      }
/*   if (check==false){
      alert("Codice fiscale errato!")
      }
      else
      {
      alert("codice fiscale corretto!!")
      }*/
   return check;
}

function checkPhone(e) {
  var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
  if(is_chrome){
    return true;
  }

  var whichCode = (e.which == null) ? e.keyCode : e.which;
  // whichCode=13 è il tasto Enter
  // whichCode=47 è lo slash /
  if (((whichCode>=48)&&(whichCode<=57)) ||(whichCode==43) ||(whichCode==13)||(whichCode==8))
     {
     return true;
     }
  else
     return false;
}

function checkFax(e) {
  var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
  if(is_chrome){
    return true;
  }

  //var whichCode = (e.which == null) ? e.keyCode : e.which;
  var whichCode = (e.which == null) ? e.keyCode : e.which;
  
  // whichCode=13 è il tasto Enter
  if (((whichCode>=48)&&(whichCode<=57)) ||(whichCode==43) ||(whichCode==13)||(whichCode==8))
     {
     return true;
     }
  else
     return false;
}
