function load()
{
	document.getElementById("javascriptmessage").style.display = "none";
	Calculate();
}

function CheckForm(form)
{
	var Total = Calculate();

	var f = form;
	
	if (f.FirstName.value.length < 1)
	{
		alert("Please enter your First Name.");
		f.FirstName.focus();
		return false
	}

	if (f.LastName.value.length < 1)
	{
		alert("Please enter your Last Name.");
		f.LastName.focus();
		return false
	}

	if (f.Address1.value.length < 1)
	{
		alert("Please enter your Address.");
		f.Address1.focus();
		return false
	}

	if (f.City.value.length < 1)
	{
		alert("Please enter your City.");
		f.City.focus();
		return false
	}

	if (f.State.value.length < 1)
	{
		alert("Please enter your State.");
		f.State.focus();
		return false
	}

	if (f.Zip.value.length < 1)
	{
		alert("Please enter your Zip Code.");
		f.Zip.focus();
		return false
	}

	if (f.AreaCode.value.length < 1)
	{
		alert("Please enter your full Phone Number.");
		f.AreaCode.focus();
		return false
	}

	if (f.Prefix.value.length < 1)
	{
		alert("Please enter your full Phone Number.");
		f.Prefix.focus();
		return false
	}

	if (f.PhoneNumber.value.length < 1)
	{
		alert("Please enter your full Phone Number.");
		f.PhoneNumber.focus();
		return false
	}

	if (f.Email.value.length < 1)
	{
		alert("Please enter your Email.");
		f.Email.focus();
		return false
	}

	if (Total <= 0)
	{
		alert("Please correct problems in red above");
		return false
	}

return false

}//end CheckForm()



