function load() {
  if (GBrowserIsCompatible()) {
	window.map = new GMap2(document.getElementById("map"));
	window.geocoder = new GClientGeocoder();
	geocoder.getLatLng("kansas city, mo", init);
	var zoomCtl = new GSmallMapControl();
	var typeCtl = new GMapTypeControl();
	map.addControl(zoomCtl);
	map.addControl(typeCtl);
  }
}

function init(point) {
	map.setCenter(point, 3);
}

function gotoAddress(point) {
	map.clearOverlays();
	var mark = new GMarker(point);
	map.addOverlay(mark);
	map.setCenter(point, 16);
}

function gotoLocation(address) {

	var html = "<a href=\"http://maps.google.com/maps?f=q&hl=en&geocode=&q="+address+"&sll=37.0625,-95.677068&sspn=59.727033,70.839844&ie=UTF8&z=17&iwloc=addr&om=1&pw=2\" target=\"_blank\">PRINT</a>";

	var print = document.getElementById('print');
	print.innerHTML = html;
	print.style.display = 'block';

	geocoder.getLatLng(address, gotoAddress);
}


/**** VERIFICATION ****/

function locationVerify(form) {
	var valid = true;
	var zipUsed = false;
	with ( form ) {
		with ( zip ) {
			if ( value.match(/[^0-9]+/) ) {
				valid = false;
				alert("Please only use digits (0-9) for the zipcode");
			}
			if ( value.length > 0 && value.length < 5 ) {
				valid = false;
				alert("Please enter all 5 digits of the zipcode.");
			}
			if ( valid == false ) {
				focus();
				select();
			}
			if ( valid && value.length > 0 ) {
				zipUsed = true;
			}
		}
		with ( state ) {
			if ( !zipUsed ) {
				if ( value == "" ) {
					alert("You must enter a zipcode or choose a state.");
					valid = false;
				}
			}
		}
	}
	return valid;
}

/**** AJAX ****/

function getResults(url, zip, state, imgDir) {
	var pars = "";
	if ( zip ) pars = "zip=" + zip;
	if ( state ) {
		if ( zip ) pars += "&";
		pars += "state=" + state;
	}
	if ( imgDir ) {
		if ( zip || state ) pars += "&";
		pars += "imgDir=" + imgDir;
	}
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: showResults } );
}

function showResults (originalRequest) {
	var html = originalRequest.responseText;
	document.getElementById("locationResults").innerHTML = html;            
}

function resultsPop(url, zip, state, imgDir) {
	popResults();
	getResults(url, zip, state, imgDir);
}

function popResults() {
	var pop = document.getElementById('resultsTitle');
	pop.style.display = 'block';
	var pop = document.getElementById('locationResults');
	pop.style.display = 'block';
}


