// JavaScript Document

var MONTH_MAX_DAYS = Array(0,31,29,31,30,31,30,31,31,30,31,30,31);

function changeMaxDaysFrom()
{
var F = document.forms['adminform'];
var selectedIndex = F.frommonth.options[F.frommonth.selectedIndex].value;
var index = MONTH_MAX_DAYS[selectedIndex];
F.fromday.options.length = 0;
for(i=0 ; i < index; i++)F.fromday.options[i] = new Option(i+1,i+1);
}


function changeMaxDaysTo()
{
var F = document.forms['adminform'];
var selectedIndex = F.tomonth.options[F.tomonth.selectedIndex].value;
var index = MONTH_MAX_DAYS[selectedIndex];
F.today.options.length = 0;
for(i=0 ; i < index; i++)F.today.options[i] = new Option(i+1,i+1);
}



function y2k(number) { return (number < 1000) ? number + 1900 : number; }


function sameYear()
{
var F = document.adminform;

var year = parseInt(F.fromyear.options[F.fromyear.selectedIndex].value);

var toyear = parseInt(F.toyear.options[F.toyear.selectedIndex].value);

F.toyear.selectedIndex = F.fromyear.selectedIndex;
}


function validateBookDatas() {
//alert("Validate routine...");
	var CheckAvailability2=document.adminform;
    var startday = CheckAvailability2.fromday.options[CheckAvailability2.fromday.selectedIndex].value;
    var startmonth = CheckAvailability2.frommonth.options[CheckAvailability2.frommonth.selectedIndex].value;
    var startyear = CheckAvailability2.fromyear.options[CheckAvailability2.fromyear.selectedIndex].value;
    var endday = CheckAvailability2.today.options[CheckAvailability2.today.selectedIndex].value;
    var endmonth = CheckAvailability2.tomonth.options[CheckAvailability2.tomonth.selectedIndex].value;
    var endyear = CheckAvailability2.toyear.options[CheckAvailability2.toyear.selectedIndex].value;

    unvalidstartdate = startday + '/' + startmonth + '/' + startyear;
    unvalidenddate = endday + '/' + endmonth + '/' + endyear;

    var startdate = new Date(startyear-0,startmonth-1,startday-0);
    var enddate = new Date(endyear-0,endmonth-1,endday-0);

    starttime = Date.UTC(y2k(startdate.getYear()),startdate.getMonth(),startdate.getDate(),0,0,0);
    endtime = Date.UTC(y2k(enddate.getYear()),enddate.getMonth(),enddate.getDate(),0,0,0);
    
	
	
	var boolCondition_1 = false;
	var boolCondition_2 = false;
	
     if (starttime < endtime) {
		boolCondition_1=true;
    }
    else {
		var msg = (lang=='it') ? "Attenzione : La data di arrivo è posteriore od uguale alla data di partenza" : "Warning : Check out date is lower or equal than Check in date";
        alert(msg);
        boolCondition_1=false;
    }
    
    currentdate = new Date();
    currenttime = Date.UTC(y2k(currentdate.getYear()),(currentdate.getMonth()),currentdate.getDate(),0,0,0);
	//alert("DEBUGGING:\n\n"+y2k(currentdate.getYear())+"\n"+(currentdate.getMonth()+1)+"\n"+currentdate.getDate());
    //alert("DEBUGGING:\n\nstarttime:"+startdate+"\nendtime:"+enddate+"\ncurrentdate:"+currentdate);
    if (endtime > currenttime) {
        boolCondition_2=true;
    }
    else {
        alert('Attenzione : La data di partenza è anteriore od uguale alla data odierna');
        boolCondition_2=false;
    }
	
	
	if (starttime > currenttime) {
        boolCondition_3=true;
    }
    else {
        alert('Attenzione : La data di arrivo è anteriore od uguale alla data odierna');
        boolCondition_3=false;
    }
	
	if(boolCondition_1 && boolCondition_2 && boolCondition_3)return true;
	
    return false;
}



function hotelBooking()
{

var validation = validateBookDatas();
if(!validation)return;
//alert(cluster);
var F = document.adminform;

var year = F.fromyear.options[F.fromyear.selectedIndex].value;
//alert(year);
var month = F.frommonth.options[F.frommonth.selectedIndex].value;
var day = F.fromday.options[F.fromday.selectedIndex].value;
var toyear = F.toyear.options[F.toyear.selectedIndex].value;
//alert(year);
var tomonth = F.tomonth.options[F.tomonth.selectedIndex].value;
var today = F.today.options[F.today.selectedIndex].value;

var adults = F.adulteresa.options[F.adulteresa.selectedIndex].value;

var startdate = new Date(year-0,month-1,day-0);
var enddate = new Date(toyear-0,tomonth-1,today-0);

    starttime = Date.UTC(y2k(startdate.getYear()),startdate.getMonth(),startdate.getDate(),0,0,0);
    endtime = Date.UTC(y2k(enddate.getYear()),enddate.getMonth(),enddate.getDate(),0,0,0);
    
var ftime = startdate.getTime();
var ttime = enddate.getTime();
	var fromtimestamp =  ftime.toString().substring(0,10);
	var totimestamp = ttime.toString().substring(0,10);
	


var pattern = cluster+"&arrivalDateMonth="+month+"&arrivalDateDay="+day+"&arrivalDateYear="+year+"&departureDateMonth="+tomonth+"&departureDateDay="+today+"&departureDateYear="+toyear+"&guests="+adults+"&lang="+lang+"&fromtimestamp="+fromtimestamp+"&totimestamp="+totimestamp+"&search=1";

if(uiType==0)
{
	window.open(pattern,null,"width=500,height=300,scrollbars=yes");
}else{
var destination = 'index.php?option=com_booking&pattern='+decodeURI(pattern);
//alert(destination);
window.location = destination;
//document.getElementById("result").src = pattern;
//document.getElementById("result").setAttribute("class","result2");
//document.getElementById("result").setAttribute("className","result2");
} 
}