
//function OnLoad()
//{
//	with (document.mainform){
		//prepopulate control from question Code
//		Q9000.value = DATA_Q9000.value;
//		Q9001.value = DATA_Q9001.value;
//		Q9002.value = DATA_Q9002.value;
//		Q19003.value = DATA_Q19003.value;
//	}
//}

function OnLoadOffer()
{
	with (document.mainform)
	{
		if (IsOffer1(DATA_Q9000.value))
		{
			document.getElementById('offer1').style.display='block';
		}
		else if (IsOffer2(DATA_Q9000.value))
		{szDetail1 = "e offer";
			nPrice1  = 123;	
			szDetail2 = "f offer";
			nPrice2 = 123;
			bMultiProduct = true;
			document.getElementById('offer2').style.display='block';
		}
	}
}

function IsOffer1(model)
{
	var bRet = false;
	var reg1 = /^[Ss].*/;
	var reg2 = /^[Qq].*/;
//	var reg3 = /^[Ll].*/;

	bRet = reg1.test(model);

	if (!bRet)
	{
		bRet = reg2.test(model);
	}
/*	if (!bRet)
	{
		bRet = reg3.test(modelszDetail1 = "e offer";
			nPrice1  = 123;	
			szDetail2 = "f offer";
			nPrice2 = 123;
			bMultiProduct = true;);
	}
*/

	return bRet;
}

function IsOffer2(model)
{
	var bRet = false;
	var reg1 = /^[Tt].*/;
	var reg2 = /^[Pp].*/;

	bRet = reg1.test(model);
	if (!bRet)
	{
		bRet = reg2.test(model);
	}
	return bRet;
}

function OnLoadBuy()
{
	with (document.mainform)
	{
		for (var i=0; i<Q00511.length; i++)
		{
			if (Q00511.options[i].value == DATA_Q00057.value)
			{
				Q00511.selectedIndex = i;
			}
		}
	}
}

