
function getXMLHttpRequest(objDiv, url)
{
        var httpRequest;

        if (window.XMLHttpRequest) 
        { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) 
            {
                //httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) 
        { // IE
            try 
            {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (e) 
            {
				try 
				{
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch (e) {}
            }
        }

        if (!httpRequest) 
        {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        
		var path = 'http://' + location.host;
		if (location.host == 'localhost')
			path = path + '/falconhawaii/';
		path = path + '/home/proxy.php?url=' + encodeURIComponent(url);
			
		//path = 'http://localhost/falconhawaii/proxy.php?url=' + encodeURIComponent(url);
		//path = 'http://www.falconhawaii.com/proxy.php?url=' + encodeURIComponent(url);
		//alert(path);

        httpRequest.onreadystatechange = function() { loadXML(httpRequest, objDiv, url); };
        httpRequest.open('GET', path, true);
        httpRequest.send(null);
}

function loadXML(httpRequest, objDiv, url)
{
  try 
  {
	 if (httpRequest.readyState == 4)
	{
		if (httpRequest.status==200)     //|| window.location.href.indexOf("http")==-1))
		{
			//document.getElementById(objDiv).innerHTML=httpRequest.responseText;
			//alert(httpRequest.responseText);
			formatXML(objDiv, httpRequest.responseXML, url);

        } 
        else 
        {
			//alert('There was a problem with the request. httpRequest.status='+httpRequest.status);
        }
     }
     //else
		//alert('There was a problem with the request. httpRequest.readyState='+httpRequest.readyState);
 }
  catch( e ) 
  {
	//alert('Caught Exception: ' + e.description);
  }

}

function formatXML(objDiv, objXML, url)
{

	if (url == 'http://www.worldpress.org/feeds/topstories.xml')
	{
		formatXMLworldpress(objDiv, objXML);
	}
	if (url.substring(0,23) == 'http://xoap.weather.com')
	{
		var ind = url.indexOf('/local/');
		ind = ind + 7;
		var CityCode = url.substring(ind,ind+8);
		formatXMLweather(objDiv, objXML, CityCode);
	}
}

function formatXMLworldpress(objDiv, objXML)
{
	var tag = objXML.getElementsByTagName('item');
	var title = '';
	var link = '';
	var description = '';
	var pubDate = '';
	
	var html = " <p><span style='font-size:10px;t'>" +  
			   objXML.getElementsByTagName('title').item(0).firstChild.data + 
			   " </span><a style='font-size:10px;text-decoration:underline;' href = '" + 
			  objXML.getElementsByTagName('link').item(0).firstChild.data + "'>" +  
			  objXML.getElementsByTagName('link').item(0).firstChild.data + "</a><br>" +
			  "</p>";
	
	for (var i = 0; i < Math.min(tag.length,7); i++) 
	{
		childtags =  tag.item(i).childNodes;
		for (var j = 0; j < childtags.length; j++) 
		{
			if (childtags[j].nodeName == 'title' && childtags[j].firstChild)
				title = childtags[j].firstChild.data;
			if (childtags[j].nodeName == 'link' && childtags[j].firstChild)
				link = childtags[j].firstChild.data;
			if (childtags[j].nodeName == 'description' && childtags[j].firstChild)
				description = childtags[j].firstChild.data;
			if (childtags[j].nodeName == 'pubDate' && childtags[j].firstChild)
			{
				pubDate = childtags[j].firstChild.data;
				if (pubDate.length > 16)
					pubDate = pubDate.substring(5,16);
			}
		}
		
		html = html + "<p><a style='font-size:10px;text-decoration:underline;' href = '" + link + "'>" +  title + "</a><br>" +
			   "<span style='font-size:9px;t'>" +  description + " " + pubDate + "</span></p>"
		
		//var tagLinkItem = tagLink.item(0);
		//alert(tagLink.item(i))
		//alert(tagLinkItem)
		//alert('firstChild ' + tagLink.item(i).firstChild)
	}
	//alert (document.getElementById(objDiv).innerHTML)
	document.getElementById(objDiv).innerHTML = html;
	document.getElementById(objDiv).style.width = '';
	document.getElementById(objDiv).style.height = '';
}

function formatXMLweather(objDiv, objXML, CityCode)
{
	var html = '';
	var degree = '';
	var mph = '';
	var ccicon = '';
	var ccdesc = '';
	var temp = '';
	var feelslike = '';
	var winddir = '';
	var windspeed = '';
	
	var cc = objXML.getElementsByTagName('cc').item(0).childNodes
	for (var j = 0; j < cc.length; j++) 
	{	
 		if (cc[j].tagName == 'icon')
 			ccicon = cc[j].firstChild.data;
 		if (cc[j].tagName == 't')
			ccdesc = cc[j].firstChild.data;
 		if (cc[j].tagName == 'tmp')
			temp = cc[j].firstChild.data;
 		if (cc[j].tagName == 'flik')
			feelslike = cc[j].firstChild.data;
 		if (cc[j].tagName == 't')
			winddir = cc[j].firstChild.data;
 		if (cc[j].tagName == 'wind')
 		{
 			var wind = cc[j].childNodes
			for (var i = 0; i < wind.length; i++) 
			{	
				if (wind[i].tagName == 't')
					winddir = wind[i].firstChild.data;
 				if (wind[i].tagName == 's')
					windspeed = wind[i].firstChild.data;
			}
		}
	}
	
	var degree = objXML.getElementsByTagName('ut').item(0).firstChild.data;
	var mph = objXML.getElementsByTagName('us').item(0).firstChild.data;

	// City name
	html = html + "<span style='font-size:10px;t'><b>Currently in " +  
					"<a href='http://www.weather.com/weather/local/" + CityCode + "'>" +
					objXML.getElementsByTagName('dnam').item(0).firstChild.data + 
					"</a></b></span><br>";
	html = html + "<table width=100%><tr>";
	//Conditions
	html = html + "<td align=center valign=bottom style='font-size:14px; background-color: transparent;'>" +
					" <img src = 'weathersdk/64x64/" + ccicon + ".png'>" + 
					"<br>" + 
					"<b>" +	ccdesc + "</b>" + 
					"</td>";
	//temp
	html = html + "<td align=center valign=bottom width=50% style='font-size:14px; background-color: transparent;'>" +
			"<span style='font-size:30px;t'><b>" + temp + "&deg;" + degree + "</b></span>" +  
			"<br>" + 
			"<b>Feels Like</b><br><b>" + feelslike + "&deg;" + degree + "</b>" +
			"</td>";
	html = html + "</tr>";
	
	//wind
	html = html + "<tr>";
	html = html + "<td  colspan=2 align=center valign=bottom style='font-size:10px; background-color: transparent;'>";
	if(winddir=='VAR') 
			winddir = 'Variable';
	else 
		if (winddir=='CALM') 
			winddir = 'Calm';
		else
			winddir = 'From ' + winddir;
			
	html = html + 'Wind: ' + winddir;
	if (winddir!='Calm') 
		html = html + ' at ' + windspeed + ' ' + mph;
	html = html + "</td>";
	html = html + "</tr></table><br>";
	//Updated
	html = html + " <span style='font-size:9px;'>Updated " +  
					objXML.getElementsByTagName('lsup').item(0).firstChild.data + 
					" </span><br>";
					
	document.getElementById(objDiv).innerHTML = html;
}



