/* copyright © 2002 - 2007 JAM Performance Transmissions, LLC  All rights reserved.  */
/*
To DO:
Get all prices from this JS file, to be used by all pricing screens.
Change text in comboboxes to Arial 	0pt
*/

var totalRows = parts.length;

//set the 1st item in all select boxes & set all price fields
function init()
{	
	initSelects();
	var elements = document.Table3FORM.elements;
	for (var i = 0; i < elements.length; i++)
	{
		var element = elements[i];
		if (element.type=="checkbox" )
		{			
			var partId = element.name.substring(2);
			var part = findPartForDesc(partId);

			if (part != null)
			{
				var priceField = getField("Price" + partId);
				if (priceField != null)
				{	

					if (element.checked == true)
					{
						priceField.value = part[2];
					}
					else
						priceField.value = "";
				}
			}			
		}
	}
	calculateTotal();
}

//only do this on first time load!
function initSelects()
{
//TODO better to iterate rather than hard code


	if (initialLoad == false)
	{
		initSelectOptions(ChoiceGMTransmissions, getField("ChoiceGMTransmission"));
		initSelectOptions(ChoiceShifters, getField("ChoiceShifter"));
		initSelectOptions(ChoiceSlipYokes, getField("ChoiceSlipYoke"), null);
		initSelectOptions(Choice5spSpeedoAdapters, getField("Choice5spSpeedoAdapter"));
		initSelectOptions(Choice5spShifterBalls, getField("Choice5spShifterBall"));
		initSelectOptions(ChoiceGMClutches, getField("ChoiceGMClutch"));
		initSelectOptions(ChoiceTransMounts, getField("ChoiceTransMount"));
		
		var elements = document.Table3FORM.elements;
		for (var i = 0; i < elements.length; i++)
		{
			var element = elements[i];
			if (element.name.indexOf("Choice")> -1)
			{
				selectChanged(element);
			}	
		}
		initialLoad == true; 
	}
}

function hide(divId, showItBoolean)
{

   if (showItBoolean == true || showItBoolean == "true")
	   getField(divId).style.display="block";
   else
	   getField(divId).style.display="none";
}


function initSelectOptions(choiceItemsArray, selectObject)
{
	for (var i = 0; i < choiceItemsArray.length; i++)
	{
		var part = choiceItemsArray[i];
		var partId = part[0];
		var partPrice = part[2];
		var partDesc = part[1];
		var option = new Option(partDesc + " $" + partPrice,partPrice);
		selectObject.options[i] = option;
		var partIdToPreselect = selectObject.defaultPartId;
		if (partIdToPreselect != null && partIdToPreselect == partId)
		{
			selectObject.options[i].selected = true;
		}
	}
}

function selectChanged(item)
{
	var partId= item.id.substring(6,item.id.length);
	var cb = getField("CB" + partId);

	if (cb != null)
	{
		var isChecked = cb.checked;
	    if (isChecked == true)
	    {
	    	var price = item.options[item.selectedIndex].value;
	    	getField("Price" + partId).value = price;
	    }
	    else
	    {
		getField("Price" + partId).value = "";	
	    }
	}
	calculateTotal();
}


function cbChanged(item)
{
	var partId = item.id.substring(2,item.id.length);
	var isChecked = item.checked;
	if (isChecked == true)
	{
		//get select, and call selectChanged
		var select = getField("Choice" + partId);
		if (select != null)  
		{
			selectChanged(select);
		}
		else
		{
			var part = parts[item.itemId];
			var partId = part[0];
			var partPrice = part[2];		
	  		getField("Price" + partId).value = partPrice ;
		}
	}
	else
	{
		getField("Price" + partId).value = "";	
	}
	calculateTotal();
}

function calculateTotal()
{

	if (initialLoad == false)
	{
		var subTotalPrice = 0;
		var elements = document.Table3FORM.elements;
		for (var i = 0; i < elements.length; i++)
		{
			var element = elements[i];
			if (element.name.indexOf("CB")> -1)
			{
	  			var isChecked = element.checked;
		  		if (isChecked == true)
			  	{
					var partId = element.id.substring(2,element.id.length);
					subTotalPrice = subTotalPrice  + parseFloat(getField("Price" + partId).value);
		  		}			
			}	
		}	
	}
	
	getField("SubtotalPrice").value = subTotalPrice;
	var shipping = parseFloat(getField("ShippingPrice").value);
	getField("TotalPrice").value = subTotalPrice + shipping;
}
 
function getField(fieldId)
{
	var field = document.getElementById(fieldId);
	//if (field == null)
	//      alert("didn't find " + fieldId);
	//else
		return field; 

}


