// <!--

// Routines comuni

// --------------------- 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 '';
}


// --------------------- DETERMINA BROWSER ------------------------
function browser() {
var brw; 
if (document.layers) brw="ns4"; 
else if (document.getElementById){ 
   if (document.all) { 
      str=document.lastModified.substring(0,1); 
      if (str=="0" || str=="1" || str=="2") brw="iex"; else brw="opera"; 
    } 
    else if (navigator.appName=="Netscape") brw="ns6"; 
    else brw="opera"; 
  } 
  if (navigator.appName=="Konqueror")    brw="kon";
  return brw;
}



// --------------------- CONTROLLA VALIDITA EMAIL ------------------------
function checkemail(str,messaggio){
  var testresults;
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (!messaggio) {messaggio="Inserire un indirizzo di email valido !"}
 
  if (filter.test(str))
    testresults=true
  else{
    alert(messaggio);
    testresults=false
  }
  return (testresults)
}


// ------------------- GESTIONE LIVELLI MULTIBROWSER
// var isNetscape=(navigator.appName=="Netscape");

// var isNetscape=(navigator.appName.indexOf("Netscape") >= 0 && parseFloat(navigator.appVersion) >= 4) ? 1 : 0;

var isNetscape=(browser()=='ns4')

var layerRef="";
var styleRef="";

if (isNetscape){
  layerRef=".layers";
  styleRef="";
}
else {
  layerRef=".all";
  styleRef=".style";
}

var LenCampi="22";
var TagFont="";
if (isNetscape){
  // TagFont="<font face=verdana size=1 color=navy>";
  LenCampi="15";
  }

function livello(id,stato,testo){
  if (isNetscape) stato=(stato=="hidden") ? "hide" : (stato=="visible") ? "show" : stato;
  if (!stato){
     var v=eval("document"+layerRef+"[\'"+id+"\']"+styleRef+".visibility");
     stato=(v=="hidden" || !v)?"visible":"hidden";
   } 
//  eval("document"+layerRef+"[\'"+id+"\']"+styleRef+".visibility=\'"+stato+"\'");
  eval("document.getElementById(\'"+id+"\')"+styleRef+".visibility=\'"+stato+"\'");
  if (testo!=null){
    if (isNetscape) {
     eval("document."+id+".document.write(testo)");
     eval("document."+id+".document.close()");
     }
    else {
     eval("document.getElementById('"+id+"').innerHTML=testo");
     }
   }
}




// ------------------- 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;
}



// ------------------- FINESTRA POPUP
function  finestra(strUrl,larghezza,altezza,barre){
      if (!larghezza){ larghezza=650;altezza=650; barre=1;}
      if(screen.width<larghezza){larghezza=screen.width}winNuova=window.open(strUrl,"finestra","resizable=1,toolbar=0,directories=0,menubar=0,scrollbars="+barre+",width="+larghezza+",height="+altezza+",left=10,top=10");
      winNuova.focus();
      return false;
  }




// ------------------- GESTIONE CAMPO SELECT
function Add2Select (fldSelect,optName,optValue) {
  var t=fldSelect.options.length;     
  var lastoption = new Option();
  fldSelect.options[t]=lastoption;
  fldSelect.options[t].text=optName;
  if (optValue!=""){fldSelect.options[t].value=optValue;}
}

function AzzeraSelect(fldSelect) {
   var t2=fldSelect.length;
   for (var i=0;i<t2;i++){fldSelect.options[0]=null; }
}




// ------------------- FORMATTAZIONE VALORE 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;
};






// --------------------- ROUTINES DI GESTIONE COOKIE ------------------------

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();
}


// --------------------- GESTIONE VALORI FORM ------------------------

var CampiDaSalvare;
var CampiDaNonSalvare;
var TipiCampoDaNonSalvare='hidden,button,submit,reset';


function carica (theForm){
   var l;

   var campi=theForm.elements;
 
   for (l=0;l<campi.length;l++) { 
       var campo=campi[l];
       var tipo=campo.type.toLowerCase();
       var valore=GetCookie(campi[l].name);
       
       sw=true;
           
       if (TipiCampoDaNonSalvare.indexOf(campo.type.toLowerCase())!=-1) {
             sw=false
             }
       else if (CampiDaSalvare) {
             if (!esiste_in_array(CampiDaSalvare.split(","),campo.name)) {sw=false}
             }
       else if (CampiDaNonSalvare) {
             if (esiste_in_array(CampiDaNonSalvare.split(","),campo.name)) {sw=false}
             }
       
       if (sw) {
         // alert(campo.name+"\n"+CampiDaNonSalvare);
         if (tipo == "text") { campo.value=valore }
         else if (tipo == "checkbox" && valore==campo.value) { campo.checked=true }
         else if (tipo == "select-one") { campo.value=valore }
         }
       
     }
}


function salva (theForm){
   // 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=theForm.elements;
   
   for (l=0;l<campi.length;l++) 
       {  sw=true;
           
          if (TipiCampoDaNonSalvare.indexOf(campi[l].type.toLowerCase())!=-1) {
             sw=false
             }
          else if (CampiDaSalvare) {
             if (!esiste_in_array(CampiDaSalvare.split(","),campi[l].name)) {sw=false}
             }
          else if (CampiDaNonSalvare) {
             if (esiste_in_array(CampiDaNonSalvare.split(","),campi[l].name)) {sw=false}
             }
            
          if (sw){
               SetCookie(campi[l].name,EstraeValoreCampo(campi[l]),expdate);   
             }
       }
       
}



function EstraeValoreCampo(campo) {
  var tipo=campo.type.toLowerCase();
  var valore=campo.value;
  
  var v="";
  if (tipo == "text") { v=valore }
  else if (tipo == "checkbox" && campo.checked) { v=valore }
  else if (tipo == "select-one") { v=campo[campo.selectedIndex].value }
  
  // alert(v);
  
  return v;
}


function elimina (theForm){
     var campi=theForm.elements;
     for (l=0;l<campi.length;l++) { DeleteCookie(campi[l].name) }
}


function esiste_in_array(arr,valore) {
  for (i=0;i<arr.length;i++){
      if (arr[i]==valore){return true}
    }
  return false;    
}



// --------------------- 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 = "";  
  }
}

// ------------------- 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>');
}

// -->
