
var validationArray = new Array();
var errorTextArray = new Array();

/**
* Handles the validation of the add on sku selectors
*/
function ValidateForm(formName)
{
	var errorText = '';
	var formError = false;
	//iterate through all product skuSelector sets
	for (index in validationArray)
	{	
		//if this product is being added, check that we have a sku for it
		if (document.forms[formName].elements[index+"yes_no"].value == "yes")
		{			
			if (document.forms[formName].elements[index+"sku"].value == '')
			{
				errorText += errorTextArray[index];
				formError = true;
			}			
		}
	}
	
	if (formError)
	{
		document.getElementById("errorText").innerHTML = errorText;
		alert("Please complete the form before continuing");
		return false;
	}
	else
	{
		return true;
	}
}

/**
* Handles the behavior of the yes/no dropdown, reflected in the sku selectors
*/
function HandleYesNo(formName, selectNamePrefix, selectNumber, selectOptionArray, selectPromptArray, notYetText)
{
	//what is the yes/no dropdown set to?
	var flag = document.forms[formName].elements[selectNamePrefix+"yes_no"].value;

	if (flag == "no")
	{
		PopulateDropdown(formName, selectNamePrefix, selectNumber, selectOptionArray, selectPromptArray, notYetText);
		document.forms[formName].elements[selectNamePrefix+"sku"].value = '';
	}
}

/**
* Delegates 'select' box population depending on selected value
*/
function HandleSelect(formName, selectNamePrefix, selectNumber, selectOptionArray, selectPromptArray, notYetText)
{
	//compose the select dropdown name
	var selectName = selectNamePrefix + selectNumber;

	//find the select dropdown on the form
	var select = document.forms[formName].elements[selectName];

	//find the select value
	//var selectValue = select.options[select.selectedIndex].value;
	var selectValue = select.value;

	//check value
	if (selectValue == null || selectValue == "")//reset the selects starting with THIS one since nothing was selected
	{
		PopulateDropdown(formName, selectNamePrefix, selectNumber, selectOptionArray, selectPromptArray, '');
		document.forms[formName].elements[selectNamePrefix+"sku"].value = '';
	}
	else//reset the list starting with the NEXT select
	{
		//set the yes/no dropdown to yes if some attribute has been selected
		document.forms[formName].elements[selectNamePrefix+"yes_no"].value = "yes";
	
		complete = PopulateDropdown(formName, selectNamePrefix, selectNumber+1, selectOptionArray, selectPromptArray, notYetText);

		if (complete)
		{
			//alert("complete!");

			//find the leaf node by drilling down using currently selected values
			thisSelectOptionArray = selectOptionArray;
			for (i=1;i<=selectNumber;i++)
			{
				tempSelectName = selectNamePrefix + i;
				tempSelect = document.forms[formName].elements[tempSelectName];
				//tempSelectValue = tempSelect.options[tempSelect.selectedIndex].value;
				tempSelectValue = tempSelect.value;
				thisSelectOptionArray = thisSelectOptionArray[tempSelectValue];
			}
			//got it!
			//alert(thisSelectOptionArray);
			//now set the hidden input value with it...
			document.forms[formName].elements[selectNamePrefix+"sku"].value = thisSelectOptionArray;
		}	
	}
}
/**
* Delegates 'select' box population depending on Radio Button
*/
function HandleRadio(formName, selectNamePrefix, selectNumber, selectOptionArray, selectPromptArray, notYetText)
{
	//compose the select dropdown name
	var selectName = selectNamePrefix + selectNumber;

	//find the select dropdown on the form
	var select = document.forms[formName].elements[selectName];

	//find the select value
	//var selectValue = select.options[select.selectedIndex].value;
	var selectValue = select.value;

	//check value
	if (selectValue == null || selectValue == "")//reset the selects starting with THIS one since nothing was selected
	{	
		//The next line was causing the drop down to lose its values...when removed, it works fine
		//PopulateDropdown(formName, selectNamePrefix, selectNumber, selectOptionArray, selectPromptArray, '');
		document.forms[formName].elements[selectNamePrefix+"sku"].value = '';
	}
	else//reset the list starting with the NEXT select
	{
		//set the yes/no dropdown to yes if some attribute has been selected
		document.forms[formName].elements[selectNamePrefix+"yes_no"].value = "yes";
	
		complete = PopulateDropdown(formName, selectNamePrefix, selectNumber+1, selectOptionArray, selectPromptArray, notYetText);

		if (complete)
		{
			//alert("complete!");

			//find the leaf node by drilling down using currently selected values
			thisSelectOptionArray = selectOptionArray;
			for (i=1;i<=selectNumber;i++)
			{
				if(i<=1){
					tempSelectName = selectNamePrefix + i;
					tempSelect = document.forms[formName].elements[tempSelectName];
					//tempSelectValue = tempSelect.options[tempSelect.selectedIndex].value;
						myOption = -1;
						for (w=tempSelect.length-1; w > -1; w--) {
						if (tempSelect[w].checked) {
						myOption = w;}}
					tempSelectValue = tempSelect[myOption].value;
					thisSelectOptionArray = thisSelectOptionArray[tempSelectValue];
				}
				else{
					tempSelectName = selectNamePrefix + i;
					tempSelect = document.forms[formName].elements[tempSelectName];
					//tempSelectValue = tempSelect.options[tempSelect.selectedIndex].value;
					tempSelectValue = tempSelect.value;
					thisSelectOptionArray = thisSelectOptionArray[tempSelectValue];
				}
			}
			//got it!
			//now set the hidden input value with it...
			document.forms[formName].elements[selectNamePrefix+"sku"].value = thisSelectOptionArray;
		}	
	}
}

