var avntPlanNav;	// ARRAY Structure: Tab ID | Nav Image On | Nav Image Off
var avntTabHistory;	// ARRAY Structure: Plan ID | Tab ID
var strCookieName = "TabHistory";
var re = new Image;
var fe = new Image;
var toggle = 1;
var intDiscountType = 0;
var intDiscountAmount = 0;
var fltDiscountRate = 0;


function preloadPlanNav(strTabs)
{
	var astrTabIDs = strTabs.split(",");
	
	avntPlanNav = Array(astrTabIDs.length);
	
	for (var intIndex = 0; intIndex < astrTabIDs.length; intIndex++)
	{
		avntPlanNav[intIndex] = Array(2);
		
		avntPlanNav[intIndex][0] = astrTabIDs[intIndex];
		avntPlanNav[intIndex][1] = new Image();
		avntPlanNav[intIndex][1].src = "images/nav/tab_" + astrTabIDs[intIndex] + "_on.gif";
		avntPlanNav[intIndex][2] = new Image();
		avntPlanNav[intIndex][2].src = "images/nav/tab_" + astrTabIDs[intIndex] + "_off.gif";
	}
}

function flipit()
{
	if (toggle == 1)
	{
		document.images['elevations'].src = re.src;
		document.getElementById("ELink").innerHTML = "Front Elevation";
		toggle = 2;
	}
	else if (toggle == 2)
	{
		document.images['elevations'].src = fe.src;
		document.getElementById("ELink").innerHTML = "Rear Elevation";
		toggle = 1;
	}
}

function ShowTab(strTabID)
{
	for (var intIndex = 0; intIndex < avntPlanNav.length; intIndex++)
	{
		if (strTabID == avntPlanNav[intIndex][0])
		{
			document.getElementById("img" + avntPlanNav[intIndex][0]).src = avntPlanNav[intIndex][1].src;
			document.getElementById("div" + avntPlanNav[intIndex][0]).style.display = 'block';
		//	usercookie = 
		}
		else
		{
			document.getElementById("img" + avntPlanNav[intIndex][0]).src = avntPlanNav[intIndex][2].src;
			document.getElementById("div" + avntPlanNav[intIndex][0]).style.display = 'none';
		}
	}
}


function ShowMoreInfo(divID)
{
	if (document.getElementById("div" + divID).style.display == 'inline')
	{
		document.getElementById("div" + divID).style.display = 'none';
		document.getElementById("lnk" + divID).innerHTML = 'Show detail';
		document.getElementById("img" + divID).src = 'images/arrowDown.gif';
	}
	else
	{
		document.getElementById("div" + divID).style.display = 'inline';
		document.getElementById("lnk" + divID).innerHTML = 'Hide detail';
		document.getElementById("img" + divID).src = 'images/arrowUp.gif';
	}
}

function ShowPics(PlanID)
{
	WindowOpener('slideshow.asp?id=' + PlanID, 'PlanPictures', 'width=550,height=550,toolbar=no,resizable=yes,scrollbars=yes,screenX=50,ScreenY=50,left=100,top=100');
}

function ShowAlts(PlanID)
{
	WindowOpener('slideshowAlt.asp?id=' + PlanID, 'Elevations', 'width=550,height=550,toolbar=no,resizable=yes,scrollbars=yes,screenX=50,ScreenY=50,left=100,top=100');
}

function ReverseImages()
{
	var strImageID = "";
	
	for (var intIndex = 0; intIndex < document.images.length; intIndex++)
	{
		if (document.images[intIndex].id.length > 0)
		{
			strImageID = document.images[intIndex].id.toString();
			
			if (strImageID != "elevations")
			{
				if (document.getElementById(strImageID).className == "normal")
				{
					document.getElementById(strImageID).className = "reverse";
					document.getElementById("ReverseLink_" + strImageID).innerHTML = "unreverse plan";
				}
				else if (document.getElementById(strImageID).className == "reverse")
				{
					document.getElementById(strImageID).className = "normal";
					document.getElementById("ReverseLink_" + strImageID).innerHTML = "reverse plan";
				}
			}
			else
			{
				if (document.getElementById(strImageID).className == "normal")
				{
					document.getElementById("elevations").className = "reverse";
					document.getElementById("ReverseLink_elevations").innerHTML = "Unreverse Elevation";
				}
				else
				{
					document.getElementById("elevations").className = "normal";
					document.getElementById("ReverseLink_elevations").innerHTML = "Reverse Elevation";
				}
			}
		}
	}
}

