// JavaScript Document - ALv1.0-RateCalculatorJS.js
// Limousine Rate Calculator
//
// Designed by: Ben Henry @ Correct Techniques Productions for Associated Limousines, Inc.
// 6/23/2005

/////////////////////////////////////////////////////////////////
//  Global Variables
/////////////////////////////////////////////////////////////////

// tabs - 2D-Array containing picture sources for tabs on top of rate calculator.
var tabs = [	[ 'Images/AL1TabMenu1.png', 'Images/AL1TabMenu1-Selected.png' ],
				[ 'Images/AL1TabMenu2.png', 'Images/AL1TabMenu2-Selected.png' ],
				[ 'Images/AL1TabMenu3.png', 'Images/AL1TabMenu3-Selected.png' ],
				[ 'Images/AL1TabMenu4.png', 'Images/AL1TabMenu4-Selected.png' ],
				[ 'Images/AL1TabMenu5.png', 'Images/AL1TabMenu5-Selected.png' ]	];

// cities - 2D-Array containing all transfer/ airport pricing information.  Accessed by index cities[0][x].
//			[ index, Name, To/From SF, To/From SFO, Bay, Travel Time ]
var cities = [	[ 0, "SFO", 85, 0, "S", 0.0 ],
				[ 1, "OAK", 115, 115, "E", 0.5 ],
				[ 2, "SJC", 150, 150, "S", 1.0 ],
				[ 3, "----------------" ],
			  	[ 4, "San Francisco", 55, 55, "S", 0.0 ],
				[ 5, "Oakland", 105, 105, "E", 0.5 ],
				[ 6, "San Jose", 150, 150, "S", 1.0 ],
				[ 7, "----------------" ],
				[ 8, "Alameda", 115, 125, "E", 0.5 ],
				[ 9, "Alamo", 145, 145, "E", 0.75 ],
				[ 10, "Albany", 115, 125, "E", 0.5 ],
				[ 11, "Antioch", 185, 185, "E", 1.0 ],
				[ 12, "Atherton", 105, 105, "S", 0.75 ],
				[ 13, "Bakersfield", 1050, 1050, "S", 5.0 ],
				[ 14, "Belmont", 90, 90, "S", 0.5 ],
				[ 15, "Belvedere", 115, 125, "N", 0.5 ],
				[ 16, "Benicia", 140, 165, "E", 0.75 ],
				[ 17, "Berkeley", 110, 125, "E", 0.5 ],
				[ 18, "Big Sur", 550, 550, "S", 3.0 ],
				[ 19, "Bodega", 250, 275, "N", 1.75 ],
				[ 20, "Bohemian Grove", 300, 325, "N", 2.0 ],
				[ 21, "Boulder Creek", 300, 300, "S", 1.75 ],
				[ 22, "Burlingame", 90, 75, "S", 0.25 ],
				[ 23, "Calistoga", 300, 325, "E", 2.0 ],
				[ 24, "Carmel", 400, 400, "S", 2.25 ],
				[ 25, "Carson City", 800, 850, "E", 4.25 ],
				[ 26, "Castro Valley", 110, 110, "E", 0.75 ],
				[ 27, "Chico", 650, 675, "E", 3.25 ],
				[ 28, "Clayton", 170, 170, "E", 1.0 ],
				[ 29, "Clear Lake", 360, 385, "E", 2.75 ],
				[ 30, "Cloverdale", 300, 325, "N", 1.75 ],
				[ 31, "Concord", 145, 160, "E", 0.75 ],
				[ 32, "Corte Madera", 115, 125, "N", 0.75 ],
				[ 33, "Cotati", 180, 200, "N", 1.25 ],
				[ 34, "Crockett", 120, 145, "E", 0.75 ],
				[ 35, "Cupertino", 150, 140, "S", 1.0 ],
				[ 36, "Danville", 150, 150, "E", 0.75 ],
				[ 37, "Davis", 280, 300, "E", 1.5 ],
				[ 38, "Dixon", 255, 280, "E", 1.5 ],
				[ 39, "Dublin", 150, 150, "E", 1.0 ],
				[ 40, "East Palo Alto", 140, 130, "S", 0.75 ],
				[ 41, "East San Jose", 190, 175, "S", 1.0 ],
				[ 42, "El Cerrito", 110, 125, "E", 0.5 ],
				[ 43, "El Sobrante", 100, 125, "E", 0.5 ],
				[ 44, "Emeryville", 90, 100, "E", 0.5 ],
				[ 45, "Eureka", 1050, 1075, "N", 6.0 ],
				[ 46, "Fairfax", 130, 145, "N", 0.75 ],
				[ 47, "Fairfield", 180, 205, "E", 1.25 ],
				[ 48, "Folsom", 400, 425, "E", 2.0 ],
				[ 49, "Foster City", 105, 90, "S", 0.5 ],
				[ 50, "Fremont", 135, 135, "E", 0.75 ],
				[ 51, "Fresno", 680, 680, "E", 3.5 ],
				[ 52, "Fulton", 225, 250, "N", 1.25 ],
				[ 53, "Gilroy", 305, 275, "S", 1.5 ],
				[ 54, "Greenbrae", 115, 125, "N", 0.75 ],
				[ 55, "Half Moon Bay", 145, 125, "S", 0.5 ],
				[ 56, "Hayward", 135, 135, "E", 0.75 ],
				[ 57, "Healdsburg", 275, 300, "N", 1.5 ],
				[ 58, "Hercules", 125, 145, "E", 0.75 ],
				[ 59, "Hillsborough", 85, 85, "S", 0.5 ],
				[ 60, "Hollister", 340, 375, "S", 2.0 ],
				[ 61, "Ignacio", 95, 120, "N", 0.75 ],
				[ 62, "Kentfield", 115, 125, "N", 0.75 ],
				[ 63, "Kenwood", 200, 225, "N", 1.5 ],
				[ 64, "La Honda", 180, 180, "S", 1.25 ],
				[ 65, "Lafayette", 135, 135, "E", 0.75 ],
				[ 66, "Lake Tahoe North", 750, 775, "E", 4.5 ],
				[ 67, "Lake Tahoe South", 750, 750, "E", 4.0 ],
				[ 68, "Larkspur", 115, 125, "N", 0.5 ],
				[ 69, "Las Vegas", 2100, 2100, "S", 9.0 ],
				[ 70, "Livermore", 170, 170, "E", 1.0 ],
				[ 71, "Los Altos", 140, 130, "S", 1.0 ],
				[ 72, "Los Altos Hills", 150, 120, "S", 1.0 ],
				[ 73, "Los Angeles", 1400, 1400, "S", 6.5 ],
				[ 74, "Los Gatos", 180, 170, "S", 1.0 ],
				[ 75, "Marin City", 100, 120, "N", 0.5 ],
				[ 76, "Martinez", 100, 120, "E", 1.0 ],
				[ 77, "Mendocino", 585, 610, "N", 3.5 ],
				[ 78, "Menlo Park", 130, 110, "S", 0.75 ],
				[ 79, "Merced", 485, 485, "E", 2.5 ],
				[ 80, "Mill Valley", 100, 120, "N", 0.5 ],
				[ 81, "Millbrae", 75, 75, "S", 0.25 ],
				[ 82, "Milpitas", 150, 150, "S", 1.0 ],
				[ 83, "Modesto", 345, 345, "E", 1.75 ],
				[ 84, "Montera", 80, 105, "S", 0.5 ],
				[ 85, "Montclair", 110, 110, "N", 0.75 ],
				[ 86, "Monte Rio", 300, 325, "N", 2.0 ],
				[ 87, "Monterey", 400, 400, "S", 2.5 ],
				[ 88, "Moraga", 135, 135, "E", 0.75 ],
				[ 89, "Morgan Hill", 200, 180, "S", 1.25 ],
				[ 90, "Mountain View", 140, 135, "S", 0.75 ],
				[ 91, "Napa", 200, 200, "E", 1.25 ],
				[ 92, "Nevada City", 750, 750, "E", 2.75 ],
				[ 93, "Newark", 130, 130, "E", 1.0 ],
				[ 94, "Novato", 150, 150, "N", 0.75 ],
				[ 95, "Oakland", 105, 105, "E", 0.5 ],
				[ 96, "Oakley", 195, 180, "E", 1.25 ],
				[ 97, "Orinda", 135, 135, "E", 0.5 ],
				[ 98, "Pacheco", 170, 170, "E", 0.75 ],
				[ 99, "Pacific Grove", 400, 400, "S", 2.25 ],
				[ 100, "Pacifica", 80, 80, "S", 0.25 ],
				[ 101, "Palo Alto", 140, 110, "S", 0.75 ],
				[ 102, "Pebble Beach", 400, 400, "S", 2.5 ],
				[ 103, "Petaluma", 170, 195, "N", 1.0 ],
				[ 104, "Piedmont", 105, 105, "E", 0.5 ],
				[ 105, "Pinole", 140, 140, "E", 0.75 ],
				[ 106, "Pittsburgh", 190, 190, "E", 1.0 ],
				[ 107, "Pleasent Hill", 140, 140, "E", 0.75 ],
				[ 108, "Pleasenton", 145, 145, "E", 1.0 ],
				[ 109, "Portola Valley", 140, 140, "S", 0.75 ],
				[ 110, "Redding", 825, 850, "N", 3.5 ],
				[ 111, "Redwood City", 120, 105, "S", 0.5 ],
				[ 112, "Reno", 850, 875, "E", 3.5 ],
				[ 113, "Richmond", 110, 110, "E", 0.5 ],
				[ 114, "Rodeo", 145, 145, "E", 0.75 ],
				[ 115, "Rohnert Park", 200, 235, "N", 1.25 ],
				[ 116, "Ross", 115, 125, "N", 0.5 ],
				[ 117, "Sacramento", 325, 350, "E", 1.75 ],
				[ 118, "Salinas", 400, 400, "S", 2.0 ],
				[ 119, "San Bruno", 75, 75, "S", 0.25 ],
				[ 120, "San Carlos", 90, 90, "S", 0.5 ],
				[ 121, "San Jose(N)", 150, 150, "S", 1.0 ],
				[ 122, "San Jose(S)", 175, 175, "S", 1.0 ],
				[ 123, "San Juan Bautista", 350, 350, "S", 1.75 ],
				[ 124, "San Leandro", 110, 110, "E", 0.5 ],
				[ 125, "San Lorenzo", 110, 110, "E", 0.75 ],
				[ 126, "San Luis Obispo", 880, 900, "S", 3.75 ],
				[ 127, "San Mateo", 90, 90, "S", 0.5 ],
				[ 128, "San Pablo", 110, 110, "E", 0.5 ],
				[ 129, "San Rafael", 115, 135, "N", 0.5 ],
				[ 130, "San Simeon", 800, 800, "S", 4.0 ],
				[ 131, "Santa Clara", 140, 140, "S", 0.75 ],
				[ 132, "Santa Cruz", 285, 260, "S", 1.5 ],
				[ 133, "Santa Rosa", 210, 235, "N", 1.0 ],
				[ 134, "Saratoga", 180, 170, "S", 1.0 ],
				[ 135, "Sausolito", 100, 110, "N", 0.5 ],
				[ 136, "Scotts Valley", 275, 250, "S", 1.25 ],
				[ 137, "Sebastopol", 210, 235, "N", 1.25 ],
				[ 138, "Silverado Resort", 200, 235, "N", 1.5 ],
				[ 139, "Sonoma", 200, 225, "N", 1.25 ],
				[ 140, "St. Helena", 275, 275, "N", 1.75 ],
				[ 141, "Stinson Beach", 130, 150, "N", 1.0 ],
				[ 142, "Stockton", 320, 320, "E", 1.5 ],
				[ 143, "Sunnyvale", 140, 140, "S", 0.75 ],
				[ 144, "Terra Linda", 115, 130, "N", 0.75 ],
				[ 145, "Tiburon", 115, 130, "N", 0.5 ],
				[ 146, "Tracy", 230, 230, "E", 1.25 ],
				[ 147, "Ukiah", 450, 475, "N", 2.25 ],
				[ 148, "Union City", 130, 130, "E", 1.0 ],
				[ 149, "Vacaville", 215, 240, "E", 1.25 ],
				[ 150, "Vallejo", 160, 160, "E", 0.75 ],
				[ 151, "Walnut Creek", 135, 150, "E", 0.75 ],
				[ 152, "West Sacramento", 325, 350, "E", 1.75 ],
				[ 153, "Woodside", 130, 105, "S", 0.75 ],
				[ 154, "Yosemite", 780, 780, "E", 5.0 ],
				[ 155, "Yountville", 215, 240, "E", 1.25 ]	];