/**
* Delegates 'select' box population depending on Radio Button selected value
*/
function RadioSelect(formName, selectNamePrefix, selectNumber, selectOptionArray, selectPromptArray, notYetText)
{
	//this makes sure the sku input value is cleared when you select a radio button
	document.forms[formName].elements[selectNamePrefix+"sku"].value = '';
	
	//compose the select dropdown name
	var selectName = selectNamePrefix + selectNumber;
	
	//find the select dropdown on the form
	var select = document.forms[formName].elements[selectName];

	myOption = -1;
	for (w=select.length-1; w > -1; w--) {
	if (select[w].checked) {
	myOption = w;}}

	//find the select value
	//var selectValue = select.options[select.selectedIndex].value;
	var selectValue = select[myOption].value;

	//check value
	if (selectValue == null || selectValue == "")//reset the selects starting with THIS one since nothing was selected
	{
		PopulateRadioDrop(formName, selectNamePrefix, selectNumber, selectOptionArray, selectPromptArray, '');
		document.forms[formName].elements[selectNamePrefix+"sku"].value = '';
	}
	else//reset the list starting with the NEXT select
	{
		//set the yes/no dropdown to yes if some attribute has been selected
		document.forms[formName].elements[selectNamePrefix+"yes_no"].value = "yes";
	
		complete = PopulateRadioDrop(formName, selectNamePrefix, selectNumber+1, selectOptionArray, selectPromptArray, notYetText);

		if (complete)
		{
			//alert("complete!");

			//find the leaf node by drilling down using currently selected values
			thisSelectOptionArray = selectOptionArray;
			for (i=1;i<=selectNumber;i++)
			{	
				tempSelectName = selectNamePrefix + i;
				tempSelect = document.forms[formName].elements[tempSelectName];
				//tempSelectValue = tempSelect.options[tempSelect.selectedIndex].value;
					myOption = -1;
					for (w=tempSelect.length-1; w > -1; w--) {
					if (tempSelect[w].checked) {
					myOption = w;}}
				tempSelectValue = tempSelect[myOption].value;
				thisSelectOptionArray = thisSelectOptionArray[tempSelectValue];
			}
			//got it!
			//alert(thisSelectOptionArray);
			//now set the hidden input value with it...
			document.forms[formName].elements[selectNamePrefix+"sku"].value = thisSelectOptionArray;
		}	
	}
}

/**
 * Populates the 'select' form input with elements of the 'selectOptionArray'.and recursively cascades through dependent selects
 */
