/*  	D. E. Simmons
		29 Oct 20007
		Easy Dispatch, Inc.
		Online Reservations
*/

/******************* Function Definitions **********************/
function initializeDate() {
	startYear = getEarliest('y');
	startMonth = getEarliest('mon');
	startDate = getEarliest('d');
	startHour = getEarliest('h');			
	startMinute = getEarliest('min');
	startAMPM = 'AM';
	startDateTime.setFullYear(startYear);
	startDateTime.setMonth(startMonth);
	startDateTime.setDate(startDate);
	startDateTime.setHours(startHour);
	startDateTime.setMinutes(startMinute);
	startDateTime.setSeconds(0);
	var comboYear = document.resForm.cmbYear; // objects cached for speed
	var comboHours = document.resForm.cmbHour;
	var comboMinutes = document.resForm.cmbMin;
	var comboAMPM = document.resForm.cmbAMPM;
	var textMonth = document.resForm.txtMonth;
	
	// set Year
	comboYear.options[0] = new Option(startYear, startYear);
	if(startMonth > 9) {
		comboYear.options[1] = new Option(startYear + 1, startYear + 1);
	}
	// set Month, Date, Day
	if(startMonth > 8) {
		maxMonth = 11;
	}
	else {
		maxMonth = startMonth + 2;
	}
	fillInit();
	fillHours();
	fillMinutes();
	fillAMPM();
	
	// this converts from military time to initially select earliest p/u time
	if(startHour <= 12) {
		if (startHour == 12) {
			comboAMPM.selectedIndex = 1;
		}
		else {
			comboAMPM.selectedIndex = 0;
		}
		if(startHour == 0) {
			comboHours.selectedIndex = 11;
		}
		else {
			comboHours.selectedIndex = startHour - 1;
		}				
	}
	else {
		comboAMPM.selectedIndex = 1;
		comboHours.selectedIndex = startHour - 13;
	}
	// set Minutes
	if(startMinute == 0) {
		comboMinutes.selectedIndex = 0;
	}
	else if(startMinute <= 15) {
		comboMinutes.selectedIndex = 1;
	}
	else if(startMinute <= 30) {
		comboMinutes.selectedIndex = 2;
	}
	else if(startMinute <= 45) {
		comboMinutes.selectedIndex = 3;
	}
	else {
		comboMinutes.selectedIndex = 0;
	}
	document.resForm.taFeedback.value = 'This is the soonest we can pick you up '
										+ 'if you reserve your ride now.';
	textMonth.style.visibility = 'hidden';
	document.resForm.Phone.focus();
}

function getEarliest(myPart) {
	var todayOffset = new Date();
	var todayMinutes;

/* TEST CODE */  // Uncomment and use these to test with artificial current dates/times.
/* TEST CODE */	 //todayOffset.setDate(1);
/* TEST CODE */	 //todayOffset.setMonth(1);
/* TEST CODE */  //todayOffset.setFullYear(2005);
/* TEST CODE */  //todayOffset.setMonth(1);
/* TEST CODE */  //todayOffset.setDate(28);
/* TEST CODE */  //todayOffset.setHours(19);
/* TEST CODE */  //todayOffset.setMinutes(56);

	todayOffset.setMinutes(todayOffset.getMinutes() + MINUTESOFFSET);
	todayMinutes = todayOffset.getMinutes();
	if(todayMinutes <= 15) {
		todayMinutes = 15;
	}
	else if(todayMinutes <= 30) {
		todayMinutes = 30;
	}
	else if(todayMinutes <= 45) {
		todayMinutes = 45;
	}
	else {
		todayMinutes = 60;
	}
	todayOffset.setMinutes(todayMinutes);

	switch(myPart) {
		case 'y':
			return todayOffset.getFullYear();
			break;
		case 'mon':
			return (todayOffset.getMonth());
			break;
		case 'd':
			return todayOffset.getDate();
			break;
		case 'dow':
			return todayOffset.getDay();
			break;
		case 'h':
			return todayOffset.getHours();
			break;
		case 'min':
			return todayOffset.getMinutes();
			break;
		case 'full':
			return todayOffset;
	}
}


