/* Funzioni JS specifiche per www.teatroanfitrione.it */

function f_show_canceled(pObjectId, e) {
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
  Xpos=e.clientX+5;
  Ypos=e.clientY+5+st;
  myCanc=document.getElementById(pObjectId);
  myCanc.style.display='block';
  myCanc.style.left=Xpos+"px";
  myCanc.style.top=Ypos+"px";
}

function f_hide_canceled(pObjectId) {
	myCanc=document.getElementById(pObjectId);
  myCanc.style.display='none';
}


/* Specifiche per sezione "Bill" mode=preview */
var timeout;

function f_show() {
	clearTimeout(timeout);

	container=document.getElementById('select_bill_list');
	container.style.display='block';
}

function f_hide() {
	clearTimeout(timeout);
	document.getElementById('select_bill_list').style.display='none';
}

function f_check_reservation() {

  //Disabilita il pulsante Submit per evitare prenotazioni multiple
  document.getElementById('b_submit').disabled=true;

  //Alias degli elementi della form
  var t_accept_cond = document.frm_reservations.t_accept_cond; 
  var t_cod_show = document.frm_reservations.t_cod_show; 
  var t_cod_event = document.frm_reservations.t_cod_event; 
  var t_email = document.frm_reservations.t_email; 
  var t_tel_pref1 = document.frm_reservations.t_tel_pref1; 
  var t_tel_numb1 = document.frm_reservations.t_tel_numb1; 
  var t_tel_pref2 = document.frm_reservations.t_tel_pref2; 
  var t_tel_numb2 = document.frm_reservations.t_tel_numb2; 
  var t_nome = document.frm_reservations.t_nome;
  var tel_check=true;

  if (!t_accept_cond.checked) {
    alert("E' necessario accettare le condizioni di prenotazione."); 
    document.getElementById('b_submit').disabled=false;
    return false; 
  }
  
  if (t_cod_show.value==null || t_cod_show.value=="") { 
    alert("Selezionare uno spettacolo."); 
    document.getElementById('b_submit').disabled=false;
    return false; 
  } 

  if (t_cod_event.value==null || t_cod_event.value=="") { 
    alert("Selezionare una data."); 
    document.getElementById('b_submit').disabled=false;
    return false; 
  } 
  
  name_check=new String(t_nome.value); 
  if (name_check.length<3) { 
    alert("Campo Cognome obbligatorio. Immettere almeno 3 caratteri."); 
    document.getElementById('b_submit').disabled=false;
    return false; 
  }
  
  if (!f_check_mail(t_email.value)) { 
    alert("Immettere un indirizzo e-mail valido."); 
    t_email.focus(); 
    document.getElementById('b_submit').disabled=false;
    return false; 
  } 

  if ((t_tel_pref1.value==null || t_tel_pref1.value=='')
    &&(t_tel_numb1.value==null || t_tel_numb1.value=='')
    &&(t_tel_pref2.value==null || t_tel_pref2.value=='')
    &&(t_tel_numb2.value==null || t_tel_numb2.value=='')) {
      alert("Immettere almeno un contatto telefonico.");
      document.getElementById('b_submit').disabled=false;
      return false;
  }
  
  tel_check=f_check_tel(t_tel_pref1.value, t_tel_numb1.value, t_tel_pref2.value, t_tel_numb2.value);

  if (!tel_check) {
    alert("Immettere sia il prefisso che il numero: sono consentiti solo caratteri numerici.");
    document.getElementById('b_submit').disabled=false;
  	return false;
  }

  return true; 

}

function f_check_tel(p_tel_pref1, p_tel_numb1, p_tel_pref2, p_tel_numb2) {
  var ret=true;
  var pref1=new String(p_tel_pref1); 
  var numb1=new String(p_tel_numb1);
  var pref2=new String(p_tel_pref2); 
  var numb2=new String(p_tel_numb2);
  
  if (pref1!=null && pref1!='') {
  	if (pref1.length!=3) {
  	  ret=false;
  	}
  	if (!f_is_numeric(pref1)) {
      ret=false;
    }
    if (numb1==null || numb1=='') {
      ret=false;
    } else {
    	if (!f_is_numeric(numb1)) {
    	  ret=false;
    	}
    	if (numb1.length<3) {
    	  ret=false;
    	}
    }
  } else {
    if (numb1!=null && numb1!='') {
      ret=false;
    }  	
  }
  
  if (pref2!=null && pref2!='') {
  	if (pref2.length<2 || pref2.length>4) {
  	  ret=false;
  	}
  	if (!f_is_numeric(pref2)) {
      ret=false;
    }
    if (numb2==null || numb2=='') {
      ret=false;
    } else {
    	if (!f_is_numeric(numb2)) {
    	  ret=false;
    	}
    	if (numb2.length<3) {
    	  ret=false;
    	}
    }
  } else {
    if (numb2!=null && numb2!='') {
      ret=false;
    }  	
  }
  
  return ret;
}


function f_is_numeric(p_string) {
  var strValidChars = "0123456789"; 
  var ret=true;

  for (i = 0; i < p_string.length; i++) { 
    strChar = p_string.charAt(i); 
    if (strValidChars.indexOf(strChar) == -1) { 
      ret=false; 
    }
  }
  return ret; 
}