// JavaScript Document

addLoadEvent(pageInit);

/* Page Inti * *************************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *
 *
 ****************************************************************************************/
 
function pageInit()
{
	setupDependencies();
	
	updatePriceWig(getElement("tableInputqty"));
	
	// Set WIG Explanations **************
	setWigExplanations();
}

function setupDependencies()
{
	var dependencyElements = getTagGroup("td", "colRight");
	var dependencyElement = "";
	
	if (dependencyElements.length > 0)
	{
		for(var ctr=0; ctr<dependencyElements.length; ctr++)
		{
			dependencyElement = dependencyElements[ctr];
			
			if (dependencyElement)
			{
				
				if ((dependencyElement.innerHTML.indexOf("<SELECT") != -1) || (dependencyElement.innerHTML.indexOf("<select") != -1))
				{
					
					var selectElement = dependencyElement.getElementsByTagName("select"); 
					
					if (selectElement.length > 0)
					{
						if (selectElement[0].onchange)
						{
							selectElement[0].onchange();
						}
					}
				}
			}
		}
	}
	
}

function contractcontent(omit){
var inc=0
while (ccollect[inc]){
if (ccollect[inc].id!=omit)
ccollect[inc].style.display="none"
inc++
}
}

function contractcontent_single(cid){
if (typeof ccollect!="undefined"){
	if (document.getElementById(cid)){	
		document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="none")? "none" : "none"
		}
	}
}

function expandcontent(cid){
if (typeof ccollect!="undefined"){
	if (document.getElementById(cid)){
		document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "block"
		}
	}
}

function checkShowElement(elementId)
{
	if (getElement(elementId) != null)
	{
		showElement(elementId);	
	}
}

function checkHideElement(elementId)
{
	if (getElement(elementId) != null)
	{
		hideElement(elementId);	
	}
}

function expandcontent_group(name, num)
{
	for (var ctr=0; ctr<=num; ctr++)
	{

		temp1 = name + "_" + ctr;
		checkShowElement(temp1)
	}
}

function contract_group(name, num)
{
	for (var ctr=0; ctr<=num; ctr++)
	{

		temp1 = name + "_" + ctr;
		checkHideElement(temp1)
	}
}

function check_content(form_name, value, cid, num)
{
	num++;

	if (form_name[form_name.selectedIndex].text==value)
	{
		for (var ctr=1; ctr<=num; ctr++)
		{
			temp = cid + ctr;
			checkShowElement(temp);
		}
		
	}
	else
	{
		for (var ctr=1; ctr<=num; ctr++)
		{
			temp = cid + ctr;
			checkHideElement(temp);
		}
	}
}

// Clear Option Values --------------------------------

function clear_option(option_name)
{
	document.getElementById(option_name).value = "";
}

function clear_option_value(option_name, trigger, test_value)
{
	if ( test_value.value == trigger )
	{
		document.getElementById(option_name).value = "";
	}
}

// Option dependency -----------------------------------
var no_contractcontent = false;

function clear_dependency()
{
	 no_contractcontent = false;
}

function check_dependency(value, option_setup, trigger)
{
		var temp1 = "";
		
		for( var temp_ctr=0; temp_ctr<=3; temp_ctr++ )
		{ 
			temp1 = option_setup + "_" + temp_ctr;
			
			if ( value.value == trigger )
			{
				no_contractcontent = true;
				checkShowElement(temp1);
			}
			else
			{
				if ( no_contractcontent == false )
				{
					checkHideElement(temp1);
				}
			}
		}

}

function check_dependency_value(value, main_option, option_setup, trigger, option_table_name)
{
	var temp1 = "";
	temp1 = option_setup;
	temp2 = main_option;
	
	check_dependency(value, temp2, trigger);	
	
	if ( value.value == trigger )
	{
		no_contractcontent = true;
		checkShowElement(temp1);
	}
	else
	{
		// Dropdown Options
		var elementCheck = document.getElementById(option_table_name);
		if (elementCheck != null)
		{
			document.getElementById(option_table_name).value = "";
		}
		
		checkHideElement(temp1);
		
		// Radio Btns
		var elementsCheck = getTagGroup("input", option_table_name + "_");
		elementCheck = "";
		
		if (elementsCheck.length > 0)
		{
			for(var ctr=0; ctr<elementsCheck.length; ctr++)
			{
				elementCheck = elementsCheck[ctr];
				
				if (elementCheck)
				{
					/*getElement("debugT").innerHTML += "display: " + getElement(temp1).style.display + "<br>";
					getElement("debugT").innerHTML += "elementCheck.id: " + elementCheck.id + "<br>";
					getElement("debugT").innerHTML += "elementCheck.value: " + elementCheck.value + "#<br><br>";
					getElement("debugT").innerHTML += "value: " + value.value + "#<br><br>";*/
					
					if (getElement(temp1).style.display != "block")
					{
						elementCheck.checked = false;	
					}
				}
			}
		}
		
		if ( no_contractcontent == false )
		{
			
		}
		
	}
	
}

