function isCorrettoCF(cf)
{
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 )
		return 0;
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			return 0;
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
		return 0;
	return 1;
}

function isCorrettoPIVA(pi)
{
	if( pi == '' )  return '';
	if( pi.length != 11 )
		return 0;
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return 0;
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
		return 0;
	return 1;
}

function openwnp()
{
a=window.open("","openwnp","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no,copyhistory=yes,width=350,height=280")
setTimeout("a.focus()",100);
}
function openwnmai()
{
a=window.open("","openwnmai","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no,copyhistory=yes,width=400,height=400")
setTimeout("a.focus()",100);
}
function openinfo()
{
a=window.open("","openinfo","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no,copyhistory=yes,width=500,height=530")
setTimeout("a.focus()",100);
}
function openprin()
{
a=window.open("","openprin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no,copyhistory=yes,width=500,height=500")
setTimeout("a.focus()",100);
}
function openprint()
{
a=window.open("","openprint","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=635,height=530")
setTimeout("a.focus()",100);
}
function openorder()
{
a=window.open("","openorder","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=518,height=500")
setTimeout("a.focus()",100);
}
function openoneclick()
{
a=window.open("","openoneclick","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=648,height=540")
setTimeout("a.focus()",100);
}

//template base_ita.tmp
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//template base_ita_registrato
function impostaSelect(cosa, valore)
{
  //dato un campo select, lo imposta a valore
  var i;

  for(i = 0; i < cosa.length; i++)
    if(cosa.options[i].text == valore)
    {
      cosa.selectedIndex = i;
      break;
    }
}
function eliminatore(cosa)
{
  document.riepilogo.codlib.value=cosa;
  document.riepilogo.elimina.value='si';
  document.riepilogo.submit();
}
  //controlla se il tipo di spedizione e' impostato
  function controllaTipoSpedizione(miaForm)
  {
    if(! miaForm.paese_sped.selectedIndex > 0)
    {
      alert("Insert Country of destination");
      return(false);
    }
    else
      return(true);
  }
  //controlla form laterali ricerca
  function controllacus()
  {
    if(document.cus.autore.value.length < 2 && document.cus.titolo.value.length < 2)
    {
      alert("Insert a minimum of 2 characters for title and author");
      return(false);
    }
    else
      return(true);
  }
  //form laterale login
  function controlla()
  {
    if(document.f.utente.value.length == 0 || document.f.password.value.length == 0)
    {
      alert("Insert Login and Password");
      return(false);
    }
    else
      return(true);
  }
//lista registrati
function DISPLAYSTATUSMSG(msgStr) { //v1.2
  status=msgStr;
  document.returnValue = true;
}
  function ritorno(finestra){
 var newwindow=window.open(finestra,"","width=350,height=300")
newwindow.creator=self
}
function loadurl(url)
{
window.creator.location=url
window.close()
}


function catalogo(finestra){
 var newwindow=window.open(finestra,"","width=400,height=500")
newwindow.creator=self
}
function loadurl(url)
{
window.creator.location=url
window.close()
}

function controllari()
  {
    ok = 0;

    if(document.ricerca.sezioni)
    {
      if(document.ricerca.sezioni[0].checked)
        ok = 1;
      if(document.ricerca.sezioni[1].checked)
        ok = 1;
      if(document.ricerca.sezioni[2].checked)
        ok = 1;
      if(document.ricerca.sezioni[3].checked)
        ok = 1;
    }
    if(document.ricerca.autore)
    if(document.ricerca.autore.value.length > 3)
      ok = 1;
    if(document.ricerca.titolo)
    if(document.ricerca.titolo.value.length > 3)
      ok = 1;
    if(document.ricerca.collana)
    if(document.ricerca.collana.value.length > 3)
      ok = 1;
    if(document.ricerca.editore)
    if(document.ricerca.editore.value.length > 3)
      ok = 1;
    if(document.ricerca.periodo)
    if(document.ricerca.periodo.selectedIndex > 0)
      ok = 1;
    if(document.ricerca.luogo)
    if(document.ricerca.luogo.selectedIndex > 0)
      ok = 1;
    if(document.ricerca.sottosoggetto)
    if(document.ricerca.sottosoggetto.selectedIndex > 0)
      ok = 1;
    if(document.ricerca.extra)
    if(document.ricerca.extra.selectedIndex > 0)
      ok = 1;
    if(document.ricerca.lingua_libro)
    if(document.ricerca.lingua_libro.selectedIndex > 0)
      ok = 1;
    if(document.ricerca.isbn)
    if(document.ricerca.isbn.value.length > 3)
      ok = 1;
    if(!ok)
    {
alert("Choose at least one criteria to complete your search");
      return(false);
    }
    else
      return(true);
  }