function ShowQualityText(chrQuality)
{
	if (chrQuality == "P")
	{
		document.getElementById("divPremiumWarning").style.display = "block";
	}
	else
	{
		document.getElementById("divPremiumWarning").style.display = "none";
	}
}

function SubmitPlanOrder()
{
	if (blnValidatePlanOrder())
	{
		document.frmPricing.submit();
	}
}

function blnValidatePlanOrder()
{
	var objPlanType = null;
	var intIndex = 0;
	var strErrorMsg = "";
	
	
	with (document.frmPricing)
	{
		if (PlanType.length > 0)
		{
			while ((objPlanType == null) && (intIndex < PlanType.length))
			{
				(PlanType[intIndex].checked) ? objPlanType = PlanType[intIndex] : intIndex++;
			}
		}
		else if (PlanType.checked)
		{
			objPlanType = PlanType;
		}
		
		
		if (objPlanType != null)
		{
			//Study Sets do not have foundations
			if (objPlanType.value.toLowerCase() != "studyset")
			{
				//If Construction Set selected, verify Mirror Reversed sets less than total number of sets selected
				if ((objPlanType.value.toLowerCase() == "constructionset") && (avntConstructionOptions.length > 1))
				{
					var intTotalSets = 5;
					intIndex = 0;
					
					for (intIndex = 0; intIndex < avntConstructionOptions.length; intIndex++)
					{
						var objConstructionOption = elements[avntConstructionOptions[intIndex][0]];
						
						//Additional Sets
						if (avntConstructionOptions[intIndex][0] == "additional_sets")
						{
							intTotalSets += parseInt(objConstructionOption[objConstructionOption.selectedIndex].value);
						}
						//Mirror Reversed Sets
						else if ((avntConstructionOptions[intIndex][0] == "mirror_read") && (objConstructionOption[objConstructionOption.selectedIndex].value >= intTotalSets))
						{
							strErrorMsg = "You have selected more Mirror Reversed Sets than Total Plan Sets.\nPlease select fewer Mirror Reversed Sets or more Additional Construction Sets.\n\n";
							objConstructionOption.focus();
						}
					}
				}
				
				//Verify a Foundation was selected
				var blnFoundationSelected = false;
				
				if (foundation.length > 0)
				{
					intIndex = 0;
					
					while ((! blnFoundationSelected) && (intIndex < foundation.length))
					{
						(foundation[intIndex].checked) ? blnFoundationSelected = true : intIndex++;
					}
				}
				else if (foundation.checked)
				{
					blnFoundationSelected = true;
				}
				
				if (! blnFoundationSelected)
				{
					if (strErrorMsg.length == 0)
					{
						(foundation.length > 1) ? foundation[0].focus() : foundation.focus();
					}
					
					strErrorMsg += "Please select a Foundation Option.\n";
				}
			}
		}
		else
		{
			strErrorMsg = "Please select a House Plan Package.\n";
			(PlanType.length > 1) ? PlanType[0].focus() : PlanType.focus();
		}
	}
	
	if (strErrorMsg.length == 0)
	{
		return true;
	}
	else
	{
		alert(strErrorMsg);
		return false;
	}
}


