function OnNewLatLng()
{
	/*

	3.1	Validation four screen inputs
	3.2	If pass
		3.2.1	set four script bbox parameters
		3.2.2	set token bBBoxFromStateCounty=false;
		3.2.3	Invoke updatebbox () 
		3.2.4	Set state list and county list (Section C) to default


	*/


	var northElement = get_element("north");
	var southElement = get_element("south");
	var eastElement = get_element("east");
	var westElement = get_element("west");
	var bError = false;
	if(northElement.value.length<=0){
		alert("North value is empty, please define one value between -90 and 90 and try again.");
		bError = true;
	}
	if(southElement.value.length<=0){
		alert("South value is empty, please define one value between -90 and 90 and try again.");
		bError = true;				
	}
	if(eastElement.value.length<=0){
		alert("East value is empty, please define one value between -180 and 180 and try again.");
		bError = true;				
	}
	if(westElement.value.length<=0){
		alert("West value is empty, please define one value between -180 and 180 and try again.");
		bError = true;				
	}
	
	if(bError){
		return;
	}
	
	var tmpBBoxNorth = parseFloat(northElement.value);
	var tmpBBoxSouth = parseFloat(southElement.value);
	var tmpBBoxEast = parseFloat(eastElement.value);
	var tmpBBoxWest = parseFloat(westElement.value);
	
	if(tmpBBoxNorth<tmpBBoxSouth){
		bError = true;
		alert("The North value should be greater than South value.\nHowever, I got: North="+tmpBBoxNorth+" and South="+tmpBBoxSouth+". \nPlease modify them and try again.");
	}
	
	if(tmpBBoxEast<tmpBBoxWest){
		bError = true;
		alert("The East value should be greater than West value.\nHowever, I got: East="+tmpBBoxEast+" and West="+tmpBBoxWest+". \nPlease modify them and try again.");
	}
	if(bError){
		return;
	}
	
	//update these four parameters
	BBoxWest = tmpBBoxWest;
	BBoxEast = tmpBBoxEast;
	BBoxNorth = tmpBBoxNorth;
	BBoxSouth = tmpBBoxSouth;
	
	//update map view and summary view, with new bbox values
	UpdateBBox();		
}
function updateProjection() {
	var crsselect = document.queryform.crs;
	if(crsselect.value != 'EPSG:4326')
		return;
	
	var minx = document.queryform.west.value;
	var maxx = document.queryform.east.value;
	
	var minzone = parseInt((parseFloat(minx) + 180.0)/6) + 1;
	var maxzone = parseInt((parseFloat(maxx) + 180.0)/6) + 1;

//	if(minzone > 60 || minzone < 1 || maxzone > 60 || maxzone < 1) {
	if(minzone > 90 || minzone < 1 || maxzone > 90 || maxzone < 1) {
		for(i=0; i<crsselect.length; i++) {
			if(crsselect.options[i].value != "EPSG:4326")
				crsselect.options[i].disabled = true;
		}
	} else {
		minzone = 32600 + minzone;
		maxzone = 32600 + maxzone;
		for(i=0; i<crsselect.length; i++) {
			crs = crsselect.options[i].value;
			if(crs != "EPSG:4326") {
				utmzone = parseInt(crs.substring(5));
				if(utmzone <= maxzone && utmzone >= minzone)
					crsselect.options[i].disabled = false;
				else
					crsselect.options[i].disabled = true;
			}
		}
	}
}

function onProjectionChange() {
	// Retrieve old crs and current bbox
	var scrs = document.queryform.scrs.value;
	var tcrs = document.queryform.crs.value;
	var minx = document.queryform.west.value;
	var miny = document.queryform.south.value;
	var maxx = document.queryform.east.value;
	var maxy = document.queryform.north.value;

	// Do convertion
	var tbbox = convert_bbox(scrs, tcrs, minx, miny, maxx, maxy);
	
	// Set new bbox
	setbbox(tbbox[0], tbbox[1], tbbox[2], tbbox[3]);
	document.queryform.scrs.value = tcrs;
}

function setbbox(minx, miny, maxx, maxy) {
	document.getElementById('west').value = minx;
	document.getElementById('south').value = miny;
	document.getElementById('east').value = maxx;
	document.getElementById('north').value = maxy;
	return true;
}

