var source;
var tipo;
var campi = new Array();


function controlla_data()
{

var len=document.ricerca_camere.data_arrivo.value;

 
 
 
	var dt=document.ricerca_camere.data_arrivo;
	//alert (isDate(dt.value));

	if (isDate(dt.value)==false)
	{
		dt.focus()
		return false;
	}
	else
	return true;

}


function cal(val, e, t) {
	
	var dt = new Array();
	//xPos = (document.layers) ? e.pageX : ((document.all) ? event.x : e.clientX);
	//yPos = (document.layers) ? e.pageY : ((document.all) ? event.y : e.clientY);
	xPos = document.ricerca_camere.posizione_x.value;
	yPos = document.ricerca_camere.posizione_y.value;
	//alert(yPos);

	tipo = t;
	source = val;
	if (t == 0) {
		dt[0] = val[0].value;
		dt[1] = val[1].value;
		dt[2] = val[2].value;
	}
	else
		dt = val.value.split('/');
	
	showCalendar(new Date(dt[2], dt[1] - 1, dt[0]), xPos, yPos);
}


function controlla_form_ricerca()
{
 
 if (document.ricerca_camere.destinazione.value==0){
 alert ("Selezionare la destinazione.");
 return false;
 }
 if (document.ricerca_camere.numero_notti.value==0){
 alert ("Selezionare il numero di notti.");
 return false;
 }
 if (document.ricerca_camere.tipo_camera.value==0){
 alert ("Selezionare il tipo di camera.");
 return false;
 }
 if (document.ricerca_camere.numero_camere.value==0){
 alert ("Selezionare il numero di camere");
 return false;
 }
 
 if (document.ricerca_camere.data_arrivo.value==''){
 alert ("Immettere una data per effettuare la ricerca");
 return false;
 }
 
 var len=document.ricerca_camere.data_arrivo.value;
 
 
 if (len.length<10)
 {
 alert ("immettere la data nel formato gg/mm/aaaa");
 return false;
 
 }
 
 
	var dt=document.ricerca_camere.data_arrivo
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
   
 
	     
 return true;
}


var dtCh= "/";
var minYear=2006;
var maxYear=2010;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	
	
	  var data = new Date();
	  var gg, mm, aaaa;
	  gg=parseInt(data.getDate());
	  mm=parseInt(data.getMonth())+1;
	  aaaa=parseInt(data.getYear());
	  
	  if (gg<10)
	   gg='0'+gg;
	  
	  if (mm<10)
	  	mm='0'+mm; 
	   
	  
	  data_odierna=aaaa+''+mm+''+gg;
	  
	  gg=parseInt(strDay);
	  mm1=strMonth;
	  aaaa=parseInt(strYear);
	  
	  if (gg<10)
	   gg='0'+gg;
	  
	  if (mm1.length<1)
	  	mm1='0'+mm1; 
	   
	  
	  data_ricerca=aaaa+''+mm1+''+gg;
	   data_ricerca1=aaaa+''+mm1+''+strDay;
	  
	  
	  //var data_ricerca=parseInt(strYear)+parseInt(strMonth)+parseInt(strDay)
	  
	 xx1=parseInt(data_odierna);
	 xx2=parseInt(data_ricerca);
	 
	 xx3=xx2-xx1;
	 
	 //alert (xx2);
	 //alert (xx1);
	 
	 if(xx3<0)
	 {
	 //alert("error");
	 //return false;
	 }
	 	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}




function controlla_form_ricerca_en()
{
 if (document.ricerca_camere.destinazione.value==0){
 alert ("Choose destinazione.");
 return false;
 }
 if (document.ricerca_camere.numero_notti.value==0){
 alert ("Choose number of nights.");
 return false;
 }
 if (document.ricerca_camere.tipo_camera.value==0){
 alert ("Choose room type.");
 return false;
 }
 if (document.ricerca_camere.numero_camere.value==0){
 alert ("Choose number of rooms");
 return false;
 }
 
 if (document.ricerca_camere.data_arrivo.value==''){
 alert ("date is not valid");
 return false;
 }
 
 var len=document.ricerca_camere.data_arrivo.value;

 
 if (len.length<10)
 {
 alert ("date must be dd/mm/yyyy");
 return false;
 
 }
 
	var dt=document.ricerca_camere.data_arrivo
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
   
 
	     
 return true;
}
function controlla_form_ricerca_carrello()
{
 if (document.ricerca_camere.destinazione.value==0){
 alert ("Selezionare la destinazione.");
 return false;
 }
 if (document.ricerca_camere.numero_notti.value==0){
 alert ("Selezionare il numero di notti.");
 return false;
 }
 if (document.ricerca_camere.tipo_camera.value==0){
 alert ("Selezionare il tipo di camera.");
 return false;
 }
 if (document.ricerca_camere.numero_camere.value==0){
 alert ("Selezionare il numero di camere");
 return false;
 }if (document.ricerca_camere.data_arrivo.value==''){
 alert ("Immettere una data per effettuare la ricerca");
 return false;
 }
 
	var dt=document.ricerca_camere.data_arrivo
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
	var len=document.ricerca_camere.data_arrivo.value;
 
 if (len.length<10)
 {
 alert ("immettere la data nel formato gg/mm/aaaa");
 return false;
 
 }
     
 
 return carrello_pieno();
}