function Calculate()
{			
	var Total = 0 ;
	var Msg;

	var regex = new RegExp("[0-9]");

	vehiclesUsedAtOnce = 0;
	paidVehicles = 0;
	paidForPeople = 0;
	
	var inputUsedVehicles = document.getElementById("txtVehiclesUsed");
	if ((inputUsedVehicles.value.length < 1) || !regex.test(inputUsedVehicles.value))
	{
		Msg = "Please enter a non-negative number";
		SetMessage("UsedVehicles", Msg, 0);
	}
	else if (parseFloat(inputUsedVehicles.value) < 0)
	{
		Msg = "Please enter a non-negative number";
		SetMessage("UsedVehicles", Msg, 0);
	}
	else
	{
		SetMessage("UsedVehicles", "", 0)
		vehiclesUsedAtOnce = inputUsedVehicles.value;
	}
	
	// Calculate the Vehicles first, as the Person fees are dependant upon them
	// Passenger Vehicles
	for (i=1; i <= PassengerVehicleCount; i++)
	{
		var inp1 = document.getElementById("PassengerVehicleType" + i);

		if (inp1.value.length == 0)
		{
			Msg = "";
			SetMessage("PassengerVehicleFee" + i, Msg, 0);
		}
		else if (inp1.value.length < 1)	
		{
			Total = -1;
			Msg = "Please choose a Vehicle Type";
			SetMessage("PassengerVehicleFee" + i, Msg, 1);
		}
		else 
		{
			if (i <= vehiclesUsedAtOnce)
			{
				// Add this vehicles cost to the total
				if (Total >= 0)
					Total += parseFloat(inp1.value);
					
				Msg = "$" + parseFloat(inp1.value).toFixed(2);
				SetMessage("PassengerVehicleFee" + i, Msg, 0);
				paidVehicles++;
			}
			else 
			{
				// This one won't be used, no cost
				Msg = "$0 - Above number used at once";
				SetMessage("PassengerVehicleFee" + i, Msg, 0);
			}

		}
	}
	// Camping Vehicles
	for (i=1; i <= CampingVehicleCount; i++)
	{
		var inp1 = document.getElementById("CampingVehicleType" + i);

		if (inp1.value.length == 0)
		{
			Msg = "";
			SetMessage("CampingVehicleFee" + i, Msg, 0);
		}
		else if (inp1.value.length < 1)	
		{
			Total = -1;
			Msg = "Please choose a Vehicle Type";
			SetMessage("CampingVehicleFee" + i, Msg, 1);
		}
		else 
		{
			if (Total >= 0)
				Total += parseFloat(inp1.value);
				
			Msg = "$" + parseFloat(inp1.value).toFixed(2);
			SetMessage("CampingVehicleFee" + i, Msg, 0);
			paidVehicles++;
		}
	}
	// Other Vehicles
	for (i=1; i <= OtherVehicleCount; i++)
	{
		var inp1 = document.getElementById("OtherVehicleType" + i);

		if (inp1.value.length == 0)
		{
			Msg = "";
			SetMessage("OtherVehicleFee" + i, Msg, 0);
		}
		else if (inp1.value.length < 1)	
		{
			Total = -1;
			Msg = "Please choose a Vehicle Type";
			SetMessage("OtherVehicleFee" + i, Msg, 1);
		}
		else 
		{
			if (Total >= 0)
				Total += parseFloat(inp1.value);
				
			Msg = "$" + parseFloat(inp1.value).toFixed(2);
			SetMessage("OtherVehicleFee" + i, Msg, 0);
			paidVehicles++;
		}
	}

	// Person Fees calculations
	for (i=1; i <= HouseholdCount; i++)
	{
		var inp1 = document.getElementById("Age" + i);

		if (inp1.value.length == 0)
		{
			Msg = "";
			SetMessage("Fee" + i, Msg, 0);
		}
		else if (inp1.value > 150 || !regex.test(inp1.value))
		{
			Total = -1;
			Msg = "Please enter Age between 1 and 150";
			SetMessage("Fee" + i, Msg, 1);
		}
		else if (inp1.value < 16)
		{
			Msg = "No charge, under 16";
			SetMessage("Fee" + i, Msg, 0);
		}
		else
		{
			// People only have to pay if there are no paid vehicles covering them
			if ((paidForPeople < paidVehicles ) && (paidVehicles > 0))
			{
				// This one is covered by a vehicle
				paidForPeople++;
				Msg = "$0 : Covered by Vehicle #" + paidForPeople;
				SetMessage("Fee" + i, Msg, 0);
			}
			else
			{
				// This one has to pay a fee
				if (Total >= 0)
					Total += ExtraAdultCost;			

				Msg = "$" + parseFloat(ExtraAdultCost).toFixed(2);
				SetMessage("Fee" + i, Msg, 0);
			}
		}
	
	}
	
	SetMessage("subTotal", "$" + parseFloat(Total).toFixed(2),0);
	
    //Calculate sales tax
    var taxPer = 0.06;
    var totalTax = Total * taxPer;
    
    Total += totalTax;
    //alert(totalTax);
    SetMessage("salestax", "$" + parseFloat(totalTax).toFixed(2),0);
	//if (Total < 1)
	//	SetMessage("totalfee", "Please correct problems in red above", 1);
	//else
		SetMessage("totalfee", "$" + parseFloat(Total).toFixed(2), 0);
		

return Total;
}//end Calculate()


function SetMessage(Target, Message, Problem)
{
	if (Problem)
	{
		var font = document.createElement("font");
		font.style.color = "red";
		font.style.fontStyle = "italic";

		var msg = document.createTextNode(Message);

		font.appendChild(msg);

		var mycell = document.getElementById(Target);

		if (mycell.hasChildNodes())
			mycell.replaceChild(font, mycell.firstChild);
		else
			mycell.appendChild(font);
	
	}
	else
	{
		var font = document.createElement("font");
		font.style.color = "black";

		var msg = document.createTextNode(Message);

		font.appendChild(msg);

		var mycell = document.getElementById(Target);

		if (mycell.hasChildNodes())
			mycell.replaceChild(font, mycell.firstChild);
		else
			mycell.appendChild(font);

	}


}//end SetMessage

