var maxHeight = 48;  //max height constant
var margin = 1.2;  //margin constant
var handling = 5; //shipping & handling constant
var dblSidedPrinting = 10;  //double sided printing dollar constant
var oneWeekTurn = 0;  //one week turn dollar constant
var twoDayTurn = 25;  //two day turn dollar constant
var oneDayTurn = 50;  //one day turn dollar constant
var errorMessage = "Your project requires a custom quote.  Please send us your requirements by <a href='/customquote.html'>clicking here</a>";  //max height error constant
var finalPrice;  //final price variable
var optionList;  //Variable to hold all of the options

//This function is the best function that I have found for rounding numbers in currency format.
function r2(n) 
{ 
  ans = (Math.round(n * 100))/100 + "" 
  dot = ans.indexOf(".",0) 
  if (dot == -1) {ans = ans + ".00"} 
  else if (dot == ans.length - 2) {ans = ans + "0"} 
  return ans 
} 

/* This function will calculate the price after every selection that can affect the price is made by the user.  Then it will display the
 * final price to the user.  If the user enters a value higher than the max height, it will display the error message constant.
 */
function calculatePrice()
{
	optionList = null;
	var height = document.getElementById("heightTextBox").value;
	var width = document.getElementById("widthTextBox").value;
	
	finalPrice = 0;  //Sets the finalPrice back to zero each time the price is changed.
	if(height > maxHeight && width > maxHeight) {
	
		document.getElementById("finalPrice").innerHTML = errorMessage;
	}
	//only calculate the final price if the height and width have both been filled in.
	else if(height > 0 && width > 0)
	{
		//To add anymore prices all you need to do is create a new else if statement, with the range that you would like to add
		//and then multiply the sqInches by the price per square inch.
		var sqInches = height * width;
		if(sqInches < 432)
		{
			finalPrice += 38;
		}
		
		else if(sqInches >= 432 && sqInches < 576)
		{
			finalPrice += sqInches * 0.074074 * margin + handling;
		}
		else if(sqInches >= 576 && sqInches < 720)
		{
			finalPrice += sqInches * 0.059722 * margin + handling;
		}
		else if(sqInches >= 720 && sqInches < 864)
		{
			finalPrice += sqInches * 0.069444 * margin + handling;
		}
		else if(sqInches >= 864 && sqInches < 1008)
		{
			finalPrice += sqInches * 0.052778 * margin + handling;
		}
		else if(sqInches >= 1008 && sqInches < 1152)
		{
			finalPrice += sqInches * 0.050794 * margin + handling;
		}
		else if(sqInches >= 1152 && sqInches < 1296)
		{
			finalPrice += sqInches * 0.049306 * margin + handling;
		}
		else if(sqInches >= 1296 && sqInches < 1440)
		{
			finalPrice += sqInches * 0.048148 * margin + handling;
		}
		else if(sqInches >= 1440 && sqInches < 1584)
		{
			finalPrice += sqInches * 0.047222 * margin + handling;
		}
		else if(sqInches >= 1584 && sqInches < 1728)
		{
			finalPrice += sqInches * 0.046465 * margin + handling;
		}
		else if(sqInches >= 1728 && sqInches < 1872)
		{
			finalPrice += sqInches * 0.045833 * margin + handling;
		}
		else if(sqInches >= 1872 && sqInches < 2016)
		{
			finalPrice += sqInches * 0.045299 * margin + handling;
		}
		else if(sqInches >= 2016 && sqInches < 2160)
		{
			finalPrice += sqInches * 0.044841 * margin + handling;
		}
		else if(sqInches >= 2160 && sqInches < 2304)
		{
			finalPrice += sqInches * 0.042500 * margin + handling;
		}
		else if(sqInches >= 2304 && sqInches < 2448)
		{
			finalPrice += sqInches * 0.042153 * margin + handling;
		}
		else if(sqInches >= 2448 && sqInches < 2592)
		{
			finalPrice += sqInches * 0.041846 * margin + handling;
		}
		else if(sqInches >= 2592 && sqInches < 2736)
		{
			finalPrice += sqInches * 0.041574 * margin + handling;
		}
		else if(sqInches >= 2736 && sqInches < 2880)
		{
			finalPrice += sqInches * 0.041330 * margin + handling;
		}
		else if(sqInches >= 2880 && sqInches < 3024)
		{
			finalPrice += sqInches * 0.041111 * margin + handling;
		}
		else if(sqInches >= 3024 && sqInches < 3168)
		{
			finalPrice += sqInches * 0.040913 * margin + handling;
		}
		else if(sqInches >= 3168 && sqInches < 3456)
		{
			finalPrice += sqInches * 0.040732 * margin + handling;
		}
		else if(sqInches >= 3456 && sqInches < 3744)
		{
			finalPrice += sqInches * 0.040417 * margin + handling;
		}
		else if(sqInches >= 3744 && sqInches < 3888)
		{
			finalPrice += sqInches * 0.038205 * margin + handling;
		}
		else if(sqInches >= 3888 && sqInches < 4032)
		{
			finalPrice += sqInches * 0.038086 * margin + handling;
		}
		
//Edit
		else if(sqInches >= 4032 && sqInches < 4320)
		{
			finalPrice += sqInches * 0.037976 * margin + handling;
		}

		else if(sqInches >= 4320 && sqInches < 4608)
		{
			finalPrice += sqInches * 0.037778 * margin + handling;
		}
		else if(sqInches >= 4608 && sqInches < 4752)
		{
			finalPrice += sqInches * 0.037604 * margin + handling;
		}
		else if(sqInches >= 4752 && sqInches < 4896)
		{
			finalPrice += sqInches * 0.037525 * margin + handling;
		}
		else if(sqInches >= 4896 && sqInches < 5184)
		{
			finalPrice += sqInches * 0.037451 * margin + handling;
		}
		else if(sqInches >= 5184 && sqInches < 5472)
		{
			finalPrice += sqInches * 0.037315 * margin + handling;
		}
		else if(sqInches >= 5472 && sqInches < 5616)
		{
			finalPrice += sqInches * 0.037193 * margin + handling;
		}
		else if(sqInches >= 5616 && sqInches < 5760)
		{
			finalPrice += sqInches * 0.037137 * margin + handling;
		}
		else if(sqInches >= 5760 && sqInches < 6048)
		{
			finalPrice += sqInches * 0.037083 * margin + handling;
		}
		else if(sqInches >= 6048 && sqInches < 6336)
		{
			finalPrice += sqInches * 0.036984 * margin + handling;
		}
		else if(sqInches >= 6336 && sqInches < 6480)
		{
			finalPrice += sqInches * 0.036894* margin + handling;
		}
		else if(sqInches >= 6480 && sqInches < 6912)
		{
			finalPrice += sqInches * 0.036852 * margin + handling;
		}
		else if(sqInches >= 6912 && sqInches < 7344)
		{
			finalPrice += sqInches * 0.036736 * margin + handling;
		}
		else if(sqInches >= 7344 && sqInches < 7488)
		{
			finalPrice += sqInches * 0.036634 * margin + handling;
		}
		else if(sqInches >= 7488 && sqInches < 7776)
		{
			finalPrice += sqInches * 0.036603 * margin + handling;
		}
		else if(sqInches >= 7776 && sqInches < 8064)
		{
			finalPrice += sqInches * 0.036543 * margin + handling;
		}
		else if(sqInches >= 8064 && sqInches < 8208)
		{
			finalPrice += sqInches * 0.036488 * margin + handling;
		}
		else if(sqInches >= 8208 && sqInches < 8640)
		{
			finalPrice += sqInches * 0.036462 * margin + handling;
		}
		else if(sqInches >= 8640 && sqInches < 9126)
		{
			finalPrice += sqInches * 0.036389 * margin + handling;
		}
		else if(sqInches >= 9126 && sqInches < 10368)
		{
			finalPrice += sqInches * 0.036302 * margin + handling;
		}
		else if(sqInches >= 10368 && sqInches < 10944)
		{
			finalPrice += sqInches * 0.036157 * margin + handling;
		}
		else if(sqInches >= 10944 && sqInches < 11520)
		{
			finalPrice += sqInches * 0.036096 * margin + handling;
		}

//edit
		else if(sqInches >= 11520)
		{
			finalPrice += sqInches * 0.036042 * margin + handling;
		}
		
		if(document.input.printedDropDownList.value == "vinyl")
		{
			optionList = "&option[]=Vinyl";
		}
		else if(document.input.printedDropDownList.value == "adhesive_vinyl")
		{
			optionList = "&option[]=Adhesive_Vinyl";
		}
		else if(document.input.printedDropDownList.value == "paper_matte")
		{
			optionList = "&option[]=Paper (Matte)";
		}
		else if(document.input.printedDropDownList.value == "paper_glossy")
		{
			optionList = "&option[]=Paper (Glossy)";
		}
		
		if(document.getElementById("doubleSideCheckBox").checked)
		{
			finalPrice += dblSidedPrinting;
			optionList += "&option[]=Double-Sided Printing";
		}
		
		if(document.getElementById("grommetsCheckBox").checked)
		{
			optionList += "&option[]=Grommets";
		}
		
		if(document.input.turnAround[0].checked)
		{
			finalPrice += oneWeekTurn;
			optionList += "&option[]=One Week Turn-Around";
		}
		else if(document.input.turnAround[1].checked)
		{
			finalPrice += twoDayTurn;
			optionList += "&option[]=Two-Day Turn-Around";
		}
		else if(document.input.turnAround[2].checked)
		{
			finalPrice += oneDayTurn;
			optionList += "&option[]=One-Day Turn-Around";
		}
		
		if(document.getElementById("quantityTextBox").value > 0)
		{
			displayPrice = r2(finalPrice) * document.getElementById("quantityTextBox").value;
		}
		else
		{
			document.getElementById("quantityTextBox").value = 1;
		}
		
		document.getElementById("finalPrice").innerHTML = "<b style='font-size:14px'>Total $" + r2(displayPrice) + "</b>";
		document.getElementById("purchaseLink").innerHTML = "<a href='https://azure.webserversystems.com/~webhaus/cart/2/phpcart.php?action=add&id=0001&descr=Custom+Banner&price=" + r2(finalPrice) + "&option[]=" + document.getElementById("heightTextBox").value + "h x " + document.getElementById("widthTextBox").value + "w" + optionList + "&quantity=" + document.getElementById("quantityTextBox").value + "'><img src='/cart/2/templates/PosterDance/images/pdanceOrder.gif' style='border:none' /></a>";
	}
	else
	{
		document.getElementById("finalPrice").innerHTML = "";
	}

}