function OnLoadConfirmPage()
{
	var idx;
	
	with (document.mainform)
	{
		idx = parseInt(DATA_Q00504.value);
		Q00504.options[idx].selected = true;

		idx = parseInt(DATA_Q00506.value);
		Q00506.options[idx].selected = true;
	
		idx = parseInt(DATA_Q00507.value - 2006);	
		Q00507.options[idx].selected = true;

		for (var i=0; i<Q00511.length; i++)
		{
			if (Q00511.options[i].value == DATA_Q00511.value)
			{
				Q00511.selectedIndex = i;
			}
		}

		var szDetail1, szDetail2;
		var nPrice1, nPrice2, nSubtotal, nTax, nTotal;
		var bMultiProduct = false;
		var bNoWarrenty = false;
		nPrice1 = 0;
		nPrice2 = 0;
		nTax	= 0;
		
		if (DATA_Q20000.value == "35")
		{
			szDetail1 = "WSN-PEGQ3R-3 Year SystemGuard + 2nd/3rd Year Extended Service Plan";
			nPrice1  = 269.00;	
		}
		if (DATA_Q20000.value == "27")
		{
			szDetail1 = "WSN-PEGP3R-3 Year SystemGuard + Service Express + 2nd/3rd Year Extended Service Plan";
			nPrice1 = 319.00;
		}
		if (DATA_Q20000.value == "37")	//**
		{
			szDetail1 = "WSN-PEGN3R-3 Year SystemGuard + On-Site Repair + 2nd/3rd Year Extended Service Plan";
			nPrice1 = 359.00;
		}
		if (DATA_Q20000.value == "29") //**
		{
			szDetail1 = "WSN-PEQQ3R-2nd/3rd Year Extended Service Plan";
			nPrice1 = 139.00; 
		}
		if (DATA_Q20000.value == "30")
		{
			szDetail1 = "WSN-PEQQ4R-2nd/3rd/4th Year Extended Service Plan";
			nPrice1 = 209.00;
		}
		if (DATA_Q20000.value == "1")
		{
			szDetail1 = "WSN-PEGQ3R-3 Year SystemGuard + 2nd/3rd Year Extended Service Plan";
			nPrice1  = 269.00;
			szDetail2 = "LFL-PE-36-3 Years of LoJack for Laptops";
			nPrice2 = 99.95;
			bMultiProduct = true;
		}
		if (DATA_Q20000.value == "2")
		{
			szDetail1 = "WSN-PEGP3R-3 Year SystemGuard + Service Express + 2nd/3rd Year Extended Service Plan";
			nPrice1 = 319.00;	
			szDetail2 = "LFL-PE-36-3 Years of LoJack for Laptops";
			nPrice2 = 99.95;
			bMultiProduct = true;
		}
		if (DATA_Q20000.value == "3")
		{
			szDetail1 = "WSN-PEGN3R-3 Year SystemGuard + On-Site Repair + 2nd/3rd Year Extended Service Plan";
			nPrice1 = 359.00;	
			szDetail2 = "LFL-PE-36-3 Years of LoJack for Laptops";
			nPrice2 = 99.95;
			bMultiProduct = true;
		}
		if (DATA_Q20000.value == "4")
		{
			szDetail1 = "WSN-PEQQ3R-2nd/3rd Year Extended Service Plan";
			nPrice1 = 139.00; 	
			szDetail2 = "LFL-PE-36-3 Years of LoJack for Laptops";
			nPrice2 = 99.95;
			bMultiProduct = true;
		}
		if (DATA_Q20000.value == "5")
		{
			szDetail1 = "WSN-PEQQ4R-2nd/3rd/4th Year Extended Service Plan";
			nPrice1 = 209.00;	
			szDetail2 = "LFL-PE-36-3 Years of LoJack for Laptops";
			nPrice2 = 99.95;
			bMultiProduct = true;
		}
		if (DATA_Q20000.value == "9")
		{
			szDetail2 = "LFL-PE-36-3 Years of LoJack for Laptops";
			nPrice2 = 99.95;
			bNoWarrenty = true;
		}
		if (DATA_Q20000.value == "12")
		{
			//g offer
			szDetail1 = "WSN-PEGQ3R-3 Year SystemGuard + 2nd/3rd Year Extended Service Plan(bundle)";
			nPrice1  = 219.05;	
			szDetail2 = "LFL-PE-36-3 Years of LoJack for Laptops(bundle)";
			nPrice2 = 99.95;
			bMultiProduct = true;
		}

		
		nTotal = nPrice1 + nPrice2 + nTax;

		//nGST.toFixed(2);
		//nGST = ChopTailingZero(nGST, 2);
		//nHST.toFixed(2);
		//nHST = ChopTailingZero(nHST, 2);
		//nPST.toFixed(2);
		//nPST = ChopTailingZero(nPST, 2);

		nPrice1 = nPrice1.toFixed(2);
		//nPrice1 = ChopTailingZero(nPrice1, 2);
		nPrice2 = nPrice2.toFixed(2);
		nTotal = nTotal.toFixed(2);
		//nTotal = ChopTailingZero(nTotal, 2);
	
		//product in first line
		if (!bNoWarrenty)
		{
			Q50001.value = szDetail1;
			Q50000.value = nPrice1;
		}
		
		//product in second line, if any
		if (bNoWarrenty || bMultiProduct)
		{
			Q50006.value = szDetail2;
			Q50005.value = nPrice2;
		}

		//total amount
		Q00503.value = nTotal;
	}
}