var width = 10;
var currentTab = 0;

////////////////////////////////////////////////////////////////////
//  General Functions
////////////////////////////////////////////////////////////////////

// preloadImages() - Simply instantiates instances of each image for quicker loading.
function preloadImages() {
	for (var y = 0; y < 2; y++) {
		for (var x = 0; x < tabs.length; x++) {
			image = new Image();
			image.src = tabs[x][y];
		}
	}
}

// switchTab() - Changes source of picture displayed to dynamically change images in one page.
function switchTab(tab) {
	for (var x = 0; x < tabs.length; x++) {
		if (x != tab)
			document.images["tab"+x].src = tabs[x][0];
	}
	document.images["tab"+tab].src = tabs[tab][1];
}

///////////////////////////////////////////////////////////////////////
//  Step-specific Functions
///////////////////////////////////////////////////////////////////////

// drawTabs() - Initializer.  Sets first step and initial content.
function drawTabs() {
	document.write("<br><table cellspacing=0 cellpadding=0 border=1><tr>");
	for (var x = 0; x < tabs.length; x++) {
		document.write("<td width="+width+"><img name='tab"+x+"' border=0 src="+tabs[x][0]+"></td>");
	}
	document.write("</td><td></td></tr><tr><td id=currentTab colspan="+(tabs.length+1)+">");
	step1Content();
	document.write("</td></tr></table><br>");
}

