
// Preload certain global images
earlier1 = new Image();		earlier1.src = "/watl/images/symbols/earlier.gif";
earlier0 = new Image();		earlier0.src = "/watl/images/symbols/earlier-0.gif";
later1 = new Image();		later1.src = "/watl/images/symbols/later.gif";
later0 = new Image();		later0.src = "/watl/images/symbols/later-0.gif";

minimap1 = new Image();		minimap1.src = "/watl/images/maps/map_mini/nsw/map_mini_nsw.gif";
minimap2 = new Image();		minimap2.src = "/watl/images/maps/map_mini/vic/map_mini_vic.gif";
minimap3 = new Image();		minimap3.src = "/watl/images/maps/map_mini/tas/map_mini_tas.gif";
minimap4 = new Image();		minimap4.src = "/watl/images/maps/map_mini/sa/map_mini_sa.gif";
minimap5 = new Image();		minimap5.src = "/watl/images/maps/map_mini/wa/map_mini_wa.gif";
minimap6 = new Image();		minimap6.src = "/watl/images/maps/map_mini/nt/map_mini_nt.gif";
minimap7 = new Image();		minimap7.src = "/watl/images/maps/map_mini/qld/map_mini_qld.gif";

function getXMLHttpObject() {
	var xmlHttp = null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your web browser appears to be too old to display these pages. Please update your browser.");
				return false;
			}
		}
	}
	return xmlHttp;
}


