/* This Script File for all the pages with the standard category drop lists */
/* for  PRODUCTS ONLY . . . not spares */

var bDebug = false; // this will allow me to test browsers that don't support Ajax
var prodGrpOptions;
var mozillaFlag = false; // Need to remove whitespace from XML documents if Mozilla based browser
var XMLHttpRequestObject = false; 
var XMLRequestAsynchronous = true;
// The page scope variable below is to fix a discrepancy with the way Firefox 3 handles anonymous function variables
// (when compared to FF2 and IE7 and Opera)
var strCurSelectListName = ""; 

// IE7 must support window.XMLHttpRequest 
// but errors on XMLHttpRequestObject.overrideMimeType("text/xml");
// so check for IE first
if (window.ActiveXObject) { // Internet Explorer
	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) { // Opera, Firefox and others
	mozillaFlag = true;
	XMLHttpRequestObject = new XMLHttpRequest();
	XMLHttpRequestObject.overrideMimeType("text/xml");
}

function getOptions(strQryVariable,strGetOptionsSelectListName) {
	// This page scope variable being updated below "strCurSelectListName"
	// is to fix a problem with the way Firefox 3 handles anonymous function variables,
	// FF3 can reference this variable each time it is called in the anonymous function call below
	strCurSelectListName = strGetOptionsSelectListName;
	
	var xmlURL = '';
	// Javascript "Select Case"
	switch(strGetOptionsSelectListName) {
		case('cat'): {
			xmlURL = '/common/includes/prod_cats_xml.asp?any='+bAny+'&arch='+bArchives;
			if (strQryVariable!='') {
				xmlURL = xmlURL + '&majorcat=' + strQryVariable;
			}
			break;
		}
		case('subcat'): {
			xmlURL = '/common/includes/prod_sub_cats_xml.asp?any='+bAny+'&arch='+bArchives;
			if (strQryVariable!='') {
				xmlURL = xmlURL + '&cat=' + strQryVariable;
			}
			break;
		}
		case('group'): {
			xmlURL = '/common/includes/prod_grps_xml.asp?any='+bAny+'&arch='+bArchives;
			if (strQryVariable!='') {
				xmlURL = xmlURL + '&subcat=' + strQryVariable;
			}
			break;
		}
		default: {
			alert('Unknown Select List!');
		} 
	}  // end-switch
	
	if(XMLHttpRequestObject) {
		XMLHttpRequestObject.open("GET",xmlURL,XMLRequestAsynchronous); 
		
		// Anonymous function call below:
		XMLHttpRequestObject.onreadystatechange = function() { 
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
				var xmlDocument = XMLHttpRequestObject.responseXML;
				// If it is a Mozilla based browser
				// it will treat any whitespace between XML elements
				// as an extra text node - must be removed
				if(mozillaFlag){
				  removeWhitespace(xmlDocument);
				}
				selectListOptions = xmlDocument.getElementsByTagName("option");
				// Below modification for FF3 compatability
				// listOptions(selectListOptions,strGetOptionsSelectListName);
				listOptions(selectListOptions,strCurSelectListName);
			}
		}
	XMLHttpRequestObject.send(null); 
	}
}

function removeWhitespace(xml) {
	var loopIndex;
	for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex++) {
		var currentNode = xml.childNodes[loopIndex];
		if (currentNode.nodeType == 1) {
			removeWhitespace(currentNode);
		}
		if (((/^\s+$/.test(currentNode.nodeValue))) &&  (currentNode.nodeType == 3)) {
			xml.removeChild(xml.childNodes[loopIndex--]);
		}
	}
}

function listOptions(xmlElements,strListOptionsSelectListName) {
	var loopIndex;
	var selectControl = '';	
	var strSelectListValue = '';
	
	// The below line does not work in Firefox . . . because it is expecting an id (e.g. 1,2,3) NOT a name  . . .
	// selectControl = document.getElementById(strListOptionsSelectListName);
	// The only reason IE and Opera work with the above is they incorrectly work with a name NOT an id as they should
	selectControl = eval(strProdSelectionForm + '.' + strListOptionsSelectListName);
	
	// Clear all options for the selected drop list
	selectControl.options.length = 0;
	
	for (loopIndex = 0; loopIndex < xmlElements.length; loopIndex++ ) {
		// selectControl.options[loopIndex] = new Option(xmlElements[loopIndex].firstChild.data,xmlElements[loopIndex].firstChild.data);
		// new Option('Option Text','Option Value');
		selectControl.options[loopIndex] = new Option(xmlElements[loopIndex].childNodes[1].firstChild.data,xmlElements[loopIndex].childNodes[0].firstChild.data);
	}
	// Call the below function to set the class back to normal
	// call it here because if you use it in the updateProductGroups funtion
	// it will be called before the list is returned
	loadingPleaseWait(selectControl,false);	
	
	// If there are only two items in the select list
	// set it to the second option (the first option will be "please select")
	// then populate the next drop list
	
	if (selectControl.options.length==2) {
		selectControl.options.selectedIndex=1;
		
		strSelectListValue = selectControl.options[1].value
		
		// Javascript "Select Case"
		// Call the function to populate the next list
		switch(strListOptionsSelectListName) {
			case('cat'): {
				updateSubCategories(strSelectListValue)
				break;
			}
			case('subcat'): {
				updateProductGroups(strSelectListValue)
				break;
			}
			case('group'): {
				// Do nothing - no next list to poulate
				break;
			}
			default: {
				alert(strListOptionsSelectListName+' = Unknown Select List!');
			} 
		}  // end-switch
	}
}