function showLoadImage(visible) {
	if(visible)
		document.getElementById('loadimg_div').display = 'block';
	else
		document.getElementById('loadimg_div').display = 'none';
}

function gettabindex() {
	return document.getElementById("tabindex").value;
}

function nobboxchange() {
	if (document.queryform.NoBBox.checked) {
		document.queryform.west.disabled = true;
		document.queryform.south.disabled = true;
		document.queryform.east.disabled = true;
		document.queryform.north.disabled = true;
		document.queryform.crs.disabled = true;

	} else {
		document.queryform.west.disabled = false;
		document.queryform.south.disabled = false;
		document.queryform.east.disabled = false;
		document.queryform.north.disabled = false;
		document.queryform.crs.disabled = false;

	}
	return true;
}

function getBBox(url) {	
	hr = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		hr = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		try {
			hr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				hr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!hr) {
		alert('Cannot create an XMLHTTP instance.');
		return false;
	}
	hr.onreadystatechange = gotbbox;
	hr.open('GET', url, true);
	hr.send(null);

}





//---------------------------------------------------------------------


//---------------------------------------------------------------------
function OnMoved()
{
	var maxy,miny,maxx,minx;
	if(bGetBBox){
		maxy = BBoxNorth;
		miny = BBoxSouth; 
		minx = BBoxWest;
		maxx = BBoxEast;
//		bGetBBox = false;
	}else{
		var curGBounds = map.getBounds();
		maxy = curGBounds.getNorthEast().lat();
		miny = curGBounds.getSouthWest().lat();
		maxx = curGBounds.getNorthEast().lng();
		minx = curGBounds.getSouthWest().lng(); 
	}				
	document.getElementById("north").value = maxy;
	document.getElementById("south").value = miny;
	document.getElementById("east").value = maxx;
	document.getElementById("west").value = minx;								
	map.clearOverlays();	

	var rectBounds = new GLatLngBounds(new GLatLng(miny, minx),new GLatLng(maxy, maxx));
	markerLatLon = new Rectangle(rectBounds);
	map.addOverlay(markerLatLon);				
}

//---------------------------------------------------------------------

//---------------------------------------------------------------------
function UpdateBBox()
{
	/*
		7	UpdateBBox
		Upon new four script bbox parameters, to update map view and summary message
		And may need to update four screen parameters
		7.1	If four script bbox parameters are not empty (length>0)
			7.1.1	Map Overlay
				7.1.1.1	clear all overlays
				7.1.1.2	Add a new map overlay
				7.1.1.3	Update zoom level
				7.1.1.4	Recenter
			7.1.2	Summary message(Section D)
				7.1.2.1	Define new message
			7.1.3	Screen display
				7.1.3.1	If bBBoxFromStateCounty is true,
					which means this updateBBox is invoked from server script which is triggered by selecting state and county, need to update screen display(Section A, and B)
					7.1.3.1.1	define new four values
					7.1.3.1.2	Uncheck the global checkbox
		7.2	Else (being cleared)
			7.2.1	Map overlay
				7.2.1.1	Clear all overlays
				7.2.1.2	Set to default zoom level and center point
			7.2.2	Summary message
				7.2.2.1	Set empty
			7.2.3	Set state list and county list (Section C) to default
	*/

	//four values may be empty, eg. being cleared
	if(BBoxWest.length<=0 && 
			BBoxEast.length<=0 && 
			BBoxNorth.length<=0 && 
			BBoxSouth.length<=0 )
	{
		map.clearOverlays();
		//map.centerAndZoom(new GPoint(0, 0), 16);
		map.setCenter(new GLatLng(0, 0), 1);
	}else{
		//add a new overlay in google map
		//found that google map lng [-85,85]
		var disBBoxNorth = BBoxNorth;
		var disBBoxSouth = BBoxSouth; 
		var disBBoxWest = BBoxWest;
		var disBBoxEast = BBoxEast;
		if(disBBoxNorth>85){
			disBBoxNorth=85;
		}
		if(disBBoxSouth<-85){
			disBBoxSouth=-85;
		}
		
/*		map.clearOverlays();	
		markerLatLon = new GPolyline([
							new GPoint(disBBoxWest, disBBoxNorth),
							new GPoint(disBBoxEast, disBBoxNorth),
							new GPoint(disBBoxEast, disBBoxSouth),
							new GPoint(disBBoxWest, disBBoxSouth),
							new GPoint(disBBoxWest, disBBoxNorth)
							], 
							"#ff0000", 3);
							  
		var n = gnl(BBoxEast,BBoxWest,BBoxNorth,BBoxSouth);
		map.centerAndZoom(new GPoint( (BBoxWest+BBoxEast)*1.0/2.0, (BBoxNorth+BBoxSouth)*1.0/2.0),n);
		map.addOverlay(markerLatLon);
*/
		var n = getNewLevel(BBoxEast,BBoxWest,BBoxNorth,BBoxSouth);
		map.setCenter(new GLatLng((BBoxNorth+BBoxSouth)*1.0/2.0, (BBoxWest+BBoxEast)*1.0/2.0), 16-n);
		var rectBounds = new GLatLngBounds(new GLatLng(disBBoxSouth, disBBoxWest),new GLatLng(disBBoxNorth, disBBoxEast));
		markerLatLon = new Rectangle(rectBounds);
		map.addOverlay(markerLatLon);
	}
}