function ValidateSubmit()															 
{
	with (document.mainform)
	{
		ShouldSubmit.value = '0';
		
		var pCountryCode = null;

		if (calling_page.value == "01_Offer1")
		{
			//First Page
		/*	pCountryCode = Q00015;

			if (pCountryCode.value == "Please")
			{
				alert("Please select your country.");
				return false;
			}*/
			//Warranty Selection validation

			if   (!document.mainform.Q20000a.checked &&
				!document.mainform.Q20000b.checked && 
				!document.mainform.Q20000c.checked && 
				!document.mainform.Q20000d.checked && 
				!document.mainform.Q20000e.checked && 
				!document.mainform.Q20000f.checked && 
				!document.mainform.Q20000g.checked)

			{
				alert("Please select one of the service plans.");
				return false;
			}
			//Product Model validation
		/*	if (Q9000.value == "")
			{
				alert("Please enter your product model.");
				return false;
			}
			//Part number validation
			//if (Q9001.value == "")
			//{
			//	alert("Please enter your part number.");
			//	return false;
			//}
			//Serial number validation
			if (Q9002.value == "")
			{
				alert("Please enter your serial number.");
				return false;
			}
			//Purchase Date validation
			if (Q19003.value == "")
			{
				alert("Please enter your purchase date.");
				return false;
			}*/			
		}
		else if (calling_page.value == "02_buy")
		{
			//Second Page
			pCountryCode = DATA_Q00015;

	
			if (Q00002.value == "")
			{
				alert("Please enter your First Name."); <!-- First Name -->   
				return false; 
			}
			if (Q00004.value == "")
			{
				alert("Please enter your Last Name."); <!-- Last Name -->  
				return false;

			}

			if (!ValidateRegEx(Q00519.value, "([-.\\w]+@[-.\\w]+\\.\\w{2,3})"))
			{
				alert("Please provide your Email Address in the following format: myname@email.com."); <!-- Email -->
				return false;
			}
			//if (!ValidateRegEx(confirmEmail.value, "([-.\\w]+@[-.\\w]+\\.\\w{2,3})"))
			//{
			//	alert("Please confirm your Email Address."); <!-- Email -->
			//	return false;
			//}
			if (!ValidateRegEx(Q00012.value, "([0-9]{3}\-\[0-9]{3}\-\[0-9]{4})"))
			{
				alert("Please enter a phone number with the format 555-555-5555.");
				return false;
			}
			//if (Q00519.value != confirmEmail.value)
			//{
			//	alert("Please provide your correct Email Address"); <!--email confirmation-->
			//	return false;
			//}
			if (Q00508.value == "")
			{
				alert("Please enter in the name on the credit card."); <!--CC name-->
				return false;
			}

			if (Q00509.value == "")
			{
				alert("Please enter your Billing Address."); <!--Address-->
				return false;
			}
			if (Q00510.value == "")
			{
				alert("Please enter your City or Town."); <!--City/Town-->
				return false;
			}
			if (Q00511.value == "n_a")
			{
				alert("Please select your state from the drop down list.");
				return false;
			}

			if (!ValidateRegEx(Q00512.value, "^[0-9]{5}(\-[0-9]{4})?$"))
			{
				alert("Please provide your zip code in the following format: 12345 or 12345-1234"); 
				return false;
			}
	

			//if (Q00520.value == "n_a")
			//{
			//	alert("Please select a Province from the list."); <!--Province-->
			//	return false;
			//}

			//if (pCountryCode.value == '2') // check for appropriate zip code
			//{
				// validata canadian postal code
			//	if (!ValidateRegEx(Q00512.value, "([A-Za-z][0-9][A-Za-z] [0-9][A-Za-z][0-9])"))
			//	{
			//		alert("Please provide your Postal Code in the following format: B2C 3D4.");
			//		return false;
			//	}
			//
			//	}
			//
			//phone number
/*			if (!ValidateRegEx(Q00012_AREA.value, "[0-9]{3}"))
			{
				alert("Please provide your three digit area code of your phone number.");
				return false;
			}
			if (!ValidateRegEx(Q00012_PREFIX.value, "[0-9]{3}"))
			{
				alert("Please provide the first three digits of your phone number.");
				return false;
			}
			if (!ValidateRegEx(Q00012_POSTFIX.value, "[0-9]{4}"))
			{
				alert("Please provide the last four digits of your phone number.");
				return false;
			}

			Q00012.value = Q00012_AREA.value + "-" + Q00012_PREFIX.value + "-" + Q00012_POSTFIX.value;

			// validate phone number if anything is entered
			if (Q21000_AREA.value != "" || Q21000_PREFIX.value != "" || Q21000_POSTFIX.value != "")
			{
				if (!ValidateRegEx(Q21000_AREA.value, "[0-9]{3}"))
				{
					alert("Please provide your three digit area code of your evening phone number.");
					return false;
				}
				if (!ValidateRegEx(Q21000_PREFIX.value, "[0-9]{3}"))
				{
					alert("Please provide the first three digits of your evening phone number.");
					return false;
				}
				if (!ValidateRegEx(Q21000_POSTFIX.value, "[0-9]{4}"))
				{
					alert("Please provide the last four digits of your evening phone number.");
					return false;
				}

				Q21000.value = Q21000_AREA.value + "-" + Q21000_PREFIX.value + "-" + Q21000_POSTFIX.value;
			}
*/		
			//email address
//			if (!ValidateRegEx(Q00014.value, "(\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,3})"))
			if (!ValidateRegEx(Q00505.value, "[0-9]{16}"))
			{
				alert("Please provide your sixteen digit credit card number."); <!-- Email -->
				return false;
			}
			if (Q00504.value == "n_a")
			{
		   	//Purchase Place
				alert("Please select the type of credit card you are using..");
				return false;
			}

			if (Q00506.value == "n_a")
			{
		   	//Purchase Place
				alert("Please select the month of your credit card's expiration date.");
				return false;
			}
			if (Q00507.value == "n_a")
			{
		   	//Purchase Place
				alert("Please select the year of your credit card's expiration date.");
				return false;
			}

		}			
		else if (calling_page.value == "03_confirm")
		{
			if (!Q20001.checked)
			{
				alert("Please accept terms and conditions");
				return false;
			}
		}

		CalculateProductPurchaseCode();

		//successfully validated
		ShouldSubmit.value = '1';
	}
	
	return true;
}