function toMonthNum(strMonth) {
	switch(strMonth) {
		case 'January':
			return 0;
			break;
		case 'February':
			return 1;
			break;
		case 'March':
			return 2;
			break;
		case 'April':
			return 3;
			break;
		case 'May':
			return 4;
			break;
		case 'June':
			return 5;
			break;
		case 'July':
			return 6;
			break;
		case 'August':
			return 7;
			break;
		case 'September':
			return 8;
			break;
		case 'October':
			return 9;
			break;
		case 'November':
			return 10;
			break;
		case 'December':
			return 11;
	}
}

function changedYear() {
	var workingDate = new Date();
	var thisForm = document.resForm;
	
	if(thisForm.cmbYear.value == startYear) {
		fillInit();
	}
	else {
		workingDate.setDate(1);
		workingDate.setFullYear(startYear + 1);			
		workingDate.setMonth(0);
		clearMonths();
		thisForm.cmbMonth.options[0] = new Option(MONTHS[0], MONTHS[0]);
		fillDates(workingDate);
		if(startMonth == 11) {
			thisForm.cmbMonth.options[1] = new Option(MONTHS[1], MONTHS[1]);
		}
	}
	spellDate()
}

function changedMonth() {
	var thisForm = document.resForm;
	if(thisForm.cmbYear.value == startYear && toMonthNum(thisForm.cmbMonth.value) == startMonth) {
		fillInit();
	}
	else {
		var workingDate = new Date();
		workingDate.setDate(1);
		workingDate.setFullYear(document.resForm.cmbYear.value);
		workingDate.setMonth(toMonthNum(document.resForm.cmbMonth.value));
		fillDates(workingDate);
	}
	spellDate()
}

function changedDay() {
	var workingDate = new Date();
	var thisForm = document.resForm;
	workingDate.setFullYear(thisForm.cmbYear.value);			
	workingDate.setMonth(toMonthNum(thisForm.cmbMonth.value));
	workingDate.setDate(thisForm.cmbDay.value);
	spellDate()
}

function changedTime() {
	spellDate()
}

function clearMonths() {
	var comboMonth = document.resForm.cmbMonth;
	for(i=comboMonth.length;i>=0;i--) {
		comboMonth.options[i] = null;
	}
}

function clearDates() {
	var comboDay = document.resForm.cmbDay;
	for(i=comboDay.length;i>=0;i--) {
		comboDay.options[i] = null;
	}
}

function fillInit() {
	var comboMonth = document.resForm.cmbMonth;
	var comboDay = document.resForm.cmbDay;
	clearMonths();
	for(i=0;((i+startMonth)<=maxMonth);i++) {
		comboMonth.options[i] = 
			new Option(MONTHS[i + startMonth], MONTHS[i + startMonth]);
	}
	clearDates();
	for(i=0;((startDate+i)<=getLastDayOfMonth(getEarliest('full')));i++) {
		comboDay.options[i] = new Option(startDate + i, startDate + i);
	}
}

function fillDates(thisDate) {
	var comboDay = document.resForm.cmbDay;
	var anotherDate = new Date();
	anotherDate.setTime(thisDate);
	clearDates();
	for(i=0;i<getLastDayOfMonth(anotherDate);i++) {
		comboDay.options[i] = new Option(i + 1, i + 1);
	}
}

function fillHours() {
	var comboHour = document.resForm.cmbHour;
	for(i=0;i<=11;i++) {
		comboHour.options[i] = new Option(i + 1, i + 1);
	}
}

function fillMinutes() {
	var comboMinute = document.resForm.cmbMin;
	comboMinute.options[0] = new Option('00');
	for(i=1;i<=3;i++) {
		comboMinute.options[i] = new Option(i * 15, i * 15);
	}
}