function apriDescrizione(argomento)
{
window.open("/cgi-bin/argomenti.cgi?argomento=" + argomento ,"apriDescrizione","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no,copyhistory=yes,width=455,height=350")
}
function aprihelp()
{
window.open("","apriDescrizione","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no,copyhistory=yes,width=455,height=350")
}

function impostaHidden(miaForm)
{
  var origine = miaForm.riepilogoTerne;
  var destinazione = miaForm.tutti;

  destinazione.value = "Luogo-|-sottosoggetto-|-Periodo\n";
  for(i=0;i<origine.length;i++)
    destinazione.value = destinazione.value + origine.options[i].text + "\n";
}

function primaRiga(tipoRiepilogo)
{
  if(tipoRiepilogo.length == 1)
  {
    if(tipoRiepilogo.options[0].text.substring(0, 3) == "---")
      return 1;
    else
      return 0;
  }
  else
    return 0;
}


function aggiungiTrio(miaForm)
{
  var destinazione = miaForm.riepilogoTerne;
  var selezionato = destinazione.selectedIndex;
  var luogo = miaForm.luogo;
  var sottosoggetto = miaForm.sottosoggetto;
  var periodo = miaForm.periodo;

  if(luogo.selectedIndex<1 &&
     sottosoggetto.selectedIndex<1 &&
     periodo.selectedIndex<1)
  {
    alert("Choose at least one item");
    return
  }
  if(primaRiga(destinazione))
    destinazione.options[0]=null;
  nuova_option = new Option(luogo.options[luogo.selectedIndex].text + "-|-" 
                           +sottosoggetto.options[sottosoggetto.selectedIndex].text+ "-|-"
                           +periodo.options[periodo.selectedIndex].text );
  destinazione.options[destinazione.length] = nuova_option;
}

function rimuoviTrio(miaForm)
{
  var origine = miaForm.riepilogoTerne;
  var scelta;
  var selezionato = origine.selectedIndex;

  if(selezionato != -1)
  {
    scelta = origine.options[selezionato].text;
    if(scelta != "")
    {
      if(!primaRiga(origine))
        origine.options[selezionato]=null;
      else
        alert(" No items have been selected");
    }
    else
      alert("No items have been selected");
  }
  else
    alert("No items have been selected");
}

function aggiungiExtra(miaForm)
{
  var destinazione = miaForm.riepilogoExtra;
  var selezionato = destinazione.selectedIndex;
  var extra = miaForm.extra;

  if(extra.selectedIndex<1)
  {
    alert("Select an extra subject");
    return
  }
  if(primaRiga(destinazione))
    destinazione.options[0]=null;
  nuova_option = new Option(extra.options[extra.selectedIndex].text);
  destinazione.options[destinazione.length] = nuova_option;
}

