var xmlDoc;
var oHttpRequest = false;

//window.attachEvent("onload", lnXMLRequest);
// onload="ln_XMLRequest('http://the.honoluluadvertiser.com/rss/localnews.xml', 'divLocalNews')"

//-- Method to request the XML document from the external server...
function GenericAJAXRequest(inTask, inParam, inTS, boolReturnData) {
	oHttpRequest = false;
	var sURL = "AJAX_Processes/GenericAJAX.aspx?task=" + inTask + "&" + inParam + "&ts=" + inTS;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		oHttpRequest = new XMLHttpRequest();
		if (oHttpRequest.overrideMimeType) {
			oHttpRequest.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			oHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				oHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!oHttpRequest) {
		//alert('Giving up :( Cannot create an XMLHTTP instance');
		//alert("Your browser doesn't support AJAX.");
		//-- Hide loading image, show some other image
		return false;
	}
	
	//-- Call the site and get the data...
	oHttpRequest.onreadystatechange = function() { GetData(oHttpRequest, boolReturnData); };
	oHttpRequest.open("GET", sURL, true);
    oHttpRequest.send(null);
}

//-- This method checks the status and if everything is OK calls the method to handle the data
function GetData(oObj, boolReturnData) {
	//-- If boolReturnData is FALSE then no return data is requested...
	if(boolReturnData == true)
	{
		//alert(oObj.readyState);
		if (oObj.readyState == 4) {
			//alert(oObj.status);
			if (oObj.status == 200) {
				//alert(oObj.responseText);
				//-- Single picture request
				BuildDOMDocument(oObj);
			} else {
				//alert('There was a problem with the request.\nStatus: ' + oObj.responseText);
				//alert('There was a problem with the request.');
			}
		}
	}
}


//-- This method builds the DOM Document for the current browser
function BuildDOMDocument(oObj)
{
	//-- code for IE
	if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.loadXML(oObj.responseText);
		//xmlDocSB.load("Test.xml");
		BuildDisplay()
	}
	//-- code for Mozilla, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("","",null);
		
		var domParser = new DOMParser();
		var sXML = oObj.responseText;
		//alert(sXML);
		xmlDoc = domParser.parseFromString(sXML, 'application/xml');
		var parseError = checkForParseError(xmlDoc);
		if (parseError.errorCode == 0) {
			BuildDisplay();
		}
		else{
			//alert('error: ' + parseError.srcText);	
			return;
		}
		//xmlDocSB.onload=createTable;
	}
	else
	{
		//alert('Your browser cannot load the DOM Document.');
	}
}



function BuildDisplay()
{
	//alert(xmlDocSB.getElementsByTagName("Pic").length);
	//alert(xmlDocSB.getElementsByTagName("Pic")[0].attributes.getNamedItem("Type").value);
	//alert(xmlDocSB.getElementsByTagName("Pic")[0].getAttribute("Type"));
	//alert(xmlDocSB.xml);
	var sTemp = xmlDoc.getElementsByTagName('Return')[0].firstChild.nodeValue;
	
	//-- Populate the item div tag
	SetText('divItem', sTemp);	
}

function SetText( object, sCaption ){
	if (document.getElementById && document.getElementById(object) != null){
		node = document.getElementById(object).innerHTML = sCaption;
	}
	else if (document.layers && document.layers[object] != null){
		document.layers[object].innerHTML = sCaption;
	}
	else if (document.all){
		//document.all[object].innerHTML = sCaption;
	}
}



function checkForParseError (xmlDocument) 
{
	var errorNamespace = 'http://www.mozilla.org/newlayout/xml/parsererror.xml';
	var documentElement = xmlDocument.documentElement;
	var parseError = { errorCode : 0 };
	if (documentElement.nodeName == 'parsererror' && documentElement.namespaceURI == errorNamespace) 
	{
		parseError.errorCode = 1;
		var sourceText = documentElement.getElementsByTagNameNS(errorNamespace, 'sourcetext')[0];
		if (sourceText != null) 
		{
			parseError.srcText = sourceText.firstChild.data;
		}
		parseError.reason = documentElement.firstChild.data;
	}
	return parseError;
}

