function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = '; expires='+date.toGMTString();
  }
  else expires = '';
  document.cookie = name+'='+value+expires+'; path=/';
}

function readCookie(name) {
  var nameEQ = name + '=';
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function setFontSize(size) {
	var body = document.getElementsByTagName('body')[0];
	var percentuale = "80%"; // default
	if (size == 1) percentuale = "69%";
	if (size == 2) percentuale = "80%";
	if (size == 3) percentuale = "90%";
	if (size == 4) percentuale = "100%";
	if (size == 5) percentuale = "110%";
	body.style.fontSize = percentuale;
	createCookie('TuoSitoFontSize',size,365);
}

window.onload = function() {
	size = readCookie('TuoSitoFontSize');
	setFontSize(size);
}

function controlla_codice(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("Attenzione, il valore inserito nel campo obbligatorio \"Codice fiscale\" è errato. Prova di nuovo.")
      }
   return check;
}

function myLayer(x){
//object finding 
	var lay;
	if(document.layers){ // browser="NN4"; 
		lay=document.layers[x];
	} 
	if(document.all){ // browser="IE"; 
		lay=eval("document.all." + x);
	} 
	if(!document.all && document.getElementById){ // browser="NN6+ or IE5+ if you’re willing to dump the !document.all stuff"; 
		lay = document.getElementById(x);
	}
	return lay;
}


function fnCaptcha(){
	var numero=Math.random()*7*666;
	numero=Math.floor(numero);
	if(document.getElementById('captchaNumber')){
		myLayer('captchaNumber').innerHTML = numero;
		document.registrati.captchaHidden.value = numero;
	}
}


function fnCaptchaCheck(f){
	if ((f.captcha.value == "") || (f.captcha.value != f.captchaHidden.value)){
		alert("Different numbers. Check KO ");
		return false;
	}else{
		alert("Same numbers. Check OK. ");
	}
	return false();
}
window.onload=function(){fnCaptcha();}

function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
  else
    {
    return true;
    }
  }
}

function checkTec(form){
	if (!form.accetto.checked){
		alert("Attenzione, devi accettare i Termini e le Condizioni di vendita prima di procedere con il pagamento.");
		return false;
	} else {
		return true;
	}
}

function checkForTipo(obj) { 
 if (!document.layers) { 
 var txt = document.getElementById("hidden"); 
 if (obj.value == "1f" | obj.value == "1p" | obj.value == "ordini" | obj.value == "pagamenti" | obj.value == "consegne") { 
 txt.style.display = "block"; 
 } else { 
 txt.style.display = "none"; 
 } 
 } 
} 

