
// declare a global  XMLHTTP Request object
var req;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function loadXMLDocEditor(url) {
		req = false;
		// branch for native XMLHttpRequest object
		if(window.XMLHttpRequest) {
			try {
			req = new XMLHttpRequest();
				} catch(e) {
			req = false;
				}
		// branch for IE/Windows ActiveX version
		} else if(window.ActiveXObject) {
				try {
					req = new ActiveXObject("Msxml2.XMLHTTP");
				} catch(e) {
					try {
							req = new ActiveXObject("Microsoft.XMLHTTP");
					} catch(e) {
							req = false;
					}
		}
		}
	if(req) {
		req.onreadystatechange = processReqChangeEditor;
		req.open("GET", url, true);
		req.send("");
	}
}

function loadXMLDocBrand(url) {
		req = false;
		// branch for native XMLHttpRequest object
		if(window.XMLHttpRequest) {
			try {
			req = new XMLHttpRequest();
				} catch(e) {
			req = false;
				}
		// branch for IE/Windows ActiveX version
		} else if(window.ActiveXObject) {
				try {
					req = new ActiveXObject("Msxml2.XMLHTTP");
				} catch(e) {
					try {
							req = new ActiveXObject("Microsoft.XMLHTTP");
					} catch(e) {
							req = false;
					}
		}
		}
	if(req) {
		req.onreadystatechange = processReqChangeBrand;
		req.open("GET", url, true);
		req.send("");
	}
}

// called from onChange or onClick event of the continent dropdown list
function brandOnChange() 
{
    var brand = document.getElementById("brand");
    
    // get selected continent from dropdown list
    var selectedBrand = brand.options[brand.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    // use the following line if using asp
    requestUrl = "/submit/xml_data_provider.php" + "?type=brand&filter=" + encodeURIComponent(selectedBrand);
    // use the following line if using php
    // requestUrl = "xml_data_provider.php" + "?filter=" + encodeURIComponent(selectedContinent);
    loadXMLDocBrand(requestUrl);
		
}

function editorOnChange() 
{
    var editor = document.getElementById("editor");
    
    // get selected continent from dropdown list
    var selectedEditor = editor.options[editor.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    // use the following line if using asp
    requestUrl = "/submit/xml_data_provider.php" + "?type=editor&filter=" + encodeURIComponent(selectedEditor);
    // use the following line if using php
    // requestUrl = "xml_data_provider.php" + "?filter=" + encodeURIComponent(selectedContinent);
    loadXMLDocEditor(requestUrl);
		
}


// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function processReqChangeBrand()
{
	// state ==4 indicates receiving response data from server is completed
	if(req.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(req.status == 200)
		{			
			Populatemodel(req.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + req.status);
		}
	}
}

function processReqChangeEditor()
{
	// state ==4 indicates receiving response data from server is completed
	if(req.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(req.status == 200)
		{			
			Populatesoftware(req.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + req.status);
		}
	}
}

// populate the contents of the club dropdown list
function Populatemodel(modelNode)
{
    var model = document.getElementById("model");
	// clear the model list 
	for (var count = model.options.length-1; count >-1; count--)
	{
		model.options[count] = null;
	}

	var modelNodes = modelNode.getElementsByTagName('model');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < modelNodes.length; count++)
	{
   	textValue = GetInnerText(modelNodes[count]);
		idValue = modelNodes[count].getAttribute("id");
		optionItem = new Option(textValue, idValue,  false, false);
		model.options[model.length] = optionItem;
	}
}

function Populatesoftware(softwareNode)
{
    var software = document.getElementById("software");
	// clear the software list 
	for (var count = software.options.length-1; count >-1; count--)
	{
		software.options[count] = null;
	}

	var softwareNodes = softwareNode.getElementsByTagName('software');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < softwareNodes.length; count++)
	{
   	textValue = GetInnerText(softwareNodes[count]);
		idValue = softwareNodes[count].getAttribute("id");
		optionItem = new Option(textValue, idValue,  false, false);
		software.options[software.length] = optionItem;
	}
}

// returns the node text value 
function GetInnerText (node)
{
	 //return (node.textContent || node.innerText || node.text) ;
	if (typeof node == "string") return node;
	if (typeof node == "undefined") { return node };
	if (node.innerText) return node.innerText;	//Not needed but it is faster
	var str = "";
	
	var cs = node.childNodes;
	var l = cs.length;
	for (var i = 0; i < l; i++) {
		switch (cs[i].nodeType) {
			case 1: //nodeEMENT_NODE
				str += ts_getInnerText(cs[i]);
				break;
			case 3:	//TEXT_NODE
				str += cs[i].nodeValue;
				break;
		}
	}
	return str;
}