function CalculateTotal()
{
	var objPlanType = null;
	var blnStudySet = false;
	var intIndex = 0;
	var intTotal = 0;
	
	with (document.frmPricing)
	{
		/***************
		 Plan Type Price
		 ***************/
		
		// More than 1 plan type, find the selected value
		if (PlanType.length > 0)
		{
			while ((objPlanType == null) && (intIndex < PlanType.length))
			{
				if (PlanType[intIndex].checked)
				{
					objPlanType = PlanType[intIndex];
					intTotal = aintMediaPrices[intIndex];
				}
				else
				{
					intIndex++;
				}
			}
		}
		// Only 1 plan type, is it selected?
		else if (PlanType.checked)
		{
			objPlanType = PlanType;
			intTotal = aintMediaPrices[0];
		}
		
		// Plan type was selected, check for additional constraints
		if (objPlanType != null)
		{
			// Construction Set Option Price
			if ((objPlanType.value.toLowerCase() == "constructionset") || ((objPlanType.value.toLowerCase() == "eightset")))
			{
				(objPlanType.value.toLowerCase() == "eightset") ? document.getElementById("copyNumber").innerHTML = '8' : document.getElementById("copyNumber").innerHTML = '5';
				
				for (var intIndex2 = 0; intIndex2 < avntConstructionOptions.length; intIndex2++)
				{
					var objConstructionOption = elements[avntConstructionOptions[intIndex2][0]];
					
					// Mirror Reversed Sets
					if (avntConstructionOptions[intIndex2][0] == "mirror_read")
					{
						if (objConstructionOption[objConstructionOption.selectedIndex].value > 0)
						{
							intTotal += avntConstructionOptions[intIndex2][1];
						}
					}
					// Additional Sets
					else
					{
						intTotal += (objConstructionOption[objConstructionOption.selectedIndex].value * avntConstructionOptions[intIndex2][1]);
					}
				}
				
				EnableFoundations();
			}
			else if (objPlanType.value.toLowerCase() == "studyset")
			{
				blnStudySet = true;
				DisableFoundations();
			}
			else
			{
				EnableFoundations();
			}
		}
		
		
		/****************
		 Foundation Price
		 ****************/
		if (! blnStudySet)
		{
			if (foundation.length > 0)
			{
				blnValueFound = false;
				intIndex = 0;
				
				while ((! blnValueFound) && (intIndex < foundation.length))
				{
					if (foundation[intIndex].checked)
					{
						blnValueFound = true;
						intTotal += aintFoundationPrices[intIndex];
					}
					else
					{
						intIndex++;
					}
				}
			}
			else if (foundation.checked)
			{
				intTotal += aintFoundationPrices[0];
			}
		}
		
		
		/*******************
		 Other Options Price
		 *******************/
		for (intIndex = 0; intIndex < avntOtherOptions.length; intIndex++)
		{
			if (elements[avntOtherOptions[intIndex][0]].checked)
			{
				intTotal += avntOtherOptions[intIndex][1];
			}
		}
	}
	
	
	/******************************
	 Calculate the Discounted Price
	 ******************************/
	if (intDiscountType > 0)
	{
		document.getElementById('spnSubtotal').innerHTML = strCurrency(intTotal);
		
		if (! blnStudySet)
		{
			if (intDiscountType == 1)
			{
				if (intTotal > intDiscountAmount)
				{
					intTotal -= intDiscountAmount;
					document.getElementById('spnDiscount').innerHTML = "- " + strCurrency(intDiscountAmount);
				}
			}
			else if (intDiscountType == 2)
			{
				var fltDiscount = intTotal * fltDiscountRate;
				
				intTotal -= fltDiscount;
				document.getElementById('spnDiscount').innerHTML = "- " + strCurrency(fltDiscount);
			}
		}
		else
		{
			document.getElementById('spnDiscount').innerHTML = "- " + strCurrency(0);
		}
	}
	
	
	document.getElementById('spnTotal').innerHTML = strCurrency(intTotal);
}


function strCurrency(fltValue)
{
	//Separate the parts of the Currency
	var intWholeNums = parseInt(fltValue);
	var fltDecimals = fltValue - intWholeNums;
	
	//Format the Whole Numbers
	var strValue = intWholeNums.toString();
	var objRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
	
	while(objRegExp.test(strValue))
	{
		//replace original string with first group match, a comma, then second group match
		strValue = strValue.replace(objRegExp, '$1,$2');
	}
	
	//Format the R
	if (fltDecimals == 0)
	{
		strValue += ".00";
	}
	else
	{
		fltDecimals *= 100;
		strValue += "." + Math.round(fltDecimals);
	}
	
	return "$" + strValue;
}

function SubmitCostToBuild()
{
	if (blnValidateCostToBuild())
	{
		document.frmCostToBuild.submit();
	}
}


function blnValidateCostToBuild()
{
	var blnErrorFound = false;
	var blnFoundationSelected = false;
	var blnQualitySelected = false;
	var intIndex = 0;
	var strErrorMsg = "";
	
	with (document.frmCostToBuild.ZipCode)
	{
		value = value.trim();
		
		if (value.length == 0)
		{
			focus();
			blnErrorFound = true;
			strErrorMsg = "Please enter the Zip or Postal Code of the construction location.\n";
		}
		else if ((! blnValidateZipCode(value)) && (! blnValidatePostalCode(value)))
		{
			focus();
			blnErrorFound = true;
			strErrorMsg = "Please enter a valid Zip or Postal Code of the construction location.\n";
		}
	}
	
	with (document.frmCostToBuild)
	{
		//Foundation Type
		if (foundation.length > 0)
		{
			while ((! blnFoundationSelected) && (intIndex < foundation.length))
			{
				(foundation[intIndex].checked) ? blnFoundationSelected = true : intIndex++;
			}
		}
		else if (foundation.checked)
		{
			blnFoundationSelected = true;
		}
		
		if (! blnFoundationSelected)
		{
			strErrorMsg += "Please select a Foundation Type.\n";
			
			if (! blnErrorFound)
			{
				(foundation.length > 1) ? foundation[0].focus() : foundation.focus();
				blnErrorFound = true;
			}
		}
		
		intIndex = 0;
		
		//Construction Quality
		while ((! blnQualitySelected) && (intIndex < quality.length))
		{
			(quality[intIndex].checked) ? blnQualitySelected = true : intIndex++;
		}
		
		if (! blnQualitySelected)
		{
			strErrorMsg += "Please select the Construction Quality.\n";
			
			if (! blnErrorFound)
			{
				quality[0].focus();
				blnErrorFound = true;
			}
		}
	}
	
	if (blnErrorFound)
	{
		alert(strErrorMsg);
	}
	
	return (! blnErrorFound);
}


