// JScript File

function DATE_date_object()
{
	var myDate = new Date();
	this.arr_yearmonth	= self.document.aspnetForm.Arrival_yearmonth;
	this.arr_day		= self.document.aspnetForm.Arrival_day;
	this.dep_yearmonth	= self.document.aspnetForm.Departure_yearmonth;
	this.dep_day		= self.document.aspnetForm.Departure_day;
	//Now getting current date and time from the client rather than the browser
	this.start_month	= myDate.getMonth() + 1; 
	this.start_day		= myDate.getDate(); 
	this.start_year		= myDate.getFullYear(); 
}

function DATE_date_object_Special()
{
	var myDate = new Date();
	this.arr_yearmonth	= document.getElementById('SpecialArrival_yearmonth');
	this.arr_day		= document.getElementById('SpecialArrival_day');
	this.dep_yearmonth	= document.getElementById('SpecialDeparture_yearmonth');
	this.dep_day		= document.getElementById('SpecialDeparture_day');
	//Now getting current date and time from the client rather than the browser
	this.start_month	= myDate.getMonth() + 1; 
	this.start_day		= myDate.getDate(); 
	this.start_year		= myDate.getFullYear(); 
}

function DATE_date_objectByDate(Arrival_yearmonth,Arrival_day,Departure_yearmonth,Departure_day)
{
	var myDate = new Date();
	this.arr_yearmonth	= Arrival_yearmonth;
	this.arr_day		= Arrival_day;
	this.dep_yearmonth	= Departure_yearmonth;
	this.dep_day		= Departure_day;
	//Now getting current date and time from the client rather than the browser
	this.start_month	= myDate.getMonth() + 1; 
	this.start_day		= myDate.getDate(); 
	this.start_year		= myDate.getFullYear(); 
}

// -------------------------------------------------------------------------
// Function	  : DATE_set_date()
// Description : This function is called by the onchange method in the  
// Arrival_Date selects. The logic is procedural which means that the 
// order of the code should not be change without deep considerations.
// -------------------------------------------------------------------------

