var xmlDocSB;
var oHttpRequestSB = 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 StarBullXMLRequest() {
	oHttpRequestSB = false;
	//var sURL = "AJAX_Processes/RSS_HonoluluAdvertiser.aspx?type=" + inType;
	var sURL = "AJAX_Processes/RSS_Request.aspx?RssUrl=http://starbulletin.com/rss.xml";
	//var sURL = "AJAX_Processes/RSS_Request.aspx?RssUrl=http://www.lifferth.com/poop.xml";
	var oItem = "divStarBulletin";
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		oHttpRequestSB = new XMLHttpRequest();
		if (oHttpRequestSB.overrideMimeType) {
			oHttpRequestSB.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			oHttpRequestSB = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				oHttpRequestSB = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!oHttpRequestSB) {
		//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); };
	oHttpRequestSB.onreadystatechange = function() { StarBullGetData(oHttpRequestSB, oItem); };
	oHttpRequestSB.open("GET", sURL, true);
    oHttpRequestSB.send(null);
}

//-- This method checks the status and if everything is OK calls the method to handle the data
function StarBullGetData(oObj, oItem) {
	//alert(oObj.readyState);
	if (oObj.readyState == 4) {
		//alert(oObj.status);
		if (oObj.status == 200) {
			//alert(oObj.responseText);
			//-- Single picture request
			StarBullBuildDOMDocument(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 StarBullBuildDOMDocument(oObj, oItem)
{
	//-- code for IE
	if (window.ActiveXObject)
	{
		xmlDocSB = new ActiveXObject("Microsoft.XMLDOM");
		xmlDocSB.async=false;
		xmlDocSB.loadXML(oObj.responseText);
		//xmlDocSB.load("Test.xml");
		StarBullBuildDisplay(oItem)
	}
	//-- code for Mozilla, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDocSB = document.implementation.createDocument("","",null);
		
		var domParser = new DOMParser();
		var sXML = oObj.responseText;
		xmlDocSB = domParser.parseFromString(sXML, 'application/xml');
		var parseError = checkForParseError(xmlDocSB);
		if (parseError.errorCode == 0) {
			StarBullBuildDisplay(oItem);
		}
		else{
			//**************************************
			//TODO:  BETTER ERROR HANDLING!!!!!!!
			//**************************************
			//alert('error: ' + parseError.srcText);	
			return;
		}
		//xmlDocSB.onload=createTable;
	}
	else
	{
		alert('Your browser cannot load the DOM Document.');
	}
}



function StarBullBuildDisplay(oItem)
{
	//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 = "";
	var sLink = new Array(5);
	var sTitle = new Array(5);
	var sDesc = new Array(5);
	
	//-- Check for an error
	if(xmlDocSB.getElementsByTagName("ItemError").length > 0) {
		StarBullBuildErrorDisplay(xmlDocSB.getElementsByTagName("ItemError")[0].getAttribute("ErrText"),'divStarBulletinLoadingImage','divStarBullError','divSB_ErrorText');
		return;
	}
	
	//-- get the XML root item
	var oRoot = xmlDocSB.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;
	}
	//alert(sTemp);
	
	StarBullBuildTable(sLink, sTitle, sDesc);

}


//-- This method builds the table that is displayed on the page
function StarBullBuildTable(sLink, sTitle, sDesc)
{
	hideItem('divStarBulletinLoadingImage');
	showItem('divStarBull_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 += '"	class="news">';
		sTemp += sTitle[i] + '</a>';
		
		//-- Populate the item div tag
		SetText_SB('divSBItem' + (i + 1), sTemp);	
	}
}

function SetText_SB( 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 StarBullBuildErrorDisplay(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;
	}

}