var xmlHttp;

function getElevation() {
	validate();

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	} 

	var url = "/java-kosherja/zmanim/elevation.jsp?longitude=" + document.zmanim.longitude.value + "&latitude=" + document.zmanim.latitude.value;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function stateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		var response  = xmlHttp.responseXML.documentElement;
		var m = xmlHttp.responseXML.getElementsByTagName("meters")[0].firstChild.nodeValue;
		if (m == -9999 || m == 'Unknown') {
			alert("No data coverage");
			document.zmanim.elevation.value=0;
		} else if(! validateNumeric(document.zmanim.elevation.value)){
			alert("Error looking up elevation");
		} else {
			document.zmanim.elevation.value=m;
		}
	} 
} 

function GetXmlHttpObject() { 
	var objXMLHttp=null;
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
} 

function reset(){
	document.zmanim.latitude.value="";
	document.zmanim.longitude.value="";
	document.zmanim.elevation.value=0;
	document.zmanim.timeZone.selectedIndex =0;
}

function validate(){
	document.zmanim.latitude.value = trimAll(document.zmanim.latitude.value);
	if(! validateNumeric(document.zmanim.latitude.value)){
		alert("The latitude does not have a valid value");
		return false;
	}
	document.zmanim.longitude.value = trimAll(document.zmanim.longitude.value);
	if(! validateNumeric(document.zmanim.longitude.value)){
		alert("The longitude does not have a valid numeric value");
		return false;
	}
	document.zmanim.elevation.value = trimAll(document.zmanim.elevation.value);
	if(document.zmanim.elevation.value == ""){
		document.zmanim.elevation.value = 0;
	}
	if(! validateNumeric(document.zmanim.elevation.value)){
		alert("The elevation does not have a valid numeric value");
		return false;
	}
	if(document.zmanim.timeZone.selectedIndex == 0){
		alert("Please select a timezone");
		return false;
	}
	return true;
}

function alertOnLocationChange(){
	alert("The values in the other fields are not affected by changing the location name. Please manually change the latitude, longitude and timezone, or use the Google Map look-up to help locate the proper latitude and longitude");
	//var agree=confirm("The values in the other fields are only valid for one location. Changing the location name will clear all other fields. Do you want to do this?");
	//if (agree){
	//	reset();
	//}
}


function  validateNumeric( strValue ) {
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

  return objRegExp.test(strValue);
}

function trimAll( strValue ) {
	var objRegExp = /^(\s*)$/;

	//check for all spaces
	if(objRegExp.test(strValue)) {
		strValue = strValue.replace(objRegExp, '');
		if( strValue.length == 0){
			return strValue;
		}
	}

	//check for leading & trailing spaces
	objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	if(objRegExp.test(strValue)) {
		//remove leading and trailing whitespace characters
		strValue = strValue.replace(objRegExp, '$2');
	}
	return strValue;
}