function step1Content(rate) {
	document.getElementById("currentTab").innerHTML = "<div align='center'><img src='Images/AL1RateCalcTitle.jpg'>" +
		"<br>First, what type of service and vehicle would be interested in?<br><br><form action='#'>" +
		"<div align='center'><input name='ServiceTypeSelector' type='radio' value='Airport' checked>Airport - To/From any Bay Area Airport<br>" +
		"<input name='ServiceTypeSelector' type='radio' value='Transfer'>Transfer - One-Way Luxury Transportation<br>" +
		"<input name='ServiceTypeSelector' type='radio' value='Charter' >Charter - Hourly Service</p><br><br>" +
		"Vehicle type<br>" +
		"<select name='VehicleType'><option value='4px' selected>Lincoln Sedan (4-Passenger)</option>" +
		"<option value='6px'>Lincoln Limousine (6-Passenger)</option><option value='8px'>Lincoln Limousine (8-Passenger)" +
		"</option><option value='10px'>Lincoln Limousine (10-Passenger)</option><option value='SUV'>" +
		"Cadillac SUV (6-Passenger)</option><option value='LVan'>Luxury Conversion Van (8-Passenger)</option>" +
		"<option value='PVan'>Passenger Van (13-Passenger)</option></select></div><p align='center'>" +
		"<br><input name='Prev' type='image' src='Images/AL1CalcButton2-Disabled.jpg' disabled>" +
		"<input name='Next' type='image' src='Images/AL1CalcButton1.jpg' onClick='step1Next(rate)'></form></div>";
	document.images["tab0"].src = tabs[0][1];
	return rate;
}