function DATE_set_date()
{

	var dob = new DATE_date_object();
	//Main Rule: dep > arr >= start
	
	//Assert dimmed day is not selected
	if(dob.arr_day.options[dob.arr_day.selectedIndex].value == "")
	{
	    dob.arr_day.options[DATE_get_number_of_days_in_month(getYMYear(dob.arr_yearmonth), getYMMonth(dob.arr_yearmonth)) - 1].selected = true;
	}
	if(dob.dep_day.options[dob.dep_day.selectedIndex].value == "")
	{
	    dob.dep_day.options[DATE_get_number_of_days_in_month(getYMYear(dob.dep_yearmonth), getYMMonth(dob.dep_yearmonth)) - 1].selected = true;
	}
	
	//Assert arr_year += 1, if arr_year == start_year and arr_month > start_month
	if(getYMYear(dob.arr_yearmonth) == dob.start_year && 
		getYMMonth(dob.arr_yearmonth) < dob.start_month - 1)
	{
        setYearMonth(dob.arr_yearmonth,(getYMYear(dob.arr_yearmonth)*1)+1, getYMMonth(dob.arr_yearmonth));
	}
	
	//Assert arr_day == start_day, if arr_year == start_year and arr_month == start_month and arr_day < start_day
	if(getYMYear(dob.arr_yearmonth) == dob.start_year && 
		getYMMonth(dob.arr_yearmonth) == dob.start_month &&
		dob.arr_day.options[dob.arr_day.selectedIndex].value < dob.start_day )
	{
		
		dob.arr_day.options[dob.start_day - 1].selected = true;
	}
	
	//Assert dep_year == arr_year, if dep_year < arr_year 
	if(getYMYear(dob.dep_yearmonth) < getYMYear(dob.arr_yearmonth))
	{
        setYearMonth(dob.dep_yearmonth,getYMYear(dob.arr_yearmonth),getYMMonth(dob.arr_yearmonth));
	} 

	//Assert dep_month == arr_month, if dep_year == arr_year and dep_month < arr_month
	if(getYMYear(dob.dep_yearmonth) == getYMYear(dob.arr_yearmonth) &&
		getYMMonth(dob.dep_yearmonth) < getYMMonth(dob.arr_yearmonth) ) 
	{
	    setYearMonth(dob.dep_yearmonth,getYMYear(dob.dep_yearmonth),getYMMonth(dob.arr_yearmonth));
	} 

	//Assert dep_day == arr_day + 1, if dep_year == arr_year and dep_month == arr_month and dep_day <= arr_day
	if(getYMYear(dob.dep_yearmonth) == getYMYear(dob.arr_yearmonth) &&
		getYMMonth(dob.dep_yearmonth) == getYMMonth(dob.arr_yearmonth) &&
		dob.dep_day.selectedIndex <= dob.arr_day.selectedIndex)
	{
		//Set dep_day == arr_day, if arr_day + 1 < DATE_get_number_of_days_in_month(y, m)
		if(dob.arr_day.options[dob.arr_day.selectedIndex].value < DATE_get_number_of_days_in_month(getYMYear(dob.arr_yearmonth), getYMMonth(dob.arr_yearmonth))) {
			dob.dep_day.options[dob.arr_day.selectedIndex + 1].selected = true;
  		} else if(dob.arr_yearmonth.selectedIndex == dob.arr_yearmonth.length - 1) {
            //Set dep == max date if max date is reached
			dob.dep_yearmonth.options[dob.arr_yearmonth.length - 1].selected = true;
			dob.dep_day.options[DATE_get_number_of_days_in_month(getYMYear(dob.arr_yearmonth), getYMMonth(dob.arr_yearmonth))-1].selected = true;
		} else {
			//Set dep_yeat == arr_year + 1 and dep_month == 0 and dep_day == 0
			dob.dep_yearmonth.options[dob.arr_yearmonth.selectedIndex + 1].selected = true;
			dob.dep_day.options[0].selected = true;
		}
	}
	
	DATE_set_days(dob.arr_yearmonth, dob.arr_day); 
	DATE_set_days(dob.dep_yearmonth, dob.dep_day);
	
    var arr_yearmonth	= document.getElementById('SpecialArrival_yearmonth');
	var arr_day		    = document.getElementById('SpecialArrival_day');
	var dep_yearmonth	= document.getElementById('SpecialDeparture_yearmonth');
	var dep_day		    = document.getElementById('SpecialDeparture_day');
	
	if(arr_yearmonth!=null)
	    arr_yearmonth.options[dob.arr_yearmonth.selectedIndex].selected = true;
	if(arr_day!=null)
	    arr_day.options[dob.arr_day.selectedIndex].selected = true;
	if(dep_yearmonth!=null)
	    dep_yearmonth.options[dob.dep_yearmonth.selectedIndex].selected = true;
	if(dep_day!=null)
	    dep_day.options[dob.dep_day.selectedIndex].selected = true;	 	
}