// Added by Yaxing, 01/24/2008
function validateBBox(){
	var c_west, c_south, c_east, c_north;					
	c_west = parseFloat(document.getElementById('west').value);
	c_south = parseFloat(document.getElementById('south').value);
	c_east = parseFloat(document.getElementById('east').value);
	c_north = parseFloat(document.getElementById('north').value);				
	if(isNaN(c_west) || isNaN(c_south) || isNaN(c_east) || isNaN(c_north)){
		alert('BoundingBox is not valid! ');
		return false;
	}
	if((c_west >= c_east) || (c_south >= c_north)){
		alert('BoundingBox is not valid! ');
		return false;						
	}
	return true;				
}


// Used to auto-centering the status bar
var ns1 = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
var startX = (ns1 ? innerWidth : iecompattest().clientWidth)/2 - 50; //set x offset of bar in pixels
var startY = (ns1 ? innerHeight : iecompattest().clientHeight)/2 - 50; //set y offset of bar in pixels
var verticalpos="fromtop"; //enter "fromtop" or "frombottom"

function staticbar(){
	barheight=document.getElementById("statusbar").offsetHeight;
	var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
	var d = document;
	function ml(id){
		var el=d.getElementById(id);
		el.style.visibility="visible";
		if(d.layers)
			el.style=el;
		el.sP=function(x,y) {this.style.left=x+"px";this.style.top=y+"px";};
		el.x = startX;
		if (verticalpos=="fromtop")
			el.y = startY;
		else{
			el.y = ns ? (pageYOffset + innerHeight) : (iecompattest().scrollTop + iecompattest().clientHeight);
			el.y -= startY;
		}
		return el;
	}
	window.stayTopLeft=function(){
		if (verticalpos=="fromtop"){
			var pY = ns ? pageYOffset : iecompattest().scrollTop;
			ftlObj.y += (pY + startY - ftlObj.y)/8;
		}
		else{
			var pY = ns ? (pageYOffset + innerHeight - barheight) : (iecompattest().scrollTop + iecompattest().clientHeight - barheight);
			ftlObj.y += (pY - startY - ftlObj.y)/8;
		}
		ftlObj.sP(ftlObj.x, ftlObj.y);
		setTimeout("stayTopLeft()", 10);
	}
	ftlObj = ml("statusbar");
	stayTopLeft();
}

if (window.addEventListener)
	window.addEventListener("load", staticbar, false)
else if (window.attachEvent)
	window.attachEvent("onload", staticbar)
else if (document.getElementById)
	window.onload=staticbar
	
function makeQuery() {
	if (validation())  {
		document.getElementById('inner_div').style.visibility='visible';	// Show the status bar
		queryform.submit();
	}
}



function onUnload() {
	document.getElementById('inner_div').style.visibility='hidden';
}


   function addressMarker(address) {
    var geocoder;
    geocoder = new GClientGeocoder();
    geocoder.getLatLng(address,
                       function(point) {
                         if(!point) {
                           alert("The address you typed in, " + address + ", not found");
                         } 
	                   else {
                           map.setCenter(point, 13);
 	                     marker = new GMarker(point);
	                     map.addOverlay(marker);
                         }
                      }
                     )
  }