function step1Next(rate) {
	var myForm = document.forms[0];

	var serviceTypeObj = myForm.ServiceTypeSelector;
	var serviceType = " ";
	var vehicleIdx = myForm.VehicleType.selectedIndex;
	var vehicle = myForm.VehicleType.options[vehicleIdx].value;
	
	for( var i = 0; i < serviceTypeObj.length; i++ ) {
		if( serviceTypeObj[i].checked ) {
			serviceType = serviceTypeObj[i].value;
		}
	}
	resetRate( rate );
	rate.vehicle = vehicle;
	rate.type = serviceType;

	//if( serviceType == "Airport" ) {
		switchTab(1);
		step2Content(rate);
	//}
	return rate;
}

// STEP 2 - In the 2nd step, we set the pickup location and relative index.
// Switches to the 2nd page of the calculator which displays a dropdown menu for pickup location based on type.
// In the 2nd step, we set the service  and vehicle type.  In all steps, we switch the image on the tab at top of the 
// table to show progression through the calculator, then switch the HTML content below the tabs.
function step2Content(rate) {
	var formString = "<form action='#' ><div align='center'><select name='Pickup' >";
	if( rate.type == "Airport" ) {
		for( var i=0;  i < cities.length; i++ ) {
			formString += "<option value='";
			formString += cities[i][1];
			formString += "'";
			if( i == 0 ) formString += " selected"
			formString += ">";
			formString += cities[i][1];
			formString += "</option>";
		}
	}
	else {
		for( var i=4;  i < cities.length; i++ ) {
			formString += "<option value='";
			formString += cities[i][1];
			formString += "'";
			if( i == 4 ) formString += " selected"
			formString += ">";
			formString += cities[i][1];
			formString += "</option>";
		}
	}
	formString += "</select></form>";

	//if( rate.type == "Airport" || rate.type == "Charter" ) {
		document.getElementById("currentTab").innerHTML = "<div align='center'><br>Great!  You've chosen " + 
		rate.type + " service!<br><br>Where will we picking you up?<br><br>";
		document.getElementById("currentTab").innerHTML += formString;

		document.getElementById("currentTab").innerHTML += "<br><div align='center'>" +
		"<input name='Prev' type='image' src='Images/AL1CalcButton2.jpg' onClick='step2Prev(rate)'>" +
		"<input name='Next' type='image' src='Images/AL1CalcButton1.jpg' onClick='step2Next(rate)'></div><br>";
	//}
	//else if( rate.type == "Charter" ) {
		//document.getElementById("currentTab").innerHTML = "<div align='center'><br>"
	//}
	
	return rate;
}

function step2Prev(rate) {
	switchTab(0);
	step1Content(rate);
	return rate;
}

function step2Next(rate) {
	var myForm = document.forms[0];

	var pickupIdx = myForm.Pickup.selectedIndex;
	var pickup = myForm.Pickup.options[pickupIdx].value;
	
	for( var i=0; i < cities.length; i++ ) {
		if( cities[i][1] == pickup ) rate.pickupIdx = i;
	}
	rate.pickup = pickup;

	if( rate.pickupIdx != 3 && rate.pickupIdx != 7 ) {
		switchTab(2);
		step3Content(rate);
	}
	return rate;
}

