// --------------------- 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,messaggio)
{
var i=0;
var campi=theForm2.elements;
var errati="";

if (!messaggio) {messaggio="Inserire un valore per i seguenti campi"}

for (i=0;i<campi.length;i++) {
   var nome=campi[i].name;
   var tipo=campi[i].type;
   var valore=campi[i].value;
   
   // alert('nome='+nome+'\ntipo='+tipo+'\nvalore='+valore);
   
   if (tipo=="checkbox"){valore=(campi[i].checked)?valore:"";}
   if (tipo=="radio"){valore=(campi[i].checked)?valore:"";}
   //if (tipo=="select-one"){valore=campi[campi[i].selectedIndex].value;}
   
   nome=ConvCampo(nome,obbligatori);
   if (nome!="" && valore==""){
       if (nome.substr(0,1)=="_") nome=nome.substr(1)
       errati+=nome+"\n";
   }
}
  if (errati != "")
  {
    alert(messaggio+":\n\n"+errati);
    return (false);
  }
  
    
  return (true);
}


function ConvCampo (campo,obbligatori) {
  var arr;
  var arr1;
  arr=obbligatori.split(',');
  for (n=0;n<arr.length;n++) {
      arr1=arr[n].split('|');
      if (arr1[0].toLowerCase()==campo.toLowerCase()) {
         var v=(arr1.length>1)?arr1[1]:arr1[0];
         return v;
      }
   }
  return '';
}

function convalida_old(theForm2,obbligatori)
{
var i=0;
var campi=theForm2.elements;
var errati="";

for (i=0;i<campi.length;i++) {
   var nome=campi[i].name;
   if (obbligatori.indexOf(nome.toLowerCase())!= -1 
       && campi[i].value==""){
       errati+=campi[i].name+"\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") != -1) || (safeIEstring.indexOf ("MSIE 6") != -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=="Netscape");
var layerRef="";
var styleRef="";

if (isNetscape){
  layerRef=".layers";
  styleRef="";
}
else {
  layerRef=".all";
  styleRef=".style";
}

function livello(id,stato,testo){
  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;
}



// ------------------- EMAIL ANTI-SPIDER
function MyEmail(nome,label,dominio,stile) {
  if (!nome) var nome='info';
  if (!dominio) var dominio='sevim.it';
  var email=nome + '@' + dominio;
  if (!label) var label=email;
  if (!stile) var stile='';
  document.write('<a href=\"mailto:' + email + '\" '+stile+'>');
  document.write(label + '</a>');
}



// ------------------- FINESTRA LARGA
function finestra_nuova(strUrl,larghezza,altezza,barre){
  if (!larghezza) {
     larghezza=750;
     altezza=450;
     barre=1;
  }

  if (screen.width<larghezza){larghezza=screen.width}

  win1=window.open(strUrl,
  "finestra",
  "toolbar=no,directories=no,resizable=yes,menubar=no,scrollbars="+barre+",width="+larghezza+",height="+altezza+",left=10,top=10");
  win1.focus();
  
  return false;
};


// ------------------- FINESTRA DESTRA
 function  finestra_destra(strUrl,larghezza,altezza,barre){
   if(!larghezza){larghezza=660;altezza=450;barre=1;}
   if(screen.width<larghezza){larghezza=screen.width}
   win1=window.open(strUrl,"finestra","toolbar=no,directories=no,menubar=no,scrollbars="+barre+",width="+larghezza+",height="+altezza+",left=10,top=10");
   win1.focus();
   return false;
   }
   
// ------------------- FINESTRA PIEDE
 function finestra_piede(strUrl,larghezza,altezza,barre){
  if (!larghezza) {
     larghezza=660;
     altezza=450;
     barre=1;
     }

  if (screen.width<larghezza){larghezza=screen.width}

  win1=window.open(strUrl,
  "finestra",
  "toolbar=no,directories=no,menubar=no,scrollbars="+barre+",width="+larghezza+",height="+altezza+",left=10,top=10");
  win1.focus();
  
  return false;   
 
 }
  
  
  
// ------------------- FINESTRA

 function finestra(strUrl,larghezza,altezza,barre){
  if (!larghezza) {
     larghezza=660;
     altezza=450;
     barre=1;
  }

  if (screen.width<larghezza){larghezza=screen.width}

  win1=window.open(strUrl,
  "finestra",
  "toolbar=no,directories=no,menubar=no,scrollbars="+barre+",width="+larghezza+",height="+altezza+",left=10,top=10");
  win1.focus();
  
  return false;
};
 
  
// ------------------- AGGIUNGE PAGINA A PREFERITI 
 function AddPreferiti () { 
    window.external.AddFavorite(top.location.href,document.title);
  }
  
  
// ------------------- IMPOSTA COME HOME PAGE 
 function SetPaginaIniziale(objLink){
    objLink.style.behavior='url(#default#homepage)';
    objLink.setHomePage(top.location.href);
  }
   