function CalculateProductPurchaseCode()
{
	with(document.mainform)
	{
		//with Lo-Jack
		if (Q20000f.checked == true)
		{
			if (Q20000a.checked == true)
			{
				Q20000.value = "1";
			}
			else if (Q20000b.checked == true)
			{
				Q20000.value = "2";
			}
			else if (Q20000c.checked == true)
			{
				Q20000.value = "3";
			}
			else if (Q20000d.checked == true)
			{
				Q20000.value = "4";
			}
			else if (Q20000e.checked == true)
			{
				Q20000.value = "5";
			}
			else
			{
				//Lo-Jack only
				Q20000.value = "9";
			}
		}	
		else if (Q20000a.checked == true)	//start of W/O Lo-Jack
		{
			Q20000.value = "35";
		}
		else if (Q20000b.checked == true)
		{
			Q20000.value = "27";
		}
		else if (Q20000c.checked == true)
		{
			Q20000.value = "37";
		}
		else if (Q20000d.checked == true)
		{
			Q20000.value = "29";
		}
		else if (Q20000e.checked == true)
		{
			Q20000.value = "30";
		}
		else if (Q20000g.checked == true)	//bundle product
		{
			Q20000.value = "12"
		}
	}
}

function ValidateRegEx(Value, RegularExpression )
{
	var StrValue = new String(arguments[0]);
	var RegEx = new RegExp(arguments[1]);

	if (StrValue.match(RegEx))
		return true;
	else
		return false;
}
								
function change(which) 
{
    document.getElementById('1').style.display = 'none';
    document.getElementById('2').style.display = 'none';

	if (which != "n_a")
	{
	    document.getElementById(which).style.display = 'block';
	}
}

function InternationalRegistration()
{
	document.mainform.ShouldSubmit.value = '1';
	document.mainform.InternationalReg.value = '1';
	document.mainform.submit();
}


function ShowPreferences()
{
	with (document.mainform)
	{
		document.getElementById('contact').style.display = 'none';
		document.getElementById('pref').style.display = 'block';

		ShouldSubmit.value = '0';
	}
}

