var _FabricId = "";
var _ItemId = "";
var _ColorId = "";
var _SizeId = "";

function ValidateFabricSelection()
{
	var fabricDrop = GetDdlFabric();
	if(fabricDrop != null)
	{
		var selectedFabricId = fabricDrop.value;
		PopulateConfiguratorOptions(selectedFabricId, null, null, null);
	}
}

function ValidateItemSelection()
{
	var fabricDrop = GetDdlFabric();
	var itemDrop = GetDdlItem();
	var colorDrop = GetDdlColor();
	if(fabricDrop != null && itemDrop != null && colorDrop != null)
	{
		var selectedFabricId = fabricDrop.value;
		var selectedItemId = itemDrop.value;
		PopulateConfiguratorOptions(selectedFabricId, selectedItemId, null, null);
	}
}

function ValidateColorSelection()
{
	var fabricDrop = GetDdlFabric();
	var itemDrop = GetDdlItem();
	var colorDrop = GetDdlColor();
	var styleDrop = GetDdlSize();
	if(fabricDrop != null && itemDrop != null && colorDrop != null)
	{
		var selectedFabricId = fabricDrop.value;
		var selectedItemId = itemDrop.value;
		var selectedColorId = colorDrop.value;
		var selectedStyleId = styleDrop.value;
		
		PopulateConfiguratorOptions(selectedFabricId, selectedItemId, selectedColorId, selectedStyleId);
	}
}

function ValidateSizeSelection()
{
	var fabricDrop = GetDdlFabric();
	var itemDrop = GetDdlItem();
	var colorDrop = GetDdlColor();
	var styleDrop = GetDdlSize();
	if(fabricDrop != null && itemDrop != null && colorDrop != null && styleDrop != null)
	{
		var selectedFabricId = fabricDrop.value;
		var selectedItemId = itemDrop.value;
		var selectedColorId = colorDrop.value;
		var selectedStyleId = styleDrop.value;
		PopulateConfiguratorOptions(selectedFabricId, selectedItemId, selectedColorId, selectedStyleId);
	}
}

function PopulateConfiguratorOptions(selectedFabricId, selectedItemId, selectedColorId, selectedSizeId)
{
	// fabricGroupId assigned in code behind
	var groupId = (_FabricGroupId == null ? "" : _FabricGroupId);

	_FabricId = (selectedFabricId == null ? "" : selectedFabricId);
	_ItemId = (selectedItemId == null ? "" : selectedItemId);
	_ColorId = (selectedColorId == null ? "" : selectedColorId);
	_SizeId = (selectedSizeId == null ? "" : selectedSizeId);
	
	// Prevent two selections before postback
	GetDdlFabric().disabled = true;
	GetDdlItem().disabled = true;
	GetDdlColor().disabled = true;
	GetDdlSize().disabled = true;
  GetTxtBoxQuantity().disabled = true;
  
	// Make ajax call to update options
	var requestUrl = ajaxRequestUrl + "?request=" + optionsRequestId + "&groupId=" + groupId + 
		"&fabricId=" + _FabricId + "&itemId=" + _ItemId + "&colorId=" + _ColorId + "&sizeId=" + _SizeId;

	xmlHttpConfigurator = GetXmlHttpObject(PopulateOptionsFromXml);
	xmlHttp_Get(xmlHttpConfigurator, requestUrl);
}

// Ajax callback method
function PopulateOptionsFromXml()
{
	if(xmlHttpConfigurator.readyState == 4 || xmlHttpConfigurator.readyState == 'complete')
	{
		// Gather results from object on callback
		var xmlDoc = xmlHttpConfigurator.responseXML;
		var errorMessage = GetErrorMessage(xmlDoc);
    
    _FabricId = ClearAndAddOptions(GetDdlFabric(), xmlDoc, "Fabric", _FabricId, "Select Fabric");
    _ItemId = ClearAndAddOptions(GetDdlItem(), xmlDoc, "Item", _ItemId, "Select Item");
    
    if(errorMessage == null || errorMessage == "")
    {
			_ColorId = ClearAndAddOptions(GetDdlColor(), xmlDoc, "Color", _ColorId, "Select Color");
			_SizeId = ClearAndAddOptions(GetDdlSize(), xmlDoc, "Style", _SizeId, "Select Size");
		}
		else
		{
			alert(errorMessage);
			_ColorId = ClearAndAddOptions(GetDdlColor(), xmlDoc, "Color", "", "Select Color");
			_SizeId = ClearAndAddOptions(GetDdlSize(), xmlDoc, "Style", "", "Select Size");
		}
    PopulatePrice(GetLabelPrice(), xmlDoc);
		GetTxtBoxQuantity().disabled = false;
  }
}

