//To Do: add preventDefault, domReady methods, replace onload, update onlick links

//generic 
var bom ={
	//methods: addEvent, setCookie, getCookie, eraseCookie,preventDefault, domReady
	addEvent: function (obj,evt,fn){
		if(obj.addEventListener){
			obj.addEventListener(evt, fn, false);
		}
		else if(obj.attachEvent){
			obj.attachEvent('on' + evt,fn);
		}
	},
	
	setCookie: function (c_name,value,expiredays){
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+ ((expiredays===null) ? "" : ";expires="+exdate.toGMTString());
	},
	
	getCookie: function(c_name){
		if (document.cookie.length>0){
			var c_start=document.cookie.indexOf(c_name + "=");
				if (c_start!=-1){ 
					c_start=c_start + c_name.length+1; 
					var c_end=document.cookie.indexOf(";",c_start);
					if (c_end===-1){ c_end=document.cookie.length;}
					return unescape(document.cookie.substring(c_start,c_end));
				} 
		}
		return "";
	},
	eraseCookie: function(name) {bom.setCookie(name,"",-1);},
	preventDefault: function (){
	   //to do
	},
	domReady: function (){
	   //to do
	}
};

var watlLoc={
	//methods: checkCookie, resetButtons, setLoc, gotoURL,
	checkCookie: function(){
		if(document.cookie){
			var stateLoc = bom.getCookie('watl');
			if (stateLoc!==null && stateLoc!==""){
				document.getElementById('productSelector').where.selectedIndex = stateLoc*1;
			}
		}
	},
	resetButtons: function(){
		var radiobuts = document.productSelector.what;
		for (var i = 0; radiobuts[i]; i++) {
			 radiobuts[i].checked = false;
		}
	},
	
	//to do - highlight location on map
	/* order= NSW, VIC, QLD, WA, SA, Tas,NT, ACT,  */
	setLoc: function (val){
		document.productSelector.where.selectedIndex=val;
		watlLoc.resetButtons();
	},
				
	gotoURL: function (){
		var feedback;
		var noLocation=true;
		var theLocation = document.productSelector.where.selectedIndex;
			if(document.productSelector.where.options[theLocation].value == "null"){ 
				feedback="Please select a location first";
				alert(feedback);
				return false;
		   }
		
		else{
			theLocation=theLocation*1;
			//setCookie if location change
			bom.setCookie('watl',theLocation,60);
			var set,url;
			noLocation=false;
			//check the product
			var noProduct=true;
			var theProduct= document.productSelector.what;
			for(var i=0;i<theProduct.length;i++){
					if(theProduct[i].checked){
						noProduct=false;
						//get array name
						var arrayName=theProduct[i].value;
                        url= watlLoc[theProduct[i].value][theLocation-1];
                        window.location.href = url;
						return false;
					}
			}
		}
	},
/* order= NSW, VIC, QLD, WA, SA, Tas,NT, ACT,  */
	/* state forecasts*/ 
	/* old vic /cgi-bin/wrap_fwo.pl?IDV10310.txt */
	sf:["/cgi-bin/wrap_fwo.pl?IDN10031.txt",
	"/products/IDV10310.shtml",
	"/cgi-bin/wrap_fwo.pl?IDQ10700.txt",
	"/cgi-bin/wrap_fwo.pl?IDW12000.txt",
	"/cgi-bin/wrap_fwo.pl?IDS10033.txt",
	"/cgi-bin/wrap_fwo.pl?IDT13100.html",
	"/cgi-bin/wrap_fwo.pl?IDD10111.html",
	"/cgi-bin/wrap_fwo.pl?IDN10035.txt"
	],
	
	/*towns and cities, no NT product, NT from above repeated */
	tf:[
	"/cgi-bin/wrap_fwo.pl?IDN10084.txt",
	"/weather/vic/maps/vic-forecast-map-7-day.shtml",
	"/cgi-bin/wrap_fwo.pl?IDQ10600.txt",
	"/cgi-bin/wrap_fwo.pl?IDW14500.txt",
	"/cgi-bin/wrap_fwo.pl?IDS10043.txt",
	"/cgi-bin/wrap_fwo.pl?IDT13400.html",
	"/cgi-bin/wrap_fwo.pl?IDD10114.html",
	"/cgi-bin/wrap_fwo.pl?IDN10049.txt"
	],
	
	/*district forecast clickable maps, ACT direct to forecast */
	df: [
	"/weather/nsw/nsw-forecast-map.shtml",
	"/weather/vic/vic-forecast-map.shtml",
	"/weather/qld/qld-forecast-map.shtml",
	"/weather/wa/wa-forecast-map.shtml",
	"/weather/sa/sa-forecast-map.shtml",
	"/weather/tas/tas-forecast-map.shtml",
	"/weather/nt/nt-forecast-map.shtml",
	"/cgi-bin/wrap_fwo.pl?IDN10035.txt"
	],
	
	/*latest weather*/
	lw:[
	"/products/IDN60800.shtml",
	"/products/IDV60800.shtml",
	"/products/IDQ60800.shtml",
	"/products/IDW60800.shtml",
	"/products/IDS60800.shtml",
	"/products/IDT60800.shtml",
	"/products/IDD60800.shtml",
	"/products/IDN60800.shtml"
	],
	
	/* latest weather graphs...temp values */
	lg: [
	"/watl/weather/observations/nsw-latest-weather-graphs.shtml",
	"/watl/weather/observations/vic-latest-weather-graphs.shtml",
	"/watl/weather/observations/qld-latest-weather-graphs.shtml",
	"/watl/weather/observations/wa-latest-weather-graphs.shtml",
	"/watl/weather/observations/sa-latest-weather-graphs.shtml",
	"/watl/weather/observations/tas-latest-weather-graphs.shtml",
	"/watl/weather/observations/nt-latest-weather-graphs.shtml",
	"/watl/weather/observations/act-latest-weather-graphs.shtml"
	],
	
	/* thermal comfort */
	tc:[
	"/products/IDN65179.shtml",
	"/products/IDV65079.shtml",
	"/products/IDQ65214.shtml",
	"/products/IDW65100.shtml",
	"/products/IDS65004.shtml",
	"/products/IDT65050.shtml",
	"/products/IDD65155.shtml",
	"/products/IDN65179.shtml"				 
	],
	
	/*ag_bull*/
	ab:[
	"/products/IDN65176.shtml",
	"/products/IDV65176.shtml",
	"/products/IDQ60604.shtml",
	"/products/IDW65176.shtml",
	"/products/IDS65176.shtml",
	"/products/IDT65176.shtml",
	"/products/IDD65176.shtml",
	"/products/IDN65176.shtml"
	],
	
	/*daily_weather_obs*/
	dw: [
	"/climate/dwo/IDCJDW0200.shtml",
	"/climate/dwo/IDCJDW0300.shtml",
	"/climate/dwo/IDCJDW0400.shtml",
	"/climate/dwo/IDCJDW0600.shtml",
	"/climate/dwo/IDCJDW0500.shtml",
	"/climate/dwo/IDCJDW0700.shtml",
	"/climate/dwo/IDCJDW0800.shtml",
	"/climate/dwo/IDCJDW0100.shtml"
	],
	///climate/data/weather-data.shtml
	/*monthly_weather_obs*/
	md: [
	"/climate/data/weather-data.shtml",
	"/climate/data/weather-data.shtml",
	"/climate/data/weather-data.shtml",
	"/climate/data/weather-data.shtml",
	"/climate/data/weather-data.shtml",
	"/climate/data/weather-data.shtml",
	"/climate/data/weather-data.shtml",
	"/climate/data/weather-data.shtml"
	],
	
	ca: [			 
	"/climate/averages/index.shtml?map_type=cdio&code=1", 
	"/climate/averages/index.shtml?map_type=cdio&code=3", 
	"/climate/averages/index.shtml?map_type=cdio&code=5", 
	"/climate/averages/index.shtml?map_type=cdio&code=7",
	"/climate/averages/index.shtml?map_type=cdio&code=9",
	"/climate/averages/index.shtml?map_type=cdio&code=11",
	"/climate/averages/index.shtml?map_type=cdio&code=12",
	"/climate/averages/index.shtml?map_type=cdio&code=1"
	]

};
	
//change to domready
window.onLoad=bom.addEvent(window, 'load', watlLoc.checkCookie);