function ShowContact()
{
    document.getElementById('pref').style.display = 'none';
    document.getElementById('contact').style.display = 'block';

	document.mainform.ShouldSubmit.value = '0';
}

function SubmitOnlyOnContinue()
{
	if (document.mainform.ShouldSubmit.value == '1')
		return true;
	else
		return false;
}

function ClientCloseWindow( boolRegOnline, nClientMajVer )
{
	window.close();
	window.open('http://www.broderbund.com/jump.jsp?itemID=442&itemType=CATEGORY','Incentives');
}

function ShowPrivacyPolicy( strQueryString )
{
	var windowprops = "width=575,height=400,location=no,toolbar=no,menubar=no,scrollbars=yes";
	var url = "privacy_info.asp";
	if( strQueryString != "" ) url += "?" + strQueryString;
	window.open(url, "", windowprops);
}

function ValidateBirth()
{
	return (ValidateBirthMonth() && ValidateBirthYear())
}

function ValidateBirthMonth()
{
	with (document.mainform)
	{
		if (Q20026.value=="")
		{
			return true;
		}

		if (!ValidateRegEx(Q20026.value, "^[1-9]([0-2])?$") || Q20026.value > 12)
		{
			alert("Please enter a valid month from 1 to 12 for your month of birth.");
			return false;
		}
	}
	return true;
}

function ValidateBirthYear()
{
	with (document.mainform)
	{
		if (Q20027.value=="")
		{
			return true;
		}

		if (!ValidateRegEx(Q20027.value, "19[0-9][0-9]"))
		{
			alert("Please enter a valid year from 1900 to 1995 for your year of birth.");
			return false;
		}
	}
	return true;
}

function ClearChildAnswers(Form)
{
	checkControls(Form.Q20004[1], "false");
	checkControls(Form.Q20005[0], "false");
	checkControls(Form.Q20005[1], "false");
	checkControls(Form.Q20006[0], "false");
	checkControls(Form.Q20006[1], "false");
	checkControls(Form.Q20007[0], "false");
	checkControls(Form.Q20007[1], "false");
	checkControls(Form.Q20008[0], "false");
	checkControls(Form.Q20008[1], "false");

	Form.Q20009.value = "";
	Form.Q20010.value = "";
	Form.Q20011.value = "";
	Form.Q20012.value = "";
	Form.Q20009.disabled = true;
	Form.Q20010.disabled = true;
	Form.Q20011.disabled = true;
	Form.Q20012.disabled = true;
}

function ClearNoChildAnswer(Form, Question)
{
	checkControls(Form.Q20004[0], "false");
	Question.disabled = false;
}

function ClearNoCard(Form)
{
	checkControls(Form.Q20000g, "false");


}

function OnClickQ20000a(tgtform)
{
	  checkControls(tgtform.Q20000b,"false");
	  checkControls(tgtform.Q20000c,"false");
	  checkControls(tgtform.Q20000d,"false");
	  checkControls(tgtform.Q20000e,"false");
	  checkControls(tgtform.Q20000g,"false");
}
	
function OnClickQ20000b(tgtform)
{
	  checkControls(tgtform.Q20000a,"false");
	  checkControls(tgtform.Q20000c,"false");
	  checkControls(tgtform.Q20000d,"false");
	  checkControls(tgtform.Q20000e,"false");
	  checkControls(tgtform.Q20000g,"false");
}
function OnClickQ20000c(tgtform)
{
	  checkControls(tgtform.Q20000a,"false");
	  checkControls(tgtform.Q20000b,"false");
	  checkControls(tgtform.Q20000d,"false");
	  checkControls(tgtform.Q20000e,"false");
	  checkControls(tgtform.Q20000g,"false");
}
function OnClickQ20000d(tgtform)
{
	  checkControls(tgtform.Q20000a,"false");
	  checkControls(tgtform.Q20000b,"false");
	  checkControls(tgtform.Q20000c,"false");
	  checkControls(tgtform.Q20000e,"false");
	  checkControls(tgtform.Q20000g,"false");
}
function OnClickQ20000e(tgtform)
{
	  checkControls(tgtform.Q20000a,"false");
	  checkControls(tgtform.Q20000b,"false");
	  checkControls(tgtform.Q20000c,"false");
	  checkControls(tgtform.Q20000d,"false");
	  checkControls(tgtform.Q20000g,"false");
}


