var num_of_records = 0;
var nodes;
var success = false;
var req;
var ajax_busy;
var xmlhttp;

function xmlhttpPost() 
{
	try {
		// Mozilla / Safari / IE7
		xmlhttp = new XMLHttpRequest();

		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
		success = true;
	} catch (e) {
		// IE
		
		var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.6.0',
								 'MSXML2.XMLHTTP.3.0',
								 'Microsoft.XMLHTTP' );
		
		//var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
		//						 'MSXML2.XMLHTTP.4.0',
		//						 'MSXML2.XMLHTTP.3.0',
		//						 'MSXML2.XMLHTTP',
		//						 'Microsoft.XMLHTTP' );
		// db 2007-11-16
		//

		
		for (var i=0; i < XMLHTTP_IDS.length && !success; i++) {
		  try {
				xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
				success = true;
			} catch (e) {}
		}
		
		if (!xmlhttp) {
			alert("Your browser version is not supported, some features of this site may be disabled!");        
			throw new Error('Unable to create XMLHttpRequest.');
		}
	}

	return xmlhttp;
}

function processXmlResponse( response )
{
	if(response)
	{
		nodes = response.getElementsByTagName('item'); // returns only nodes that are item nodes.
													   // this removes the response and xml nodes
		num_of_records = nodes.length;
	}
	return true;
}

function retrieve_node(field, row)
{
	for (var i=0; i < num_of_records; i++ )
    {
        if(i == row)
		{
			var node = nodes.item(i);
			return getData(node, field);
		}
	}
}

function get_ajax(serverPage, method, async)
{
   	ajax_busy = true;	
	num_of_records = 0;
	
	if(req==null)
	{
		req = xmlhttpPost();
	}
	else
	{
		req.abort();
	}
	
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			try
			{
				if (req.status == 200 || req.status == 0) {
					ajax_busy = false;	
					processXmlResponse( req.responseXML );
				} else {
					ajax_busy = true;	
					alert('Loading Error: ['+req.status+'] '+req.statusText);
				}
			} catch(e) {
				ajax_busy = false;	
				processXmlResponse( req.responseXML );
			}
		}
		else
		{
			ajax_busy = true;	
		}
	}
	req.open(method,serverPage,async);
	req.send(null);
		
	if(is_firefox == true || is_gecko == true)
	{
		if (req.readyState == 4) {
			if (req.status == 200 || req.status == 0) {
				ajax_busy = false;	
				processXmlResponse( req.responseXML );
			} else {
				ajax_busy = true;	
				alert('Loading Error: ['+req.status+'] '+req.statusText);
			}
		}
		else
		{
			ajax_busy = true;	
		}
	}

}

function getData( myNode, myNodeName )
{
    var subNodes = myNode.childNodes;

    for ( j=0; j < subNodes.length; j++ )
    {
        var subNode = subNodes.item(j);

        if ( subNode.nodeName == myNodeName )
        {
            // this is cross browser ( ie and firefox ) compatible
            if(subNode.childNodes.item(0))
			{
				return subNode.childNodes.item(0).nodeValue;
			}
			else
			{
				return '';	
			}
	   	}
    }
    return "";
}

function get_xml_field(field_id, row_id)
{
	return URLDecode( retrieve_node(field_id, row_id) );
}
