//***********************************************************************
//This GIS Co-ordinate convertor was developed by:
//Andrew de Klerk
//ŠIntermap (PTY) Ltd
//www.intermap.co.za
//Version 2.0
//***********************************************************************

//#############################################
//#########     CONVERT DMS TO DD  ##############
//#############################################
function dmdtodd()
{
var deg,min,sec,result,dec;
dec = Math.pow(10,5)
	if (document.getElementById("txtd").value ==""){alert("Please enter a value in the degree textbox");result=0;return result;}
	if (document.getElementById("txtm").value ==""){document.getElementById("txtm").value="0";}
	if (document.getElementById("txts").value ==""){document.getElementById("txts").value="0";}

deg=parseInt(document.getElementById("txtd").value); 
min=parseInt(document.getElementById("txtm").value); 
sec=parseInt(document.getElementById("txts").value);

result = deg + ((sec / 60) + min) / 60;
result=result*dec;
result = Math.round(result);
 result = result / dec;
 
document.getElementById("txtd").value="";  
document.getElementById("txtm").value="";  
document.getElementById("txts").value="";
return result;
}

//#############################################
//######### VALIDATE VALUES ON BLUR ##############
//#############################################

//Check Degrees function for <100 and > 0 and Valid integer
function checkdegrees(Val){
var test = isNaN(Val.value)
if (test){
	alert("Please enter a valid number");
	Val.value="";
	Val.focus();
	}
else
	{
		if ((parseInt(Val.value) / Val.value) == 1 || Val.value ==0)
		{
			if (parseInt(Val.value) > 99)
			{
				alert("Please enter a number < 100 for degrees");
				Val.value = "";
				Val.focus();
			}
			else
			{
				if (parseInt(Val.value) < 0)
				{
					Val.value = parseInt(Val.value) * -1
				}
				else
				{}
			}		
		}
		else
		{
			alert("Please enter a valid integer (no decimal points)");
			Val.value = "";
			Val.focus();
		}
    }
}

//Check Minutes/seconds function for <60 and > 0 and Valid integer
function checkminsec(Val){
var test = isNaN(Val.value)
if (test){
	alert("Please enter a valid number");
	Val.value="";
	Val.focus();
	}
else
	{
		if ((parseInt(Val.value) / Val.value) == 1 || Val.value ==0)
		{
			if (parseInt(Val.value) > 59)
			{
				alert("Please enter a number < 60");
				Val.value = "";
				Val.focus();
			}
			else
			{
				if (parseInt(Val.value) < 0)
				{
					Val.value = parseInt(Val.value) * -1
				}
				else
				{}
			}		
		}
		else
		{
			alert("Please enter a valid integer (no decimal points)");
			Val.value = "";
			Val.focus();
		}
    }
}
//Check degrees decimal function for <100 and > 0 and Valid integer
function checkdd(Val){
var test = isNaN(Val.value)
if (test){
	alert("Please enter a valid number");
	Val.value="";
	Val.focus();
	}
else
	{
			if (parseFloat(Val.value) > 100)
			{
				alert("Please enter a number < 100");
				Val.value = "";
				Val.focus();
			}
			else
			{
				if (parseFloat(Val.value) < 0)
				{
					Val.value = parseFloat(Val.value) * -1
				}
				else
				{}
			}		
    }
}

//#############################################
//######## GREY OUT INACTIVE TEXTBOX #############
//#############################################

function dmsclick()
{
document.getElementById("txtdd").disabled=true;
document.getElementById("txtdd").style.backgroundColor="#f5f5f5";

document.getElementById("txtd").disabled=false;
document.getElementById("txtd").style.backgroundColor="white";
document.getElementById("txtm").disabled=false;
document.getElementById("txtm").style.backgroundColor="white";
document.getElementById("txts").disabled=false;
document.getElementById("txts").style.backgroundColor="white";

}

function ddclick()
{
document.getElementById("txtd").disabled=true;
document.getElementById("txtd").value="";
document.getElementById("txtd").style.backgroundColor="#f5f5f5";
document.getElementById("txtm").disabled=true;
document.getElementById("txtm").value="";
document.getElementById("txtm").style.backgroundColor="#f5f5f5";
document.getElementById("txts").disabled=true;
document.getElementById("txts").value="";
document.getElementById("txts").style.backgroundColor="#f5f5f5";

document.getElementById("txtdd").disabled=false;
document.getElementById("txtdd").style.backgroundColor="white";
}

