//<![CDATA[

function Weather() {
	this.zip = "";
	this.temp = 0;
	this.wind = 0;
	this.windir = 0;
	this.condition = 0;
	this.status = 0;
	this.msg = "";
	this.city = "";
	this.url = "";
	this.state = "";
	this.foot = '<center><span class="cfoot">Provided By Yahoo! Weather</span></center>';
	
	this.process = processWeather;
	this.clear = clearWeather;
}

function Search() {
	this.query = "";
	this.url = "";
	this.titles = new Array();
	this.urls = new Array();
	this.descs = new Array();
	
	this.start = startSearch;
	this.gosearch = querySearch;
}

function clearWeather() {
	text = '<br />'+this.msg+'Enter Zip: <input type="text" size=5 id="weatherWidgetZip" /><br />'+this.foot;
	document.getElementById("weatherWidget").innerHTML=text;
	addEvent(document.getElementById("weatherWidgetZip"), 'keyup', function() { weather.process("fetch"); }, false);
}
	
function processWeather(cmd) {
	this.url = "weather.php?cmd="+cmd;
	if(cmd == "fetch") {
		if(document.getElementById("weatherWidgetZip").value.length != 5 ||  document.getElementById("weatherWidgetZip").value != parseFloat(document.getElementById("weatherWidgetZip").value)) {
			return false;
		}
		this.zip = document.getElementById("weatherWidgetZip").value;
		this.url = "weather.php?cmd=fetch&zip="+this.zip;
	}
	text = '<br /><center><img src="loading.gif" width="32" height="65" alt="Loading.." /></center><br />'+this.foot;
	document.getElementById("weatherWidget").innerHTML=text;
	var xmlDoc
	if(window.XMLHttpRequest) {
		/*Fix for Safari..*/
		var errorHappendHere = "Error handling XMLHttpRequest request";
		var d = new XMLHttpRequest();
		d.open("GET",this.url, false);
		d.send(null);
		xmlDoc=d.responseXML;
	}
	else {
		xmlDoc = this.newxmldoc( );
		xmlDoc.async = false;
		xmlDoc.load(this.xmlFile);
	}
	
	this.status = xmlDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;
	if(this.status == 0) {
		this.msg = xmlDoc.getElementsByTagName("message")[0].childNodes[0].nodeValue;
		if(this.msg != " ") {
			this.msg = '<span class="message">'+this.msg+'</span><br />';
		}
		text = '<br />'+this.msg+'Enter Zip: <input type="text" size=5 id="weatherWidgetZip" /><br />'+this.foot;
		document.getElementById("weatherWidget").innerHTML=text;
		addEvent(document.getElementById("weatherWidgetZip"), 'keyup', function() { weather.process("fetch"); }, false);
	}
	else if(this.status == 1) {
			this.temp = xmlDoc.getElementsByTagName("temp")[0].childNodes[0].nodeValue;
			this.city = xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
			this.state = xmlDoc.getElementsByTagName("state")[0].childNodes[0].nodeValue;
			this.city = xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
			this.cond = xmlDoc.getElementsByTagName("condition")[0].childNodes[0].nodeValue;
			this.ccode = xmlDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;
			
			text = '<img src="http://l.yimg.com/us.yimg.com/i/us/we/52/'+this.ccode+'.gif" width="52" height="52" alt="'+this.cond+'" /><br /><strong>'+this.cond+', '+this.temp+' F</strong><br /><span class="smalltext">'+this.city+', '+this.state+' <span class="pseudolink" id="weatherWidgetChange">change?</span></span><br />'+this.foot;
			document.getElementById("weatherWidget").innerHTML=text;
			addEvent(document.getElementById("weatherWidgetChange"), 'click', function() { weather.clear(); }, false);
	}
	
}

function startSearch() {
	text = '<br />	<input type="text" size=15 id="searchField" /> <input type="submit" id="searchSubmit" value="Search" />';
	document.getElementById("searchWidget").innerHTML=text;
	addEvent(document.getElementById("searchSubmit"), 'click', function() { ysearch.gosearch(document.getElementById("searchField").value); }, false);
}

function querySearch(q) {
	if(q.length  == 0) {
		return false;
	}
	this.query = q;
	this.url = "search.php?q="+escape(this.query);
	text = '<br /><center><img src="loading.gif" width="32" height="65" alt="Loading.." /></center>';
	document.getElementById("searchWidget").innerHTML=text;
	
	var xmlDoc
	if(window.XMLHttpRequest) {
		/*Fix for Safari..*/
		var errorHappendHere = "Error handling XMLHttpRequest request";
		var d = new XMLHttpRequest();
		d.open("GET",this.url, false);
		d.send(null);
		xmlDoc=d.responseXML;
	}
	else {
		xmlDoc = this.newxmldoc( );
		xmlDoc.async = false;
		xmlDoc.load(this.xmlFile);
	}
	text = '<br />	<input type="text" size=15 id="searchField" value="'+this.query+'" /> <input type="submit" id="searchSubmit" value="Search" /><br /><br />';
	this.titles = xmlDoc.getElementsByTagName("Title");
	this.urls = xmlDoc.getElementsByTagName("Url");
	this.descs = xmlDoc.getElementsByTagName("Summary");
	
	for(i=0;i<this.descs.length;i++) {
		text += '<a href="'+this.urls[i*2].childNodes[0].nodeValue+'" title="'+this.titles[i].childNodes[0].nodeValue+'" class="searchlink">'+this.titles[i].childNodes[0].nodeValue+'</a><br /><span class="smalltext">'+this.descs[i].childNodes[0].nodeValue+'</span><br /><br />';
	}
	document.getElementById("searchWidget").innerHTML=text;
	addEvent(document.getElementById("searchSubmit"), 'click', function() { ysearch.gosearch(document.getElementById("searchField").value); }, false);
}

function fetchNews() {
	var xmlDoc
	if(window.XMLHttpRequest) {
		/*Fix for Safari..*/
		var errorHappendHere = "Error handling XMLHttpRequest request";
		var d = new XMLHttpRequest();
		d.open("GET","news.php", false);
		d.send(null);
		xmlDoc=d.responseXML;
	}
	else {
		xmlDoc = this.newxmldoc( );
		xmlDoc.async = false;
		xmlDoc.load(this.xmlFile);
	}
	
	text = '';
	this.titles = xmlDoc.getElementsByTagName("title");
	this.links = xmlDoc.getElementsByTagName("link");
	this.descs = xmlDoc.getElementsByTagName("description");
	
	for(i=1;i<this.descs.length;i++) {
		if(i%2 == 1) {
			odd = " odd";
		}
		else {
			odd = "";
		}
		text += '<strong><a href="'+this.links[i+1].childNodes[0].nodeValue+'">'+this.titles[i+1].childNodes[0].nodeValue+'</a></strong><br />'+this.descs[i].childNodes[0].nodeValue+'<br /><br />';
	}
	document.getElementById("newsWidget").innerHTML=text;
}
	
/*Initialization*/
function afterload() {
	/*Weather*/
	weather = new Weather();
	weather.process("start");
	/*Search*/
	ysearch = new  Search();
	ysearch.start();
	/*News*/
	fetchNews();
}

FunctionOnWinLoad(afterload);
    //]]>