function PopulateDropdown(formName, selectNamePrefix, selectNumber, selectOptionArray, selectPromptArray, notYetText)
{

	//compose the select dropdown name
	var selectName = selectNamePrefix + selectNumber;

	//find the select dropdown on the form
	var select = document.forms[formName].elements[selectName];
	
	//check for null
	if (select == null)
	{
		//set the complete flag=true since the final select has been set
		return true;
	}

	//find the appropriate prompt text 
	//if there is no text passed in, create a normal prompt, and pass along "select X first" to next selects
	if (notYetText == null || notYetText.length==0)
	{
		promptText = selectPromptArray[selectName];
		notYetText = promptText + " first";
	}

	//if there IS text passed in, use it
	else
	{
		promptText = notYetText;
	}

	//set the select prompt
	select.length = 1;
	select.options[0].text = promptText;
	select.options[0].value = "";

	//find this selectOptionArray by drilling down using currently selected values
	thisSelectOptionArray = selectOptionArray;
	
	for (i=1;i<selectNumber;i++)
	{	
		tempSelectName = selectNamePrefix + i;
		tempSelect = document.forms[formName].elements[tempSelectName];
		//tempSelectValue = tempSelect.options[tempSelect.selectedIndex].value;
		tempSelectValue = tempSelect.value;
		thisSelectOptionArray = thisSelectOptionArray[tempSelectValue];
		if (thisSelectOptionArray == null)
		{
			break;
		}
	}

	//set the select options if appropriate
	if (thisSelectOptionArray != null)
	{
		var n = 1;
		for (index in thisSelectOptionArray)
		{	
			select.length = n + 1;
			select.options[n].text = index;
			select.options[n].value = index;
			++n;
		}
	}

	//set the selection to be the first one in the list
	//select.selectedIndex = 0;

	//propagate the reset through the rest of the selects in the list
	
	PopulateDropdown(formName, selectNamePrefix, selectNumber+1, selectOptionArray, selectPromptArray, notYetText);

	//set the complete flag=false
	return false;
}

function PopulateRadioDrop(formName, selectNamePrefix, selectNumber, selectOptionArray, selectPromptArray, notYetText)
{

	//compose the select dropdown name
	var selectName = selectNamePrefix + selectNumber;

	//find the select dropdown on the form
	var select = document.forms[formName].elements[selectName];
	
	//check for null
	if (select == null)
	{
		//set the complete flag=true since the final select has been set
		return true;
	}

	//find the appropriate prompt text 
	//if there is no text passed in, create a normal prompt, and pass along "select X first" to next selects
	if (notYetText == null || notYetText.length==0)
	{
		promptText = selectPromptArray[selectName];
		notYetText = promptText + " first";
	}

	//if there IS text passed in, use it
	else
	{
		promptText = notYetText;
	}

	//set the select prompt
	select.length = 1;
	select.options[0].text = promptText;
	select.options[0].value = "";

	//find this selectOptionArray by drilling down using currently selected values
	thisSelectOptionArray = selectOptionArray;
	
	for (i=1;i<selectNumber;i++)
	{	
		tempSelectName = selectNamePrefix + i;
		tempSelect = document.forms[formName].elements[tempSelectName];
		//tempSelectValue = tempSelect.options[tempSelect.selectedIndex].value;
			myOption = -1;
			for (w=tempSelect.length-1; w > -1; w--) {
			if (tempSelect[w].checked) {
			myOption = w;}}
		tempSelectValue = tempSelect[myOption].value;
		thisSelectOptionArray = thisSelectOptionArray[tempSelectValue];
		if (thisSelectOptionArray == null)
		{
			break;
		}
	}

	//set the select options if appropriate
	if (thisSelectOptionArray != null)
	{
		var n = 1;
		for (index in thisSelectOptionArray)
		{	
			select.length = n + 1;
			select.options[n].text = index;
			select.options[n].value = index;
			++n;
		}
	}

	//set the selection to be the first one in the list
	//select.selectedIndex = 0;

	//propagate the reset through the rest of the selects in the list
	
	PopulateRadioDrop(formName, selectNamePrefix, selectNumber+1, selectOptionArray, selectPromptArray, notYetText);

	//set the complete flag=false
	return false;
}