function loadForecastChanges(menu, source) {
	var url;
	var p;
	var i;
	var xmlHttp = getXMLHttpObject();
	xmlHttp.onreadystatechange = function() {
		// 0 - not initialized;    1 - request has been set up;    2 - sent;    3 - in process;    4 - complete
		if(xmlHttp.readyState==4) {
			// read in HTTP response
			p = eval("(" + xmlHttp.responseText + ")");
			
			// load image and usemap
			operaPreload = new Image();
			operaPreload.src = p.imgSrc;
			document.getElementById('forecastImg').src=p.imgSrc;
			//document.getElementById('forecastImg').useMap=p.usemap;  // old method
			// attempting to remove and set usemap attribute to deal with Opera bug.
			document.getElementById('forecastImg').removeAttribute('usemap');
			document.getElementById('forecastImg').setAttribute('usemap',p.usemap,0);//the third parameter is an IE workaround to make the attribute case insensitive
			
			document.getElementById('updatedImg').src=p.updatedImgSrc;
			
			if(p.model == "amnt" || p.model == "prob") {
				
				document.getElementById('date_row_4day').style.display = "none";
				document.getElementById('location_row').style.display = "";
				document.getElementById('date_row_amnt').style.display = "";
			
				if((source == "state" || source == "model") && p.model == "amnt") {
					// populate district drop down
					document.getElementById('district').options.length = p.districtDDvalues.length +1;
					document.getElementById('district').options[0] = new Option("Select district");
					document.getElementById('district').options[0].value = "";
					for(i = 0; i < p.districtDDvalues.length; i++) {
						document.getElementById('district').options[i+1] = new Option(p.districtDDdisplay[i]);
						document.getElementById('district').options[i+1].value = p.districtDDvalues[i];
					}
				}
				if(p.disableDistrict == "true" || p.model == "prob") document.getElementById('district').disabled = "disabled";
				else document.getElementById('district').disabled = "";
				
				if(source == "date") {					
					// earlier later button logic
					if(menu.selectedIndex <= 0) {
						document.getElementById('earlier').innerHTML = "<img src=\"/watl/images/symbols/earlier-0.gif\" alt=\"No earlier forecast available\" title=\"No earlier forecast available\" width=\"64\" height=\"11\" /><br />";
					} else  {
						document.getElementById('earlier').innerHTML = "<a href=\"javascript:func();\" onclick=\"loadForecastEarlier()\"><img src=\"/watl/images/symbols/earlier.gif\" alt=\"Previous day\" title=\"Previous day\" width=\"64\" height=\"11\" /></a><br />";
					}
					if(menu.selectedIndex >= 4) {
						document.getElementById('later').innerHTML = "<img src=\"/watl/images/symbols/later-0.gif\" alt=\"No later forecast available\" title=\"No later forecast available\" width=\"64\" height=\"11\" /><br />";
					} else {
						document.getElementById('later').innerHTML = "<a href=\"javascript:func();\" onclick=\"loadForecastLater()\"><img src=\"/watl/images/symbols/later.gif\" alt=\"Next day\" title=\"Next day\" width=\"64\" height=\"11\" /></a><br />";
					}
				}
				
			
			} else if(p.model == "4day") {
				document.getElementById('date_row_4day').style.display = "";
				document.getElementById('location_row').style.display = "none";
				document.getElementById('date_row_amnt').style.display = "none";
/*				document.getElementById('date_row_4day').innerHTML = "<td><span class=\"control-label\">Date</span></td><td>";
				document.getElementById('date_row_4day').innerHTML += "<label class=\"normal\"><input type=\"radio\" name=\"aggdate\" id=\"aggdatea\" value=\"1to4\" onclick=\"loadForecastChanges(this, 'model')\"/>" + p.aggDate1 + " to " + p.aggDate2 + "</label><br />";
				document.getElementById('date_row_4day').innerHTML += "<label class=\"normal\"><input type=\"radio\" name=\"aggdate\" id=\"aggdateb\" value=\"5to8\" onclick=\"loadForecastChanges(this, 'model')\"/>" + p.aggDate3 + " to " + p.aggDate4 + "</label><br />";
				document.getElementById('date_row_4day').innerHTML += "</td><td colspan=\"2\"></td>";
*/
				
				
			}
			

			// top date change
			document.getElementById('dt_display').innerHTML = p.displayDateCombined;

			// mini map logic
			if(source == "state" || source == "district" || source == "model") {
				if((p.state == "" && p.district == "") || p.model == "4day") {
					document.getElementById('maplocator').innerHTML = "<p><img src=\"/watl/images/maps/map_mini/map_mini_aus.gif\" alt=\"Current view Australia\" width=\"71\" height=\"65\" title=\"Current view Australia\" /></p>";
				} else if(!p.state == "" && p.district == "") {
					document.getElementById('maplocator').innerHTML =  "<p><a href=\"javascript:func()\" onclick=\"zoomOutFull()\"><img src=\"/watl/images/maps/map_mini/"+p.state+"/map_mini_"+p.state+".gif\" alt=\"Current view "+p.displayStateScreen+", click to zoom out\" width=\"71\" height=\"65\" title=\"Current view "+p.displayStateScreen+", click to zoom out\" /></a></p>";
					document.getElementById('maplocator').innerHTML += "<p><a href=\"javascript:func()\" onclick=\"zoomOutFull()\"><img src=\"/watl/images/symbols/zoom-out.gif\" alt=\"Click for wider view\" width=\"13\" height=\"15\" align=\"middle\" />Zoom out</a></p>";
				} else {
					document.getElementById('maplocator').innerHTML =  "<p><a href=\"javascript:func()\" onclick=\"loadForecastState()\"><img src=\"/watl/images/maps/map_mini/"+p.state+"/fd_"+p.district+".gif\" alt=\"Current view "+p.displayStateScreen+" - "+p.displayDistrictScreen+", click to zoom out\" width=\"71\" height=\"65\" title=\"Current view "+p.displayStateScreen+" - "+p.displayDistrictScreen+", click to zoom out\" /></a></p>";
					document.getElementById('maplocator').innerHTML += "<p><a href=\"javascript:func()\" onclick=\"loadForecastState()\"><img src=\"/watl/images/symbols/zoom-out.gif\" alt=\"Click for wider view\" width=\"13\" height=\"15\" align=\"middle\" />Zoom out</a></p>";
				}
			}

			if(source == "model") {
				// prod code
				document.getElementById('prodCodeDisplay').innerHTML = "<b>Product Code:</b> "+p.prodCode;
				// top title change
				if(p.model=="amnt") {
					document.getElementById('toptitle').innerHTML = "Rainfall: Forecast Rainfall";
				} else if(p.model=="prob"){
					document.getElementById('toptitle').innerHTML = "Rainfall: Chance of Rainfall"; 
				} else if(p.model=="4day"){
					document.getElementById('toptitle').innerHTML = "Rainfall: 4 Day Total Forecast Rainfall"; 
				}
			}

		}
	}
	
	var modelRadioChoice = getModelChoice();
	var date4dayRadioChoice;
	if(modelRadioChoice=="4day") {
		date4dayRadioChoice = get4dayRadioChoice();
	} else {
		date4dayRadioChoice = menu.value;
	}
		
	url = "pme_loader.jsp?model="+ modelRadioChoice +"&date=" + date4dayRadioChoice + "&state=" + document.PMEReload.state.value + "&district=" + document.PMEReload.district.value;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function getModelChoice() {
	if(document.PMEReload.modela.checked) {
		return "amnt";
	} else if(document.PMEReload.modelb.checked) {
		return "prob";
	} else if(document.PMEReload.modelc.checked) {
		return "4day";
	} else {
		return "";
	}
}

function get4dayRadioChoice() {
	if(document.getElementById('aggdatea').checked) {
		return document.getElementById('aggdatea').value;
	} else if(document.getElementById('aggdateb').checked) {
		return document.getElementById('aggdateb').value;
	} else {
		return "";
	}
}



function loadForecastEarlier() {
	if(document.PMEReload.date.selectedIndex > 0) {
		// set new index
		document.PMEReload.date.selectedIndex = document.PMEReload.date.selectedIndex - 1;
		loadForecastChanges(document.PMEReload.date, "date");
	}
}
function loadForecastLater() {
	if(document.PMEReload.date.selectedIndex < 4) {
		// set new index
		document.PMEReload.date.selectedIndex = document.PMEReload.date.selectedIndex + 1;
		loadForecastChanges(document.PMEReload.date, "date");
	}
}

function loadForecastModel() {
	document.getElementById('district').selectedIndex = 0;
	loadForecastChanges(document.PMEReload.date, "model");
}
function loadForecastState() {
	document.getElementById('district').selectedIndex = 0;
	loadForecastChanges(document.PMEReload.date, "state");
}
function loadForecastDistrict() {
	loadForecastChanges(document.PMEReload.date, "district");
}
function zoomOutFull() {
	document.getElementById('district').selectedIndex = 0;
	document.getElementById('state').selectedIndex = 0;
	loadForecastChanges(document.PMEReload.date, "state");
}

function zoomToState(stateValue) {
	var i;
	for(i = 0; i < document.getElementById('state').options.length; i++) {
		if(document.getElementById('state').options[i].value == stateValue) {
			break;
		}
	}
	document.getElementById('state').selectedIndex = i;
	loadForecastState();
}

function zoomToDistrict(districtValue) {
	var i;
	for(i = 0; i < document.getElementById('district').options.length; i++) {
		if(document.getElementById('district').options[i].value == districtValue) {
			break;
		}
	}
	document.getElementById('district').selectedIndex = i;
	loadForecastDistrict();
}



function func() { } // empty method to handle image button clicks