function DATE_set_date_Special()
{
	var dob = new DATE_date_object_Special();
	//Main Rule: dep > arr >= start
	
	//Assert dimmed day is not selected
	if(dob.arr_day.options[dob.arr_day.selectedIndex].value == "")
	{
	    dob.arr_day.options[DATE_get_number_of_days_in_month(getYMYear(dob.arr_yearmonth), getYMMonth(dob.arr_yearmonth)) - 1].selected = true;
	}
	if(dob.dep_day.options[dob.dep_day.selectedIndex].value == "")
	{
	    dob.dep_day.options[DATE_get_number_of_days_in_month(getYMYear(dob.dep_yearmonth), getYMMonth(dob.arr_yearmonth)) - 1].selected = true;
	}
	
	//Assert arr_month == start_month, if arr_year == start_year and arr_month > start_month 
	if(getYMYear(dob.arr_yearmonth) == dob.start_year && 
		getYMMonth(dob.arr_yearmonth) < dob.start_month - 1)
	{
	    setYearMonth(dob.arr_yearmonth,(getYMYear(dob.arr_yearmonth)*1)+1,getYMMonth(dob.arr_yearmonth));
	}
	
	//Assert arr_day == start_day, if arr_year == start_year and arr_month == start_month and arr_day < start_day
	if(getYMYear(dob.arr_yearmonth) == dob.start_year && 
		getYMMonth(dob.arr_yearmonth)  == dob.start_month &&
		dob.arr_day.options[dob.arr_day.selectedIndex].value < dob.start_day )
	{
		
		dob.arr_day.options[dob.start_day - 1].selected = true;
	}
	
	//Assert dep_year == arr_year, if dep_year < arr_year 
	if(getYMYear(dob.dep_yearmonth) < getYMYear(dob.arr_yearmonth))
	{
	    setYearMonth(dob.dep_yearmonth,getYMYear(dob.arr_yearmonth),getYMMonth(dob.arr_yearmonth));
	} 

	//Assert dep_month == arr_month, if dep_year == arr_year and dep_month < arr_month
	if(getYMYear(dob.dep_yearmonth) == getYMYear(dob.arr_yearmonth) &&
		getYMMonth(dob.dep_yearmonth) < getYMMonth(dob.arr_yearmonth) ) 
	{
	    setYearMonth(dob.dep_yearmonth,getYMYear(dob.dep_yearmonth),getYMMonth(dob.arr_yearmonth));
	} 
	//Assert dep_day == arr_day + 1, if dep_year == arr_year and dep_month == arr_month and dep_day <= arr_day
	if(getYMYear(dob.dep_yearmonth) == getYMYear(dob.arr_yearmonth) &&
		getYMMonth(dob.dep_yearmonth) == getYMMonth(dob.arr_yearmonth) &&
		dob.dep_day.selectedIndex <= dob.arr_day.selectedIndex)
	{
		//Set dep_day == arr_day, if arr_day + 1 < DATE_get_number_of_days_in_month(y, m)
		
		if(dob.arr_day.options[dob.arr_day.selectedIndex].value < DATE_get_number_of_days_in_month(getYMYear(dob.arr_yearmonth), getYMMonth(dob.arr_yearmonth)))
		{
			dob.dep_day.options[dob.arr_day.selectedIndex + 1].selected = true;
			//Set dep == max date if max date is reached
		} else if(dob.arr_yearmonth.selectedIndex == dob.arr_yearmonth.length - 1) {
		    dob.dep_yearmonth.options[dob.arr_year.length - 1].selected = true;
			dob.dep_day.options[30].selected = true;
			//Set dep_yeat == arr_year + 1 and dep_month == 0 and dep_day == 0
		} else {
            setYearMonth(dob.dep_yearmonth,getYMYear(dob.arr_yearmonth)+1,"01");
            dob.dep_day.options[0].selected = true;
		}
	}
	
	DATE_set_days(dob.arr_yearmonth, dob.arr_day);
	DATE_set_days(dob.dep_yearmonth, dob.dep_day);
}

function DATE_set_date_Arrival()
{
    DATE_set_date();
}

function DATE_set_date_Departure()
{
    DATE_set_date();
    DATE_set_date_SpecialDeparture();
}

function DATE_set_date_SpecialArrival()
{
    DATE_set_date_Special();
}

function DATE_set_date_SpecialDeparture()
{
    DATE_set_date_Special();
}

function DATE_adjust_dates(ArrivalCalendar, DepartureCalendar)
{   
    //TODO: CHANGE HERE THE getElementById as a generic function as controls may move content holders   
	arr = document.getElementById(ArrivalCalendar);
	dep = document.getElementById(DepartureCalendar);
		
	if(arr.alt!='')	
        setDateValue(document.forms['aspnetForm'], 'ArrivalDate', arr);
	if(dep.alt!='')	
        setDateValue(document.forms['aspnetForm'], 'DepartureDate', dep);        

    DATE_set_date();
    
    return;
}