//// STEP 3 - Dropoff/ Charter hours
// In the 3rd step, we set the dropoff location and relative index and calculate the base rate.
function step3Content(rate) {
	var formString = "<form action='#' ><div align='center'><select name='Dropoff' >";
	if( rate.type == "Airport" ) {
		if( rate.pickupIdx < 3 )  { //Pickup at Airport - Baggage Meet Rate
			for( var i=4;  i < cities.length; i++ ) {
				formString += "<option value='";
				formString += cities[i][1];
				formString += "'";
				if( i == 4 ) formString += " selected"
				formString += ">";
				formString += cities[i][1];
				formString += "</option>";
			}
		}
		else { //Airport drop
			for( var i=0;  i < 3; i++ ) {
				formString += "<option value='";
				formString += cities[i][1];
				formString += "'";
				if( i == 0 ) formString += " selected"
				formString += ">";
				formString += cities[i][1];
				formString += "</option>";
			}
		}
	}
	else if( rate.type == "Charter" ) {
		for( var i=4;  i < cities.length; i++ ) {
			formString += "<option value='";
			formString += cities[i][1];
			formString += "'";
			if( i == 4 ) formString += " selected"
			formString += ">";
			formString += cities[i][1];
			formString += "</option>";
		}
	}
	else {
		for( var i=4;  i < cities.length; i++ ) {
			formString += "<option value='";
			formString += cities[i][1];
			formString += "'";
			if( i == 4 ) formString += " selected"
			formString += ">";
			formString += cities[i][1];
			formString += "</option>";
		}
	}
	formString += "</select></form>";	
	
	if( rate.type == "Airport" ) {
		document.getElementById("currentTab").innerHTML = "<div align='center'><br>OK.  We would pick you up from " + rate.pickup + "!<br><br>";
		if( rate.pickup != "SFO" && rate.pickup != "OAK" && rate.pickup != "SJC" ) {
			document.getElementById("currentTab").innerHTML += "<div align='center'>Which airport would we take you?<br><br>";
		}
		else { document.getElementById("currentTab").innerHTML += "<div align='center'>Where would we take you?<br><br>"; }
		document.getElementById("currentTab").innerHTML += formString;
		document.getElementById("currentTab").innerHTML += "<div align='center'><br>" +
		"<input name='Prev' type='image' src='Images/AL1CalcButton2.jpg' onClick='step3Prev(rate)'>" +
		"<input name='Next' type='image' src='Images/AL1CalcButton1.jpg' onClick='step3Next(rate)'><br><br></div>";
	}
	else if( rate.type == "Transfer" ) {
		document.getElementById("currentTab").innerHTML = "<div align='center'><br>OK.  We would pick you up from " + rate.pickup + "!<br><br>";
		document.getElementById("currentTab").innerHTML += "<div align='center'>Where would we take you?<br><br>"; 
		document.getElementById("currentTab").innerHTML += formString;
		document.getElementById("currentTab").innerHTML += "<div align='center'><br>" +
		"<input name='Prev' type='image' src='Images/AL1CalcButton2.jpg' onClick='step3Prev(rate)'>" +
		"<input name='Next' type='image' src='Images/AL1CalcButton1.jpg' onClick='step3Next(rate)'><br><br></div>";
	}
	else {
		document.getElementById("currentTab").innerHTML = "<div align='center'><br>OK.  We would pick you up from " + rate.pickup + "!<br>" +
		"<br>Where will we be dropping you off?<br><br>";		
		document.getElementById("currentTab").innerHTML += formString;
		document.getElementById("currentTab").innerHTML += "<div align='center'><br>How many hours would your service be?*<br>" +
		"<br><form action='#'>" +
		"<select name='CharterHoursSelect'><option selected value='3'>3 Hours</option>" +
		"<option value='4'>4 Hours</option><option value='5'>5 Hours</option><option value='6'>6 Hours</option>" +
		"<option value='7'>7 Hours</option><option value='8'>8 Hours</option><option value='9'>9 Hours</option>" +
		"</select><br></form>(* Please note that out-of-SF travel time to your pickup location and<br>" +
		"from your dropoff location will be added to the total number of hours.)<br><br>" +
		"<input name='Prev' type='image' src='Images/AL1CalcButton2.jpg' onClick='step3Prev(rate)'>" +
		"<input name='Next' type='image' src='Images/AL1CalcButton1.jpg' onClick='step3Next(rate)'><br><br></div>";
	}
	return rate;
}

function step3Prev(rate) {
	switchTab(1);
	step2Content(rate);
	return rate;
}

function step3Next(rate) {
	var myForm1 = document.forms[0];

	var dropoffIdx = myForm1.Dropoff.selectedIndex;
	var dropoff = myForm1.Dropoff.options[dropoffIdx].value;

	for( var i=0; i < cities.length; i++ ) {
		if( cities[i][1] == dropoff ) rate.dropoffIdx = i;
	}
	rate.dropoff = dropoff;

	if( rate.type != "Charter" ) {
		// OK now we know the pickup and dropoff points, thus we can calculate the base rate
		calculateBase(rate);
	}
	else {
		var myForm2 = document.forms[1];

		var hoursIdx = myForm2.CharterHoursSelect.selectedIndex;
		var hours = parseFloat(myForm2.CharterHoursSelect.options[hoursIdx].value);

		// Adjust for travel time
		hours += cities[rate.pickupIdx][5];
		hours += cities[rate.dropoffIdx][5];	
		
		rate.hours = hours;
		
		calculateCharterBase(rate);		
	}
	
	if( rate.dropoffIdx != 7  ) {
		switchTab(3);
		step4Content(rate);
	}
	return rate;
}

////  STEP 4 - In the 4th step, we calculate the remaining parts of the rate and calculate the total.
function step4Content(rate) {
	document.getElementById("currentTab").innerHTML = "<div align='center'><br>OK.  We would drop you off at " + 
	rate.dropoff + "!<br><br>Please check any that additional charges that may apply<br>" +
	"<form><input name='ExtrasCheckboxes' type='checkbox' value=20 >Late night service (between 11pm and 4am): $20</input>" +
	"<br><input name='ExtrasCheckboxes' type='checkbox' value=25 >Champagne: $25</input></div>";

	if( rate.type != "Charter" ) {
		document.getElementById("currentTab").innerHTML += "<br><div align='center'><input name='ExtrasCheckboxes' type='checkbox' value=10 >Extra stop in SF*: $10</input>" +
		"<br><input name='ExtrasCheckboxes' type='checkbox' value=20 >Extra stop out of SF*: $20</input></div>";
	}
	document.getElementById("currentTab").innerHTML += "</form>";
	if( rate.type != "Charter" ) {
		document.getElementById("currentTab").innerHTML += "<div align='center'>*(Please note that rates for extra stops may not be accurate.<br>Please call for precise rate.)<br></div>";
	}

	document.getElementById("currentTab").innerHTML += "<br><div align='center'><input name='Prev' type='image' src='Images/AL1CalcButton2.jpg' onClick='step4Prev(rate)'>" +
		"<input name='Next' type='image' src='Images/AL1CalcButton1.jpg' onClick='step4Next(rate)'></div><br>";
	
	return rate;
}

