var xmlDocLN;
var oHttpRequestLN = 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 HonAdvXMLRequest(inType) {
	oHttpRequestLN = false;
	var sURL = "AJAX_Processes/RSS_Request.aspx?RssUrl=http://the.honoluluadvertiser.com/rss/localnews.xml";
	//var sURL = "AJAX_Processes/RSS_Request.aspx?RssUrl=http://www.lifferth.com/poop.xml";
	var oItem = "divHonoluluAdvertiser";
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		oHttpRequestLN = new XMLHttpRequest();
		if (oHttpRequestLN.overrideMimeType) {
			oHttpRequestLN.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			oHttpRequestLN = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				oHttpRequestLN = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!oHttpRequestLN) {
		//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...
	//http_request.onreadystatechange = function() { GetData(http_request, 'single', sImgObject); };
	oHttpRequestLN.onreadystatechange = function() { HonAdvGetData(oHttpRequestLN, oItem); };
	oHttpRequestLN.open("GET", sURL, true);
    oHttpRequestLN.send(null);
}

//-- This method checks the status and if everything is OK calls the method to handle the data
function HonAdvGetData(oObj, oItem) {
	//alert(oObj.readyState);
	if (oObj.readyState == 4) {
		//alert(oObj.status);
		if (oObj.status == 200) {
			//alert(oObj.responseText);
			//-- Single picture request
			HonAdvBuildDOMDocument(oObj, oItem);
		} else {
			//**************************************
			//TODO:  BETTER ERROR HANDLING!!!!!!!
			//**************************************
			//alert('There was a problem with the request.\nStatus: ' + oObj.responseText);
		}
	}
}


//-- This method builds the DOM Document for the current browser
function HonAdvBuildDOMDocument(oObj, oItem)
{
	//-- code for IE
	if (window.ActiveXObject)
	{
		xmlDocLN = new ActiveXObject("Microsoft.XMLDOM");
		xmlDocLN.async=false;
		xmlDocLN.loadXML(oObj.responseText);
		//xmlDocLN.load("Test.xml");
		HonAdvBuildDisplay(oItem)
	}
	//-- code for Mozilla, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDocLN = document.implementation.createDocument("","",null);
		
		var domParser = new DOMParser();
		var sXML = oObj.responseText;
		xmlDocLN = domParser.parseFromString(sXML, 'application/xml');
		var parseError = checkForParseError(xmlDocLN);
		if (parseError.errorCode == 0) {
			HonAdvBuildDisplay(oItem);
		}
		else{	
			//**************************************
			//TODO:  BETTER ERROR HANDLING!!!!!!!
			//**************************************
			//alert('error: ' + parseError.srcText);	
			return;
		}
		//xmlDocLN.onload=createTable;
	}
	else
	{
		alert('Your browser cannot load the DOM Document.');
	}
}



function HonAdvBuildDisplay(oItem)
{
	//alert(xmlDocLN.getElementsByTagName("Pic").length);
	//alert(xmlDocLN.getElementsByTagName("Pic")[0].attributes.getNamedItem("Type").value);
	//alert(xmlDocLN.getElementsByTagName("Pic")[0].getAttribute("Type"));
	//alert(xmlDocLN.xml);
	var sTemp = "";
	var sLink = new Array(5);
	var sTitle = new Array(5);
	var sDesc = new Array(5);

	//-- Check for an error
	if(xmlDocLN.getElementsByTagName("ItemError").length > 0) {
		HonAdvBuildErrorDisplay(xmlDocLN.getElementsByTagName("ItemError")[0].getAttribute("ErrText"),'divLocalNewsLoadingImage','divLocalNewsError','divLN_ErrorText');
		return;
	}
	
	//-- get the XML root item
	var oRoot = xmlDocLN.getElementsByTagName('channel')[0];
	var oItems = oRoot.getElementsByTagName("item");
	for (var i = 0 ; i < oItems.length ; i++) {
		//-- get one item after another
		var oItem = oItems[i];
		//-- now we have the item object, time to get the contents
		//-- get the LINK of the item
		sLink[i] = oItem.getElementsByTagName("link")[0].firstChild.nodeValue;
		//-- get the TITLE
		sTitle[i] = oItem.getElementsByTagName("title")[0].firstChild.nodeValue;
		//-- Get the Description
		sDesc[i] = oItem.getElementsByTagName("description")[0].firstChild.nodeValue;		
		//sTemp += sTitle + " (" + sLink + ")\n" + sDescription + "\n\n";
	}
	//alert(sTemp);
	
	BuildTable(sLink, sTitle, sDesc);

}


//-- This method builds the table that is displayed on the page
function BuildTable(sLink, sTitle, sDesc)
{
	hideItem('divLocalNewsLoadingImage');
	showItem('divLocalNews_Items');
	var sTemp, sLinkCode;
	var sLen = sLink.length;
	for(var i = 0; i < sLink.length; i++)
	{
		//-- Build the title
		sLinkCode  = '<a href="' + sLink[i] + '" onClick="window.open';
		sLinkCode += "('" + sLink[i] + "', 'LBI_LocalNews');";
		sLinkCode += 'return false" ';
		sTemp = sLinkCode;
		sTemp += 'onMouseOver="showItem';
		sTemp += "('divLNItemDesc" + (i + 1) + "');hideOthers_LN(" + (i + 1) + ", " + sLen + ");";
		//sTemp += '" ';
		//sTemp += 'onMouseOut="hideItem';
		//sTemp += "('divLNItemDesc" + (i + 1) + "')";
		sTemp += '"	class="news">';
		sTemp += sTitle[i] + '</a>';
		
		//-- Build the Description box
		sDisplay = 'none';
		if(i == 0) sDisplay = 'block';
		sTemp += '<div id="divLNItemDesc' + (i + 1) + '" style="display: ' + sDisplay + ';" class="clsLNItem">';
		sTemp += sDesc[i] + '<br>' + sLinkCode + 'class="copyright">[Read full article...]</a></div>';
		
		//-- Populate the item div tag
		SetText_LN('divLNItem' + (i + 1), sTemp);	
		
		//alert(sTemp);
	}
}

function hideOthers_LN(iThis, inLength)
{
	var oObj;
	for(var i = 0; i < inLength; i++)
	{
		oObj = 'divLNItemDesc' + (i + 1) + '';
		if((i + 1) != iThis) hideItem(oObj);
	}
}


function SetText_LN( 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;
	}
}





//**************************************************************************************************
//*********** ERROR BUILDER
function HonAdvBuildErrorDisplay(inErrText,inLoadingDiv,inErrorDiv,inErrorTextDiv)
{
	hideItem(inLoadingDiv);
	showItem(inErrorDiv);
	
	if (document.getElementById && document.getElementById(inErrorTextDiv) != null){
		node = document.getElementById(inErrorTextDiv).innerHTML = inErrText;
	}
	else if (document.layers && document.layers[inErrorTextDiv] != null){
		document.layers[inErrorTextDiv].innerHTML = inErrText;
	}
	else if (document.all){
		//document.all[inErrorTextDiv].innerHTML = inErrText;
	}

}
