function handleRequest(req, select, url, postData)
{
	//writeToConsole(escapeHTML(req.responseText));

	var doc = req.responseXML;
	if (doc && doc.documentElement && doc.getElementsByTagName('option').length > 0) 
		getSelect(select, doc.getElementsByTagName('option'));
	else
	{
		var errMsg = 'type:error,desc:badXml,url:' + url + ',postData:' + postData;
		errMsg += ',func:' + getFunctionName(arguments.callee.toString());
		document.location.href = '../err/raiseerr.aspx?msg=' + encodeURIComponent(errMsg) + '&filename=' + document.location.pathname;
	}
}

function getSelect(select, options) 
{
  select.options.length = 0; 

	// auto-select for single-item dropdowns
	if (options.length == 1)
		select.options[0] = new Option(options[0].firstChild.nodeValue, options[0].getAttribute('value'));
	else
	{
		select.options[0] = new Option('Select', '-1');
		for (var i=0;i<options.length;i++)
			select.options[i + 1] = new Option(options[i].firstChild.nodeValue, options[i].getAttribute('value'));
	}
}

function updateChild(e)
{
	if (!e) var e = window.event;
	var tg = e.target || e.srcElement;
	var child = null;

    var x = document.getElementsByTagName('SELECT');
    for (var i=0; i < x.length; i++) 
    {
        if (x[i].getAttribute('parent') == tg.id)
        {
            child = x[i];
            break;
        }
    }

    if (tg.selectedIndex == 0) 
    {
        disableDependentSelects(child);
    }
    else
    {
        sendRequest(child.getAttribute('url') + getAncestorOrSelfNameValue(tg), handleRequest, null, child);
        child.disabled = false;
    }
    
    if(child.getAttribute('child'))
    {
		disableDependentSelects(document.getElementById(child.getAttribute('child')));
    }
}

function getAncestorOrSelfNameValue(el)
{
    str = '&' + el.name + '=' + el.options[el.selectedIndex].value;
    while (el.getAttribute('parent'))
    {
        el = document.getElementById(el.getAttribute('parent'));
        str += '&' + el.name + '=' + el.options[el.selectedIndex].value;
    }
    return(str);    
}

function sendRequest(url,callback,postData,el) {
    //writeToConsole('xmlhttp url: ' + url);
    //writeToConsole('xmlhttp post: ' + postData);
    
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0 Live Earth');
	if (postData) 
        req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
            var errMsg = 'type:error,desc:httpError,status:' + req.status + ',url:' + url + ',postData:' + postData;
            document.location.href = '../err/raiseerr.aspx?msg=' + encodeURIComponent(errMsg) + '&filename=' + document.location.pathname;
			return;
		}
		callback(req,el,url,postData);
	}
	if (req.readyState == 4) return;
    req.send(postData);
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")},
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++)
	{
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

function disableDependentSelects(select)
{
    select.selectedIndex = 0;
    select.options.length = 0;    
    select.options[0] = new Option('Select', '-1');
    //select.disabled = true;
   
    var dependentSelect = null;
    
    var x = document.getElementsByTagName('SELECT');
    for (var i=0; i < x.length; i++) 
    {
        if (x[i].getAttribute('parent') == select.id)
        {
            dependentSelect = x[i];
            break;
        }
    }
    
    if (dependentSelect) disableDependentSelects(dependentSelect);
}

function getScore(req, el, url, postData) 
{
	//writeToConsole(escapeHTML(req.responseText));

	var elError = document.getElementById('error');
	elError.style.visibility = 'hidden';
	
	
	if(step == 0)
	{
		var retakeTest = document.getElementById('retakeLink');
		retakeTest.style.visibility = 'visible';
	}
	
	
	var doc = req.responseXML;
	if (doc && doc.documentElement) 
	{
		if (doc.getElementsByTagName('record')[0].getAttribute('errorid'))
		{
			if (elError)
			{
				elError.style.visibility = 'visible';
				if(step == 0)
				{
					retakeTest.style.visibility = 'hidden';
				}
				
				elError.innerHTML = doc.getElementsByTagName('record')[0].getAttribute('errordesc');
				enableNextButton();
				return;
			}
		}
		else
		{
			// if cities are returned
			if (doc.getElementsByTagName('option').length > 0)
				getSelect(document.getElementById('us_city'), doc.getElementsByTagName('option'));

			document.forms[0].UID.value = doc.getElementsByTagName('record')[0].getAttribute('UID');
			document.forms[0].lGUID.value = doc.getElementsByTagName('record')[0].getAttribute('lGUID');
			score = doc.getElementsByTagName('record')[0].getAttribute('carbonscore');
			co2 = doc.getElementsByTagName('record')[0].getAttribute('Co2Metrictons');
			document.getElementById('score').innerHTML = score;
			document.getElementById('carbon').innerHTML = co2;
		}
		fetchNextStep();
	}
	else
	{
		var errMsg = 'type:error,desc:badXml,url:' + url + ',postData:' + postData;
		errMsg += ',func:' + getFunctionName(arguments.callee.toString());
		document.location.href = '../err/raiseerr.aspx?msg=' + encodeURIComponent(errMsg) + '&filename=' + document.location.pathname;
	}
}
