
function convertSP2LatLong(uX,uY) {
	a = 20925604.48;   				//major radius of ellipsoid, map units (NAD 83)
	ec = 0.08181905782; 			//eccentricity of ellipsoid (NAD 83)
	angRad = 0.01745329252;  		//number of radians in a degree
	pi4 = Math.PI / 4;  			//Pi / 4
	p0 = 45.33333333333 * angRad; 	 //latitude of origin
	p1 = 45.83333333333 * angRad;  	//latitude of first standard parallel
	p2 = 47.33333333333 * angRad;  	//latitude of second standard parallel
	m0 = -120.5 * angRad;  			//central meridian
	x0 = 1640416.66666667; 			//False easting of central meridian, map units

	// Calculate the coordinate system constants.
	with (Math) {
		m1 = cos(p1) / sqrt(1 - (pow(ec,2)) * pow(sin(p1),2));  
		m2 = cos(p2) / sqrt(1 - (pow(ec,2)) * pow(sin(p2),2));
		t0 = tan(pi4 - (p0 / 2));
		t1 = tan(pi4 - (p1 / 2));
		t2 = tan(pi4 - (p2 / 2));
		t0 = t0 / pow(((1 - (ec * (sin(p0)))) / (1 + (ec * (sin(p0))))),ec/2);  
		t1 = t1 / pow(((1 - (ec * (sin(p1)))) / (1 + (ec * (sin(p1))))),ec/2);
		t2 = t2 / pow(((1 - (ec * (sin(p2)))) / (1 + (ec * (sin(p2))))),ec/2);
		n = log(m1 / m2) / log(t1 / t2);
		f = m1 / (n * pow(t1,n)); 
		rho0 = a * f * pow(t0,n);
	
		// Convert the coordinate to Latitude/Longitude.
		// Calculate the Longitude.
		uX = uX - x0;
		pi2 = pi4 * 2;
		rho = sqrt(pow(uX,2) + pow((rho0 - uY),2));  
		theta = atan(uX / (rho0 - uY));
		txy = pow((rho / (a * f)),(1 / n));
		lon = (theta / n) + m0;
		uX = uX + x0;
		
		// Estimate the Latitude
		lat0 = pi2 - (2 * atan(txy));
		
		// Substitute the estimate into the iterative calculation that
		// converges on the correct Latitude value.
		part1 = (1 - (ec * sin(lat0))) / (1 + (ec * sin(lat0)));
		lat1 = pi2 - (2 * atan(txy * pow(part1,(ec/2))));
	
		while ((abs(lat1 - lat0)) > 0.000000002) {
		  lat0 = lat1;
		  part1 = (1 - (ec * sin(lat0))) / (1 + (ec * sin(lat0)));
		  lat1 = pi2 - (2 * atan(txy * pow(part1,(ec/2))));
		}
		
		// Convert from radians to degrees.
		Lat = lat1 / angRad;
		Lon = lon / angRad;
		// Convert from Decimal to DMS
		//Lat = decimalsToDegsMinsSecs(Lat);
		//dmsLon = decimalsToDegsMinsSecs(Lon);
		var coordinates = new Array(Lon,Lat);
		
		
		return coordinates;
		
	}
}

function convert(uX,uY) {
		alert(convertSP(uX,uY));
}

// Code to convert from map units to decimal degrees.
function convertSP(uX,uY) {
	a = 20925604.48;   				//major radius of ellipsoid, map units (NAD 83)
	ec = 0.08181905782; 			//eccentricity of ellipsoid (NAD 83)
	angRad = 0.01745329252;  		//number of radians in a degree
	pi4 = Math.PI / 4;  			//Pi / 4
	p0 = 45.33333333333 * angRad; 	 //latitude of origin
	p1 = 45.83333333333 * angRad;  	//latitude of first standard parallel
	p2 = 47.33333333333 * angRad;  	//latitude of second standard parallel
	m0 = -120.5 * angRad;  			//central meridian
	x0 = 1640416.66666667; 			//False easting of central meridian, map units

	// Calculate the coordinate system constants.
	with (Math) {
		m1 = cos(p1) / sqrt(1 - (pow(ec,2)) * pow(sin(p1),2));  
		m2 = cos(p2) / sqrt(1 - (pow(ec,2)) * pow(sin(p2),2));
		t0 = tan(pi4 - (p0 / 2));
		t1 = tan(pi4 - (p1 / 2));
		t2 = tan(pi4 - (p2 / 2));
		t0 = t0 / pow(((1 - (ec * (sin(p0)))) / (1 + (ec * (sin(p0))))),ec/2);  
		t1 = t1 / pow(((1 - (ec * (sin(p1)))) / (1 + (ec * (sin(p1))))),ec/2);
		t2 = t2 / pow(((1 - (ec * (sin(p2)))) / (1 + (ec * (sin(p2))))),ec/2);
		n = log(m1 / m2) / log(t1 / t2);
		f = m1 / (n * pow(t1,n)); 
		rho0 = a * f * pow(t0,n);
	
		// Convert the coordinate to Latitude/Longitude.
		// Calculate the Longitude.

		uX = uX - x0;
		pi2 = pi4 * 2;
		rho = sqrt(pow(uX,2) + pow((rho0 - uY),2));  
		theta = atan(uX / (rho0 - uY));
		txy = pow((rho / (a * f)),(1 / n));
		lon = (theta / n) + m0;
		uX = uX + x0;
		
		// Estimate the Latitude
		lat0 = pi2 - (2 * atan(txy));
		
		// Substitute the estimate into the iterative calculation that
		// converges on the correct Latitude value.
		part1 = (1 - (ec * sin(lat0))) / (1 + (ec * sin(lat0)));
		lat1 = pi2 - (2 * atan(txy * pow(part1,(ec/2))));
	
		while ((abs(lat1 - lat0)) > 0.000000002) {
		  lat0 = lat1;
		  part1 = (1 - (ec * sin(lat0))) / (1 + (ec * sin(lat0)));
		  lat1 = pi2 - (2 * atan(txy * pow(part1,(ec/2))));
		}
		
		// Convert from radians to degrees.
		Lat = lat1 / angRad;
		Lon = lon / angRad;
		// Convert from Decimal to DMS
		Lat = decimalsToDegsMinsSecs(Lat);
		dmsLon = decimalsToDegsMinsSecs(Lon);
		
		return dmsLon + " W  |  " + Lat + " N";
		
	}
}
	
function trunc(val){
	return Math.floor(Math.abs(val));
}

// Convert from Decimal to DMS
function decimalsToDegsMinsSecs(degs) {
	g_Degrees = trunc(degs)
	g_Minutes = trunc((Math.abs(degs) - g_Degrees) * 60);
	g_Seconds = (Math.abs(degs) * 3600) - (g_Minutes * 60) - (g_Degrees * 3600);
	dms = g_Degrees + String.fromCharCode(176) + " " + g_Minutes + "' " + Math.round(g_Seconds) + "\"";
	return dms;
 }
//End of Code for converting map units to DMS.