Click here to Skip to main content
15,885,278 members
Articles / Web Development / ASP.NET

Latitude/longitude input control: an ASP.NET 2.0 server control

Rate me:
Please Sign up or sign in to vote.
4.07/5 (7 votes)
6 Mar 2006CPOL2 min read 53.8K   748   26  
Latitude longitude input control - as ASP.NET 2.0 server control.
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 oTable = document.all[Name+"_table"];
		var MaxDegreeLength = oTable.maxdegreelength;
		
		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 = oTable.inputformat
			oTable.inputformat="7";
			Sync(Obj);
			oTable.inputformat = save;
		}

		if(MaxDegreeLength == "3" && FullValue > 180.00)
		{
			window.alert("Invalid degree value, reseting to zero");
			document.all[Name].value = FullValue = 0.00;
			var save = oTable.inputformat
			oTable.inputformat="7";
			Sync(Obj);
			oTable.inputformat = 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 = oTable.inputformat;
		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 oTable = document.all[Name+"_table"];
		var Save = oTable.inputformat;
		oTable.inputformat = "9"
		Sync(Obj);
		oTable.inputformat = 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);

		var oTable = document.all[Name+"_table"];
		oTable.inputformat = index.toString();

		oSpan = SpecificSpanObj(Obj, index);
		oSpan.style.display="inline";
	}
	function ClickUp(Obj)
	{
		var Name = ObjId(Obj);
		var oTable = document.all[Name+"_table"];
		var inputformat = parseInt(oTable.inputformat);
		oTable.inputformat = "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 oTable = document.all[Name+"_table"];
		var inputformat = parseInt(oTable.inputformat);
		oTable.inputformat = "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();

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions