
// Cookie Stuff
function getexpirydate(nodays){
	Today = new Date();
	Today.setTime(Date.parse(Today) + (nodays * 24 * 60 * 60 * 1000));
	return Today.toUTCString();
}

function setcookie(name, value, duration){
	cookiestring = name + "=" + escape(value) + ";EXPIRES=" + getexpirydate(duration);
	document.cookie = cookiestring;
}

function getcookie(cookiename) {
	var cookiestring = '' + document.cookie;
	var index1 = cookiestring.indexOf(cookiename);
	
	if (index1 == -1 || cookiename == "") return ""; 
	
	var index2 = cookiestring.indexOf(';', index1);
	
	if (index2 == -1) index2 = cookiestring.length;
	
	return unescape(cookiestring.substring(index1 + cookiename.length + 1, index2));
}
// END Cookie stuff

// The weather part
var loading_preload = new Image();
loading_preload.src = 'images/loading.gif';

function updateWeather() {
	var new_weather = weatherclient.getJSONObject();
	document.getElementById('div_weather').innerHTML = '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td width="185"><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td colspan="2" align="right"><strong>' + new_weather.CURRENTCONDITIONS.TEMPRETURE + ' &deg;C</strong></td></tr><tr><td colspan="2" align="right"><strong>' + new_weather.CURRENTCONDITIONS.DESCRIPTION + '</strong></td></tr><tr><td width="52%" align="right">Humidity  : </td><td width="48%" align="left">&nbsp;&nbsp;' + new_weather.CURRENTCONDITIONS.HUMIDITY + '</td></tr><tr><td align="right">Wind Speed  : </td><td align="left">&nbsp;' + new_weather.CURRENTCONDITIONS.WIND_SPEED + '&nbsp;' + new_weather.CURRENTCONDITIONS.WIND_DIRECTION + '</td></tr></table></td><td width="15"><img alt="' + new_weather.CURRENTCONDITIONS.DESCRIPTION + '" id="img_weather_icon" src="weather/Images/64x64/' + new_weather.CURRENTCONDITIONS.ICON_ID + '.png" width="51" height="51"><br></td></tr></table>';
	if (is_ie && !is_ie7up) {
		var img_weather_icon_pointer = document.getElementById('img_weather_icon');
		img_weather_icon_pointer.src = 'images/blank.gif';
		img_weather_icon_pointer.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='weather/Images/64x64/" + new_weather.CURRENTCONDITIONS.ICON_ID + ".png', sizingMethod='scale', enabled=true)";
	}
}

var weatherclient = new JSONClient(false, 'weather.cfm', '', updateWeather);

function getWeather() {
	var sel_location_codes_pointer = document.getElementById('sel_location_codes');
	weatherclient.setParams('code=' + sel_location_codes_pointer.options[sel_location_codes_pointer.selectedIndex].value);
	setcookie('weather_code', sel_location_codes_pointer.options[sel_location_codes_pointer.selectedIndex].value, 30);
	document.getElementById('div_weather').innerHTML = '<div style=" text-align:center;" ><img src="images/loading.gif" alt="Loading Weather" style=" margin-left:auto; margin-right:auto; margin-top:5px; margin-bottom:5px;"></div>';
	weatherclient.sendRequest();
}
// END The weather part

// The News Part
/*function updateNews() {
	var new_news = newsclient.getJSONObject();
	var html_begin = '<div style="height:250px; overflow-x:hidden; overflow-y:scroll;"><table width="100%" cellpadding="4" cellspacing="1" class="data">';
	var html_middle = '';
	var html_end = '</table></div>';
	
	if (new_news != null) {
		for (var i = 0; i < 15; i++) {
			html_middle = html_middle + '<tr bgcolor="#' + (i % 2 == 0 ? 'EBEBEB' : 'FFFFFF') + '"><td><a href="' + new_news[i].URL + '" target="_blank">' + new_news[i].HEADLINE_TEXT + '</a><br><span class="news">(<strong>' + new_news[i].SOURCE + '</strong>) on ' + new_news[i].HARVEST_TIME + '</span><br></td></tr>'
		}
	} else {
		html_middle = '<tr><td>&nbsp;</td></tr>';
		alert('There was an error retrieving the news, try selcting a different news category.\nThis problem should correct itself in a few hours.');
	}
	
	document.getElementById('div_news').innerHTML = '' + html_begin + html_middle + html_end;
}



var newsclient = new JSONClient(false, 'news.cfm', '', updateNews);

function getNews() {
	var sel_news_name_pointer = document.getElementById('sel_news_name');
	newsclient.setParams('name=' + sel_news_name_pointer.options[sel_news_name_pointer.selectedIndex].value);
	setcookie('news_name', sel_news_name_pointer.options[sel_news_name_pointer.selectedIndex].value, 30);
	document.getElementById('div_news').innerHTML = '<div style=" text-align:center;" ><img src="images/loading.gif" alt="Loading News" style=" margin-left:auto; margin-right:auto; margin-top:5px; margin-bottom:5px;"></div>';
	newsclient.sendRequest();
}*/
// END The News Part

// init
function init_content() {
	var weather_cookie = getcookie('weather_code');
	//var news_cookie = getcookie('news_name');
	if (weather_cookie == '') weather_cookie = 'SFXX0011';
	document.getElementById('opt_' + weather_cookie).selected = true;
	//if (news_cookie != '') document.getElementById("opt_" + news_cookie).selected = true;

	//getWeather();
	//getNews();
}
// END init

