// JavaScript Document
function loadXMLString(txt) 
{
	try //Internet Explorer
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		xmlDoc.loadXML(txt);
		return xmlDoc; 
	}
	catch(e)
	{
		try //Firefox, Mozilla, Opera, etc.
		{
			var parser = new DOMParser();
			xmlDoc = parser.parseFromString(txt, "text/xml");
			return xmlDoc;
		}
		catch(e) {alert(e.message)}
	}
	return null;
}
function getXmlValue(xmlDoc, tagName, defaultVal)
{
	var node = xmlDoc.getElementsByTagName(tagName)[0];
	
	if (defaultVal == undefined)
		defaultVal = "";
	
	if (node == null)
		return defaultVal;
	
	if (node.childNodes.length > 0)
		return node.childNodes[0].nodeValue;
	else
		return defaultVal;
}
function loadXMLDoc(docurl, callback, async)
{
	if (async)
		return loadXMLDocAsync(docurl, callback);
	else
		return loadXMLDocSync(docurl);
}

function loadXMLDocAsync(docurl, callback)
{
	var xmlhttp = null;
	var xmlDoc = null;
	
	if (window.XMLHttpRequest)
	{
		// code for all new browsers
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE5 and IE6
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (xmlhttp != null)
	{
		xmlhttp.onreadystatechange = function()
			{
				var error = false;
				
				if (xmlhttp.readyState == 4)
				{
					// 4 = "loaded"
					if (xmlhttp.status == 200)
					{
						// 200 = OK
						xmlDoc = loadXMLString(xmlhttp.responseText);
						
						if (xmlDoc == null)
						{
							alert("Problem parsing XML string");
						}
						else if (validateAjaxResponse(xmlDoc))
						{
							if (typeof callback == 'function')
							{
								callback(xmlDoc);
							}
							else
							{
								alert("Callback function '" + callback + "' could not be found.");
							}
						}
					}
					else
					{
						alert("Problem retrieving XML data");
					}
				}
			};
		xmlhttp.open("GET", docurl, true);
		xmlhttp.send(null);
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}

function loadXMLDocSync(docurl)
{
	try // Internet Explorer
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	}
	catch(e)
	{
		try // Firefox, Mozilla, Opera, etc.
		{
			xmlDoc = document.implementation.createDocument("", "", null);
		}
		catch(e)
		{
			alert(e.message)
		}
	}
	
	try 
	{
		xmlDoc.async = false;
		xmlDoc.load(docurl);
		
		if (validateAjaxResponse(xmlDoc))
			return xmlDoc;
	}
	catch(e)
	{
		alert(e.message)
	}
	return null;
}

function validateAjaxResponse(xmlDoc)
{
	var nodes = xmlDoc.getElementsByTagName("error");
	
	if (nodes.length > 0)
	{
		switch (nodes[0].firstChild.nodeValue)
		{
			case "Not logged in":
				document.location.href = RootPath + "notauthorised.html";
				break;
			default:
				return true;
		}
		
		return false;
	}
	
	return true;
}
