// aimsCustom.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*		aimsClick.js, aimsNavigation.js,
*/

// global variables
	aimsCustomPresent=true;
	// change these to send XML response to custom function.
	// use numbers >= 1000 and match in useCustomFunction()
	// defaults are defined in aimsXML.js and use standard functions
	
	// xml response mode for selection
	selectXMLMode = 6;
	// xml response mode for identify
	identifyXMLMode = 7;
	// xml response mode for query
	queryXMLMode = 8;
	// xml response mode for find
	findXMLMode = 14;
	// xml response mode hyperlink
	hyperlinkXMLMode = 15;




// custom function for handling clicks 
// 		flow redirected here when
//		toolMode set to >=1000
function customMapTool(e) {
				if (toolMode == 1001) {
						// insert code here
						return false;
					
				}
				if (toolMode == 1002) {
						// insert code here

				}

}

// send  XML response to custom function
//		flow  redirected here when
//		XMLMode >=1000
function useCustomFunction(theReply) {
	if (XMLMode==1001) {
		// insert code here
	} else if (XMLMode==1002) {
		// insert code here
	} else {
		alert(msgList[55] + XMLMode + msgList[56]);
	}
	hideLayer("LoadData");
}

// add custom stuff to Map XML request. . . between selection and geocode
function addCustomToMap1(){
	var customString = "";
	/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coord="5 ' + (iHeight-10) + '" label="This is a test">\n';
		customString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		customString += 'threed="TRUE" glowing="' + modeMapGlow + '" />\n</TEXT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
	*/
	return customString;
}

// add custom stuff to Map XML request. . . between clickpoints and copyright
function addCustomToMap2(){
	var customString = "";
	
	return customString;
}

// add custom stuff to Map XML request. . . under modeOnMap
function addCustomToMap3(){
	var customString = "";
		/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coord="5 ' + (iHeight-10) + '" label="This is a test">\n';
		customString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		customString += 'threed="TRUE" glowing="' + modeMapGlow + '" />\n</TEXT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
		alert(customString);
		*/
	return customString;
}

// add custom stuff to Map XML request. . . on top of everything
function addCustomToMap4(){
	var customString = "";
	
	return customString;
}

// extract layers to download
function extractIt() {
	hideLayer("measureBox");
	alert(msgList[51]);
}


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 = 3.141592653582 / 4;  	//Pi / 4
p0 = 39.66666667 * angRad; 	 	//latitude of origin
p1 = 40.43333333 * angRad;  		//latitude of first standard parallel
p2 = 41.70000000 * angRad;  	//latitude of second standard parallel
m0 = -82.50000000 * angRad;  	//central meridian
x0 = 1968500.000000000; 		//False easting of central meridian, map units


//    lat_1=41.700000000 
//    lat_2=40.433333333 
//    lat_0=39.666666667 
//    lon_0=-82.500000000 
//    x_0=600000.000 
//    y_0=0.000 
//    ellps=GRS80 
//    datum=NAD83 
//    units=m 
//    no_defs 



// 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;

//alert(lat);
//alert(lon);

//Round the latitude and longitude
//lat = (CLng(lat * 100000)) / 100000;
//lon = (CLng(lon * 100000)) / 100000;

//Lat = lat.toPrecision(7);
//Lon = lon.toPrecision(8);

return Lon + "|" + Lat;

}
}
//End of Code for converting map units to decimal degrees.