function ClearAndAddOptions(dropDown, xml, optionId, value, selectCaption)
{
	ClearOptions(dropDown);

  var option;
	var options = GetOptionsFromXml(xml, optionId);
	var selectedValue = value;
   
	if(options.length == 0)
	{
		option = GetDropOption(selectCaption, "");
    AddOption(dropDown, option);
    dropDown.value = option.value;
		dropDown.disabled = true;
	}
	else if(options.length == 1)
	{
    AddOption(dropDown, options[0]);
    dropDown.value = options[0].value;
    selectedValue = options[0].value;
		dropDown.disabled = false;
	}
	else
	{
    option = GetDropOption(selectCaption, "");
    AddOption(dropDown, option);
		var lastOption = ""
		for (var index = 0; index < options.length; index ++)   
		{
			if(options[index].value != lastOption)
			{
		    AddOption(dropDown, options[index]);
				lastOption = options[index].value;
			}
		}
    if(value != null && value != "")
    {
			dropDown.value = value;
    }
    else
    {
			dropDown.value = "";
    }
		dropDown.disabled = false;
	}
	return selectedValue;
}

function ClearOptions(dropDownControl)
{
	var index ;
	for(var index = dropDownControl.options.length - 1; index >=0 ; index--)
	{
		dropDownControl.remove(index);
	}
}

function GetOptionsFromXml(xmlDoc, elementId)
{
	var dropOptions = new Array();
	var optionNodes = xmlDoc.getElementsByTagName(elementId);
	for(var index = 0; index < optionNodes.length; index++)
	{
		dropOptions[index] = GetDropOption(GetNodeText(optionNodes[index].childNodes[1]), 
																			GetNodeText(optionNodes[index].childNodes[0]));
	}
	return dropOptions;
}

function GetDropOption(displayText, value )
{
	var option = document.createElement("OPTION");
	//option.setAttribute("value", value);
	//var textNode = document.createTextNode(displayText);
	//option.appendChild(textNode);
	option.text = displayText;
	option.value = value;
	return option;
}

/*
function GetOptionValue(value)
{
  if(value == null || value == "All")
  {
		return "";
  }
  else
  {
		return value;
  }
}
  
function CriteriaHasSelection(dropDownControl)
{
   return (dropDownControl.value != null && dropDownControl.value != "All" && dropDownControl.value != "");
}
*/

function ResetCriteria()
{
		PopulateOptions(null, true);
}

function DisableAllControls(sender)
{
	SetTextControlsDisable(sender, true)
	SetDropControlsDisable(sender, true)
}

function EnableAllControls()
{
	SetTextControlsDisable(null, false)
	SetDropControlsDisable(null, false)
}

function SetDropControlsDisable(sender, disable)
{
	for(var index = 0; index < dropControls.length; index++)
	{
		if(sender == null || dropControls[index] != sender)
		{
			dropControls[index].disabled = disable;
		}
	}
}

function PopulatePrice(priceLabel, xmlDoc)
{
	if(_SizeId != null && _SizeId != "")
	{
		var value = "";
		var priceNodes = xmlDoc.getElementsByTagName("Price");
	
		if(priceNodes != null && priceNodes.length > 0)
		{
			value = GetNodeText(priceNodes[0]);
		}
		// Using both innerText and textContent for multi-browser compatibility w/Span text
		priceLabel.innerText = value;
		priceLabel.textContent = value;
		priceLabel.style.visibility="visible";
	}
	else
	{
		priceLabel.innerText = "";
		priceLabel.textContent = "";
		priceLabel.style.visibility="hidden";
	}
	return; 
}

function GetErrorMessage(xmlDoc)
{
	var message = "";
	var errorNodes = xmlDoc.getElementsByTagName("ErrorMessage");
	
	if(errorNodes != null && errorNodes.length > 0)
	{
		message = GetNodeText(errorNodes[0]);
	}
	return message; 
}

function GetNodeText(node)
{
	var nodeText = node.text;
	if(nodeText == undefined)
	{
		nodeText = node.textContent;
	}
	return nodeText;
}
	
function AddOption(dropControl, option)
{
	if (navigator.userAgent.indexOf("Firefox")!=-1)
	{
		dropControl.appendChild(option);
	}
	else
	{
		dropControl.options.add(option);
	}
}
	
