//set todays date
Now = new Date();
NowDay = Now.getDate();
NowMonth = Now.getMonth();
NowYear = Now.getYear();

// change below for number of days
SecondDate = DateAdd(Now,8,0,0);
SecondDay = SecondDate.getDate();
SecondMonth = SecondDate.getMonth();
SecondYear = SecondDate.getYear();

//set now to two days time date
Now =  DateAdd(Now,1,0,0);
NowDay = Now.getDate();
NowMonth = Now.getMonth();
NowYear = Now.getYear();


function changeLocation(LocationVar)
{
	dropoffLocationObject = eval("document.quoteForm.dropoffLocation");

	if(dropoffLocationObject.options.selectedIndex == 0 )
	{
		for(i=0; i < dropoffLocationObject.length; i++)
		{
			if(dropoffLocationObject.options[i].index == LocationVar)
			{
				dropoffLocationObject[i].selected = true;
			}
		}
	}
}

function checkreq(form)
{
	setCookie(form);

	var errors = "";

	if (document.quoteForm.pickupLocation.value == 'Select Location')
	{
		errors = errors + "Please select a pickup location.\n";
	}

	if (document.quoteForm.dropoffLocation.value == 'Select Location')
	{
		errors = errors + "Please select a drop off location.\n";
	}

	var pickupTimeArr = document.quoteForm.pickupTime.value.split(':');
	pickupStamp = eval("document.quoteForm.pickupYear.value+document.quoteForm.pickupMonth.value+document.quoteForm.pickupDay.value+pickupTimeArr[0]+pickupTimeArr[1]");

	var dropoffTimeArr = document.quoteForm.dropoffTime.value.split(':');
	dropoffStamp = eval("document.quoteForm.dropoffYear.value+document.quoteForm.dropoffMonth.value+document.quoteForm.dropoffDay.value+dropoffTimeArr[0]+dropoffTimeArr[1]");

	if(dropoffStamp <= pickupStamp)
	{
		errors = errors + "Your Drop off date must be after your Pick up date.\n";
	}

	if (errors)
	{
		alert(errors);
		return false;
	}
	else
	{
		return true;
	}

}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function setCookie(form)
{
	var today = new Date();
	var expires_date = new Date(today.getFullYear(), today.getMonth(), today.getDate()+1);
	document.cookie = "quote="+document.quoteForm.pickupLocation.value+"#"+document.quoteForm.pickupDay.value+"#"+document.quoteForm.pickupMonth.value+"#"+document.quoteForm.pickupYear.value+"#"+document.quoteForm.pickupTime.value+"#"+document.quoteForm.dropoffLocation.value+"#"+document.quoteForm.dropoffDay.value+"#"+document.quoteForm.dropoffMonth.value+"#"+document.quoteForm.dropoffYear.value+"#"+document.quoteForm.dropoffTime.value+"; expires="+expires_date.toGMTString()+"; path=/"
}


function DateAdd(startDate, numDays, numMonths, numYears)
{
	var returnDate = new Date(startDate.getTime());
	var yearsToAdd = numYears;
	var month = returnDate.getMonth() + numMonths;
	
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}

	returnDate.setMonth(month);	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	return returnDate;
}

function DaysInMonth(WhichMonth, WhichYear)
{
	var DaysInMonth = 31;
	if (WhichMonth == "Apr" || WhichMonth == "Jun" || WhichMonth == "Sep" || WhichMonth == "Nov") DaysInMonth = 30;
	if (WhichMonth == "Feb" && (WhichYear/4) != Math.floor(WhichYear/4))	DaysInMonth = 28;
	if (WhichMonth == "Feb" && (WhichYear/4) == Math.floor(WhichYear/4))	DaysInMonth = 29;
	return DaysInMonth;
}

function ChangeOptionDays(Which)
{
	DaysObject = eval("document.quoteForm." + Which + "Day");
	MonthObject = eval("document.quoteForm." + Which + "Month");
	YearObject = eval("document.quoteForm." + Which + "Year");

	Month = MonthObject[MonthObject.selectedIndex].text;
	Year = YearObject[YearObject.selectedIndex].text;

	DaysForThisSelection = DaysInMonth(Month, Year);
	CurrentDaysInSelection = DaysObject.length;
	
	if (CurrentDaysInSelection > DaysForThisSelection)
	{
		for (icounter=0; icounter<(CurrentDaysInSelection-DaysForThisSelection); icounter++)
		{
			DaysObject.options[DaysObject.options.length - 1] = null
		}
	}
	
	if (DaysForThisSelection > CurrentDaysInSelection)
	{
		for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++)
		{
			NewOption = new Option(DaysObject.options.length + 1);
			DaysObject.options[DaysObject.options.length]=NewOption;
		}
	}

	if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}

//function to set options to today
window.onload=function SetToToday(Which)
{
	var x = readCookie('quote')
	if (x)
	{
		x = unescape(x);
		var quoteArray = x.split('#');

		NowDay = quoteArray[1];
		NowMonth = (quoteArray[2]-1);
		NowYear = quoteArray[3];
		NowTime = quoteArray[4];

		SecondDay = quoteArray[6];
		SecondMonth = (quoteArray[7]-1);
		SecondYear = quoteArray[8];
		SecondTime = quoteArray[9];

		pickupLocationObject = eval("document.quoteForm.pickupLocation");
		for(i=0; i< pickupLocationObject.length; i++)
		{
			if( (pickupLocationObject.options[i].value) == quoteArray[0])
			{
				pickupLocationObject[i].selected = true;
			}
		}

		pickupTimeObject = eval("document.quoteForm.pickupTime");
		for(i=0; i< pickupTimeObject.length; i++)
		{
			if( (pickupTimeObject.options[i].value) == quoteArray[4])
			{
				pickupTimeObject[i].selected = true;
			}
		}

		dropoffLocationObject = eval("document.quoteForm.dropoffLocation");
		for(i=0; i< dropoffLocationObject.length; i++)
		{
			if( (dropoffLocationObject.options[i].value) == quoteArray[5])
			{
				dropoffLocationObject[i].selected = true;
			}
		}

		dropoffTimeObject = eval("document.quoteForm.dropoffTime");
		for(i=0; i< dropoffTimeObject.length; i++)
		{
			if( (dropoffTimeObject.options[i].value) == quoteArray[4])
			{
				dropoffTimeObject[i].selected = true;
			}
		}

	}


	DaysObject = eval("document.quoteForm.pickupDay");
	MonthObject = eval("document.quoteForm.pickupMonth");
	YearObject = eval("document.quoteForm.pickupYear");

	for(i=0; i< YearObject.length; i++)
	{
		if( (YearObject.options[i].text) == NowYear)
		{
			YearObject[i].selected = true;
		}
	}

	MonthObject[NowMonth].selected = true;

	Which = "pickup";
	ChangeOptionDays(Which);

	DaysObject[NowDay-1].selected = true;

	Which = "dropoff";
	ChangeOptionDays(Which);

	DaysObjectTwo = eval("document.quoteForm.dropoffDay");
	MonthObjectTwo = eval("document.quoteForm.dropoffMonth");
	YearObjectTwo = eval("document.quoteForm.dropoffYear");

	for(i=0; i< YearObjectTwo.length; i++)
	{
		if( (YearObjectTwo.options[i].text) == SecondYear)
		{
			YearObjectTwo[i].selected = true;
		}
	}

	MonthObjectTwo[SecondMonth].selected = true;
	DaysObjectTwo[SecondDay-1].selected = true;
}