// Tampa Xway Toll Calculator

// 0	Gandy Boulevard
// 1	Dale Mabry Highway
// 2	Euclid Avenue
// 3	Bay to Bay Boulevard
// 4	Willow Avenue
// 5	Plant Avenue
// 6	Florida Avenue/Channelside
// 7	Tampa Street
// 8	Jefferson
// 9	Morgan Street
// 10	Morgan/Downtown West
// 11	Nebraska
// 12	Kennedy/Downtown East
// 13	22nd Street
// 14	39th Street
// 15	50th Street
// 16	78th Street
// 17	U.S. 301
// 18	Faulkenburg Road
// 19	I-75

var ramps = new Array(
						"Gandy Boulevard",
						"Dale Mabry Highway",
						"Euclid Avenue",
						"Bay to Bay Boulevard",
						"Willow Avenue",
						"Plant Avenue",
						"Florida Avenue/Channelside",
						"Tampa Street",
						"Jefferson",
						"Morgan Street",
						"Morgan/Downtown West",
						"Nebraska",
						"Kennedy/Downtown East",
						"22nd Street",
						"39th Street",
						"50th Street",
						"78th Street",
						"U.S. 301",
						"Faulkenburg Road",
						"I-75"
	                   );
var starting = new Array(0,1,2,3,4,5,6,7,8,9,11,13,14,15,16,17,18,19);
var endings = new Array(
				new Array(4,6,13,15,16,17,18,19),
				new Array(4,6,13,15,16,17,18,19),
				new Array(4,6,13,15,16,17,18,19),
				new Array(4,6,13,15,16,17,18,19),
				new Array(0,2,3,6,13,15,16,17,18,19),
				new Array(0,2,3,4,6,13,15,16,17,18,19),
				new Array(4,15),
				new Array(0,2,3,4,5),
				new Array(13,15,16,17,18,19),
				new Array(0,2,3,4,5),
				new Array(13,15,16,17,18,19),
				new Array(0,2,3,4,5,10,12,15,16,17,18,19),
				new Array(0,2,3,4,5,10,12,13,15,16,17,18,19),
				new Array(0,2,3,4,5,10,12,13,14,16,17,18,19),
				new Array(0,2,3,4,5,10,12,13,14,15,17,18,19),
				new Array(0,2,3,4,5,10,12,13,14,15,16,18,19),
				new Array(0,2,3,4,5,10,12,13,14,15,16,17),
				new Array(0,2,3,4,5,10,12,13,14,15,16,17,18)
);

document.forms[0].start.options[0].text = "Select a starting point";
document.forms[0].start.options[0].value = -1;

document.forms[0].start.length = starting.length + 1;

for (var i = 0; i < starting.length; i++) {
	  //document.forms[0].start.options[i+1].text =  "[" + starting[i] + "] " + ramps[starting[i]];
	  document.forms[0].start.options[i+1].text =  ramps[starting[i]];
	  document.forms[0].start.options[i+1].value = starting[i];
}

document.forms[0].end.length = 1;
document.forms[0].end.options[0].text = "Select an ending point";
document.forms[0].end.options[0].value = -1;
document.forms[0].end.disabled = true;
document.forms[0].start.selectedIndex = 0;
document.forms[0].end.selectedIndex = 0;

var startvalue = -1;
var endvalue = -1;

function clearTollTable() {
	document.getElementById("directioncell").innerHTML = "&nbsp;";
	document.getElementById("stepcell").innerHTML = "&nbsp;";
	document.getElementById("tollcell").innerHTML = "&nbsp;";
	document.getElementById("totalcell").innerHTML = "&nbsp;";
	document.getElementById("totaltollcell").innerHTML = "&nbsp;";
	document.getElementById("tolllabel").innerHTML = "&nbsp;";
}

function setEnd() {
	clearTollTable();
	startvalue = parseInt(document.forms[0].start.options[document.forms[0].start.selectedIndex].value);
	//document.forms[0].end.options[0].text += " [" + (document.forms[0].start.selectedIndex - 1) + "]" + " [" + startvalue + "]";
	document.forms[0].end.selectedIndex = 0;
	if (startvalue != -1) {
		document.forms[0].end.disabled = false;
		document.forms[0].end.length = endings[document.forms[0].start.selectedIndex - 1].length + 1;
		for (var i = 0; i < endings[document.forms[0].start.selectedIndex - 1].length; i++) {
			//document.forms[0].end.options[i+1].text = "[" + endings[document.forms[0].start.selectedIndex - 1][i] + "] " + ramps[endings[document.forms[0].start.selectedIndex - 1][i]];
			document.forms[0].end.options[i+1].text = ramps[endings[document.forms[0].start.selectedIndex - 1][i]];
			document.forms[0].end.options[i+1].value = endings[document.forms[0].start.selectedIndex - 1][i];
		}
	} else {
		document.forms[0].end.disabled = true;
	}
}

