/*
This file is for any javascript related to the main dates.asp page.
Currently just changes the return date to be later than the departure date and not on a holiday.

Author: Sam Vincent
Date: April 27 2007
*/

var tempstartdate;
var tempenddate;
var milliseconds = 1000 * 24 * 60 * 60;
var quoteEnd = new Date(2009,11,31);

//Function to make sure a date chosen isnt a holiday
function IsHoliday(d, text){
	var error = "";
	
//Holiday 2006
	var memorialDay06 = new Date(2006,04,29); //may 29
	var independenceDay06 = new Date(2006,6,4); //july 4
	var labourDay06 = new Date(2006,8,4); //sept 4
	var thanksgiving06 = new Date(2006,10,23); //nov 23
	var christmasDay06 = new Date(2006,11,25); //dec 25
	var newYears06 = new Date(2006,0,1); // jan 1
	
//Holidays 2007
	var memorialDay07 = new Date(2007,04,28);
	var independenceDay07 = new Date(2007,06,04);
	var labourDay07 = new Date(2007,08,03);
	var thanksgiving07 = new Date(2007,10,22);
	var christmasDay07 = new Date(2007,11,25);
	var newYears07 = new Date(2008,0,1);
	
	if((d - memorialDay06)/milliseconds == 0){
		error = "The depot is closed on Memorial Day. Please change the " + text + " Date.\n";
	}
	else if((d - independenceDay06)/milliseconds == 0){
		error = "The depot is closed on Independence Day. Please change the " + text + " Date.\n";
	}
	else if((d - labourDay06)/milliseconds == 0){
		error = "The depot is closed on Labour Day. Please change the " + text + " Date.\n";
	}
	else if((d - thanksgiving06)/milliseconds == 0){
		error = "The depot is closed on Thanksgiving Day. Please change the " + text + " Date.\n";
	}
	else if((d - christmasDay06)/milliseconds == 0){
		error = "The depot is closed on Christmas Day. Please change the " + text + " Date.\n";
	}
	else if((d - newYears06)/milliseconds == 0){
		error = "The depot is closed on New Years Day. Please change the " + text + " Date.\n";
	}
	else if((d - memorialDay07)/milliseconds == 0){
		error = "The depot is closed on Memorial Day. Please change the " + text + " Date.\n";
	}
	else if((d - independenceDay07)/milliseconds == 0){
		error = "The depot is closed on Independence Day. Please change the " + text + " Date.\n";
	}
	else if((d - labourDay07)/milliseconds == 0){
		error = "The depot is closed on Labour Day. Please change the " + text + " Date.\n";
	}
	else if((d - thanksgiving07)/milliseconds == 0){
		error = "The depot is closed on Thanksgiving Day. Please change the " + text + " Date.\n";
	}
	else if((d - christmasDay07)/milliseconds == 0){
		error = "The depot is closed on Christmas Day. Please change the " + text + " Date.\n";
	}
	else if((d - newYears07)/milliseconds == 0){
		error = "The depot is closed on New Years Day. Please change the " + text + " Date.\n";
	}
	else{
	error = "";
	}
	return error;
}

//Function to change the End date once the Start Date has been changed. If the start date comes after
//the end date then the end date will be changed to have the correct number of days added to it
function changeDate(){
	var d1 = new Date(2005,5,27);
	var d2 = new Date(2005,8,3);
	var dayincrease = 0;
	var tempstartdate = new Date(document.quote1_at.startdate.value);
	var tempenddate = new Date(document.quote1_at.enddate.value);
	var year = tempstartdate.getFullYear();
	var febdays
	var start = tempstartdate.getDate();
	var month = tempstartdate.getMonth();
	var newMonth = month;
	var newYear = year;
	var error = ""
	
	if ((year %4 == 0) && (year%100 != 0)){
		febdays = 29;
	}
	else{
		febdays = 28;
	}
	var numdays = new Array(31,febdays,31,30,31,30,31,31,30,31,30,31);

	if (((tempstartdate - d1)/milliseconds >= 0) && ((tempstartdate - d2)/milliseconds <= 0)){
		dayincrease = 7;
	}
	else{
		dayincrease = 3;
	}
	var number = (start + dayincrease) - numdays[month];
	if ((tempstartdate - tempenddate)/milliseconds > 0){
		if (number <= 0){
			tempenddate.setMonth(tempstartdate.getMonth());
			tempenddate.setDate(tempstartdate.getDate() + dayincrease);
			tempenddate.setFullYear(tempstartdate.getFullYear());
		}
		else{
			if (month == 11){
				newMonth = 0;
				newYear = newYear + 1;
			}
			else{
				newMonth = newMonth + 1;
			}
			tempenddate.setMonth(newMonth);
			tempenddate.setDate(0 + number);
			tempenddate.setFullYear(newYear);
		}
	}
	else{
		if ((tempenddate-tempstartdate)/milliseconds < dayincrease){
			if (number <= 0){
				tempenddate.setMonth(tempstartdate.getMonth());
				tempenddate.setDate(tempstartdate.getDate() + dayincrease);
				tempenddate.setFullYear(tempstartdate.getFullYear());
			}
			else{
				if (month == 11){
					newMonth = 0;
					newYear = newYear + 1;
				}
				else{
					newMonth = newMonth + 1;
				}
				tempenddate.setMonth(newMonth);
				tempenddate.setDate(0 + number);
				tempenddate.setFullYear(newYear);
			}
		}
	}
	error += IsHoliday(tempenddate,"Return");
	while (error != ""){
		if (error != ""){
			start = tempenddate.getDate();
			month = tempenddate.getMonth();
			number = (start + 1) - numdays[month];
			if (number <= 0){
				tempenddate.setMonth(tempenddate.getMonth());
				tempenddate.setDate(tempenddate.getDate() + 1);
				tempenddate.setFullYear(tempenddate.getFullYear());
			}
			else{
				if (month == 11){
					newMonth = 0;
					newYear = newYear + 1;
				}
				else{
					newMonth = newMonth + 1;
				}
				tempenddate.setMonth(newMonth);
				tempenddate.setDate(0 + number);
				tempenddate.setFullYear(newYear);
			}
		}
		error = IsHoliday(tempenddate,"Return");
	}

	document.quote1_at.enddate.value = (tempenddate.getMonth() + 1) + "/" + tempenddate.getDate() + "/" + tempenddate.getFullYear();
	RetCalendar.setCurrentMonth(tempenddate.getMonth());
	RetCalendar.setCurrentYear(tempenddate.getFullYear());
}

//function to set focus to the StartDate field, so that the change Date function can be triggered to change the end date
function setTheDate(){
	document.quote1_at.startdate.focus();
}