//This function creates the hidden form entries that are needed to submit the form to the shopping cart, when the user clicks on 
//the Place Order button.
function submitForm()
{
	if(!document.getElementById("heightTextBox").value > maxheight && document.getElementById("heightTextBox").value > 0 && document.getElementById("widthTextBox").value > 0)
	{		
		var form = document.submitForm;
		form.innerHTML += "<input type='hidden' name='action' value='add' />";
		form.innerHTML += "<input type='hidden' name='quantity' value='1' />";
		form.innerHTML += "<input type='hidden' name='id' value='0001' />";
		form.innerHTML += "<input type='hidden' name='descr' value='Custom+Banner' />";
		form.innerHTML += "<input type='hidden' name='price' value='" + r2(finalPrice) + "' />";
		form.innerHTML += "<input type='hidden' name='option[]' value='" + document.getElementById("heightTextBox").value + "' />";
		form.innerHTML += "<input type='hidden' name='option[]' value='" + document.getElementById("widthTextBox").value + "' />";
		
		if(document.input.printedDropDownList.value == "vinyl")
		{
			form.innerHTML += "<input type='hidden' name='option[]' value='Vinyl' />";
		}
		else if(document.input.printedDropDownList.value == "adhesive_vinyl")
		{
			form.innerHTML += "<input type='hidden' name='option[]' value='Adhesive Vinyl' />";
		}
		else if(document.input.printedDropDownList.value == "paper_matte")
		{
			form.innerHTML += "<input type='hidden' name='option[]' value='Paper (Matte)' />";
		}
		else if(document.input.printedDropDownList.value == "paper_glossy")
		{
			form.innerHTML += "<input type='hidden' name='option[]' value='Paper (Glossy)' />";
		}
				
		if(document.getElementById("doubleSideCheckBox").checked)
		{
			form.innerHTML += "<input type='hidden' name='option[]' value='Double-Sided Printing:5' />";
		}
		
		if(document.getElementById("grommetsCheckBox").checked)
		{
			form.innerHTML += "<input type='hidden' name='option[]' value='Grommets' />";
		}
		
		if(document.input.turnAround[0].checked)
		{
			form.innerHTML += "<input type='hidden' name='option[]' value='One Week' />";
		}
		else if(document.input.turnAround[1].checked)
		{
			form.innerHTML += "<input type='hidden' name='option[]' value='Two-Day:25' />";
		}
		else if(document.input.turnAround[2].checked)
		{
			form.innerHTML += "<input type='hidden' name='option[]' value='One-Day:50' />";
		}
		
		document.submitForm.submit();
	}
}