function step4Prev(rate) {
	switchTab(2);
	step3Content(rate);
	
	return rate;
}

function step4Next(rate) {
	var myForm = document.forms[0];

	var extrasObj = myForm.ExtrasCheckboxes;
	var extraObj;
	var extras = 0;

	for( var i = 0; i < extrasObj.length; i++ ) {
		if( extrasObj[i].checked ) {
			extraObj = parseInt( extrasObj[i].value );
			extras += extraObj;
		}
	}

	if( rate.type == "Airport" ) {
		if( rate.dropoff == "SFO" ) {			
			rate.fees = 4;  }
		else if( rate.pickup == "SFO" ) {
			rate.fees = 5;	}
		else {
			rate.fees = 0;  }
	}
	
	rate.fees += Math.ceil(extras);
	
	calculateTolls(rate);

	rate.gratuity = rate.base * 0.2;

	// Fuel surcharge removed
	//rate.gas = rate.base * 0.10;
	//rate.total = Math.ceil(rate.base) + Math.ceil(rate.gratuity) + Math.ceil(rate.gas) + Math.ceil(rate.fees) + Math.ceil(rate.tolls);
	
	rate.total = Math.ceil(rate.base) + Math.ceil(rate.gratuity) + Math.ceil(rate.fees) + Math.ceil(rate.tolls);
	switchTab(4);
	step5Content(rate);
	return rate;
}

////  STEP 5 - Final total.  Simply displays the comprehensive results of the calculations.
function step5Content(rate) {
	document.getElementById("currentTab").innerHTML = "<div align='center'><br>Thank you.  Your rate is as follows.<br><br>" +
	//"Base rate = $" + Math.ceil(rate.base) + "<br>Gratuity(20%) and Fuel Surcharge(10%) = $" + (Math.ceil(rate.gratuity) + 
	"Base rate = $" + Math.ceil(rate.base) + "<br>Gratuity(20%) = $" + (Math.ceil(rate.gratuity) + 
	Math.ceil(rate.gas)) + "<br>Tolls, Fees & Extras = $" + (Math.ceil(rate.tolls) + Math.ceil(rate.fees)) + "<br><br>Total = $" + rate.total + "<br><br>" +
	"<input name='Prev' type='image' src='Images/AL1CalcButton2.jpg' onClick='step5Prev(rate)'>" +
	"<input name='Reset' type='image' src='Images/AL1CalcButton3.jpg' onClick='step5Reset(rate)'>" +
	"<a HREF=index.html><img src='Images/AL1CalcButton4.jpg' border=0></a><br><br></div>";
	return rate;
}

function step5Prev(rate) {
	switchTab(3);
	step4Content(rate);	
	return rate;
}

function step5Reset(rate) {
	resetRate( rate );
	//document.write( rate.hours + rate.pickupIdx );
	switchTab(0);
	step1Content(rate);
	return rate;
}

/////////////////////////////////////////////////////////////////////////
//  Calculator Functions
/////////////////////////////////////////////////////////////////////////

// rate() - The base constructor for the rate type.
function rate( type, hours, vehicle, pickup, pickupIdx, dropoff, dropoffIdx, base, gratuity, gas, tolls, fees, total ) {
	this.type = type;				this.hours = hours;				this.vehicle = vehicle;
	this.pickup = pickup;			this.pickupIdx = pickupIdx;		this.dropoff = dropoff;
	this.dropoffIdx = dropoffIdx;	this.base = base;				this.gratuity = gratuity;
	this.gas = gas;					this.tolls = tolls;				this.fees = fees;
	this.total = total;
}

// resetRate() - This function resets all of the rate attributes.
function resetRate( rate ) {
	rate.type = "";			rate.hours = 0;			rate.vehicle = "";
	rate.pickup = "";		rate.pickupIdx = 0;		rate.dropoff = "";
	rate.dropoffIdx = 0;	rate.base = 0;			rate.gratuity = 0;
	rate.gas = 0;			rate.tolls = 0;			rate.fees = 0;
	rate.total = 0;
	return rate;
}

