var productName = new Array();
var prodIdValue = new Array();
var brandName = new Array();
var brandId = new Array();
var brandProp = new Array();
var x;
var y;
var xmlhttp;
var response;

function loadXMLDoc(url, xmlType){
	xmlhttp=null;
	if (xmlType == "Product"){
	url = url + "?BrandId=" + encodeURIComponent(document.forms['Form1'].brandSelect.value);
	}
	
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
	xmlhttp=new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject)
	{
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	if (xmlhttp!=null)
	{
	xmlhttp.onreadystatechange=state_Change
	xmlhttp.open("GET",url,true)
	xmlhttp.send(null)
	}		
	else
	{
	alert("Your browser does not support XMLHTTP.")
	}
}
function state_Change() {
	if (xmlhttp.readyState==4) {
		// if "OK"
		if (xmlhttp.status==200) {
		response = (xmlhttp.responseText);
		importXML();
		} else {
			//alert("Problem retrieving XML data")
		}
	}
}
function importXML(){
//import xml
	
	if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = CreateArrayValues;
	} else if (window.ActiveXObject){
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) CreateArrayValues()			
		};
	} else {
		alert('Your browser can\'t handle this script');
		return;
	}
	
	// code for IE
	if (window.ActiveXObject){
	xmlDoc.loadXML(response);
	}else{// code for Mozilla, Firefox, Opera, etc.
	var parser=new DOMParser();
	xmlDoc=parser.parseFromString(response,"text/xml");
	CreateArrayValues();
	}
}

function CreateArrayValues()
{
	x = null;
	y = null;
	x = xmlDoc.getElementsByTagName('Product');
	y = xmlDoc.getElementsByTagName('Brand');
	if (x.length>0){
		for (i=0;i<x.length;i++){
			productName[i] = (x[i].childNodes[0].firstChild.nodeValue);
			prodIdValue[i] = (x[i].childNodes[1].firstChild.nodeValue);
		} //end for loop
		WriteOptions();
	}else if (y.length>0){
		for (i=0;i<y.length;i++){
			brandName[i] = (y[i].childNodes[1].firstChild.nodeValue);
			brandId[i] = (y[i].childNodes[0].firstChild.nodeValue);
			brandProp[i] = (y[i].childNodes[2].firstChild.nodeValue);
		} //end for loop
		WriteBrands();
	}else {
	document.forms['Form1'].productSelect.options.length = 0;
	document.forms['Form1'].productSelect.options[0] = new Option('Problem Loading Products  ','');
	}
}
function WriteOptions() {
		//erase all current options
		document.forms['Form1'].productSelect.options.length = 0;
		
	var j=0;
	//iterate through array and add options for each
	for (var i=0;i<x.length;i++){
		if (productName[i] == null){
		//do nothing and don't increment j
		} else {
			document.forms['Form1'].productSelect.options[j] = new Option(productName[i],prodIdValue[i]);
			j++;
			productName[i] = null;
			prodIdValue[i] = null;
		}//end if else
	}//end for loop
	
	if (j==0){
	document.forms['Form1'].productSelect.options[0] = new Option('No Products Available  ','');
	document.forms['Form1'].productSelect.disabled = true;
	document.getElementById('submitParts').style.display = "none";
	document.getElementById('submitPartsDisabled').style.display = "inline";
	}else {
	//enable text box and submit button, hide Disabled button
	document.forms['Form1'].productSelect.disabled = false;
	document.getElementById('submitPartsDisabled').style.display = "none";
	document.getElementById('submitParts').style.display = "inline";
	}
}
function WriteBrands (){
	if ( document.forms['Form1'] ) {
		//erase all current options
		document.forms['Form1'].brandSelect.options.length = 0;
		
		//iterate through array and add options for each
		var j=1;
		document.forms['Form1'].brandSelect.options[0] = new Option('Select Brand From List','');

		for (var i=0;i<y.length;i++){
			if (brandName[i] == null){
			//display error and don't increment j
			document.forms['Form1'].brandSelect.options[0] = new Option('Error Reading Brand List','');
			} else {
				document.forms['Form1'].brandSelect.options[j] = new Option(brandName[i],brandId[i]);
				if (brandProp[i] == 1){
				document.forms['Form1'].brandSelect.options[j].style.color = "#09446b";
				}
				j++;
				brandName[i] = null;
				brandId[i] = null;
			}//end if else
		}//end for loop
	}
}