// --------------------------------------------------------

function setup_content(form_name, value)
{
	if (value == "Light Box")
	{
		check_content(form_name, value, 'show_lb', 0);
	}
	else if ((value > 510000) & (value < 520000))
	{
		check_content(form_name, value, 'show_lbs', 0);
	}
	else
	{
		contractcontent();
	}

}

    
/*  Update Price WIG * *************************************************************
 *
 *	TAKES:		Qty Input Form
 * 	RETURNS:	NOTHING
 *
 *	NOTES:		Gets all the price elements that have a WIG (Whatever Is Greater) scheme
 *				based on whether or not both a percentage and price value was entered
 *				in the options configuration.
 *
 ****************************************************************************************/
 
	function updatePriceWig(qtyForm)
	{
		if (qtyForm)
		{
			var qtyValue = qtyForm.value;
			
			if (dataIsValid(qtyValue, "integer") == true)
			{
				var basePriceElement = getElement("basePrice"); 
				
				if (basePriceElement)
				{
					var basePrice = basePriceElement.innerHTML;
					basePrice = basePrice.replace(",", "");
					basePrice = parseFloat(basePrice);
					
					var wigOptionElements = getTagGroup("input", "wig.optionValue.");
					
					var iterateAction = function(optionElement, ctr)
					{
						var optionIdArray = optionElement.id.split(".");
						var optionTable = optionIdArray[2];
						var wigNum = optionIdArray[3];
						
						var optionValue = getElement("wig.optionValue." + optionTable + "." + wigNum).value;
						var priceValue = getElement("wig.priceValue." + optionTable + "." + wigNum).value;
						var percentValue = getElement("wig.pricePercent." + optionTable + "." + wigNum).value;
						var finalPrice = priceValue;
						
						var percentBasePrice = (basePrice * (percentValue/100)) * qtyValue;
															 
						if (percentBasePrice > priceValue)
						{
							finalPrice = percentBasePrice.toFixed(2);
						}
						
						// Set the new price **************************
						var formElement = getElement(optionTable);
						
						if (formElement.tagName == "SELECT")
						{
							for(var optionCtr=0; optionCtr<formElement.length; optionCtr++)
							{
								if (formElement.options[optionCtr].value == optionValue)
								{
									var formatedOptionPrice = formElement.options[optionCtr].text.replace(optionValue, "");
									formatedOptionPrice = formatedOptionPrice.replace(",", "");
									
									var optionPriceArray = formatedOptionPrice.split("$");
									optionPriceArray =  optionPriceArray[1].split(" ");
									var thisPriceValue = optionPriceArray[0];
									
									formElement.options[optionCtr].text = optionValue + formatedOptionPrice.replace(thisPriceValue, finalPrice);								
								}
							}
						}
						
					}
					
					iterateElementList(wigOptionElements, iterateAction);
					
				} // basePriceElement
			} // integer
		} // qtyForm
	}

/*  Set WIG Explanations * *************************************************************
 *
 *	TAKES:		NOTHING
 * 	RETURNS:	NOTHING
 *
 *	NOTES:		Based on whether or not a WIG price exists for the given options.
 *
 ****************************************************************************************/
 
	function setWigExplanations()
	{
		var wigElements = getTagGroup("input", "wig.optionValue.");
		var currentOption = "";
		var lastOption = "";
		
		var iterateAction = function (wigElement, ctr)
		{
			var arrayTemp 		= wigElement.id.split(".");
			var wigNum 			= arrayTemp[3];
			var optionTable 	= arrayTemp[2];
			var priceValue 		= getElement("wig.priceValue." + optionTable + "." + wigNum).value;
			var pricePercent	= getElement("wig.pricePercent." + optionTable + "." + wigNum).value;
			
			lastOption = currentOption
			currentOption = optionTable
			
			// One Explanation per option **************************
			if (currentOption != lastOption)
			{
				switch(optionTable)
				{
					case "lightbox_finish":
					
						var divContainer = document.createElement("div");
						divContainer.style.border = "solid 1px #c8c39e";
						divContainer.style.backgroundColor = "#f2efd9";
						divContainer.style.padding = "5px";
						divContainer.style.color = "#000000";
						divContainer.style.margin = "20px 0px 10px 0px";
						divContainer.style.textAlign = "justify";
						
						divContainer.innerHTML = "<b>Note:</b> For this lightbox model, some of the powdercoat finish option costs vary depending on quantity ordered.";
						divContainer.innerHTML += " These varying finish option costs are based on a minimum <b>per order</b> charge.";
						
						getElement("colRightlightbox_finish").appendChild(divContainer);
						
						break;
						
					default:
						break;
				}
			}
		}
		
		iterateElementList(wigElements, iterateAction);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