//#############################################
//########               GET STYLES               #############
//#############################################
// Get style block needed to display the converter6 correctly
function coordconv_getStyles() {
	var result = "";
	result+="<STYLE>\n";
	result+=".convtable {border-right: #019C4E 0.1mm solid;border-top: #019C4E 0.1mm solid;	background-image: none;\n"
	result+="border-left: #019C4E 0.1mm solid;border-bottom: #019C4E 0.1mm solid; font-size: 2; font-weight: normal; font-family: Verdana, Tahoma, Helvetica, sans-serif;background-color: #F5FAF8;}\n"
	result+=".convinput {border-right: #dcdcdc 0.1mm solid;	border-top: #dcdcdc 0.1mm solid;	border-left: #dcdcdc 0.1mm solid;\n"
	result+="border-bottom: #dcdcdc 0.1mm solid;font-family: Verdana, Tahoma, Helvetica, sans-serif;font-size: xx-small;}\n"
	result+=".convcell {font-family: Verdana, Tahoma, Helvetica, sans-serif;font-size: xx-small;}\n"
	result+=".header {font-family: Verdana, Tahoma, Helvetica, sans-serif; color: #ffffff; font-size: 11px; font-weight: bold;}\n"
	result+=".convbutton{font-weight: normal;	font-size: xx-small;\n"
	result+="width: 100px; font-family: Verdana, Tahoma, Helvetica, sans-serif;height: 18px;\n"
	result+="background-color: #ffffff; font-variant: none; color: #9B9A9A; border-right: #DCDCDC 0.1mm solid; border-left: #DCDCDC 0.1mm solid; border-top: #DCDCDC 0.1mm solid; border-bottom: #DCDCDC 0.1mm solid;}\n"
	result+="</style>"
	return result;
	}
//#############################################
//########       GENERATE DROPDOWN       #############
//#############################################

function coordconverter(thediv,outputfield,upperbound, lowerbound)
{
var result ='';
		result+='<TABLE class="convtable" width="200">\n';
		result+='	<tr>\n';
		result+='		<th class="header" bgcolor="#019C4E" align="center" colspan="3" height="20px">\n';
		result+='			Coordinate Converter</th>\n';
		result+='	</tr>\n';
		result+='	<tr>\n';
		result+='		<td class="convcell"><INPUT id="rdodms" type="radio" value="0" name="grp1" onclick="dmsclick();"></td>\n';
		result+='		<td class="convcell"><INPUT class="convinput" id="txtd" type="text" size="2" name="txtd" onblur="checkdegrees(this);">&deg;\n';
		result+='			<INPUT class="convinput" id="txtm" type="text" size="2" name="txtm" onblur="checkminsec(this);">&#39; \n';
		result+='			<INPUT class="convinput" id="txts" type="text" size="2" name="txts" onblur="checkminsec(this);">&#39;&#39;</td>\n';
		result+='		<td class="convcell">dd&deg;mm&#39;ss&#39;&#39; </td>\n';
		result+='	</tr>\n';
		result+='	<tr>\n';
		result+='		<td class="convcell"><INPUT id="rdodd" type="radio" value="1" name="grp1" onclick="ddclick();"></td>\n';
		result+='		<td class="convcell"><INPUT class="convinput" id="txtdd" type="text" size="15" name="txtdd" onblur="checkdd(this);"></td>\n';
		result+='		<td class="convcell">d.ddddd \n';
		result+='		</td>\n';
		result+='	</tr>\n';
		result+='	<tr>\n';
		result+='		<td class="convcell" colspan="3" align="left" height="24px">\n';
		result+='			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<INPUT type="button" class="convbutton" value="OK" ID="cmdsubmit" NAME="cmdsubmit" style="WIDTH: 44px; HEIGHT: 18px"\n';
		result+='				onclick="populatedd(' + '\'' + thediv + '\'' + ','+ '\'' + outputfield+ '\''+  ',' + upperbound + ',' + lowerbound +');"> <INPUT type="button" class="convbutton" value="Cancel" ID="cmdcancel" NAME="cmdcancel" style="WIDTH: 44px; HEIGHT: 18px"\n';
		result+='				onclick="hideddconverter('+ '\'' + thediv + '\'' + ');">\n';
		result+='		</td>\n';
		result+='	</tr>\n';
		result+='</TABLE>';

	document.getElementById(thediv).innerHTML = result;
	document.getElementById(thediv).style.display = 'block';
	
	dmsclick();
	document.getElementById("rdodms").checked=true;
	document.getElementById("txtd").focus();

}

//#############################################
//########        CANCEL DROPDOWN	       #############
//#############################################
function hideddconverter(thediv)
{
document.getElementById(thediv).style.display = "none";
document.getElementById(thediv).innerHTML="";
}

//#############################################
//########                 OK BUTTON	           #############
//#############################################

function populatedd(thediv,outputfield,upperbound,lowerbound)
{
    if (document.getElementById('rdodms').checked)
    {
      var result = dmdtodd()
    }
    else
    {
        if (document.getElementById('txtdd').value =="")
        {
            alert("Please enter a value in the textbox");
            return;
        }
        else
        {
           result=document.getElementById('txtdd').value
        }
    }
    
    if (result > upperbound || result < lowerbound)
    {
     alert("Please enter a coordinate > " + lowerbound + " and < " + upperbound + "");
     return;
    }
    document.getElementById(outputfield).value=result;
    document.getElementById(thediv).style.display = 'none';
    document.getElementById(thediv).innerHTML="";
}