function calculate() {

	clearTollTable();
	
	var tolls = 0;
	var axles = parseInt(document.forms[0].axles.options[document.forms[0].axles.selectedIndex].value) - 1;
	var sunpass = parseInt(document.forms[0].sunpass.options[document.forms[0].sunpass.selectedIndex].value);
	endvalue = parseInt(document.forms[0].end.options[document.forms[0].end.selectedIndex].value);
	if (startvalue == -1 || endvalue == -1) return;
	var toll = 0;
	var sundisc = .25;
	var steptext = "";
	var tolltext = " ";
	if (sunpass) {
		document.getElementById("directioncell").innerHTML="<b>SunPass&reg;</b><br>";
	} else {
		document.getElementById("directioncell").innerHTML="<b>Cash</b><br>";
	}
	if (startvalue < endvalue) {
		
		// Eastbound
		document.getElementById("directioncell").innerHTML += "<b>Traveling Eastbound</b><br>";
		steptext += "Enter at " + document.forms[0].start.options[document.forms[0].start.selectedIndex].text + "<br>";
		
		// Plant Avenue Entrance
		if (startvalue == 5) {
			toll = .75
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		} else {
			tolltext += "&nbsp;<br>";
		}
		
		// West Toll Plaza
		if (startvalue <= 4 && endvalue >= 6) {
			steptext += "West Toll Plaza<br>";
			toll = 1.25
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
		
		// East Toll Plaza
		if (startvalue <= 15 && endvalue >= 16) {
			steptext += "East Toll Plaza<br>";
			toll = 1.75
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
		
		steptext += "Exit at " + document.forms[0].end.options[document.forms[0].end.selectedIndex].text + "<br>";
		
		// Willow Avenue
		if (endvalue == 4) {
			toll = 1.00
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
		
		// 22nd Street
		if (endvalue == 13) {
			toll = .75
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
		
		// 39th Street
		if (endvalue == 14) {
			toll = .75
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
		
		// 50th Street
		if (endvalue == 15) {
			toll = 1.00
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
	
	} else if (endvalue != -1) {
		
		// Westbound
		document.getElementById("directioncell").innerHTML += "<b>Traveling Westbound</b>";
		steptext += "Enter at " + document.forms[0].start.options[document.forms[0].start.selectedIndex].text + "<br>";
		
		// Willow Avenue
		if (startvalue == 4) {
			toll = 1.00
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
		
		// Plant Avenue
		if (startvalue == 5) {
			toll = 1.25
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
		
		// 22nd Street
		if (startvalue == 13) {
			toll = .75
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
		
		// 39th Street
		if (startvalue == 14) {
			toll = 1.00
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
		
		// 50th Street
		if (startvalue == 15) {
			toll = 1.00
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
		
		if (tolls == 0) {
			tolltext += "&nbsp;<br>";
		}
		
		// West Toll Plaza
		if (startvalue >= 6 && endvalue <= 4) {
			steptext += "West Toll Plaza<br>";
			toll = 1.25
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
		
		// East Toll Plaza
		if (startvalue >= 16 && endvalue <= 15) {
			steptext += "East Toll Plaza<br>";
			toll = 1.75
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b><br>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b><br>";
				tolls += axles * toll;
			}
		}
		steptext += "Exit at " + document.forms[0].end.options[document.forms[0].end.selectedIndex].text + "<br>";
		
		// Plant Avenue
		if (endvalue == 5) {
			toll = .75
			if (sunpass) {
				tolltext += "<b>" + cent(axles * (toll - sundisc)) + "</b>";
				tolls += axles * (toll - sundisc);
			} else {
				tolltext += "<b>" + cent(axles * toll) + "</b>";
				tolls += axles * toll;
			}
		}
	}
	
	document.getElementById("stepcell").innerHTML = steptext;
	document.getElementById("tollcell").innerHTML = tolltext;
	document.getElementById("totalcell").innerHTML = "<b>Total Tolls:</b>";
	document.getElementById("totaltollcell").innerHTML = "<b>" + cent(tolls) + "</b>";
	
}

function cent(amount) {
	amount -= 0;
	var returnamount = (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
	if (returnamount == "0.00") {
		return "No&nbsp;tolls";
	} else {
		return "$" + returnamount;
	}
}