function fillAMPM() {
	var comboAMPM = document.resForm.cmbAMPM;
	comboAMPM.options[0] = new Option('AM', 'AM');
	comboAMPM.options[1] = new Option('PM', 'PM');
}

function getLastDayOfMonth(someDate) {
	var someMonth = someDate.getMonth();
	var yetAnotherDate = new Date();
	yetAnotherDate.setTime(someDate);
	yetAnotherDate.setDate(1);
	yetAnotherDate.setMonth(someMonth + 1);
	yetAnotherDate.setDate(0); // this moves date to last day of last month
	return yetAnotherDate.getDate();
}

function buildDate() {
	var thisForm = document.resForm;
	var workingBDate = new Date();
	workingBDate.setMonth(0);
	workingBDate.setDate(1);
	workingBDate.setFullYear(thisForm.cmbYear.value);
	workingBDate.setMonth(toMonthNum(thisForm.cmbMonth.value));
	workingBDate.setDate(thisForm.cmbDay.value);
	if(thisForm.cmbAMPM.value == 'AM') {
		if(thisForm.cmbHour.value == 12) {
			workingBDate.setHours(0);
		}
		else {
			workingBDate.setHours(thisForm.cmbHour.value);
		}
	}
	else {
		if(thisForm.cmbHour.value == 12) {
			workingBDate.setHours(12);
		}
		else {
			workingBDate.setHours(parseInt(thisForm.cmbHour.value) + 12);
		}
	}
	workingBDate.setMinutes(thisForm.cmbMin.value);
	return workingBDate;
}

function spellDate() {
	var thisForm = document.resForm;
	var workingSpDate = buildDate();
	var strSpell = 'Pick me up at '
	var strSpellMinutes = '';
	var strSpellHour;
	switch (thisForm.cmbMin.value) {
		case '00':
			// No change necessary
			break;
		case '15':
			strSpellMinutes = '15 minutes past ';
			break;
		case '30':
			strSpellMinutes = 'Half-Past ';
			break;
		case '45':
			strSpellMinutes= '45 minutes past ';
			break;
	}
	strSpell += strSpellMinutes;
	if(thisForm.cmbHour.value == 12) {
		if(thisForm.cmbAMPM.value == 'AM') {
			strSpellHour = 'Midnight';
		}
		else {
			strSpellHour = 'Noon';
		}
	}
	else {
		strSpellHour = thisForm.cmbHour.value + ' ' 
						+ thisForm.cmbAMPM.value;
	}
	strSpell += (strSpellHour  + ' on ');
	strSpell += (DAYS[workingSpDate.getDay()] + ' the ');
	strSpell += (DATES[thisForm.cmbDay.value - 1] + ' of ');
	strSpell += (thisForm.cmbMonth.value + ', ');
	strSpell += (thisForm.cmbYear.value + '.');			
	document.resForm.taFeedback.value = strSpell;
}