function rimuoviExtra(miaForm)
{
  var origine = miaForm.riepilogoExtra;
  var scelta;
  var selezionato = origine.selectedIndex;

  if(selezionato != -1)
  {
    scelta = origine.options[selezionato].text;
    if(scelta != "")
    {
      if(!primaRiga(origine))
        origine.options[selezionato]=null;
      else
        alert("No items have been selected");
    }
    else
      alert("No items have been selected");
  }
  else
    alert("No items have been selected");
}
function aggiungiLingua(miaForm)
{
  var destinazione = miaForm.riepilogoLingue;
  var selezionato = destinazione.selectedIndex;
  var lingua = miaForm.lingua;

  if(lingua.selectedIndex<1)
  {
    alert("Select a language");
    return
  }
  if(primaRiga(destinazione))
    destinazione.options[0]=null;
  nuova_option = new Option(lingua.options[lingua.selectedIndex].text);
  destinazione.options[destinazione.length] = nuova_option;
}

function rimuoviLingua(miaForm)
{
  var origine = miaForm.riepilogoLingue;
  var scelta;
  var selezionato = origine.selectedIndex;

  if(selezionato != -1)
  {
    scelta = origine.options[selezionato].text;
    if(scelta != "")
    {
      if(!primaRiga(origine))
        origine.options[selezionato]=null;
      else
        alert("No items have been selected");
    }
    else
      alert("No items have been selected");
  }
  else
    alert("No items have been selected");
}
function controllaMailingListen(form)
{
  var origine = form.riepilogoTerne;

  if(origine.length<=1)
  if(origine.length == 0 ||
     (origine.length == 1 && origine.options[0].value.charAt(0) == '-'))
  {
    alert("Select at least one");
    return(false);
  }
  if(!form.novita_arte.checked &&
     !form.novita_architettura.checked &&
     !form.fuori_commercio.checked &&
     !form.libri_50.checked &&
     !form.libri_banca.checked)
  {
    alert("Select at least one (New art, New architecture, ...)");
    return(false);
  }

  for(i=0;i<origine.length;i++)
    origine.options[i].selected = true;
  origine = form.riepilogoExtra;
  for(i=0;i<origine.length;i++)
    origine.options[i].selected = true;
  origine = form.riepilogoLingue;
  for(i=0;i<origine.length;i++)
    origine.options[i].selected = true;
  return(true);
}

function checkemail(cosa)
{
  var 
filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

  if (cosa.length == 0)
    return(false)
  else
  if (filter.test(cosa))
    return(true)
  else
    return(false)
}

function controllaDatiOrdine(form)
{
  if(form.codicefiscale_fat.value.length >0)
  if(!isCorrettoCF(form.codicefiscale_fat.value) &&
     !isCorrettoPIVA(form.codicefiscale_fat.value))
  {
    alert("Mismatched Codice Fiscale");
    form.codicefiscale_fat.focus();
    return(false);
  }
  return(true);
}