function AddPassengerVehicle()
{
	PassengerVehicleCount++;

	var table = document.getElementById('passengerVehicleTable');

	var tr = document.createElement('TR');
	var td1 = document.createElement('TD');
	var td2 = document.createElement('TD');
	var inp1  = document.getElementById('PassengerVehicleType1').cloneNode(true);

	inp1.setAttribute("Name", "PassengerVehicleType" + PassengerVehicleCount);
	td2.setAttribute("Name", "PassengerVehicleFee" + PassengerVehicleCount);

	inp1.setAttribute('id', "PassengerVehicleType" + PassengerVehicleCount);
	td2.setAttribute("id", "PassengerVehicleFee" + PassengerVehicleCount);

	inp1.value = '';

	table.appendChild(tr);
	tr.appendChild(td1);
	tr.appendChild(td2);

	td1.appendChild(inp1);

	// if there is only one line left, disable the delete link
	if (PassengerVehicleCount > 1) {
		document.getElementById("spanRemovePassengerVehicle").style.visibility = "visible";
	} else {
		document.getElementById("spanRemovePassengerVehicle").style.visibility = "hidden";
	}
	
return;

}//end AddPassengerVehicle()

function AddCampingVehicle()
{
	CampingVehicleCount++;

	var table = document.getElementById('campingVehicleTable');

	var tr = document.createElement('TR');
	var td1 = document.createElement('TD');
	var td2 = document.createElement('TD');
	var inp1  = document.getElementById('CampingVehicleType1').cloneNode(true);

	inp1.setAttribute("Name", "CampingVehicleType" + CampingVehicleCount);
	td2.setAttribute("Name", "CampingVehicleFee" + CampingVehicleCount);

	inp1.setAttribute('id', "CampingVehicleType" + CampingVehicleCount);
	td2.setAttribute("id", "CampingVehicleFee" + CampingVehicleCount);

	inp1.value = '';

	table.appendChild(tr);
	tr.appendChild(td1);
	tr.appendChild(td2);

	td1.appendChild(inp1);

	// if there is only one line left, disable the delete link
	if (CampingVehicleCount > 1) {
		document.getElementById("spanRemoveCampingVehicle").style.visibility = "visible";
	} else {
		document.getElementById("spanRemoveCampingVehicle").style.visibility = "hidden";
	}
	
return;

}//end AddCampingVehicle()

function AddOtherVehicle()
{
	OtherVehicleCount++;

	var table = document.getElementById('otherVehicleTable');

	var tr = document.createElement('TR');
	var td1 = document.createElement('TD');
	var td2 = document.createElement('TD');
	var inp1  = document.getElementById('OtherVehicleType1').cloneNode(true);

	inp1.setAttribute("Name", "OtherVehicleType" + OtherVehicleCount);
	td2.setAttribute("Name", "OtherVehicleFee" + OtherVehicleCount);

	inp1.setAttribute('id', "OtherVehicleType" + OtherVehicleCount);
	td2.setAttribute("id", "OtherVehicleFee" + OtherVehicleCount);

	inp1.value = '';

	table.appendChild(tr);
	tr.appendChild(td1);
	tr.appendChild(td2);

	td1.appendChild(inp1);

	// if there is only one line left, disable the delete link
	if (OtherVehicleCount > 1) {
		document.getElementById("spanRemoveOtherVehicle").style.visibility = "visible";
	} else {
		document.getElementById("spanRemoveOtherVehicle").style.visibility = "hidden";
	}
	
return;

}//end AddOtherVehicle()