function checkAndSubmit() {
	clearColors();
	var thisForm = document.resForm;
	var thisPhone = thisForm.Phone;
	var strAlert = 'Please fill in your ';
	var verifyDate = buildDate();
	verifyDate.setSeconds(0);
	verifyDate.setSeconds(verifyDate.getSeconds() + 1);													
	if(verifyDate < startDateTime) {
		strAlert = 'Please choose a reservation date and time that allows us at least '
						+ MINUTESOFFSET + ' minutes advance notice.';
		alert(strAlert);
		thisForm.cmbHour.focus();
		return;
	}
	if(thisPhone.value == '') {
		strAlert += 'phone number.';
		alert(strAlert);
		thisPhone.style.backgroundColor = missColor;
		thisPhone.focus();
		return;
	}	
	var numPhone = '';
	var singleNum;
	for(var i=0;i<thisPhone.value.length; i++) {
		singleNum = thisPhone.value.substr(i, 1);
		if(!(isNaN(singleNum)) && singleNum !=' ') {
			numPhone += singleNum;
		}
	}
	if(numPhone.length != 10) { // alter this if area code not required
		strAlert = 'Please enter your entire phone number, including area code.';
		alert(strAlert);
		thisPhone.style.backgroundColor = missColor;
		thisPhone.focus();
		return;
	}
	if(thisForm.FName.value == '') {
		strAlert += 'first name.';
		alert(strAlert);
		thisForm.FName.style.backgroundColor = missColor;
		thisForm.FName.focus();
		return;
	}
	if(thisForm.FName.value.length >= 20) {
		thisForm.FName.value = thisForm.FName.value.substring(0, 19);
	}
	if(thisForm.LName.value == '') {
		strAlert += 'last name.';
		alert(strAlert);
		thisForm.LName.style.backgroundColor = missColor;
		thisForm.LName.focus();
		return;
	}
	if(thisForm.LName.value.length >= 50) {
		thisForm.LName.value = thisForm.LName.value.substring(0, 49);
	}
	if(thisForm.PUAdd1.value == '') {
		strAlert += 'pickup address.';
		alert(strAlert);
		thisForm.PUAdd1.style.backgroundColor = missColor;
		thisForm.PUAdd1.focus();
		return;
	}
	if(thisForm.PUAdd1.value.length >= 150) {
		thisForm.PUAdd1.value = thisForm.PUAdd1.value.substring(0, 149);
	}
	if(thisForm.PUCity.value == '') {
		strAlert += 'pickup city.';
		alert(strAlert);
		thisForm.PUCity.style.backgroundColor = missColor;
		thisForm.PUCity.focus();
		return;
	}
	if(thisForm.PUCity.value.length >= 100) {
		thisForm.PUCity.value = thisForm.PUCity.value.substring(0, 99);
	}
	if(thisForm.PUZip.value == '') {
		strAlert += 'pickup Zip Code.';
		alert(strAlert);
		thisForm.PUZip.style.backgroundColor = missColor;
		thisForm.PUZip.focus();
		return;
	}
	if(thisForm.PUZip.value.length >= 10) {
		thisForm.PUZip.value = thisForm.PUZip.value.substring(0, 9);
	}
	if(thisForm.DOAdd1.value == '') {
		strAlert += 'drop off address.';
		alert(strAlert);
		thisForm.DOAdd1.style.backgroundColor = missColor;
		thisForm.DOAdd1.focus();
		return;
	}
	if(thisForm.DOAdd1.value.length >= 150) {
		thisForm.DOAdd1.value = thisForm.DOAdd1.value.substring(0, 149);
	}
	if(thisForm.DOCity.value == '') {
		strAlert += 'drop off city.';
		alert(strAlert);
		thisForm.DOCity.style.backgroundColor = missColor;
		thisForm.DOCity.focus();
		return;
	}
	if(thisForm.DOCity.value.length >= 100) {
		thisForm.DOCity.value = thisForm.DOCity.value.substring(0, 99);
	}
	if(thisForm.Acct.value == '') {
		if(thisForm.CustComments.value.length >= 225) {
			thisForm.CustComments.value = thisForm.CustComments.value.substring(0, 224);
		}
	}
	else if(thisForm.CustComments.value.length == '' ) {
		if(thisForm.Acct.value.length >= 210) {
			thisForm.Acct.value = thisForm.Acct.value.substring(0, 209);
		}
	}
	else {
		if(thisForm.CustComments.value.length >= 175) {
			thisForm.CustComments.value = thisForm.CustComments.value.substring(0, 174);
		}
		if(thisForm.Acct.value.length >= 35) {
			thisForm.Acct.value = thisForm.Acct.value.substring(0, 34);
		}
	}
	//var strComments = 'OLR - ';
	//strComments += thisForm.CustComments.value;
	//thisForm.CustComments.value = strComments;
	thisForm.txtMonth.value = toMonthNum(thisForm.cmbMonth.value) + 1;
	thisForm.submit();
}