function clearControls(tgtform)
{
	  checkControls(tgtform.Q20000a,"false");
	  checkControls(tgtform.Q20000b,"false");
	  checkControls(tgtform.Q20000c,"false");
	  checkControls(tgtform.Q20000d,"false");
	  checkControls(tgtform.Q20000e,"false");
	  checkControls(tgtform.Q20000f,"false");
}


// Sets the check state for a given control to either checked or unchecked
function checkControls(ctrl, checkflag)
{
	if (checkflag == "true")
	{
		ctrl.checked = true;
	}
	else
	{
		ctrl.checked = false;
	}
}

function CheckChildrensAges()
{
	with (document.mainform)
	{
		if (!CheckAge(Q20005, Q20009) || !CheckAge(Q20006, Q20010) 
			|| !CheckAge(Q20007, Q20011) || !CheckAge(Q20008, Q20012))

			return false;
	}
	return true;
}

function CheckAge(Question, Age)
{
	if ((Question[0].checked || Question[1].checked) 
		&&  
		(!ValidateRegEx(Age.value, "^[1-9]([0-9])?([0-9])?$") || Age.value > 125))
	{
		alert("Please enter an age for your household member.");
		Age.focus();
		return false;
	}

	return true;
}

function ValidateBirthAndAge()
{
	return ValidateBirth() && CheckChildrensAges();
}

function privacy()
{
window.open('http://www.toshibadirect.com/td/b2c/privacypolicy.to','priv','left=100,top=200,width=500,height=575,resizable=no,scrollbars=yes');
}

function terms()
{
window.open('http://www.toshibadirect.com/td/b2c/termsofuse.to','term','left=100,top=200,width=500,height=575,resizable=0,scrollbars=1');
}
function legal()
{
window.open('http://www.toshibadirect.com/td/b2c/legalstmt.to','sales','left=100,top=200,width=600,height=575,resizable=yes,scrollbars=yes');
}
function security()
{
window.open('http://www.toshibadirect.com/td/b2c/securitystmt.to','secu','left=100,top=200,width=500,height=575,resizable=no,scrollbars=yes');
}

function warranty()
{
window.open('http://toshibasales.datalodex.com/warranty_info.html','secu','left=100,top=200,width=700,height=700,resizable=no,scrollbars=yes');
}
function lojack()
{
window.open('http://toshibasales.datalodex.com/Lojack_info.html','secu','left=100,top=200,width=700,height=700,resizable=no,scrollbars=yes');
}
function refund()
{
window.open('http://toshibasales.datalodex.com/refund_info.html','secu','left=100,top=200,width=700,height=700,resizable=no,scrollbars=yes');
}
function TC()
{
window.open('http://toshibasales.datalodex.com/TC_info.html','secu','left=100,top=200,width=700,height=700,resizable=no,scrollbars=yes');
}
/*****************************************/
/** Usable Forms 2.0, November 2005     **/
/** Written by ppk, www.quirksmode.org  **/
/** Instructions for use on my site     **/
/**                                     **/
/** You may use or change this script   **/
/** only when this copyright notice     **/
/** is intact.                          **/
/**                                     **/
/** If you extend the script, please    **/
/** add a short description and your    **/
/** name below.                         **/
/*****************************************/

var containerTag = 'TR';

var compatible = (
	document.getElementById && document.getElementsByTagName && document.createElement
	&&
	!(navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1)
	);

if (compatible)
{
	document.write('<style>.accessibility{display: none}</style>');
	var waitingRoom = document.createElement('div');
}

var hiddenFormFieldsPointers = new Object();