function DisableFoundations()
{
	with (document.frmPricing)
	{
		if (foundation.length > 0)
		{
			for (var intIndex = 0; intIndex < foundation.length; intIndex++)
			{
				foundation[intIndex].checked = false;
				foundation[intIndex].disabled = true;
			}
		}
		else
		{
			foundation.checked = false;
			foundation.disabled = true;
		}
	}
}


function EnableFoundations()
{
	with (document.frmPricing)
	{
		if (foundation.length > 0)
		{
			for (var intIndex = 0; intIndex < foundation.length; intIndex++)
			{
				foundation[intIndex].disabled = false;
			}
		}
		else
		{
			foundation.checked = true;
			foundation.disabled = false;
		}
	}
}


function AdjustMirrorRevOptions()
{
	if (avntConstructionOptions.length > 1)
	{
		with (document.frmPricing)
		{
			var objPlanType = null;
			var intIndex = 0;
			var intConstructionSets = 5;
			
			// More than 1 plan type, find the selected value
			if (PlanType.length > 0)
			{
				while ((objPlanType == null) && (intIndex < PlanType.length))
				{
					(PlanType[intIndex].checked) ? objPlanType = PlanType[intIndex] : intIndex++;
				}
			}
			// Only 1 plan type, is it selected?
			else if (PlanType.checked)
			{
				objPlanType = PlanType;
			}
			
			// Plan type was selected, check if construction set is 8 or 5 set
			if (objPlanType != null)
			{
				(objPlanType.value.toLowerCase() == "eightset") ? intConstructionSets = 8 : intConstructionSets = 5;
			}
			
			intConstructionSets += additional_sets.selectedIndex;
			
			if (intConstructionSets != mirror_read.options.length)
			{
				var strValue = "";
				
				//How many Mirror Reversed sets are currently selected
				var intMirrorRevSelected = mirror_read.selectedIndex;
				
				if (intMirrorRevSelected >= intConstructionSets)
				{
					intMirrorRevSelected = intConstructionSets - 1
				}
				
				//Clear the current values in the select field
				mirror_read.length = 0;
				
				//Create the new options
				for (var intIndex = 0; intIndex < intConstructionSets; intIndex++)
				{
					if (intIndex == intMirrorRevSelected)
					{
						mirror_read[intIndex] = new Option(intIndex, intIndex, false, true);
					}
					else
					{
						mirror_read[intIndex] = new Option(intIndex, intIndex, false, false);
					}
				}
			}
		}
	}
}


function SaveCookie()
{
	var strCookieText = "";
	
	if (avntTabHistory.length > 0)
	{
		for (var i = 0; i < avntTabHistory.length; i++)
		{
			if (strCookieText.length > 0)
			{
				strCookieText += "|" + avntTabHistory[i][0] + ":" + avntTabHistory[i][1];
			}
			else
			{
				strCookieText = avntTabHistory[i][0] + ":" + avntTabHistory[i][1];
			}
		}
	}
	
	document.cookie = strCookieName + '=' + escape(strCookieText);
}


function ReadCookie()
{
	var strCookieJar = document.cookie;
	
	if (strCookieJar.length > 0)
	{
		// Find the start of the Cookie Name
		var intCookieStart = strCookieJar.indexOf(strCookieName);	
		
		if (intCookieStart > -1)
		{
			// Skip the Cookie Name and '='
			intCookieStart += strCookieName.length + 1;
			
			// Find the end of the value string (i.e. the next ';').
			var intCookieEnd = strCookieJar.indexOf(';', intCookieStart);
			
			if (intCookieEnd == -1)
			{
				intCookieEnd = strCookieJar.length;
			}
			
			// Extract the cookie into an array
			var strCookieText = unescape(strCookieJar.substring(intCookieStart, intCookieEnd));
		}
	}
}