//
// handle Cookies
//
function write_Cookie (name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString(); }
  else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function read_Cookie (name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') {
      c = c.substring(1,c.length); }
    if (c.indexOf(nameEQ) == 0) {
      return c.substring(nameEQ.length,c.length); } }
  return null;
}

//
// set parameter
//
if ((site_list_selection_set == 0)&&(read_Cookie('site_list_selection') != null)) {
  site_list_selection= parseInt(read_Cookie('site_list_selection')); }
write_Cookie('site_list_selection', site_list_selection.toString(), 365);

if ((selection_set == 0)&&(read_Cookie('selection') != null)) {
  selection= parseInt(read_Cookie('selection')); }
write_Cookie('selection', selection.toString(), 365);

if ((shadow_opacity_set == 0)&&(read_Cookie('shadow_opacity') != null)) {
  shadow_opacity= parseFloat(read_Cookie('shadow_opacity')); }
write_Cookie('shadow_opacity', shadow_opacity, 365);

if ((ETOPO_opacity_set == 0)&&(read_Cookie('ETOPO_opacity') != null)) {
  ETOPO_opacity= parseFloat(read_Cookie('ETOPO_opacity')); }
write_Cookie('ETOPO_opacity', ETOPO_opacity, 365);

if ((lat_set == 0)&&(read_Cookie('lat') != null)) {
  lat= parseFloat(read_Cookie('lat')); }

if ((lon_set == 0)&&(read_Cookie('lon') != null)) {
  lon= parseFloat(read_Cookie('lon')); }

if ((zoomlevel_set == 0)&&(read_Cookie('zoomlevel') != null)) {
  zoomlevel= parseInt(read_Cookie('zoomlevel')); }

if ((maptype_no_set == 0)&&(read_Cookie('maptype_no') != null)) {
  maptype_no= parseInt(read_Cookie('maptype_no')); }

if ((rain_overlay_url_set == 0)&&(read_Cookie('rain_overlay_url') != null)) {
  rain_overlay_url= read_Cookie('rain_overlay_url'); }
write_Cookie('rain_overlay_url', rain_overlay_url, 365);

if ((rain_overlay_north_set == 0)&&(read_Cookie('rain_overlay_north') != null)) {
  rain_overlay_north= parseFloat(read_Cookie('rain_overlay_north')); }
write_Cookie('rain_overlay_north', rain_overlay_north, 365);

if ((rain_overlay_south_set == 0)&&(read_Cookie('rain_overlay_south') != null)) {
  rain_overlay_south= parseFloat(read_Cookie('rain_overlay_south')); }
write_Cookie('rain_overlay_south', rain_overlay_south, 365);

if ((rain_overlay_west_set == 0)&&(read_Cookie('rain_overlay_west') != null)) {
  rain_overlay_west= parseFloat(read_Cookie('rain_overlay_west')); }
write_Cookie('rain_overlay_west', rain_overlay_west, 365);

if ((rain_overlay_east_set == 0)&&(read_Cookie('rain_overlay_east') != null)) {
  rain_overlay_east= parseFloat(read_Cookie('rain_overlay_east')); }
write_Cookie('rain_overlay_east', rain_overlay_east, 365);

if ((clouds_overlay_url_set == 0)&&(read_Cookie('clouds_overlay_url') != null)) {
  clouds_overlay_url= read_Cookie('clouds_overlay_url'); }
write_Cookie('clouds_overlay_url', clouds_overlay_url, 365);

if ((clouds_overlay_north_set == 0)&&(read_Cookie('clouds_overlay_north') != null)) {
  clouds_overlay_north= parseFloat(read_Cookie('clouds_overlay_north')); }
write_Cookie('clouds_overlay_north', clouds_overlay_north, 365);

if ((clouds_overlay_south_set == 0)&&(read_Cookie('clouds_overlay_south') != null)) {
  clouds_overlay_south= parseFloat(read_Cookie('clouds_overlay_south')); }
write_Cookie('clouds_overlay_south', clouds_overlay_south, 365);

if ((clouds_overlay_west_set == 0)&&(read_Cookie('clouds_overlay_west') != null)) {
  clouds_overlay_west= parseFloat(read_Cookie('clouds_overlay_west')); }
write_Cookie('clouds_overlay_west', clouds_overlay_west, 365);

if ((clouds_overlay_east_set == 0)&&(read_Cookie('clouds_overlay_east') != null)) {
  clouds_overlay_east= parseFloat(read_Cookie('clouds_overlay_east')); }
