var lookupIcons = 
[
	["A Few Clouds and Breezy"],
	["Overcast"],
	["A Few Clouds"],
	["Few Clouds"],
	["Mostly Cloudy"],
	["Cloudy"],
	["Partly Cloudy"],
	["Rain"],
	["Light Rain"],
	["Clear"],
	["Fair"],
	["Fog"],
	["Freezing Drizzle"],
	["Freezing Rain"],
	["Haze"],
	["Drizzle and Fog"],
	["Mostly Sunny"],
	["Partly Sunny"],
	["Snow"],
	["Light Snow"],
	["Light Snow Rain Fog/Mist"],
	["Light Snow Fog/Mist"],
	["Sunny"],
	["Windy"],
	["Mist"],
	["Misty"],
	["Drizzle"],
	["Dust"],
	["Not Available"],
	["Heavy Rain"],
	["Thunderstorms"]
];


var weatherIcons = 
[
	["clear"],
	["cloudy"],
	["clear"],
	["clear"],			// 3 Partly Sunny
	["cloudy"],			// 4 Intermittent Clouds
	["cloudy"],				// 5 Hazy Sunshine
	["cloudy"],			// 6 Mostly Cloudy
	["rain"],					// 7 Cloudy (am/pm)
	["rain"],					// 8 Dreary (am/pm)
	["clear"],						// 11 fog (am/pm)
	["clear"],					// 12 showers (am/pnm)
	["cloudy"],				// 13 Mostly Cloudy with Showers
	["rain"],				// 14 Partly Sunny with Showers
	["rain"],				// 15 Thunderstorms (am/pm)
	["cloudy"],				// 16 Mostly Cloudy with Thunder Showers
	["rain"],				// 17 Partly Sunnty with Thunder Showers
	["clear"],					// 18 Rain (am/pm)
	["cloudy"],				// 19 Flurries (am/pm)
	["snow"],				// 20 Mostly Cloudy with Flurries
	["snow"],				// 20 Mostly Cloudy with Flurries
	["snow"],				// 20 Mostly Cloudy with Flurries
	["snow"],				// 20 Mostly Cloudy with Flurries
	["clear"],				// 21 Partly Sunny with Flurries
	["clear"],					// 22 Snow (am/pm)
	["rain"],					// 23 Mostly Cloudy with Snow
	["rain"],						// 24 Ice (am/pm)
	["rain"],					// 25 Sleet (am/pm)
	["clear"],					// 26 Freezing Rain (am/pm)
	["clear"],				// 29 Rain and Snow Mixed (am/pm)
	["rain"],						// 30 Hot (am/pm)
	["rain"]					// 31 Cold (am/pm)
];

function importXML()
{
	
	url = 'weather.xml';

	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = processData;
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) processData()
		};
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	xmlDoc.load(url);
}

function processData()
{
	
	var obj = {error:false, errorString:null};
	var adc_Database = findChild (xmlDoc, "current_observation");
	
	var timechild = findChild (adc_Database, "observation_time_rfc822");
	
	var time = {hour:timechild.firstChild.data.slice(17,19), minute:timechild.firstChild.data.slice(20,22)};
	
	obj = calcSun(riseSetCalc, cityLatLong, cityLatLong.dayAns.selectedIndex, cityLatLong.cities.selectedIndex);


	tag = findChild (adc_Database, "weather");
	obj.icons = weatherIcons[getIndexOf(tag.firstChild.data)];
		

	morning = obj.sunrise.hour*60 + obj.sunrise.minute;
	night = obj.sunset.hour*60 + obj.sunset.minute;
	curtime = time.hour*60 + time.minute;
	
	
	if (((curtime - morning) < 0) || (curtime - 60 > night))
	{
		obj.icons = "night" + obj.icons + ".jpg";
	}
	else if (curtime > night - 40)
	{
		obj.icons = "sunset" + obj.icons + ".jpg";
	}
	else
	{
		obj.icons = "day" + obj.icons + ".jpg";
	}
	
	//alert(morning + " " + night + " " + curtime);
	//alert((curtime - morning) < 0);
	
	tag = findChild(adc_Database, "temp_f");
	document.getElementById('tempstring').innerHTML=tag.firstChild.data;	
	document.getElementById('hadr').innerHTML="<img src='./weather/" + obj.icons + "' width='450px' height='100px'>";


}

function findChild (element, nodeName)
{
	var child;
	
	for (child = element.firstChild; child != null; child = child.nextSibling)
	{
		if (child.nodeName == nodeName)
			return child;
	}
	
	return null;
}


function getIndexOf(lookupWord)
{
	for (var i = 0; (i < lookupIcons.length) && (lookupIcons[i] != lookupWord); i++);
	if (i == lookupIcons.length) i--;
	return i;
}

function trimWhiteSpace (string)
{
	return string.replace(/^\s*/, '').replace(/\s*$/, ''); 
}





