function docnum(doc){
  if(doc=='documento1'){
    $('patentenumero').disabled=false;
    $('cartaidnumero').value='';
    $('cartaidnumero').disabled=true;
  }else{
    $('cartaidnumero').disabled=false;
    $('patentenumero').value='';
    $('patentenumero').disabled=true;
  }
}
function readprivacy(){
  if ($('testoPrivacy').style.display == 'none') { 
    new Effect.Appear('testoPrivacy', { duration: 0.5 });  
  }else{
    new Effect.Fade('testoPrivacy', { duration: 0.5 });  
  }
}
function shoescursioni(){
  if (($('tipo1').checked == true)&&($('escursioni').style.display != 'none')) { 
    new Effect.Fade('escursioni', { duration: 0.5 });  
  }else{
    new Effect.Appear('escursioni', { duration: 0.5 });  
  }
}
function altri(){
  var sistemazione;
  var sistemazione1 = $('sistemazione1');
  //var sistemazione2a = $('sistemazione2a');
  var sistemazione2b = $('sistemazione2b');
  var sistemazione3 = $('sistemazione3');
  var sistemazione4 = $('sistemazione4');
    
  if(sistemazione1.checked==true){
    sistemazione = $('sistemazione1').value;
    new Effect.Fade('secondapersona', { duration: 0.5 });  
    new Effect.Fade('terzapersona', { duration: 0.5 });
  //}else if(sistemazione2a.checked==true){
  //  sistemazione = $('sistemazione2a').value;
  //  new Effect.Fade('secondapersona', { duration: 0.5 });  
  //  new Effect.Fade('terzapersona', { duration: 0.5 });
  }else if(sistemazione2b.checked==true){
    sistemazione = $('sistemazione2b').value;
    new Effect.Appear('secondapersona', { duration: 0.5 });  
    new Effect.Fade('terzapersona', { duration: 0.5 });
  }else if(sistemazione3.checked==true){
    sistemazione = $('sistemazione3').value;
    new Effect.Appear('secondapersona', { duration: 0.5 });  
    new Effect.Fade('terzapersona', { duration: 0.5 });
  }else if(sistemazione4.checked==true){
    sistemazione = $('sistemazione4').value;
    new Effect.Appear('secondapersona', { duration: 0.5 });  
    new Effect.Appear('terzapersona', { duration: 0.5 });
  }
  //alert(sistemazione);
}

/************************/

