 
function createXMLRequest()
{
    var xmlRequest = null;
    
    if(window.XMLHttpRequest)
    {
        xmlRequest = new XMLHttpRequest();
        if(xmlRequest.overrideMimeType)
            xmlRequest.overrideMimeType("text/xml");
    }
    else if(window.ActiveXObject)
    {
        try
        {
            xmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {}
        }
    }
    
    if(xmlRequest == null)
        alert("Failure to create XMLHTTP instance");
        
    return xmlRequest;
}

function getAjaxDirectory()
{
	var has3W = (window.location.href.indexOf('//www.') >= 0);
	
	if(!has3W)
		return g_NonWWWBaseDirectory+'ajax/';

	return g_BaseDirectory+'ajax/';
}

function submitAjaxRequest(oncomplete, file, parameters)
{
	var xmlRequest = createXMLRequest();
	if(xmlRequest != null)
	{
		if(oncomplete)
			xmlRequest.onreadystatechange = 
				function() 
				{ 
					if(xmlRequest.readyState == 4)
    				{
       					if(xmlRequest.status == 200)
        					oncomplete(xmlRequest);
					}
				};
		
		var randomness = '&random_assure_reload_val='+(Math.random()*12345);
		parameters += randomness;
		
		xmlRequest.open("POST", getAjaxDirectory()+file, true);//"http://www.urefer.com/templates/ajax/"+file, true);
		xmlRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlRequest.setRequestHeader("Content-length", parameters.length);
		xmlRequest.setRequestHeader("Connection", "close");
		xmlRequest.send(parameters);
	} 	
}

function submitXMLRequest(file, parameters)
{
	var xmlRequest = createXMLRequest();
	if(xmlRequest != null)
	{
		xmlRequest.open("POST", getAjaxDirectory()+file, false);//"http://www.urefer.com/templates/ajax/"+file, false);
		xmlRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlRequest.setRequestHeader("Content-length", parameters.length);
		xmlRequest.setRequestHeader("Connection", "close");
		xmlRequest.send(parameters);
	}
	return xmlRequest;
}

function createXMLTree(xmlString)
{
	// code for IE
	if(window.ActiveXObject)
	{
	  	var doc = new ActiveXObject("Microsoft.XMLDOM");
	  	doc.async = "false";
	  	doc.loadXML(xmlString);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else
	{
		var parser = new DOMParser();
	  	var doc = parser.parseFromString(xmlString, "text/xml");
	}
	
	return doc.documentElement;
}


function getXMLVal(xmlEl, tagName, num)
{
	var text = '';

	if(xmlEl && typeof(xmlEl.getElementsByTagName) != 'undefined')
	{
		var x = xmlEl.getElementsByTagName(tagName)[num ? num : 0];
		if(x && x.hasChildNodes())
		{	
			x = x.firstChild;
			text = x.nodeValue;
		}
	}
		
	return text;
}

function xmlGetChildNode(xmlEl, tagName)
{
	if(xmlEl && typeof(xmlEl.hasChildNodes) != 'undefined')
	{
		for(var i = 0; i < xmlEl.childNodes.length; i++)
		{
			if(xmlEl.childNodes[i].tagName == tagName)
				return xmlEl.childNodes[i];
		}
	}
	
	return null;
}

function xmlGetChildVal(xmlEl, tagName)
{
	var text = null;

	var el = xmlGetChildNode(xmlEl, tagName);
	if(el && el.hasChildNodes())
		text = el.firstChild.nodeValue;

	return text;
}

function getErrorMsg(rXML)
{
	var msg = '';
	
	var errors = rXML.getElementsByTagName('error');
	for(var i = 0; i < errors.length; i++)
	{
		if(i == 0)
			msg = 'The following error'+(errors.length > 1 ? 's were:' : ' was')+' found:\n\n';
		
		msg += ' -'+getXMLVal(rXML, 'error', i)+'\n';
	}
	
	return (msg.length ? msg : null);
}