function controllaIscrizione(form)
{
  if(form.login.value.length < 4)
  {
    alert("Insert login");
    form.login.focus();
    return(false);
  }
  if(!checkemail(form.email.value))
  {
    alert("Insert email");
    form.email.focus();
    return(false);
  }
  //if(form.password.value.length < 6)
  //{
  //  alert("Insert password");
  //  form.password.focus();
  //  return(false);
  //}
  //if(form.password2.value != form.password.value)
  //{
  //  alert("Incorrect Password");
  //  form.password.focus();
  //  return(false);
  //}
  if(form.nome.value.length < 6)
  {
    alert("Insert Name");
    form.nome.focus();
    return(false);
  }
  if(form.tipo.selectedIndex)
  if(form.tipo.options[form.tipo.selectedIndex].value == 'private')
  if(form.data_nascita.value.length > 0)
  {
    if (form.data_nascita.value.substring(2,3) != "/" ||
      form.data_nascita.value.substring(5,6) != "/" ||
      isNaN(form.data_nascita.value.substring(0,2)) ||
      isNaN(form.data_nascita.value.substring(3,5)) ||
      isNaN(form.data_nascita.value.substring(6,10))) {
        alert("Insert date of birth - dd/mm/yyyy");
        form.data_nascita.focus();
        return false;
    }
    else if (form.data_nascita.value.substring(0,2) > 31) {
       alert("Wrong day of month");
       form.data_nascita.focus();
       return false;
    }
    else if (form.data_nascita.value.substring(3,5) > 12) {
       alert("Wrong month");
       form.data_nascita.focus();
       return false;
    }
    else if (form.data_nascita.value.substring(6,10) < 1800) {
       alert("Wrong year");
       form.data_nascita.focus();
       return false;
    }
    oggi = new Date()
    if(oggi.getFullYear() - parseInt(form.data_nascita.value.substring(6,10)) > 18)
    {
      //nulla
    }
    else
      if(oggi.getFullYear() - parseInt(form.data_nascita.value.substring(6,10)) == 18 &&
         oggi.getMonth() +1 > parseInt(form.data_nascita.value.substring(3,5)))
      {
        //nulla
      }
      else
        if(oggi.getFullYear() - parseInt(form.data_nascita.value.substring(6,10)) == 18 &&
           oggi.getMonth() +1 == parseInt(form.data_nascita.value.substring(3,5)) &&
           oggi.getDate() >= parseInt(form.data_nascita.value.substring(0,2)))
        {
          //nulla
        }
        else
        {
          alert("You are not of age");
          return false;
        }
  }
  if(form.indirizzo.value.length < 3)
  {
    alert("Insert Address");
    form.indirizzo.focus();
    return(false);
  }
  if(form.civico.value.length < 1)
  {
    alert("Insert Number");
    form.civico.focus();
    return(false);
  }
  if(form.cap.value.length < 4)
  {
    alert("Insert ZIP - Area code");
    form.cap.focus();
    return(false);
  }
  if(form.citta.value.length < 4)
  {
    alert("Insert City");
    form.citta.focus();
    return(false);
  }
  if(form.telefono.value.length < 4)
  {
    alert("Insert telephone number");
    form.telefono.focus();
    return(false);
  }
  if(form.paese.options[form.paese.selectedIndex].value == 'Italia' ||
     form.paese.options[form.paese.selectedIndex].value == 'Italy')
  if(form.codicefiscale.value.length == 0)
  {
    alert("insert Codice Fiscale");
    form.codicefiscale.focus();
    return(false);
  }
  if(form.codicefiscale)
  if(form.codicefiscale.value.length >0)
  if(!isCorrettoCF(form.codicefiscale.value) &&
     !isCorrettoPIVA(form.codicefiscale.value))
  {
    alert("Mismatched Codice Fiscale");
    form.codicefiscale.focus();
    return(false);
  }
  if(form.codicefiscale_fat)
  if(form.codicefiscale_fat.value.length >0)
  if(!isCorrettoCF(form.codicefiscale_fat.value) &&
     !isCorrettoPIVA(form.codicefiscale_fat.value))
  {
    alert("Mismatched Codice Fiscale");
    form.codicefiscale_fat.focus();
    return(false);
  }
  if(form.tipo.options[form.tipo.selectedIndex].value != 'privato')
  if(form.tipo.options[form.tipo.selectedIndex].value != 'private')
  if(form.paese.options[form.paese.selectedIndex].value == 'Italia' ||
     form.paese.options[form.paese.selectedIndex].value == 'Italy')
  if(form.piva.value.length == 0)
  {
    alert("insert VAT #");
    form.piva.focus();
    return(false);
  }
  if(!form.accetto_contratto.checked)
  {
    alert("To proceed with your registration it is necessary to accept the terms of contract");
    return(false);
  }
  return(true);
}

//function apriApprofondimento(cosa)
//{
 // if(cosa != 'Tutti' &&
