
//
// ghmrisjumpto.js
//
var gh_stateSelect = null;
var gh_countySelect = null;
var gh_zipbox = null;
var ghstate = new Array();
var ghcounty = new Array();
var ghzip = new Array();
var gh_dynamic_select_changes = 1;
var gh_mrisjumpto = 1;

function InitMRISJumpTo(opts)
{
	var i;
	if (typeof opts.stateselect != 'undef') {
		gh_stateSelect = new ghSelectHelper({ id: opts.stateselect });
	} else {
		alert('must specify state select id');
	}
	if (typeof opts.countyselect != 'undef') {
		gh_countySelect = new ghSelectHelper({ id: opts.countyselect });
	} else {
		alert('must specify county select id');
	}
	if (typeof opts.zipbox != 'undef') {
		gh_zipbox = document.getElementById(opts.zipbox);
	} else {
		alert('must specify zip box - input id');
	}
	new Ajax.Request('/cgi-bin/aa.fcgi?go=geo&t=state&sf=24&sf=51&sf=54&sf=11&sf=42&bounds=1', {
		method: 'get',
		evalJSON: true,
		onSuccess: function(transport) {
			var i;
			eval(transport.responseText);
			for (i=0; i < ghstate.length; i++) {
				gh_stateSelect.appendOption({ value: ghstate[i].sf, text: ghstate[i].sn });
			}
		}
	});
    document.jumpto.action = 'javascript:ghZipChanged();';
}

function ghStateChanged()
{
	// Clear the county list
	var sf;
	gh_countySelect.clearOptions({ lower: 1 });
	if (gh_stateSelect.getSelectedIndex() > 0) {
		// A state was selected
		if (gh_dynamic_select_changes == 1) {
		    var opt = gh_stateSelect.getSelectedIndex();
			var north = ghstate[opt - 1].n;
			var east = ghstate[opt - 1].e;
			var south = ghstate[opt - 1].s;
			var west = ghstate[opt - 1].w;

			var southWest = new google.maps.LatLng(south,west);
			var northEast = new google.maps.LatLng(north,east);
			var bounds = new google.maps.LatLngBounds(southWest,northEast);
			map.fitBounds(bounds);
		}
		sf = gh_stateSelect.getSelected().value;
		new Ajax.Request('/cgi-bin/aa.fcgi?go=geo&t=county&bounds=1&sf=' + sf, {
			method: 'get',
			evalJSON: true,
			onSuccess: function(transport) {
				var i;
				eval(transport.responseText);
				for (i=0; i < ghcounty.length; i++) {
					gh_countySelect.appendOption({ value: ghcounty[i].cf, text: ghcounty[i].cn });
				}
			}
		});
	} // end if stateselect selected index > 0
}

function ghCountyChanged()
{
	// Clear the county list
	if (gh_countySelect.getSelectedIndex() > 0) {
		// A county was selected
		if (gh_dynamic_select_changes == 1) {
		    var opt = gh_countySelect.getSelectedIndex();
			var north = ghcounty[opt - 1].n;
			var east = ghcounty[opt - 1].e;
			var south = ghcounty[opt - 1].s;
			var west = ghcounty[opt - 1].w;

			var southWest = new google.maps.LatLng(south,west);
			var northEast = new google.maps.LatLng(north,east);
			var bounds = new google.maps.LatLngBounds(southWest,northEast);
			map.fitBounds(bounds);
		}
	} // end if stateselect selected index > 0
}

function ghZipChanged()
{
	new Ajax.Request('/cgi-bin/aa.fcgi?go=geo&t=zip&bounds=1&zip=' + gh_zipbox.value, {
		method: 'get',
		evalJSON: true,
		onSuccess: function(transport) {
			var i;
			eval(transport.responseText);
			if (ghzip.length == 1) {
				var north = ghzip[0].n;
				var east = ghzip[0].e;
				var south = ghzip[0].s;
				var west = ghzip[0].w;

				var southWest = new google.maps.LatLng(south,west);
				var northEast = new google.maps.LatLng(north,east);
				var bounds = new google.maps.LatLngBounds(southWest,northEast);
				map.fitBounds(bounds);
				gh_stateSelect.selectOption({ index: 0});
			}
		}
	});
}


