﻿function LatitudeLongitudeInputClass()
{
	function Make2Digits(val1)
	{
		val=val1.toString();
		switch(val.length)
		{
		case 0:
			return("00");
		case 1:
			return("0"+val);
		default:
			return(val.slice(0,2));
		}	
	}
	
	function Make3Digits(val)
	{
		switch(val.length)
		{
		case 0:
			return("000");
		case 1:
			return("00"+val);
		case 2:
			return("0"+val);
		default:
			return(val.slice(0,3));
		}	
	}
	function Make5Decimals(s)
	{
		var ss = s.toString();
		var Dot = ss.indexOf(".");
		if(Dot == -1)
			return(s+".000");
		ss += "00000";
		return(ss.slice(0, Dot+6));
	}
	
	// make sure that there are 2 digits before the decimal place and 3 after
	function Make3Decimals(s)
	{
		// window.alert("make 3: "+s);
		var ss = s.toString();
		var Dot = ss.indexOf(".");
		
		if(Dot == -1)
		{
			return(ss+".000");
		}
		ss += "00000";
		// window.alert(ss);
		return(ss.slice(0, Dot+4));
	}
	function ObjId(Obj)
	{
		return(Obj.id);
	}
	
	function Sync(Obj)
	{
		var Name = ObjId(Obj);
		if(document.all[Name].value == "")
			document.all[Name].value = 0.00;
		var FullValue = parseFloat(document.all[Name].value);
		o1_select_obj = document.all[Name+"_Select1"];
		o1_degrees_obj = document.all[Name+"_Degrees1"];
		o1_minutes_obj = document.all[Name+"_Minutes1"];
		o2_select_obj = document.all[Name+"_Select2"];
		o2_degrees_obj = document.all[Name+"_Degrees2"];
		o2_minutes_obj = document.all[Name+"_Minutes2"];
		o2_seconds_obj = document.all[Name+"_Seconds2"];
//		o1_select_obj.selectedIndex = 0;
//		o2_select_obj.selectedIndex = 0;
		var inputformat = document.all[Name+"_inputformat"].value;
		var MaxDegreeLength = document.all[Name+"_maxdegreelength"].value;
		
		if(FullValue < 0.00)
		{
			o1_select_obj.selectedIndex = 1;
			o2_select_obj.selectedIndex = 1;
			FullValue = -FullValue;
		}
		// if value is 0 and this is longitude, make it W
		if(FullValue == 0.00)
		{
			if(MaxDegreeLength == "3")
			{
	//			o1_select_obj.selectedIndex = 1;
	//			o2_select_obj.selectedIndex = 1;
			}
		}

		if(MaxDegreeLength == "2" && FullValue > 90.00)
		{
			window.alert("Invalid degree value, reseting to zero");
			document.all[Name].value = FullValue = 0.00;
			var save = inputformat;
			document.all[Name+"_inputformat"].value="7";
			Sync(Obj);
			document.all[Name+"_inputformat"].value = save;
		}

		if(MaxDegreeLength == "3" && FullValue > 180.00)
		{
			window.alert("Invalid degree value, reseting to zero");
			document.all[Name].value = FullValue = 0.00;
			var save = document.all[Name+"_inputformat"].value;
			document.all[Name+"_inputformat"].value="7";
			Sync(Obj);
			document.all[Name+"_inputformat"].value = save;
		}
		var tFullValue = FullValue.toString();
		var Minutes = "0"
		var Dot = tFullValue.indexOf(".");
		if(Dot < 0)
		{
			var Degrees = tFullValue;
			var Minutes = "0";
		}
		else
		{
			var Degrees = tFullValue.slice(0,Dot);
			var Minutes = tFullValue.slice(Dot);
		}

		// we don't want to undate the one we're working on because of roundoff errors
		var fMinutes = parseFloat(Minutes)*60;
		var inputformat = document.all[Name+"_inputformat"].value;
		if(inputformat != "1")
		{
			o1_degrees_obj.value = Degrees;
			// 42847
			o1_minutes_obj.value = Make3Decimals(fMinutes);
		}
		// 23.456
		// we don't want to undate the one we're working on because of roundoff errors
		if(inputformat != "2")
		{
			o2_degrees_obj.value = Degrees;
			var iMinutes = parseInt(fMinutes, 10);
			o2_minutes_obj.value = Make2Digits(iMinutes);
			var SecondsPart = fMinutes-iMinutes;
			o2_seconds_obj.value = Make2Digits(Math.round(SecondsPart * 60));
		}
	}
	this.Sync = Sync;
	
	function Init(Obj)
	{
		// sync up the various views of the data
		var Name = ObjId(Obj);
		var Save = parseInt(document.all[Name+"_inputformat"].value);
		document.all[Name+"_inputformat"].value = "9";
		Sync(Obj);
		document.all[Name+"_inputformat"].value = Save;
	}
	this.Init = Init;
	
	function UpdateDegreesFromDegreesMinutes(Obj)
	{
		var Name = ObjId(Obj);
		o1_select_obj = document.all[Name+"_Select1"];
		o1_degrees_obj = document.all[Name+"_Degrees1"];
		o1_minutes_obj = document.all[Name+"_Minutes1"];
		var SelValue = o1_select_obj.options[o1_select_obj.selectedIndex].value;
		var DegValue = o1_degrees_obj.value;
		if(DegValue == "")
			DegValue = "0";
		var MinValue = o1_minutes_obj.value;
		if(MinValue == "")
			MinValue = "0";
		var FullDegValue = (parseInt(DegValue, 10)+parseFloat(MinValue/60))*parseInt(SelValue);
		document.all[Name].value = Make5Decimals(FullDegValue);
		Sync(Obj);
	}
	this.UpdateDegreesFromDegreesMinutes = UpdateDegreesFromDegreesMinutes;
	
	function UpdateDegreesFromDegreesMinutesSeconds(Obj)
	{
		var Name = ObjId(Obj);
		o2_select_obj = document.all[Name+"_Select2"];
		o2_degrees_obj = document.all[Name+"_Degrees2"];
		o2_minutes_obj = document.all[Name+"_Minutes2"];
		o2_seconds_obj = document.all[Name+"_Seconds2"];
		var SelValue = o2_select_obj.options[o2_select_obj.selectedIndex].value;
		var DegValue = o2_degrees_obj.value;
		if(DegValue == "")
			DegValue = "0";
		var MinValue = o2_minutes_obj.value;
		if(MinValue == "")
			MinValue = "0";
		var SecValue = o2_seconds_obj.value;
		if(SecValue == "")
			SecValue = "0";
		var FullDegValue = (parseInt(DegValue, 10)+parseFloat(MinValue/60)+parseFloat(SecValue/3600))*parseInt(SelValue);
		document.all[Name].value = Make5Decimals(FullDegValue);
		Sync(Obj);
	}
	this.UpdateDegreesFromDegreesMinutesSeconds = UpdateDegreesFromDegreesMinutesSeconds;
	
	function SpecificSpanObj(Obj, index)
	{
		var Name = ObjId(Obj);
		var oSpan = document.all[Name+"_"+index.toString()];
		return(oSpan);
	}
	
	function HideAll(Obj)
	{
		for(var i=0; i<=2; i++)
		{
			var oSpan = SpecificSpanObj(Obj, i);
			oSpan.style.display="none";
		}
	}
	function ShowThisOne(Obj, index)
	{
		HideAll(Obj);
		var Name = ObjId(Obj);

		document.all[Name+"_inputformat"].value = index.toString();

		oSpan = SpecificSpanObj(Obj, index);
		oSpan.style.display="inline";
	}
	function ClickUp(Obj)
	{
		var Name = ObjId(Obj);
		var inputformat = parseInt(document.all[Name+"_inputformat"].value);
		document.all[Name+"_inputformat"].value = "7";	// want ot make sure that all objects are sync'ed
		Sync(Obj);
		inputformat--;
		if(inputformat < 0)
			inputformat = 2;
		ShowThisOne(Obj, inputformat); 
	}
	this.ClickUp = ClickUp;
	function ClickDown(Obj)
	{
		var Name = ObjId(Obj);
		var inputformat = parseInt(document.all[Name+"_inputformat"].value);
		document.all[Name+"_inputformat"].value = "7";	// want ot make sure that all objects are sync'ed
		Sync(Obj);
		inputformat++;
		if(inputformat > 2)
			inputformat = 0;
		ShowThisOne(Obj, inputformat); 
	}
	this.ClickDown = ClickDown;
}
var CLatitudeLongitudeInput = new LatitudeLongitudeInputClass();