function prepareForm()
{
	if (!compatible) return;
	var marker = document.createElement(containerTag);
	marker.style.display = 'none';

	var x = document.getElementsByTagName('select');
	for (var i=0;i<x.length;i++)
		addEvent(x[i],'change',showHideFields)

	var x = document.getElementsByTagName(containerTag);
	var hiddenFields = new Array;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].getAttribute('rel'))
		{
			var y = getAllFormFields(x[i]);
			x[i].nestedRels = new Array();
			for (var j=0;j<y.length;j++)
			{
				var rel = y[j].getAttribute('rel');
				if (!rel || rel == 'none') continue;
				x[i].nestedRels.push(rel);
			}
			if (!x[i].nestedRels.length) x[i].nestedRels = null;
			hiddenFields.push(x[i]);
		}
	}

	while (hiddenFields.length)
	{
		var rel = hiddenFields[0].getAttribute('rel');
		if (!hiddenFormFieldsPointers[rel])
			hiddenFormFieldsPointers[rel] = new Array();
		var relIndex = hiddenFormFieldsPointers[rel].length;
		hiddenFormFieldsPointers[rel][relIndex] = hiddenFields[0];
		var newMarker = marker.cloneNode(true);
		newMarker.id = rel + relIndex;
		hiddenFields[0].parentNode.replaceChild(newMarker,hiddenFields[0]);
		waitingRoom.appendChild(hiddenFields.shift());
	}
	
	setDefaults();
	addEvent(document,'click',showHideFields);
}

function setDefaults()
{
	var y = document.getElementsByTagName('input');
	for (var i=0;i<y.length;i++)
	{
		if (y[i].checked && y[i].getAttribute('rel'))
			intoMainForm(y[i].getAttribute('rel'))
	}

	var z = document.getElementsByTagName('select');
	for (var i=0;i<z.length;i++)
	{
		if (z[i].options[z[i].selectedIndex].getAttribute('rel'))
			intoMainForm(z[i].options[z[i].selectedIndex].getAttribute('rel'))
	}

}

function showHideFields(e)
{
	if (!e) var e = window.event;
	var tg = e.target || e.srcElement;

	if (tg.nodeName == 'LABEL')
	{
		var relatedFieldName = tg.getAttribute('for') || tg.getAttribute('htmlFor');
		tg = document.getElementById(relatedFieldName);
	}
		
	if (
		!(tg.nodeName == 'SELECT' && e.type == 'change')
		&&
		!(tg.nodeName == 'INPUT' && tg.getAttribute('rel'))
	   ) return;

	var fieldsToBeInserted = tg.getAttribute('rel');

	if (tg.type == 'checkbox')
	{
		if (tg.checked)
			intoMainForm(fieldsToBeInserted);
		else
			intoWaitingRoom(fieldsToBeInserted);
	}
	else if (tg.type == 'radio')
	{
		removeOthers(tg.form[tg.name],fieldsToBeInserted)
		intoMainForm(fieldsToBeInserted);
	}
	else if (tg.type == 'select-one')
	{
		fieldsToBeInserted = tg.options[tg.selectedIndex].getAttribute('rel');
		removeOthers(tg.options,fieldsToBeInserted);
		intoMainForm(fieldsToBeInserted);
	}
}

function removeOthers(others,fieldsToBeInserted)
{
	for (var i=0;i<others.length;i++)
	{
		var show = others[i].getAttribute('rel');
		if (show == fieldsToBeInserted) continue;
		intoWaitingRoom(show);
	}
}

function intoWaitingRoom(relation)
{
	if (relation == 'none') return;
	var Elements = hiddenFormFieldsPointers[relation];
	for (var i=0;i<Elements.length;i++)
	{
		waitingRoom.appendChild(Elements[i]);
		if (Elements[i].nestedRels)
			for (var j=0;j<Elements[i].nestedRels.length;j++)
				intoWaitingRoom(Elements[i].nestedRels[j]);
	}
}

