//-------
// select a manufacturer from the dropdown
// then populate next dropdown with available regions
//-------

/**********
XMLHttp
**********/

var xmlHttp;

function newXmlHttpRequest() {
	xmlHttp = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) {
		try {
			xmlHttp = new XMLHttpRequest();
			if (xmlHttp.overrideMimeType) {
				xmlHttp.overrideMimeType('text/xml');
			}

		} catch(e) {
			xmlHttp = false;
		}
	// site for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xmlHttp = false;
			}
		}
	}
}


//--------
//--- get xml stream for this URL
function getProducts(url) {
	newXmlHttpRequest();
	if(xmlHttp) {
		xmlHttp.onreadystatechange = processProducts
		//prompt('url',url);
		xmlHttp.open('GET', url, true);
		xmlHttp.send(null);
	}
}


//---
// take xml stream and build select list of products
function processProducts () {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			//alert(xmlHttp.responseText);
			xml = xmlHttp.responseXML;

			var select = document.getElementById('product_id');
			if (!select) return ;
			select.options.length = 0;
			// select.options[0] = new Option( 'Any Product', '0');
			var products = xml.getElementsByTagName("product");
			var selected = 0 ;
			for(var i=0; i<products.length; i++) {
				var node = products.item(i);

				// get attributes
				var id = node.getAttribute("id")
				var name = node.getAttribute("name")
				if (node.getAttribute("checked") == '1') selected = i ;
				select.options[i] = new Option(name, id);
			}
			if (selected) select.selectedIndex = selected ;
		} else {
			alert('There was a problem with the request.');
		}
	}
}
