var stayLength;
today = new Date();
currDay = today.getDate();
currMonth = today.getMonth();
currYear = today.getYear();

// first correct the behaviour of this microshit only script
if (currYear < 200){
	currYear = currYear + 1900;
	}


function set_defaults() {

    //var today = new Date();
    var thisYear = today.getFullYear();
	outDay = document.searchform.day_out.selectedIndex;
	outMonth = document.searchform.month_out.selectedIndex;
	outYear = document.searchform.year_out.selectedIndex + thisYear;
	//populateDays(currMonth,currYear);
	//document.searchform.month_in.selectedIndex = currMonth;
	//document.searchform.year_in.selectedIndex = currYear - thisYear;
	

	getInDay();
	getOutDay(outDay,outMonth,outYear);
	//setCheckOutDate(thisYear);
}

function changeDates(){

    var today = new Date();
    var thisYear = today.getFullYear();

	currDay = document.searchform.day_in.selectedIndex - 1;
	currMonth = document.searchform.month_in.selectedIndex;
	currYear = document.searchform.year_in.selectedIndex + thisYear;

	populateDays(currMonth,currYear);
	getInDay();
	setCheckOutDate(thisYear);

}

function populateDays(month,year){

	objForm = document.searchform;
	nDays = getNumberOfDays(month,year);

	for (var i = 0; i < objForm.day_in.length; i++) {
		objForm.day_in.options[i] = null;
	}
	for (var i = 0; i < nDays; i++) {
		objForm.day_in.options[i] = new Option(i+1);
	}
	objForm.day_in.selectedIndex = currDay + 1;
}

function populateCoDays(month,year){

	objForm = document.searchform;
	nDays = getNumberOfDays(month,year);

	for (var i = 0; i < objForm.day_in.length; i++) {
		objForm.day_out.options[i] = null;
	}
	for (var i = 0; i < nDays; i++) {
		objForm.day_out.options[i] = new Option(i+1);
	}
}

function setCheckOutDate(thisYear){

	//objForm = document.check_out; commented this line out as it doesnt seem to do anything?
	stayLength = document.searchform.nights.selectedIndex - 1;
	inDay = document.searchform.day_in.selectedIndex + 1;
	inMonth = document.searchform.month_in.selectedIndex;
	inYear = document.searchform.year_in.selectedIndex + thisYear;

	nDays = getNumberOfDays(inMonth,inYear);
	nxtmnthsndays = getNextMonthsnDays(inMonth,inYear);

	if(inDay+stayLength>nDays+nxtmnthsndays){

		outMonth = inMonth+2;
		if(outMonth>11){
			outYear = inYear + 1;
			outMonth = 0;
		}
		else{
			outYear = inYear;
		}
		over = (nDays + nxtmnthsndays) - inDay;
		outDay = stayLength - over;
	}
	else if(inDay+stayLength>nDays){

		outMonth = inMonth+1;
		if(outMonth>11){
			outYear = inYear + 1;
			outMonth = 0;
		}
		else{
			outYear = inYear;
		}
		over = nDays - inDay;
		outDay = stayLength - over;
	}
	else{
		outDay = inDay+stayLength;
		outMonth = inMonth;
		outYear = inYear;
	}

	var strMonth = getMonth(outMonth);

	populateCoDays(outMonth,thisYear);

	document.searchform.day_out.selectedIndex = outDay - 1;
	document.searchform.month_out.selectedIndex = outMonth;
	document.searchform.year_out.selectedIndex = outYear - thisYear;

	getOutDay(outDay,outMonth,outYear);

}

/*
function getMonth(outMonth){

	var Months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	return Months[outMonth];

}
*/

function getNumberOfDays(month,year){
    if((month == 3) || (month == 5) || (month == 8) || (month == 10)) nDays = 30;
    else if ( month == 1 )
        (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0)) ? nDays=29 : nDays=28;
    else nDays = 31;
	return nDays;
}

function getNextMonthsnDays(theInMonth,theInYear){
	if (theInMonth=11){
		nextMonth = 0;
		nextMonthsYear = theInYear + 1;
	}
	else{
		nextMonth = theInMonth + 1;
		nextMonthsYear = theInYear;
	}

    if((nextMonth == 3) || (nextMonth == 5) || (nextMonth == 8) || (nextMonth == 10)) nxtmnthsndays = 30;
    else if ( nextMonth == 1 )
        (((nextMonthsYear%4 == 0) && (nextMonthsYear%100 != 0)) || (nextMonthsYear%400 == 0)) ? nxtmnthsndays=29 : nxtmnthsndays=28;
    else nxtmnthsndays = 31;
	return nxtmnthsndays;
}

function changeNumNights(){

	var thisYear = today.getFullYear();

	inDay = document.searchform.day_in.selectedIndex + 1;
	inMonth = document.searchform.month_in.selectedIndex;
	inYear = document.searchform.year_in.selectedIndex + thisYear;

	outDay = document.searchform.day_out.selectedIndex + 1;
	outMonth = document.searchform.month_out.selectedIndex;
	outYear = document.searchform.year_out.selectedIndex + thisYear;

	CheckoutDay = new Date(outYear,outMonth,outDay);
	CheckinDay = new Date(inYear,inMonth,inDay);

	difference = CheckoutDay - CheckinDay;

	numberofnights = Math.round(difference/(1000*60*60*24));


/*
	if (numberofnights == 0) {
		document.searchform.nights.selectedIndex = '1';
	}
*/
	if (numberofnights < 0) {
		document.searchform.nights.selectedIndex = '0';
	}
	else if (numberofnights > 40) {
		document.searchform.nights.selectedIndex = '42';
	}
	else {
		document.searchform.nights.selectedIndex = numberofnights + 1;
	}

	getOutDay(outDay,outMonth,outYear);
}

function populateNumNightsDropDown(month,year){

	objForm = document.searchform;
	nDays = getNumberOfDays(month,year);

	for (var i = 0; i < objForm.day.length; i++) {
		objForm.day_out.options[i] = null;
	}
	for (var i = 0; i < nDays; i++) {
		objForm.day_out.options[i] = new Option(i+1);
	}
}

function checkDatesAreValid(msg){

	var selectBox = document.searchform.nights;
	var numnights = selectBox.options[selectBox.selectedIndex].value;

	// Check if query text has been entered
	if( !ValidateSearchForm(document.searchform))
	{
	    return false;
	}

	//if checkin date is before checkout
	if (numnights == '-'){
		alert(msg);
		return false;
	}

	//if numnights more than 40
	if (numnights == '+'){
		alert(msg);
		return false;
	}

	return true;
}

function ValidateSearchForm(form)
{
    if(IsEmpty(form.query))
    {
      form.query.focus();
      return false;
    }
    return true;
}

function IsEmpty(aTextField)
{
   if ((aTextField.value.length==0) || (aTextField.value==null))
   {
      return true;
   }
   else
   {
      return false;
   }
}