// calculateBase() - This function calculates the base service rate based on the service type, pickup and dropoff locations.
function calculateBase(rate) {
	if( rate.type == "Airport" ) {
		if( rate.pickup == "SFO" && rate.dropoff == "San Francisco" ) {   //SFO to SF
			switch( rate.vehicle ) {
				case "4px":
					rate.base = 75;		break;
				case "6px":
					rate.base = 85;		break;
				case "8px":
					rate.base = 95;		break;
				case "10px":
					rate.base = 120;	break;
				case "SUV":
					rate.base = 95;		break;
				case "LVan":
					rate.base = 105;	break;
				case "PVan":
					rate.base = 95;		break;
			}
		}
		else if( rate.pickup == "San Francisco" && rate.dropoff == "SFO" ) {   //SF to SFO
			switch( rate.vehicle ) {
				case "4px":
					rate.base = 55;		break;
				case "6px":
					rate.base = 70;		break;
				case "8px":
					rate.base = 80;		break;
				case "10px":
					rate.base = 110;	break;
				case "SUV":
					rate.base = 80;		break;
				case "LVan":
					rate.base = 90;		break;
				case "PVan":
					rate.base = 80;		break;			
			}
		}
		else {		
			if( rate.dropoff == "SFO" ) { rate.base = cities[rate.pickupIdx][3]; }  //??? to SFO
			else if( rate.dropoff == "OAK" ) {   //??? to OAK
				if( rate.pickup == "San Francisco" ) { rate.base = cities[1][2]; }  //SF to OAK
				else if( cities[rate.pickupIdx][4] == "E" ) {
					if( cities[rate.pickupIdx][2] > cities[rate.dropoffIdx][2] ) {	rate.base = cities[rate.pickupIdx][2] + 20; }
					else {	rate.base = cities[rate.dropoffIdx][2] + 20; }
				}
				else { rate.base = Math.sqrt((cities[rate.pickupIdx][2]*cities[rate.pickupIdx][2]) + 
					(cities[1][2]*cities[1][2])); }  //N or S to OAK
			}
			else if( rate.dropoff == "SJC" ) {   //??? to SJC
				if( rate.pickup == "San Francisco" ) { rate.base = cities[2][2]; }
				else if( cities[rate.pickupIdx][4] == "S" ) { 
					if( cities[rate.pickupIdx][2] > cities[rate.dropoffIdx][2] ) {	rate.base = cities[rate.pickupIdx][2] + 20; }
					else {	rate.base = cities[rate.dropoffIdx][2] + 20; }
				}
				else if( cities[rate.pickupIdx][4] == "E" ) { rate.base = Math.sqrt((cities[rate.pickupIdx][2]*cities[rate.pickupIdx][2])
						+ (cities[2][2]*cities[2][2])); }
				else { rate.base = .9*(cities[rate.pickupIdx][3] + cities[2][2]); }
			}
			else { //Arrival  ??? to ???
				if( rate.pickup == "SFO" ) {
					rate.base = cities[rate.dropoffIdx][3]; }
				else if( rate.pickup == "OAK" ) {		// OAK to ??
					if( rate.dropoff == "San Francisco" ) { rate.base = cities[1][2]; }
					else if( cities[rate.dropoffIdx][4] == "E" ) { 		// OAK to East Bay - Treat as extra stop
						if( cities[rate.pickupIdx][2] > cities[rate.dropoffIdx][2] ) {	rate.base = cities[rate.pickupIdx][2] + 20; }
						else {	rate.base = cities[rate.dropoffIdx][2] + 20; }
					}
					else { rate.base = Math.sqrt((cities[rate.dropoffIdx][2]*cities[rate.dropoffIdx][2]) + (cities[1][2]*cities[1][2])); }
				}
				else if( rate.pickup == "SJC" ) {
					if( rate.dropoff == "San Francisco" ) { rate.base = cities[2][2]; }
					else if( cities[rate.dropoffIdx][4] == "S" ) { 
						if( cities[rate.pickupIdx][2] > cities[rate.dropoffIdx][2] ) {	rate.base = cities[rate.pickupIdx][2] + 20; }
						else {	rate.base = cities[rate.dropoffIdx][2] + 20; }
					}
					else if( cities[rate.dropoffIdx][4] == "E" ) { rate.base = Math.sqrt((cities[rate.dropoffIdx][2]*cities[rate.dropoffIdx][2])
						+ (cities[2][2]*cities[2][2])); }
					else { rate.base = .9*(cities[rate.dropoffIdx][2] + cities[2][2]); }
				}			
			}
			switch( rate.vehicle ) {
				case "6px":
				rate.base += 25;	break;
				case "8px":
				rate.base += 40;	break;
				case "10px":
				rate.base += 60;	break;
				case "SUV":
				rate.base += 40;	break;
				case "LVan":
				rate.base += 45;	break;
				case "PVan":
				rate.base += 45;	break;
			}
		}
	}
	else { //Transfer rate
	
		if( rate.pickup == "San Francisco" ) {
			if( rate.dropoff == "San Francisco" ) { rate.base = 55; }
			else {
				rate.base = cities[rate.dropoffIdx][2]; }
		}
		else if( rate.dropoff == "San Francisco" ) {
			rate.base = cities[rate.pickupIdx][2]; }

		else {
			if( cities[rate.pickupIdx][4] == "N" ) {  //North Bay pickup
			if( cities[rate.dropoffIdx][4] == "N" ) {   //North to North Bay service, whichever greater
				if( cities[rate.pickupIdx][2] > cities[rate.dropoffIdx][2] ) {	rate.base = cities[rate.pickupIdx][2]; }
				else {	rate.base = cities[rate.dropoffIdx][2]; }
			}
			else if( cities[rate.dropoffIdx][4] == "E" ) {   //North to East Bay service. Sqrt(AA+BB)
				rate.base = Math.sqrt((cities[rate.pickupIdx][2]*cities[rate.pickupIdx][2])
					+ (cities[rate.dropoffIdx][2]*cities[rate.dropoffIdx][2]));
			}
			else if( cities[rate.dropoffIdx][4] == "S" ) {   //North to South Bay service. .9(A + B)
				rate.base = .9*(cities[rate.dropoffIdx][2] + cities[rate.pickupIdx][2]);
			}
		}
		else if( cities[rate.pickupIdx][4] == "E" ) {  //East Bay pickup
			if( cities[rate.dropoffIdx][4] == "E" ) {   //East to East Bay service, whichever greater
				if( cities[rate.pickupIdx][2] > cities[rate.dropoffIdx][2] ) {	rate.base = cities[rate.pickupIdx][2]; }
				else {	rate.base = cities[rate.dropoffIdx][2]; }
			}
			else if( cities[rate.dropoffIdx][4] == "N" ) {   //East to North Bay service. Sqrt(AA+BB)
				rate.base = Math.sqrt((cities[rate.pickupIdx][2]*cities[rate.pickupIdx][2])
					+ (cities[rate.dropoffIdx][2]*cities[rate.dropoffIdx][2]));
			}
			else if( cities[rate.dropoffIdx][4] == "S" ) {   //East to South Bay service. Sqrt(AA+BB)
				rate.base = Math.sqrt((cities[rate.pickupIdx][2]*cities[rate.pickupIdx][2])
					+ (cities[rate.dropoffIdx][2]*cities[rate.dropoffIdx][2]));
			}
		}
		else if( cities[rate.pickupIdx][4] == "S" ) {  //South Bay pickup
			if( cities[rate.dropoffIdx][4] == "S" ) {   //South to South Bay service, whichever greater
				if( cities[rate.pickupIdx][2] > cities[rate.dropoffIdx][2] ) {	rate.base = cities[rate.pickupIdx][2]; }
				else {	rate.base = cities[rate.dropoffIdx][2]; }
			}
			else if( cities[rate.dropoffIdx][4] == "E" ) {   //South to East Bay service. Sqrt(AA+BB)
				rate.base = Math.sqrt((cities[rate.pickupIdx][2]*cities[rate.pickupIdx][2])
					+ (cities[rate.dropoffIdx][2]*cities[rate.dropoffIdx][2]));
			}
			else if( cities[rate.dropoffIdx][4] == "N" ) {   //South to North Bay service. .9(A + B)
				rate.base = .9*(cities[rate.dropoffIdx][2] + cities[rate.pickupIdx][2]);
			}
		}}
		switch( rate.vehicle ) {
			case "6px":
			rate.base += 25;	break;
			case "8px":
			rate.base += 40;	break;
			case "10px":
			rate.base += 60;	break;
			case "SUV":
			rate.base += 40;	break;
			case "LVan":
			rate.base += 45;	break;
			case "PVan":
			rate.base += 45;	break;
		}
	}

	if( rate.type == "Airport" || rate.type == "Transfer" ) {

	}

	rate.base = Math.ceil( rate.base );

	return rate;
}