function AddHouseholdMember()
{
	HouseholdCount++;

	var table = document.getElementById("householdmembertable");

	var tr = document.createElement("TR");
	var td1 = document.createElement("TD");
	var td2 = document.createElement("TD");
	var td3 = document.createElement("TD");
	var inp1  = document.getElementById("Age1").cloneNode(true);

	inp1.setAttribute("Name", "Age" + HouseholdCount);
	td3.setAttribute("Name", "Fee" + HouseholdCount);

	inp1.setAttribute("id", "Age" + HouseholdCount);
	td3.setAttribute("id", "Fee" + HouseholdCount);

	inp1.value = "";
	
	table.appendChild(tr);
	tr.appendChild(td1);
	tr.appendChild(td2);
	tr.appendChild(td3);

	// Add person label
	var font = document.createElement("font");
	font.style.color = "orange";
	var msg = document.createTextNode("Person #" + HouseholdCount);
	font.appendChild(msg);
	td1.appendChild(font);

	td2.appendChild(inp1);

	// if there is only one line left, disable the delete link
	if (HouseholdCount > 1) {
		document.getElementById("spanRemoveHouseholdMember").style.visibility = "visible";
	} else {
		document.getElementById("spanRemoveHouseholdMember").style.visibility = "hidden";
	}
	
return

}//end AddHouseholdMember()

function RemovePassengerVehicle() {
	var tableBody = document.getElementById("passengerVehicleTable");
	tableBody.deleteRow(PassengerVehicleCount);
	PassengerVehicleCount--;
	
	if (PassengerVehicleCount < 2) {
		document.getElementById("spanRemovePassengerVehicle").style.visibility = "hidden";
	} else {
		document.getElementById("spanRemovePassengerVehicle").style.visibility = "visible";
	}
	Calculate();
}
function RemoveCampingVehicle() {
	var tableBody = document.getElementById("campingVehicleTable");
	tableBody.deleteRow(CampingVehicleCount);
	CampingVehicleCount--;
	
	if (CampingVehicleCount < 2) {
		document.getElementById("spanRemoveCampingVehicle").style.visibility = "hidden";
	} else {
		document.getElementById("spanRemoveCampingVehicle").style.visibility = "visible";
	}
	Calculate();
}
function RemoveOtherVehicle() {
	var tableBody = document.getElementById("otherVehicleTable");
	tableBody.deleteRow(OtherVehicleCount);
	OtherVehicleCount--;
	
	if (OtherVehicleCount < 2) {
		document.getElementById("spanRemoveOtherVehicle").style.visibility = "hidden";
	} else {
		document.getElementById("spanRemoveOtherVehicle").style.visibility = "visible";
	}
	Calculate();
}
function RemoveHouseholdMember()
{
	var tableBody = document.getElementById("householdmembertable");
	
	// Attempt to delete the last row added (highest index)
	tableBody.deleteRow(HouseholdCount);
	
	HouseholdCount--;
	
	// if there is only one person left, disable the delete link
	if (HouseholdCount < 2) {
		document.getElementById("spanRemoveHouseholdMember").style.visibility = "hidden";
	} else {
		document.getElementById("spanRemoveHouseholdMember").style.visibility = "visible";
	}
	
	// Recalculate the form
	Calculate();
} //end RemoveHouseholdMember()

function AddMap()
{
	MapCount++;

	var table = document.getElementById('mapTable');

	var tr = document.createElement('TR');
	var td1 = document.createElement('TD');
	var td2 = document.createElement('TD');
	var td3 = document.createElement('TD');
	var inp1  = document.getElementById('Map1').cloneNode(true);
	var inp2 = document.getElementById('Qty1').cloneNode(true);

	inp1.setAttribute("Name", "Map" + MapCount);
	inp2.setAttribute("Name", "Qty" + MapCount);
	td3.setAttribute("Name", "Cost" + MapCount);

	inp1.setAttribute('id', "Map" + MapCount);
	inp2.setAttribute("id", "Qty" + MapCount);
	td3.setAttribute("id", "Cost" + MapCount);

	inp1.value = '';
	inp2.value = '';

	table.appendChild(tr);
	tr.appendChild(td1);
	tr.appendChild(td2);
	tr.appendChild(td3);

	td1.appendChild(inp1);
	td2.appendChild(inp2);

	return;

}//end AddMap()
