

function SetAvailableSizes(shirtName, colorSelectedName, theSizeSelectNode) {
		
	// Clear out the existing Options for the Size Select
	for(var k = 0; k <= theSizeSelectNode.options.length; k++) {			
		//alert('removing index ' + k);		
		theSizeSelectNode.options.remove(k);
	}
	
	if (colorSelectedName == 'EMPTY') {
		theSizeSelectNode.options[0] = new Option("");
		theSizeSelectNode.options[0].selected = true;
		
		if (theSizeSelectNode.options.length > 1) {
			theSizeSelectNode.options[1] = null;
		}
		return;
	}		
	
	var SizesAvailable;
	SizesAvailable = GetAvailableSizes(shirtName, colorSelectedName )
	
	//Add the <Option> to the size <Select>
	for(var k = 0; k < SizesAvailable.length; k++) {		
		theSizeSelectNode.options[k] = SizesAvailable[k];
	}	
}

function loadColorAndSizeXML() {
	
	if (document.implementation && document.implementation.createDocument)
	{
		_ColorAndSizeXML = document.implementation.createDocument("", "", null);
		//_ColorAndSizeXML.onload = createTable;
	}
	else if (window.ActiveXObject)
	{
		_ColorAndSizeXML = new ActiveXObject("Microsoft.XMLDOM");
		//_ColorAndSizeXML.onreadystatechange = function () {
			//if (_ColorAndSizeXML.readyState == 4) createTable()
		//};
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	
	//Derek, provide the full URL to the XML file or try relative path	
	_ColorAndSizeXML.load("http://www.4fifteenclothing.com/ShirtColorAndSize.xml");
}

function createTable() {

	alert(_ColorAndSizeXML.documentElement.xml);
}

function GetAvailableSizes(shirtName, colorSelectedName) {
	var returnArrayOfOptions = new Array();	
	var TheShirtElement;
	
	// Find the Shirt node
	for(var i=0; i < _ColorAndSizeXML.documentElement.childNodes.length; i++) {
		
		//If not whitespace
		if(_ColorAndSizeXML.documentElement.childNodes[i].nodeType == 1){						
			
			if(_ColorAndSizeXML.documentElement.childNodes[i].attributes[0].value == shirtName) {
				//We have found the correct shirt
				TheShirtElement = _ColorAndSizeXML.documentElement.childNodes[i];
				
				//Now find the colorSelectedName
				var TheColorElement;
				for(var j=0; j < TheShirtElement.childNodes.length; j++) {
					
					if (TheShirtElement.childNodes[j].attributes[0].value == colorSelectedName) {
						// We have found the correct color
						TheColorElement = TheShirtElement.childNodes[j];
												
						//Now add an Option for each size
						for(var k = 0; k < TheColorElement.childNodes.length; k++) {
							var curSizeElement = TheColorElement.childNodes[k];
							var newSizeOption = new Option(curSizeElement.attributes[0].value);
							newSizeOption.value = curSizeElement.attributes[1].value;
							returnArrayOfOptions[k] = newSizeOption;							
						}
						
						break; // break out of the color loop						
					}					
				}
								
				break; // break out of the shirt loop because we already found the shirt
			}
		}
	}

	return returnArrayOfOptions;	
}