function controlloRegistrazione()
{
 with(document.formRegistrazione)
 {

   
   if(pIva.value.length > 20)
   {
     alert("Errore: inserire una PARTITA IVA corretta");
     pIva.focus();
     return false;
   }
   
   if(isVuoto(codiceFiscale.value))
   {
     alert("Errore: compilare il campo CODICE FISCALE");
     codiceFiscale.focus();
     return false;
   }
   
   if(codiceFiscale.value.length > 20 || codiceFiscale.value.length < 5)
   {
     alert("Errore: inserire un CODICE FISCALE corretto");
     codiceFiscale.focus();
     return false;
   }
   
   if(isVuoto(email.value))
   {
     alert("Errore: compilare il campo E-MAIL");
     email.focus();
     return false;
   }
   
   if(isVuoto(pass.value))
   {
     alert("Errore: compilare il campo PASSWORD");
     pass.focus();
     return false;
   }

   if(isVuoto(indirizzo.value))
   {
     alert("Errore: compilare il campo INDIRIZZO");
     indirizzo.focus();
     return false;
   } 
   
   if(isVuoto(cap.value))
   {
     alert("Errore: compilare il campo CAP");
     cap.focus();
     return false;
   }
   
   if(cap.value.length > 7)
   {
     alert("Errore: inserire un CAP corretto");
     cap.focus();
     return false;
   }

   if(isVuoto(localita.value))
   {
     alert("Errore: compilare il campo LOCALITA'");
     localita.focus();
     return false;
   }
   
   if(isVuoto(provincia.options[provincia.selectedIndex].value))
   {
     alert("Errore: selezionare una PROVINCIA");
     provincia.focus();
     return false;
   }
   
   if(isVuoto(telefono.value))
   {
     alert("Errore: compilare il campo TELEFONO");
     telefono.focus();
     return false;
   }
   
   if(telefono.value.length > 15)
   {
     alert("Errore: inserire un TELEFONO corretto");
     telefono.focus();
     return false;
   }

   if(emailFalsa(email.value))
   {
     alert("Errore: Hai inserito un'email non valida");
     email.focus();
     return false;
   }
   
   if(confrontaStringa(email.value,confermaEmail.value))
   {
    alert("Le due email non coincidono");
    confermaEmail.focus();
    return false;
   }

   if(controllaSpazi(pass.value))
   {
    alert("Non inserire spazi nella password");
    pass.focus();
    return false;
   }
   
   if(controllaLunghezzaPassword(pass.value))
   {
    alert("La password deve contenere almeno 6 caratteri!");
    pass.focus();
    return false;
   }
   
   if(confrontaStringa(pass.value,ripetiPass.value))
   {
    alert("La password non coincide con quella di conferma");
    ripetiPass.focus();
    return false;
   }
   
   if(!document.getElementById('listini').checked)
   {
    alert ("Devi richiedere l'accesso ai listini per l'operatore del settore");
    document.getElementById('listini').focus();
    return false;
   }
   
   if(!document.getElementById('informativa').checked)
   {
    alert ("Devi acconsentire al trattamento dei dati personali");
    document.getElementById('informativa').focus();
    return false;
   }
   
   
   
 }
 return true;
}


function controlloContattaci()
{
 with(document.contattaci)
 {

   if(isVuoto(nome.value))
   {
     alert("Errore: compilare il campo NOME");
     nome.focus();
     return false;
   }
   
   if(isVuoto(cognome.value))
   {
     alert("Errore: compilare il campo COGNOME");
     cognome.focus();
     return false;
   }
   
   if(isVuoto(email.value))
   {
     alert("Errore: compilare il campo E-MAIL");
     email.focus();
     return false;
   }

   if(emailFalsa(email.value))
   {
     alert("Errore: Hai inserito un'email non valida");
     email.focus();
     return false;
   }
   
   if(isVuoto(messaggio.value))
   {
     alert("Errore: inserisci un MESSAGGIO");
     messaggio.focus();
     return false;
   }
   
   
   
 }
 return true;
}