// -------------------------------------------------------------------------
// Function	  : DATE_get_number_of_days_in_month(y, m)
// Description : Returns the correct number of days. 
// -------------------------------------------------------------------------

function DATE_get_number_of_days_in_month(y, m)
{
	if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { return 31; }
	else if (m == 4 || m == 6 || m == 9 || m == 11) { return 30; }
	else if (m == 2) 
	{
	    if (y % 4 == 0 || y % 100 == 0)	
	    { 
	        return 29; 
	    }
		else
		{ 
		    return 28; 
		}
	}
}

// -------------------------------------------------------------------------
// Function	  : DATE_set_days(s_yearmonth, s_day)
// Description : Show/hide day 28-31 if nessesary. 
// -------------------------------------------------------------------------

function DATE_set_days(s_yearmonth, s_day)
{

	var dimdate = DATE_get_number_of_days_in_month(getYMYear(s_yearmonth), getYMMonth(s_yearmonth))
	if(s_day.options[s_day.selectedIndex].value > dimdate || s_day.options[s_day.selectedIndex].value == "" )
	{
		s_day.options[dimdate - 1].selected = true;		
	}
	for(d = 27; d < s_day.options.length; d++)
	{
		if(d >= dimdate)
		{
			s_day.options[d].value = "";
			s_day.options[d].text = "";
		}
		else
		{
			s_day.options[d].value = d + 1;
			s_day.options[d].text = d + 1;
		}
	}
}
 
function setDateValue(frm, flag, imageObj)
{
    var Arrival_Date;
	var Departure_Date;
	var ArrDate;
	var DepDate;
    var availForm;

    if (flag == "ArrivalDate") 
    { 		
			Arrival_Date = imageObj.alt;
	  	    ArrDate = new Date(Arrival_Date);

			for (var i = 0; i < frm.Arrival_day.length; i++) {
				if (frm.Arrival_day[i].value == ArrDate.getDate()) {
					frm.Arrival_day.selectedIndex = i;
				}
			}
			for (var i = 0; i < frm.Arrival_yearmonth.length; i++) {
				if (frm.Arrival_yearmonth[i].value == ""+ArrDate.getFullYear()+((ArrDate.getMonth()+1<10)?"0":"")+(ArrDate.getMonth()+1)) {
					frm.Arrival_yearmonth.selectedIndex = i;
				}
			}
	}
			
	if (flag  == "DepartureDate") 
	{
		    Departure_Date = imageObj.alt;
	  	    DepDate = new Date(Departure_Date);

			for (var i = 0; i < frm.Departure_day.length; i++) {
				if (frm.Departure_day[i].value == DepDate.getDate()) {
					frm.Departure_day.selectedIndex = i;
				}
			}
			for (var i = 0; i < frm.Departure_yearmonth.length; i++) {
				if (frm.Departure_yearmonth[i].value == ""+DepDate.getFullYear()+((DepDate.getMonth()+1<10)?"0":"")+(DepDate.getMonth()+1)) {
					frm.Departure_yearmonth.selectedIndex = i;
				}
			}
    }
		 
}

// Returns the year value from the yearmonth dropdown
function getYMYear(yearmonth) {
    return yearmonth.options[yearmonth.selectedIndex].value.substr(0,4);
}

// Returns the month value from the yearmonth dropdown
function getYMMonth(yearmonth) {
    return yearmonth.options[yearmonth.selectedIndex].value.substr(4,2);
}
function setYearMonth(sel, year, month) {
    var ym = year+""+month;
    if (month < 10) {
        month = "0"+month;
    }
    
    for (var i = 0; i < sel.options.length; i++) {
        if (sel.options[i].value == ym) {
            sel.options[i].selected = true;
        }
    }
}