// calculateCharterBase() - This function calculates the base service rate based on the hours and vehicle type.
function calculateCharterBase(rate) {
	var vehicleRate;
	
	switch( rate.vehicle ) {
		case "4px":
			vehicleRate = 55;	break;
		case "6px":
			vehicleRate = 75;	break;		
		case "8px":
			vehicleRate = 85;	break;
		case "10px":
			vehicleRate = 95;	break;
		case "SUV":
			vehicleRate = 85;	break;
		case "LVan":
			vehicleRate = 95;	break;
		case "PVan":
			vehicleRate = 85;	break;
	}
	
	rate.base = rate.hours * vehicleRate;
	
	return rate;
}

// calculateTolls() - This function calculates the base service rate based on the pickup and dropoff locations.
function calculateTolls(rate) {
	//Now to figure out tolls
	if( cities[rate.pickupIdx][4] == "N" ) {  //Pickup in North Bay
		rate.tolls = 5;
		if( cities[rate.dropoffIdx][4] == "E" ) { //Drop in East Bay
			rate.tolls += 3;  }
	}
	else if( cities[rate.pickupIdx][4] == "E" ) {  //Pickup in East Bay
		rate.tolls = 3;
		if( cities[rate.dropoffIdx][4] == "N" ) {  //Drop in North Bay
			rate.tolls += 5;  }
	}
	else {  //Pickup in South Bay
		if( cities[rate.dropoffIdx][4] == "E" ) {  //Drop in East Bay
			rate.tolls = 3;  }
		if( cities[rate.dropoffIdx][4] == "N" ) {  //Drop in North Bay
			rate.tolls = 5;  }
	}
	return rate;
}

//////////////////////////////////////////////////////////////////////
//  JS Initialization
//////////////////////////////////////////////////////////////////////

// Instantiation of new rate object
var myRate = new rate();

// Call to preloadImages()
preloadImages();