function getEmailRequestText()
{

var bodyMsg1 = "";

bodyMsg1 += "(Required) Phone number you can be reached at:\r\n\r\n";
bodyMsg1 += "I would like to request:\r\n";
bodyMsg1 += "  Swap details about my application (yes/no):\r\n";
bodyMsg1 += "  A formal quote for a swap (yes/no):\r\n";
bodyMsg1 += "Here's some details about my application:\r\n";
bodyMsg1 += "  Which transmission(s) are you interested in?  \r\n";
bodyMsg1 += "  Vehicle Make:  \r\n";
bodyMsg1 += "  Vehicle Model: \r\n";
bodyMsg1 += "  Year of Vehicle: \r\n";
bodyMsg1 += "  Engine description (hp, size, year of block, etc):\r\n";
bodyMsg1 += "  Current Transmission:\r\n";
bodyMsg1 += "  Rear end gear ratio:\r\n";
bodyMsg1 += "  Rear Tire size:\r\n";
bodyMsg1 += "  Bucket seats? \r\n";
bodyMsg1 += "  Intended use of this car/truck?\r\n";
bodyMsg1 += "  Zip code of your shipping address:\r\n";
bodyMsg1 += "  Additional questions/comments:\r\n";
bodyMsg1 += "\r\n";
return bodyMsg1;

}

function getQuote()
{

	var bodyMsg1 = getEmailRequestText();
	var bodyMsg = "";
	var form = document.Table3FORM;
	if (form != null)
	{
		var elements = document.Table3FORM.elements;
		for (var i = 0; i < elements.length; i++)
		{
			var element = elements[i];
			if (element.name.indexOf("CB")> -1)
			{
	  			var isChecked = element.checked;
		  		if (isChecked == true)
			  	{
					var desc = "";
					var partId = element.id.substring(2,element.id.length);
					//if select, use that first
					var select = getField("Choice" + partId);
					if (select != null)
					{
					  	desc = select.options[select.selectedIndex].text;
						bodyMsg += desc + "\r\n";
					}
					else
					{
						var cell = getField("Part" + partId);
						if (cell != null)
						{	
							var price = getField("Price" + partId).value;
							bodyMsg += cell.desc + " $" + price + "\r\n";
						}
					}
	 			 }
	  		}			
	  	}	

		var subTotal = getField("SubtotalPrice").value;
		var shipping = getField("ShippingPrice").value;
		var total = getField("TotalPrice").value;

		bodyMsg += "\r\n\r\n";
		bodyMsg += "Subtotal = " + subTotal + "\r\n";
		bodyMsg += "Shipping = " + shipping + "\r\n";
		bodyMsg += "Total = " + total + "\r\n";
	}

	sendEmail(bodyMsg1 + bodyMsg);
}

function sendEmail(msg)
{
	var mailToUrl = "mailto:sales@5speedtransmissions.com?subject=Quote Request&body=" + escape(msg);
	window.location = mailToUrl ;
}

function alreadyHaveManualTransClicked(cb)
{
	var alreadyHaveManualTrans = cb.checked;
//checkuncheck the following items:

	var elements = document.Table3FORM.elements;
	for (var i = 0; i < elements.length; i++)
	{
		var element = elements[i];
		if (element.name.indexOf("CB")> -1)
		{

			if (alreadyHaveManualTrans == true)
			{
				//only check the ones designated for a manual swap
				var isForManualConversion = element.isForManualConversion;

				if (isForManualConversion != null)
				{
					element.checked = (element.isForManualConversion=="true");
				}
				else
				{
					element.checked = false;
				}
			}
			else
			{
				element.checked = true;//check them all
			}
		}
	}

	//Change shipping est
	if (alreadyHaveManualTrans)
		getField("ShippingPrice").value = "90.00";
	else
		getField("ShippingPrice").value = "120.00";

	init();
}


function initPricesOnly()
{

	var elements = document.Table3FORM.elements;
	var subtotal = 0;
	for (var i = 0; i < elements.length; i++)
	{
		var element = elements[i];
		if (element.type=="text")
		{			
			var partId = element.name.substring(5);
			if (partId != null)
			{
				var part= findPartForDesc(partId);
				if (part != null)
				{
					var partPrice = part[2];
					subtotal = subtotal + parseFloat(partPrice);		
					element.value = partPrice;
				}
			}
		}
	}

	//now calc the totals
	getField("SubtotalPrice").value = subtotal
	var shipping = parseFloat(getField("ShippingPrice").value);
	getField("TotalPrice").value = subtotal + shipping;

}
