window.onbeforeunload = function(){}

function hideLayer3(){
	document.getElementById('object3').style.display='none';
	document.getElementById("object3").innerHTML="";
	}

var m_convert=0;
function s_convert(){
	hideLayer3();
	document.getElementById('object3').style.display='block';
	if(m_convert==0){$('#object3').load('location_convert.php');m_convert=1;}
	else{hideLayer3();m_convert=0;}
	}

var roundto = 6;
var roundtoDMM = 3;
var roundtoDMS = 2;
function checkRanges(latd, latm, lond, lonm, lats, lons) {
	errormsg = "";
	if (latd <= -90 || latd >= 90) {
		errormsg += "* Latitude Degrees must be between 0 and 89.9999\n";
	}
	if (lond <= -180 || lond >= 180) {
		errormsg += "* Longitude Degrees must be between 0 and 179.9999\n";
	}
	if (latm < 0 || latm >= 60 || lonm < 0 || lonm >= 60) {
		errormsg += "* Minutes must be between 0 and 59.9999\n";
	}
	if (errormsg != "") {
		alert ("There are problems with your entered data:\n\n" + errormsg);
		// alert ("[function checkRanges] latd: " + latd + ", latm: " + latm + ", lond: " + lond + ", lonm: " + lonm);
		return (false);
	} else {
		return (true);
	}
}
function myParseFloat(number) {
	if (number == "-0") {
		return number;
	} else {
		return parseFloat(number);
	}
}
function myParseInt(number) {
	var newInt = parseInt(number);
	if (newInt == 0 && number < 0) {
		newInt = '-0';
	}
	return newInt;
}
function convertFromDMM(theform) {
	if (theform.dmm_lat_d.value == "") { theform.dmm_lat_d.value = 0; }
	if (theform.dmm_lat_m.value == "") { theform.dmm_lat_m.value = 0; }
	if (theform.dmm_lon_d.value == "") { theform.dmm_lon_d.value = 0; }
	if (theform.dmm_lon_m.value == "") { theform.dmm_lon_m.value = 0; }
	latd = myParseFloat(theform.dmm_lat_d.value);
	latm = myParseFloat(theform.dmm_lat_m.value);
	lond = myParseFloat(theform.dmm_lon_d.value);
	lonm = myParseFloat(theform.dmm_lon_m.value);
	if (checkRanges(latd, latm, lond, lonm)) {
		convertToDMS(theform, latd, latm, lond, lonm);
		convertToDDD(theform, latd, latm, lond, lonm);
	}
}
function convertFromDMS(theform) {
	if (theform.dms_lat_d.value == "") { theform.dms_lat_d.value = 0; }
	if (theform.dms_lat_m.value == "") { theform.dms_lat_m.value = 0; }
	if (theform.dms_lat_s.value == "") { theform.dms_lat_s.value = 0; }
	if (theform.dms_lon_d.value == "") { theform.dms_lon_d.value = 0; }
	if (theform.dms_lon_m.value == "") { theform.dms_lon_m.value = 0; }
	if (theform.dms_lon_s.value == "") { theform.dms_lon_s.value = 0; }
	latd = myParseFloat(theform.dms_lat_d.value);
	latm = myParseFloat(theform.dms_lat_m.value) + myParseFloat(theform.dms_lat_s.value)/60;
	lond = myParseFloat(theform.dms_lon_d.value);
	lonm = myParseFloat(theform.dms_lon_m.value) + myParseFloat(theform.dms_lon_s.value)/60;
	if (checkRanges(latd, latm, lond, lonm)) {
		convertToDMM(theform, latd, latm, lond, lonm);
		convertToDDD(theform, latd, latm, lond, lonm);
	}
}
function convertFromDDD(theform) {
	if (theform.ddd_lat_d.value == "") { theform.ddd_lat_d.value = 0; }
	if (theform.ddd_lon_d.value == "") { theform.ddd_lon_d.value = 0; }
	latd = myParseInt(myParseFloat(theform.ddd_lat_d.value));
	latm = Math.abs((myParseFloat(theform.ddd_lat_d.value) - parseInt(myParseFloat(theform.ddd_lat_d.value))) * 60);
	lond = myParseInt(myParseFloat(theform.ddd_lon_d.value));
	lonm = Math.abs((myParseFloat(theform.ddd_lon_d.value) - parseInt(myParseFloat(theform.ddd_lon_d.value))) * 60);
	if (checkRanges(latd, latm, lond, lonm)) {
		convertToDMM(theform, latd, latm, lond, lonm);
		convertToDMS(theform, latd, latm, lond, lonm);
	}
}
function convertToDMM(theform, latd, latm, lond, lonm) {
	// alert ("[function convertToDMM] latd: " + latd + ", latm: " + latm + ", lond: " + lond + ", lonm: " + lonm);
	document.getElementById('dmm_lat_d').value = latd;
	document.getElementById('dmm_lat_m').value = roundoff(latm, roundtoDMM);
	document.getElementById('dmm_lon_d').value = lond;
	document.getElementById('dmm_lon_m').value = roundoff(lonm, roundtoDMM);
}
function convertToDMS(theform, latd, latm, lond, lonm) {
	// alert ("[function convertToDMS] latd: " + latd + ", latm: " + latm + ", lond: " + lond + ", lonm: " + lonm);
	document.getElementById('dms_lat_d').value = latd;
	document.getElementById('dms_lat_m').value = parseInt(latm);
	document.getElementById('dms_lat_s').value = roundoff((latm - parseInt(latm)) * 60, roundtoDMS);
	document.getElementById('dms_lon_d').value = lond;
	document.getElementById('dms_lon_m').value = parseInt(lonm);
	document.getElementById('dms_lon_s').value = roundoff((lonm - parseInt(lonm)) * 60, roundtoDMS);
}
function convertToDDD(theform, latd, latm, lond, lonm) {
	if (latd < 0) {
		latm = latm * -1;
	}
	if (lond < 0) {
		lonm = lonm * -1;
	}
	document.getElementById('H_LAT').value = roundoff(latd + (latm / 60), roundto);
	document.getElementById('H_LON').value = roundoff(lond + (lonm / 60), roundto);
}
function roundoff(num,factor){
  num = myParseFloat(num);
  factor = myParseFloat(factor);
  num = Math.round(num * Math.pow(10,factor)) / Math.pow(10,factor);
  return num;
}
function initround() {
	document.latlong.ddround.value = roundto;
	document.latlong.mmround.value = roundtoDMM;
	document.latlong.ssround.value = roundtoDMS;

	updatezerofloat();
	updatefieldsizes();
}
function updatezerofloat() {
	setzerofloat(document.latlong.ddd_lat_d2,roundto);
	setzerofloat(document.latlong.ddd_lon_d3,roundto);

	setzerofloat(document.latlong.dmm_lat_m6,roundtoDMM);
	setzerofloat(document.latlong.dmm_lon_m5,roundtoDMM);

	setzerofloat(document.latlong.dms_lat_s5,roundtoDMS);
	setzerofloat(document.latlong.dms_lon_s4,roundtoDMS);
}
function setzerofloat(fieldobj,places) {
	if (! myParseFloat(fieldobj.value)) {
		fieldobj.value = '0.' + xchar('0',places);
	}
}
function updateround() {
	roundto = myParseFloat(document.latlong.ddround.value);
	roundtoDMM = myParseFloat(document.latlong.mmround.value);
	roundtoDMS = myParseFloat(document.latlong.ssround.value);

	updatezerofloat();
	updatefieldsizes();
}
function updatefieldsizes() {
	document.latlong.ddd_lat_d2.size = roundto + 2;
	document.latlong.ddd_lon_d3.size = roundto + 2;

	document.latlong.dmm_lat_m6.size = roundtoDMM + 2;
	document.latlong.dmm_lon_m5.size = roundtoDMM + 2;

	document.latlong.dms_lat_s5.size = roundtoDMS + 2;
	document.latlong.dms_lon_s4.size = roundtoDMS + 2;
}
function xchar(repeatString, repeatNum) {
    var newString = ""; 
    for (var x=1; x<=repeatNum; x++) { 
        newString = newString + repeatString; 
    } 
    return (newString); 
}