function resetForm() {
	var thisForm = document.resForm;
	initializeDate()
	clearColors();
	thisForm.Phone.value = '';
	thisForm.FName.value = '';
	thisForm.LName.value = '';
	thisForm.PUAdd1.value = '';
	thisForm.PUCity.value = '';
	thisForm.PUZip.value = '';
	thisForm.DOAdd1.value = '';
	thisForm.DOCity.value = '';
	thisForm.CustComments.value = '';
	thisForm.Acct.value = '';
}
function clearColors() {
	var thisForm = document.resForm;
	thisForm.Phone.style.backgroundColor = goodColor;
	thisForm.FName.style.backgroundColor = goodColor;
	thisForm.LName.style.backgroundColor = goodColor;
	thisForm.PUAdd1.style.backgroundColor = goodColor;
	thisForm.PUCity.style.backgroundColor = goodColor;
	thisForm.PUZip.style.backgroundColor = goodColor;
	thisForm.DOAdd1.style.backgroundColor = goodColor;
	thisForm.DOCity.style.backgroundColor = goodColor;
}

/************************** Globals ***************************/

/* Set Minimum Reservation Lead Time Here */
MINUTESOFFSET = 20;  /*********************/
/******************************************/
//  Set mandatory field error color here:
var missColor = '#A4FF78';
var goodColor = 'transparent';

var MONTHS = new Array;
var DAYS = new Array;
var DATES = new Array;

MONTHS[0] = 'January';
MONTHS[1] = 'February';
MONTHS[2] = 'March';
MONTHS[3] = 'April';
MONTHS[4] = 'May';
MONTHS[5] = 'June';
MONTHS[6] = 'July';
MONTHS[7] = 'August';
MONTHS[8] = 'September';
MONTHS[9] = 'October';
MONTHS[10] = 'November';
MONTHS[11] = 'December';

DAYS[0] = 'Sunday';
DAYS[1] = 'Monday';
DAYS[2] = 'Tuesday';
DAYS[3] = 'Wednesday';
DAYS[4] = 'Thursday';
DAYS[5] = 'Friday';
DAYS[6] = 'Saturday';

DATES[0] = 'First';
DATES[1] = 'Second';
DATES[2] = 'Third';
DATES[3] = 'Fourth';
DATES[4] = 'Fifth';
DATES[5] = 'Sixth';
DATES[6] = 'Seventh';
DATES[7] = 'Eighth';
DATES[8] = 'Ninth';
DATES[9] = 'Tenth';
DATES[10] = 'Eleventh';
DATES[11] = 'Twelfth';
DATES[12] = 'Thirteenth';
DATES[13] = 'Fourteenth';
DATES[14] = 'Fifteenth';
DATES[15] = 'Sixteenth';
DATES[16] = 'Seventeenth';
DATES[17] = 'Eighteenth';
DATES[18] = 'Nineteenth';
DATES[19] = 'Twentieth';
DATES[20] = 'Twenty-First';
DATES[21] = 'Twenty-Second';
DATES[22] = 'Twenty-Third';
DATES[23] = 'Twenty-Fourth';
DATES[24] = 'Twenty-Fifth';
DATES[25] = 'Twenty-Sixth';
DATES[26] = 'Twenty-Seventh';
DATES[27] = 'Twenty-Eighth';
DATES[28] = 'Twenty-Ninth';
DATES[29] = 'Thirtieth';
DATES[30] = 'Thirty-First';

var startDateTime = new Date();
startDateTime.setMonth(0);
startDateTime.setDate(1);
startDateTime.setHours(1);
startDateTime.setMinutes(1);
var startYear, startMonth, startDate, startHour, startMinute, maxMonth;
var resYear, resMonth, resDate, resHour, resMinute