function controlla_form_ricerca_carrello_en()
{
 if (document.ricerca_camere.destinazione.value==0){
 alert ("Choose destinazione.");
 return false;
 }
 if (document.ricerca_camere.numero_notti.value==0){
 alert ("Choose number of nights.");
 return false;
 }
 if (document.ricerca_camere.tipo_camera.value==0){
 alert ("Choose room type.");
 return false;
 }
 if (document.ricerca_camere.numero_camere.value==0){
 alert ("Choose number of rooms");
 return false;
 }
 if (document.ricerca_camere.data_arrivo.value==''){
 alert ("Insert the date to search");
 return false;
 }
 
	var dt=document.ricerca_camere.data_arrivo
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
	var len=document.ricerca_camere.data_arrivo.value;
 
 
 if (len.length<10)
 {
 alert ("date must be dd/mm/yyyy");
 return false;
 
 }
   
      
 return carrello_pieno_en();
}


function carrello_pieno() {
    
    var a=0;
	if (document.ricerca_camere.where.value=1)
    dest =  document.getElementById("destinazione1").value;
	else
	dest =  document.getElementById("destinazione2").value;
	

    if(dest=="ROM")
    a=a+1
    else if(dest=="MIL")
    a=a+1
    else if(dest=="FIR")
    a=a+1
    else if(dest=="FRO")
    a=a+1
    else if(dest=="LAI")
    a=a+1
    else if(dest=="PIS")
    a=a+1
    else if(dest=="SAL")
    a=a+1
    else if(dest=="SIE")
    a=a+1
    else if(dest=="VEN")
    a=a+1
    else if(dest=="VIT")
    a=a+1
    else if(dest=="NAP")
    a=a+1
    if (a==0)
    {
    alert("Per poter prenotare per la destinazione scelta �necessario completare l'ordine in corso oppure svuotare il carrello."); 
    return false;
    }
    else
    return true;
}

function carrello_pieno_en() {
    
     var a=0;
	if (document.ricerca_camere.where.value=1)
    dest =  document.getElementById("destinazione1").value;
	else
	dest =  document.getElementById("destinazione2").value;
	
    if(dest=="ROM")
    a=a+1
    else if(dest=="MIL")
    a=a+1
    else if(dest=="FIR")
    a=a+1
    else if(dest=="FRO")
    a=a+1
    else if(dest=="LAI")
    a=a+1
    else if(dest=="PIS")
    a=a+1
    else if(dest=="SAL")
    a=a+1
    else if(dest=="SIE")
    a=a+1
    else if(dest=="VEN")
    a=a+1
    else if(dest=="VIT")
    a=a+1
    else if(dest=="NAP")
    a=a+1
    if (a==0)
    {
    alert("testo in inglese");  
    return false;
    }
    else
    return true;
}



 function PopupCentrata2(indice) {
   var w = 585;
   var h = 630;
   var l = Math.floor((screen.width-w)/2);
   var t = Math.floor((screen.height-h)/2);
      window.open('scheda_hotel_lm.asp?codice='+indice,"","width=" + w + ",height=" + h + ",top=" + t + ",left=" + l+",scrollbars=yes");
 }

function controlla_flag() {

        if (document.ricerca_camere.flag.value == 0) {
            document.ricerca_camere.flag.value = 1;
            Mostra();
        }
        else if (document.ricerca_camere.flag.value == 1) {
            document.ricerca_camere.flag.value = 0;
            Nascondi();
        }
}

function Mostra() {
        
        if (document.getElementById) {
                document.getElementById("tendina").style.visibility="visible";
        } else if (document.all) {
                document.all.tendina.style.visibility="visible";
        }
}

function Nascondi() {
        if (document.getElementById) {
                document.getElementById("tendina").style.visibility="visible";
        } else if (document.all) {
                document.all.tendina.style.visibility="visible";
        }
}

function nascondi_destinazione1() {
	document.getElementById("destinazione2").style.visibility="hidden";
	document.getElementById("destinazione1").style.visibility="visible";
	document.getElementById("destinazione2").disabled=true;
	document.getElementById("destinazione1").disabled=false;
}

function nascondi_destinazione2() {
	document.getElementById("destinazione1").style.visibility="hidden";
	document.getElementById("destinazione2").style.visibility="visible";
	document.getElementById("destinazione2").disabled=false;
	document.getElementById("destinazione1").disabled=true;
}

function nascondi_destinazione1_d() {
	document.getElementById("destinazione2_d").style.visibility="hidden";
	document.getElementById("destinazione1_d").style.visibility="visible";
	document.getElementById("destinazione2_d").disabled=true;
	document.getElementById("destinazione1_d").disabled=false;
}

function nascondi_destinazione2_d() {
	document.getElementById("destinazione1_d").style.visibility="hidden";
	document.getElementById("destinazione2_d").style.visibility="visible";
	document.getElementById("destinazione2_d").disabled=false;
	document.getElementById("destinazione1_d").disabled=true;
}