//     cosa != 'All')
//    window.open("/cgi-bin/argomenti.cgi?lingua=en&argomento=" + cosa);
//}
function apriApprofondimento(cosa)
{
  if(cosa != 'Tutti' &&
     cosa != 'All')
  { 
    a = window.open("aaa","approf","width=40,height=50");
    a.close();
    comando = "window.open(\"/cgi-bin/argomenti.cgi?lingua=en&argomento="+cosa+"#"+cosa+"\", \"approf\", \"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=418,height=500\")"
    setTimeout(comando,100);
  }
}
function apriApprofondimentoex(cosa)
{
  if(cosa != 'Tutti' &&
     cosa != 'All')
  { 
    a = window.open("aaa","approf","width=40,height=50");
    a.close();
    comando = "window.open(\"/cgi-bin/argomenti_extra.cgi?lingua=en&argomento="+cosa+"#"+cosa+"\", \"approf\", \"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=418,height=500\")"
    setTimeout(comando,100);
  }
}

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 null;
}

function setCookie (name, value)
{
var argv = setCookie.arguments;
var argc = setCookie.arguments.length;
var expires = (2 < argc) ? argv[2] : null;
var path = (3 < argc) ? argv[3] : null;
var domain = (4 < argc) ? argv[4] : null;
var secure = (5 < argc) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
  ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
  ((path == null) ? ";path=/" : ("; path=" + path)) +
  ((domain == null) ? "" : ("; domain=" + domain)) +
  ((secure == true) ? "; secure" : "");
                                                                                                                             
}

function testCookies(chi)
{
  var cosa = "-----------------";
  SetCookie("ciaobello",cosa);
  if(GetCookie("ciaobello") != cosa)
  {
    chi.blur();
    alert("ATTENZIONE: il browser non è configurato correttamente.\nNon sarà possibile effettuare il Login e procedere all'acquisto.");
    window.open("/help_cookie_eng.html");
  }
}
function testCookieseng(chi)
{
  var cosa = "-----------------";
  SetCookie("ciaobello",cosa);
  if(GetCookie("ciaobello") != cosa)
  {
    chi.blur();
    alert("ATTENTION: set up of your browser is not correct. Cookies are not accepted.\nYou cannot see your discounts nor place orders.");
    window.open("/help_cookie_eng.html");
  }
}

function hide(the_id)
{
   var obj = (document.getElementById(the_id))? document.getElementById(the_id): document.all[the_id];
   obj.style.visibility = "hidden";
   obj.style.display = "none";
}

function show(the_id)
{
   var obj = (document.getElementById(the_id))? document.getElementById(the_id): document.all[the_id];
   obj.style.visibility = "visible";
   obj.style.display = "block";
}

function show_hide(the_id)
{
     var obj = (document.getElementById(the_id))? document.getElementById(the_id): document.all[the_id];

     if(obj.style.visibility == "hidden" ||
        obj.style.display == "none"){
          obj.style.visibility = "visible";
          obj.style.display = "block";
     } else {
          obj.style.visibility = "hidden";
          obj.style.display = "none";
     }
}

function metti_il_valore_random()
{
   for(i=0;i<document.forms.length;i++)
   {
     if(document.forms[i].random)
       document.forms[i].random.value = Math.random();
   }
}
function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
        if(!document.forms[FormName])
                return;
        var objCheckBoxes = document.forms[FormName].elements[FieldName];
        if(!objCheckBoxes)
                return;
        var countCheckBoxes = objCheckBoxes.length;
        if(!countCheckBoxes)
                objCheckBoxes.checked = CheckValue;
        else
                // set the check value for all check boxes
                for(var i = 0; i < countCheckBoxes; i++)
                        objCheckBoxes[i].checked = CheckValue;
}
var AttenderePrego_vecchio_valore;
var AttenderePrego_bottone;
function AttenderePrego(bottone)
{
   AttenderePrego_vecchio_valore = bottone.value;
   AttenderePrego_bottone = bottone;
   bottone.value='Please wait ...';
   setTimeout('AttenderePrego_bottone.value=AttenderePrego_vecchio_valore', 7000);
}