function intoMainForm(relation)
{
	if (relation == 'none') return;
	var Elements = hiddenFormFieldsPointers[relation];
	for (var i=0;i<Elements.length;i++)
	{
		var insertPoint = document.getElementById(relation+i);
		insertPoint.parentNode.insertBefore(Elements[i],insertPoint);
		if (Elements[i].nestedRels)
		{
			var fields = getAllFormFields(Elements[i]);
			for (var j=0;j<fields.length;j++)
			{
				if (!fields[j].getAttribute('rel')) continue;
				if (fields[j].checked || fields[j].selected) 
					intoMainForm(fields[j].getAttribute('rel'));
			}
		}
	}
}

function getAllFormFields(node)
{
	var allFormFields = new Array;
	var x = node.getElementsByTagName('input');
	for (var i=0;i<x.length;i++)
		allFormFields.push(x[i]);
	var y = node.getElementsByTagName('option');
	for (var i=0;i<y.length;i++)
		allFormFields.push(y[i]);
	return allFormFields;
}

/** ULTRA-SIMPLE EVENT ADDING **/

function addEvent(obj,type,fn)
{
	if (obj.addEventListener)
		obj.addEventListener(type,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent("on"+type,fn);
}

addEvent(window,"load",prepareForm);


/** PUSH AND SHIFT FOR IE5 **/

function Array_push() {
	var A_p = 0
	for (A_p = 0; A_p < arguments.length; A_p++) {
		this[this.length] = arguments[A_p]
	}
	return this.length
}

if (typeof Array.prototype.push == "undefined") {
	Array.prototype.push = Array_push
}

function Array_shift() {
	var A_s = 0
	var response = this[0]
	for (A_s = 0; A_s < this.length-1; A_s++) {
		this[A_s] = this[A_s + 1]
	}
	this.length--
	return response
}

if (typeof Array.prototype.shift == "undefined") {
	Array.prototype.shift = Array_shift
}

// Tax qcodes:
//	HST - 526
//	GST - 525
//	PST - 527
//Goods & Services	Harmonized 	General 	Base for 
//Tax (GST)	Sales Tax	Rate of 	PST Including
//							(HST)	Tax (PST)	GST (a)
//
//New Foundland				N/A		15%		N/A		N/A
//Nova Scotia				N/A		15%		N/A		N/A
//New Brunswick				N/A		15%		N/A		N/A
//Prince Edward Island		7%		N/A		10%		Yes
//Ontario					7%		N/A		8%		No
//Manitoba					7%		N/A		7%		No
//Saskatchewan				7%		N/A		7%		No
//British Columbia			7%		N/A		7%	No
//Quebec					7%		N/A		7.5%	Yes
//Alberta					7%		N/A		No PST	No
//Yukon						7%		N/A		No PST	No
//North West Terretories	7%		N/A		No PST	No
//Nunavut					7%		N/A		No PST	No

//QC 520 (province) response codes

//10	NF
//11	NS
//9		NB
//14	PE
//13	ON
//8		MB
//17	SK
//6		BC
//16	QC
//2		AB
//19	YT
//12	NT
//15	NU

function GetGSTTax(subtotal, province)
{
	switch(province)
	{
		case "9":
		case "10":
		case "11":
			return 0;
		default:
			return (.06 * subtotal);
	}
}

function GetHSTTax(subtotal, province)
{
	switch(province)
	{
		case "9":
		case "10":
		case "11":
			return (.14 * subtotal);
		default:
			return 0;
	}
}

function GetPSTTax(subtotal, province)
{
	hsttax = "0";
	switch(province)
	{
		case "14":
			hsttax  = (.07 * subtotal) + subtotal;
			return (.1 * hsttax);
		case "13":
			return (.08 * subtotal);
		case "8":
			return (.07 * subtotal);
		case "17":
			return (.07 * subtotal);
		case "6":
			return (.07 * subtotal);
		case "16":
			hsttax = (.07 * subtotal) + subtotal;
			return (.075 * hsttax );
		default:
			return 0;
	}
}

function ChopTailingZero(input, offset)
{
	var nRet = input;
	var exp = Math.pow(10, offset);
	nRet *= exp;
	nRet = Math.round(nRet);
	nRet /= exp;

	return nRet;
}