function isVuoto(stringa)
{
 return (stringa == "" || stringa == "undefined" || stringa.charAt(0) == " ");
}


function controllaSpazi(stringa)
{
 
 for(i=0;i<stringa.length;i++)
 {
   if(stringa[i]==" ")
   {
     return true;
   }
 }
 return false;
}




function controlloLogin()
{
 if(document.getElementById("login_username").value=="")
 {
  alert("Inserisci la Username");
  return false;
 }
 else if(document.getElementById("login_password").value=="")
 {
  alert("Inserisci la Password");
  return false;
 }
 else return true;
}

function emailFalsa (email)
{
 contPunti = 0;
 contChiocciole = 0;
 for (i=0; i<email.length; i++)
 {
  if (email.charAt(i) == '@') contChiocciole++;
  if (email.charAt(i) == '.') contPunti++;
 }
 if (contChiocciole == 0 || contChiocciole > 1 || contPunti == 0) // email falsa
 return true;
 else return false;
}

function controllaLunghezzaPassword(pass)
{
  return !(pass.length>5);
}

function confrontaStringa(str1,str2)
{
 return !(str1==str2) ;
}

function apri (link)
{

  var larghezza = 400;
  var altezza = 280;
  var w = screen.width;
  var h = screen.height;
  var x = Math.round(w / 2) - Math.round(larghezza / 2);
  var y = Math.round(h / 2) - Math.round(altezza / 2);
  var windowprops = "width="+larghezza+", height="+altezza+", left = "+x+", screenX = "+x+", top = "+y+", screenY = "+y+", scrollbars=no, menubar = no, status=no,resizable=no,location=no";
  popup = window.open(link,'remote',windowprops);

}

function apri2 (link)
{

  var larghezza = 580;
  var altezza = 350;
  var w = screen.width;
  var h = screen.height;
  var x = Math.round(w / 2) - Math.round(larghezza / 2);
  var y = Math.round(h / 2) - Math.round(altezza / 2);
  var windowprops = "width="+larghezza+", height="+altezza+", left = "+x+", screenX = "+x+", top = "+y+", screenY = "+y+", scrollbars=yes, menubar = no, status=no,resizable=no,location=no";
  popup = window.open(link,'remote',windowprops);

}

function apriPopupCrociere (alt,largh)
{
  var larghezza = largh;
  var altezza = alt;
  var w = screen.width;
  var h = screen.height;
  var x = Math.round(w / 2) - Math.round(larghezza / 2);
  var y = Math.round(h / 2) - Math.round(altezza / 2);
  var windowprops = "width="+larghezza+", height="+altezza+", left = "+x+", screenX = "+x+", top = "+y+", screenY = "+y+", scrollbars=yes, menubar = no, status=no,resizable=no,location=no";
  popup = window.open('http://www.CruisEngine.com/BookingEngine/partners/it-IT/Step1a?Key=z%2fnhEfuWPNhUHjsVIpOD3w%3d%3d','remote',windowprops);

}

function apriPopupAlba (alt,largh)
{
  var larghezza = largh;
  var altezza = alt;
  var w = screen.width;
  var h = screen.height;
  var x = Math.round(w / 2) - Math.round(larghezza / 2);
  var y = Math.round(h / 2) - Math.round(altezza / 2);
  var windowprops = "width="+larghezza+", height="+altezza+", left = "+x+", screenX = "+x+", top = "+y+", screenY = "+y+", scrollbars=yes, menubar = no, status=no,resizable=no,location=no";
  popup = window.open('http://b2c.towers.it/?cstz=pagilo','remote',windowprops);

}

// Per il calendario

var oldLink = null;
function setActiveStyleSheet(link, title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (oldLink) oldLink.style.fontWeight = 'normal';
  oldLink = link;
  link.style.fontWeight = 'bold';
  return false;
}

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    cal.callCloseHandler();
}

function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}

function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}

function showFlatCalendar() {
  var parent = document.getElementById("display");

  var cal = new Calendar(0, null, flatSelected);

  cal.weekNumbers = false;

  cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("%A, %B %e");

  cal.create(parent);

  cal.show();
}