var menu_map=0;
var menu_coord=0;

function loadPAN(){
	tog_close();
	document.getElementById('errorDIV').innerHTML='';
	document.getElementById('errorDIV2').innerHTML='';
	document.getElementById('saveDIV').innerHTML='<img border=0 src="../images/hazjax-loader-000.gif">';
	//document.getElementById('ALPHAdiv').innerHTML='<center><font style="color:red;">Loading...</font></center>';
	}
function tog_close(){
	menu_coord=0;
	menu_map=0;}
function tog_map(){
	if(menu_map==0){
	menu_coord=0;document.getElementById('coordDIV').style.height='0px';//Close other
	menu_map=1;document.getElementById('xmapDIV').style.height='440px';}
	else{menu_map=0;document.getElementById('xmapDIV').style.height='0px';}
}
function tog_coord(){
	if(menu_coord==0){
	//menu_map=0;document.getElementById('xmapDIV').style.height='0px';//Close other
	menu_coord=1;document.getElementById('coordDIV').style.height='100px';}
	else{menu_coord=0;document.getElementById('coordDIV').style.height='0px';}
}

function distance_check(){if(!IsNumeric(document.postform.H_DISTANCE.value)){alert('Invalid Character in Distance'); return false;}}
function gain_check(){if(!IsNumeric(document.postform.H_GAIN.value)){alert('Invalid Character in Elevation Gain'); return false;}}
function accum_check(){if(!IsNumeric(document.postform.H_ACCUM_ELV.value)){alert('Invalid Character in Accumulated Elevation Gain'); return false;}}
function thelev_check(){if(!IsNumeric(document.postform.H_TH_ELEV.value)){alert('Invalid Character in Trailhead Elevation'); return false;}}
function state_change(STi){$('#allDIV').load('element-dropdown.php?IDe=99&STi='+STi);}

function DMm(pinLAT_DD,pinLAT_MS,pinLON_DD,pinLON_MS){
	pinLAT_MS=parseFloat(pinLAT_MS/60);
	document.getElementById('H_LAT').value=parseFloat(pinLAT_DD)+pinLAT_MS;

	if(pinLON_DD < -1){pinLON_DD=pinLON_DD.substring(1,pinLON_DD.length);}
	pinLON_MS=parseFloat(pinLON_MS/60);
	document.getElementById('H_LON').value=parseFloat(pinLON_DD)+pinLON_MS;
	}

function DMS(pinLAT_DD,pinLAT_MM,pinLAT_SS,pinLON_DD,pinLON_MM,pinLON_SS){
	pinLAT_MM=parseFloat(pinLAT_MM/60);
	pinLAT_SS=parseFloat(pinLAT_SS/3600);
	document.getElementById('H_LAT').value=parseFloat(pinLAT_DD)+(pinLAT_MM)+(pinLAT_SS);

	if(pinLON_DD < -1){pinLON_DD=pinLON_DD.substring(1,pinLON_DD.length);}
	pinLON_MM=parseFloat(pinLON_MM/60);
	pinLON_SS=parseFloat(pinLON_SS/3600);
	document.getElementById('H_LON').value=parseFloat(pinLON_DD)+(pinLON_MM)+(pinLON_SS);
	}
