// --------------------- ROUTINES DI GESTIONE COOKIE ------------------------
var CampiDaSalvare;

function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime (date.getTime() - skew);
}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}


function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return "";
}


function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
if (expires!=null) FixCookieDate(expires); 
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}


function DeleteCookie (name) {
  var exp = new Date();
  FixCookieDate (exp);
  exp.setTime (exp.getTime() - 1);  
  var cval = GetCookie (name);
  if (cval != null)
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
// --------------------- FINE ROUTINES DI GESTIONE COOKIE --------------------


// --------------------- CARICA VALORI COOKIE IN FORM ------------------------
function carica (){
   var l;
   var campi=document.forms[0].elements;
   
    if (CampiDaSalvare != "" && CampiDaSalvare != null){
       var ArrayCampi=CampiDaSalvare.split(",");
       for (l=0;l<ArrayCampi.length;l++) 
         {
            campi[ArrayCampi[l]].value=GetCookie(ArrayCampi[l]);
    	  }
    }
    else {
      for (l=0;l<campi.length;l++) 
       {
          if (campi[l].type == "text") {
             campi[l].value=GetCookie(campi[l].name);
             }
       }
    }
    
    // document.forms[0].elements[5].value=CampiDaSalvare;

}


// --------------------- SALVA VALORI FORM IN COOKIE ------------------------
function salva (){
   // 10 years from now the cookie will expire
   var l;
   var expdate = new Date ();
   var ArrayCampi;
   expdate.setTime (expdate.getTime() + (3600 * 24 * 60 * 60 * 1000));

   var campi=document.forms[0].elements;
  
   if (CampiDaSalvare != "" && CampiDaSalvare != null){
     var ArrayCampi=CampiDaSalvare.split(",");
     for (l=0;l<ArrayCampi.length;l++) 
       {
          SetCookie(ArrayCampi[l],campi[ArrayCampi[l]].value,expdate);    
    	}
    }
    else {
     for (l=0;l<campi.length;l++) 
       {
         if (campi[l].type == "text") {
           SetCookie(campi[l].name,campi[l].value,expdate);    
            }
       }
    }
}



// --------------------- CONVALIDA VALORI FORM ------------------------
function convalida(theForm2,obbligatori)
{
var i=0;
var campi=theForm2.elements;
var errati="";

for (i=0;i<campi.length;i++) {
   var nome=campi[i].name.toLowerCase();
   var valore=campi[i].value;
   
   if (obbligatori.indexOf(nome)!= -1){
       if (valore=="" || (nome=="email" && !checkemail(valore)) ){
          errati+=nome+"\n"  
          }
      }
  }
  
if (errati != "")
    {
    alert("Inserire un valore per i seguenti campi:\n\n"+errati);
    return (false);
    }
 
  return (true);
  
}


// --------------------- CONTROLLA VALIDITA EMAIL ------------------------
function checkemail(str){
  var testresults;
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(str))
    testresults=true
  else{
    alert("Inserire un indirizzo di email valido !")
    testresults=false
  }
  return (testresults)
}


// --------------------- STAMPA PAGINA ------------------------
function stampa () {
  safeIEstring=navigator.appVersion;
  if (document.layers || (safeIEstring.indexOf ("MSIE 5.0") != -1)) {
  // se si tratta di netscape dalla versione 4 usa il metodo print() di window  
    	print();
  }  
  else if (document.all) {
  // se invece il browser non è MSIE 5.0, ma ad esempio un browser 4.0 usa l'activex per stampare
   	var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
    	document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    	WebBrowser1.ExecWB(6, 2);
		WebBrowser1.outerHTML = "";  
  }
}


// ------------------- GESTIONE LIVELLI MULTIBROWSER
// var isNetscape=(navigator.appName.indexOf("Netscape") >= 0 && parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
var isNetscape=(navigator.appName=="Netscape");
var layerRef="";
var styleRef="";

if (isNetscape){
  layerRef=".layers";
  styleRef="";
}
else {
  layerRef=".all";
  styleRef=".style";
}

function livello(id,stato,testo){
  if (isNetscape) stato=(stato=="hidden") ? "hide" : (stato=="visible") ? "show" : stato;
  eval("document"+layerRef+"[\'"+id+"\']"+styleRef+".visibility=\'"+stato+"\'");
  if (testo!=null){
    if (isNetscape) {
     eval("document."+id+".document.write(testo)");
     eval("document."+id+".document.close()");
     }
    else {
     eval(id+".innerHTML=testo");
     }
   }
}



// ------------------- FORMATTAZIONE IMPORTO IN EURO
function ImportoEuro (n) {
  var i='';
  var d='';
  var s='';
  n=n+'';
  var p=n.indexOf('.');
  if (p>0){i=n.substr(0,p);d=n.substr(p+1,2);d=d+'00';d=d.substr(0,2)}
  else    {i=n;d='00'}
  s=i+'.'+d;
  return s;
};


// ------------------- SOSTITUZIONE CARATTERI
function replace (testo,vecchio,nuovo) {
  var d=testo.split(vecchio);
  var i;
  var t="";
  if (d.length>0){
    for (i=0;i<d.length;i++){
      if (t!=""){t+=nuovo}
      t+=d[i];
    }
   }
  else{
    t=testo;
   }
  return t;
}
