var mapimg = {};
$(document).ready(function(){
	$("#State").change(function(e){
		$("#SearchMap img").attr("src", mapimg[$("#State").val()]);
		$("#Region").attr("disabled","true");
		$("#Area").attr("disabled","true").children("option:first").attr("selected","true");
		$("#Go").attr("disabled","true");
		if($(this).val()!=="Off"){
		    $("#Region").load("/state2region.php?r="+Math.random()+"&state=" + escape($(this).val()),null,function(){
				if($(this).children().length > 1){
					$(this).removeAttr("disabled");
				}
				else{
					$(this).html("<option value=''>No Areas Available</option>");
				}
		    });
		}
		else{
			$("#Region").html("<option value=''>Select Area</option>");
		}
	});
	$("#Region").change(function(e){
		$("#Area").attr("disabled","true");
		$("#Go").attr("disabled","true");
		if($(this).val()!==""){
			var a = "/region2area.php?r="+Math.random()+"&region=" + escape($(this).val());
		    $("#Area").load(a,null,function(e,status){
				if($(this).children().length > 1){
					$(this).removeAttr("disabled");
					if($(this).children().length == 2){
						$("option:last",this).attr("selected","true");
					}
				}
				else{
					$(this).html("<option value=''>No Areas Available</option>");
				}
		    });
		}
		else{
			$("#Area").html("<option value=''>Select Region</option>");
		}
	});
	$("#Area").change(function(e){
		if($(this).val()!=="" && $(this).val()!=="0"){
			$("#Go").removeAttr("disabled");
		}
	});
	$.each(["Off","NSW","VIC","SA","WA","TAS","QLD","NT"],function(i,area){
		mapimg[area] = "/Images/Map_" + area + ".gif";
		var img = new Image();
		img.src = mapimg[area];
	});
	$("#Map area").hover(function(){
		$("#SearchMap img").attr("src", mapimg[$(this).attr("id")]);
	},function(){
		$("#SearchMap img").attr("src", mapimg[$("#State").val()]);
	}).click(function(){
		$("#State").val($(this).attr("id")).change();
	});
	$("#State").change();
});