// form validation function //
function validate(form) {
  var regione = $('regione').value;
  var provincia = $('provincia').value;
  var proloco = $('proloco').value;
  var citta1 = $('citta1').value;
  //var tipo = $('tipo').value;
  var nome = $('nome').value;
  var cognome = $('cognome').value;
  var luogonascita = $('luogonascita').value;
  var datanascita = $('datanascita').value;
  var codfisc = $('codfisc').value;
  var indirizzo = $('indirizzo').value;
  var cap = $('cap').value;
  var citta = $('citta').value;
  var provincia2 = $('provincia2').value;
  var telefono = $('telefono').value;
  var email = $('email').value;

  //var fax = $('fax').value;
  //var cellulare = $('cellulare').value;
  //var email = $('email').value;
  var documento1 = $('documento1');
  var documento2 = $('documento2');
  var patentenumero = $('patentenumero').value;
  var cartaidnumero = $('cartaidnumero').value;
  var gpartenza = $('gpartenza').value;
  var garrivo = $('garrivo').value;
  var orapartenza = $('orapartenza').value;
  var oraarrivo = $('oraarrivo').value;

  var sistemazione1 = $('sistemazione1');
  //var sistemazione2a = $('sistemazione2a');
  var sistemazione2b = $('sistemazione2b');
  var sistemazione3 = $('sistemazione3');
  var sistemazione4 = $('sistemazione4');
  var nomepersona2 = $('nomepersona2').value;
  var nomepersona3 = $('nomepersona3').value;
  
  //var persona2tipo1 = $('persona2tipo1').value;
  //var persona2tipo2 = $('persona2tipo2').value;
  //var persona2tipo3 = $('persona2tipo3').value;
  //if($('persona2tipo1').checked==true){var persona2tipo = persona2tipo1;}
  //if($('persona2tipo2').checked==true){var persona2tipo = persona2tipo2;}
  //if($('persona2tipo3').checked==true){var persona2tipo = persona2tipo3;}
  
  //var persona3tipo1 = $('persona3tipo1').value;
  //var persona3tipo2 = $('persona3tipo2').value;
  //if($('persona3tipo1').checked==true){var persona3tipo = persona3tipo1;}
  //if($('persona3tipo2').checked==true){var persona3tipo = persona3tipo2;}
  
  var mezzo1 = $('mezzo1');
  var mezzo2 = $('mezzo2');
  var mezzo3 = $('mezzo3');
  var mezzo4 = $('mezzo4');
  var mezzo5 = $('mezzo5');

  var user = $('user').value;  
  var passwd = $('passwd').value;
  var passwd2 = $('passwd2').value;
  var privacy = $('privacy');
  //var email = $('email').value;
  //var checkSet1 = $('checkSet1').value;
  //var message = $('message').value;
  //var textareainput = $('textareainput').value;
  var telefonoRegex = telefono.replace(/[\(\)\.\-\ ]/g, '');
  var formatodata = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  //var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  
  /* le select vengono sostituite da un elemento il cui id è "sareaX" dove X è un num progressivo da 0 a n che dipende dalla posizione della select nel codice html (la prima select è "sarea0" etc.)*/
  /* su explorer non funzia!*/
  if(regione == "") {
    inlineMsg('regione','Per favore indica la Regione.',5);
    return false;
  }  
  if(provincia == "") {
    inlineMsg('provincia','Per favore indica la Provincia.',5);
    return false;
  }  
  if(proloco == "" || proloco == "Pro Loco") {
    inlineMsg('proloco','Per favore indica la Pro Loco.',5);
    return false;
  }
  if(citta1 == "") {
    inlineMsg('citta1','Per favore indica la citta\'.',5);
    return false;
  }
  if(cognome == "") {
    inlineMsg('cognome','Per favore indica il cognome.',5);
    return false;
  }
  if(nome == "") {
    inlineMsg('nome','Per favore indica il nome.',5);
    return false;
  }
  if(luogonascita == "") {
    inlineMsg('luogonascita','Per favore indica il luogo di nascita.',5);
    return false;
  }
  if(datanascita == "") {
    inlineMsg('datanascita','Per favore indica la data di nascita.',5);
    return false;
  }
  if(codfisc == "") {
    inlineMsg('codfisc','Per favore inserisci il tuo codice fiscale.',5);
    return false;
  }
  if(indirizzo == "") {
    inlineMsg('indirizzo','Per favore indica l\'indirizzo.',5);
    return false;
  }
  if(cap == "") {
    inlineMsg('cap','Per favore indica il cap.',5);
    return false;
  }
  if(citta == "") {
    inlineMsg('citta','Per favore indica la citta\'.',5);
    return false;
  }
  if(provincia2 == "") {
    inlineMsg('provincia2','Per favore indica la provincia.',5);
    return false;
  } 
  if (isNaN(parseInt(telefonoRegex))) {
      inlineMsg('telefono','Il numero di telefono contiene caratteri non validi.',5);
      return false;
  }  
  //if(email == "") {
  //  inlineMsg('email','Per favore inserisci l\'indirizzo email.',5);
  //  return false;
  //} 
  if(email != ""){
    if(!email.match(emailRegex)) {
      inlineMsg('email','Formato email non valido.',2);
      return false;
    }  
  }
  /*
  if((documento1.checked == false)&&(documento2.checked == false)) {
    inlineMsg('cartaidnumero','Nessun documento specificato.',2);
    return false;
  }
  if((documento1.checked == true)&&(patentenumero == "")) {
    inlineMsg('patentenumero','Per favore specifica il numero della patente.',2);
    return false;
  }
  if((documento2.checked == true)&&(cartaidnumero == "")) {
    inlineMsg('cartaidnumero','Per favore specifica il numero della carta di identita\'.',2);
    return false;
  }
  if((documento1.checked == true)&&(patentenumero != "")) {
    patcheck(patentenumero);
  }  
  if((documento2.checked == true)&&(cartaidnumero != "")) {
    cfcheck(cartaidnumero);
  }    
  */
  if (garrivo == "") {
      inlineMsg('garrivo','Inserire la data di arrivo.',5);
      return false;
  }  
  if (!formatodata.test(garrivo)) {
      inlineMsg('garrivo','Inserire la data nel formato gg/mm/aaaa.',5);
      return false;
  }    
  //if (oraarrivo == "") {
  //    inlineMsg('oraarrivo','Inserire l\'ora di arrivo.',5);
  //    return false;
  //}  
  if (gpartenza == "") {
      inlineMsg('gpartenza','Inserire la data di partenza.',5);
      return false;
  }  
  //if (orapartenza == "") {
  //    inlineMsg('orapartenza','Inserire l\'ora della partenza.',5);
  //    return false;
  //}  
  if (!formatodata.test(gpartenza)) {
      inlineMsg('gpartenza','Inserire la data nel formato gg/mm/aaaa.',5);
      return false;
  }    
  dateDiff(); 
  //if(((sistemazione2b.checked==true)||(sistemazione3.checked==true)||(sistemazione4.checked==true))&&(nomepersona2=="")){
  //    $('secondapersona').show();
  //    inlineMsg('nomepersona2','Indicare il nome della seconda persona.',5);
  //    return false;
  //}
  //if((sistemazione4.checked==true)&&(nomepersona3=="")){
   //   $('terzapersona').show();
   //   inlineMsg('nomepersona3','Indicare il nome della terza persona.',5);
   //   return false;
  //}  
  if(user == "") {
    inlineMsg('user','Per favore specifica la username.',5);
    return false;
  }else{
    usercheck(user);  
  }
      
  if(passwd == "") {
    inlineMsg('passwd','Per favore specifica la password.',5);
    return false;
  } 
  if(passwd2 == "") {
    inlineMsg('passwd2','Per favore conferma la password.',5);
    return false;
  } 
  if(passwd != passwd2) {
    inlineMsg('passwd2','Le password non coincidono.',5);
    return false;
  } 
  if (privacy.checked==false) {
      inlineMsg('privacy','E\' necessario acconsentire al trattamento dei dati personali.',5);
      return false;
  }  
/*
  if(message.match(messageRegex)) {
    inlineMsg('message','You have entered an invalid message.');
    return false;
  }
*/
  //return true;
  //calcolaTotale();
  //alert('invia!');
  Modalbox.show('riepilogo.html', {title: 'Scheda di prenotazione', width: 600, height: 650});
}

/* modulo di contatti */
// form validation function //
function validateContatti() {

  var nome = $('nome').value;
  var cognome = $('cognome').value;
  var proloco=$('proloco').value;
  var email=$('email').value;
  var messaggio=$('messaggio').value;
  var regione=$('regione').value;
  var provincia=$('provincia').value;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

  if(nome == "") {
    inlineMsg('nome','Per favore indica il nome.',4);
    return false;
  }
  if(cognome == "") {
    inlineMsg('cognome','Per favore indica il cognome.',4);
    return false;
  }
  if(email == "") {
    inlineMsg('email','Per favore indica il tuo indirizzo email.',4);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','Formato email non valido.',2);
    return false;
  }  
  if(messaggio == "") {
    inlineMsg('messaggio','Nessun messaggio specificato.',4);
    return false;
  }

  return true;
}


// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.select();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  ScrollTo(target);
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}