function loadingPleaseWait(formControl,isLoading) {
	if (isLoading==true) { // Highlight loading
		if (formControl.type.indexOf('select')>-1) {
			// Clear subcat options
			formControl.options.length = 0;	
			// Set the text of subcat to loading
			// new Option('OptionText','OptionValue');	
			formControl.options[0] = new Option('Loading, Please Wait...','');
		}
		if (formControl.type.indexOf('text')>-1) {
			formControl.value = 'Loading...';
		}
		// Make the control to be loaded stand out to display the loading text
		formControl.className = 'FormBoxLoading';
		// Set the cursor to the hourglass
		document.body.style.cursor = 'wait';
	} else { // Set back to standard
		// Set control back to standard
		formControl.className = 'FormBox';
		// Set the cursor to the hourglass
		document.body.style.cursor = 'default';
	}
}

function updateCategories(strSelectedOption) {
	// Clear all child selection list options (from the one after you are setting downwards)
	eval(strProdSubCatSelectList + ".options.length = 0");
	eval(strProdGrpSelectList + ".options.length = 0");
	// Set the text of all child selection list options to select from the above list first
	// new Option('OptionText','OptionValue');	
	eval(strProdSubCatSelectList + ".options[0] = new Option('Please Select Category','Any')");
	eval(strProdGrpSelectList + ".options[0] = new Option('Please Select Sub Category','Any')");
	loadingPleaseWait(eval(strProdCatSelectList),true);
	if (XMLHttpRequestObject && !bDebug) { // Update prod sub categories via AJAX
		var strEncodedSelectedOption = escape(eval(strProdMajorCatSelectList + '.value'));
		getOptions(strEncodedSelectedOption,'cat');
		// Set focus to updated drop list
        eval(strProdCatSelectList).focus();
	} else { // Update product groups by posting to self
		eval(strProdCatSelectList + ".value=''");
		eval(strProdSelectionForm + ".action=strCurPageName");
		eval(strProdSelectionForm + ".submit()");
	}
}

function updateSubCategories(strSelectedOption) {
	// Clear all child selection list options (from the one after you are setting downwards)
	eval(strProdGrpSelectList + ".options.length = 0");
	// Set the text of all child selection list options to select from the above list first
	// new Option('OptionText','OptionValue');	
	eval(strProdGrpSelectList + ".options[0] = new Option('Please Select Sub Category','Any')");
	loadingPleaseWait(eval(strProdSubCatSelectList),true);
	if (XMLHttpRequestObject && !bDebug) { // Update prod sub categories via AJAX
		var strEncodedSelectedOption = escape(eval(strProdCatSelectList + '.value'));
		getOptions(strEncodedSelectedOption,'subcat');
		// Set focus to updated drop list
        eval(strProdSubCatSelectList).focus();
	} else { // Update product groups by posting to self
		eval(strProdSubCatSelectList + ".value=''");
		eval(strProdSelectionForm + ".action=strCurPageName");
		eval(strProdSelectionForm + ".submit()");
	}
}

function updateProductGroups(strSelectedOption) {
	loadingPleaseWait(eval(strProdGrpSelectList),true);
	if (XMLHttpRequestObject && !bDebug) { // Update group via AJAX
		var strEncodedSelectedOption = escape(eval(strProdSubCatSelectList + '.value'));
		getOptions(strEncodedSelectedOption,'group');
		// Set focus to updated drop list
        eval(strProdGrpSelectList).focus();
	} else { // Update product groups by posting to self
		eval(strProdSelectionForm + ".action=strCurPageName");
		eval(strProdSelectionForm + ".submit()");
	}
}

// This is used for the non-ajax version on the highest level drop list (group)
function checkParentSelections() {
	if ((eval(strProdMajorCatSelectList + '.value')=='Any')&&(eval(strProdCatSelectList + '.value')=='Any')&&(eval(strProdSubCatSelectList + '.value')=='Any')) {
		alert('Please select all parent categories first.');
		eval(strProdGrpSelectList + ".value=''");
		eval(strProdMajorCatSelectList + '.focus()');
	}
}