write_Cookie('clouds_overlay_east', clouds_overlay_east, 365);

if ((lightnings_overlay_url_set == 0)&&(read_Cookie('lightnings_overlay_url') != null)) {
  lightnings_overlay_url= read_Cookie('lightnings_overlay_url'); }
write_Cookie('lightnings_overlay_url', lightnings_overlay_url, 365);

if ((lightnings_overlay_north_set == 0)&&(read_Cookie('lightnings_overlay_north') != null)) {
  lightnings_overlay_north= parseFloat(read_Cookie('lightnings_overlay_north')); }
write_Cookie('lightnings_overlay_north', lightnings_overlay_north, 365);

if ((lightnings_overlay_south_set == 0)&&(read_Cookie('lightnings_overlay_south') != null)) {
  lightnings_overlay_south= parseFloat(read_Cookie('lightnings_overlay_south')); }
write_Cookie('lightnings_overlay_south', lightnings_overlay_south, 365);

if ((lightnings_overlay_west_set == 0)&&(read_Cookie('lightnings_overlay_west') != null)) {
  lightnings_overlay_west= parseFloat(read_Cookie('lightnings_overlay_west')); }
write_Cookie('lightnings_overlay_west', lightnings_overlay_west, 365);

if ((lightnings_overlay_east_set == 0)&&(read_Cookie('lightnings_overlay_east') != null)) {
  lightnings_overlay_east= parseFloat(read_Cookie('lightnings_overlay_east')); }
write_Cookie('lightnings_overlay_east', lightnings_overlay_east, 365);

if ((uselect_set == 0)&&(read_Cookie('uselect') != null)) {
  uselect= read_Cookie('uselect'); }
write_Cookie('uselect', uselect, 365);

if ((password_set == 0)&&(read_Cookie('password') != null)) {
  password= read_Cookie('password'); }
write_Cookie('password', password, 365);

//
// Simulates PHP's date function
//
Date.prototype.format = function(format) {
	var returnStr = '';
	var replace = Date.replaceChars;
	for (var i = 0; i < format.length; i++) {
		var curChar = format.charAt(i);
		if (replace[curChar]) {
			returnStr += replace[curChar].call(this);
		} else {
			returnStr += curChar;
		}
	}
	return returnStr;
};

Date.replaceChars = {
	shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
	longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
	longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
	
	// Day
	d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
	D: function() { return Date.replaceChars.shortDays[this.getDay()]; },
	j: function() { return this.getDate(); },
	l: function() { return Date.replaceChars.longDays[this.getDay()]; },
	N: function() { return this.getDay() + 1; },
	S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 3 && this.getDate() != 13 ? 'rd' : 'th'))); },
	w: function() { return this.getDay(); },
	z: function() { return "Not Yet Supported"; },
	// Week
	W: function() { return "Not Yet Supported"; },
	// Month
	F: function() { return Date.replaceChars.longMonths[this.getMonth()]; },
	m: function() { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); },
	M: function() { return Date.replaceChars.shortMonths[this.getMonth()]; },
	n: function() { return this.getMonth() + 1; },
	t: function() { return "Not Yet Supported"; },
	// Year
	L: function() { return (((this.getFullYear()%4==0)&&(this.getFullYear()%100 != 0)) || (this.getFullYear()%400==0)) ? '1' : '0'; },
	o: function() { return "Not Supported"; },
	Y: function() { return this.getFullYear(); },
	y: function() { return ('' + this.getFullYear()).substr(2); },
	// Time
	a: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
	A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
	B: function() { return "Not Yet Supported"; },
	g: function() { return this.getHours() % 12 || 12; },
	G: function() { return this.getHours(); },
	h: function() { return ((this.getHours() % 12 || 12) < 10 ? '0' : '') + (this.getHours() % 12 || 12); },
	H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
	i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
	s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
	// Timezone
	e: function() { return "Not Yet Supported"; },
	I: function() { return "Not Supported"; },
	O: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + '00'; },
	P: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + ':' + (Math.abs(this.getTimezoneOffset() % 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() % 60)); },
	T: function() { var m = this.getMonth(); this.setMonth(0); var result = this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/, '$1'); this.setMonth(m); return result;},
	Z: function() { return -this.getTimezoneOffset() * 60; },
	// Full Date/Time
	c: function() { return this.format("Y-m-d") + "T" + this.format("H:i:sP"); },
	r: function() { return this.toString(); },
	U: function() { return this.getTime() / 1000; }
};
