// 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) {

		xIn = mouseX;
		yIn = mouseY;
		pixelX = xDistance / iWidth;
		mapX = pixelX * mouseX + eLeft;
		mouseY = iHeight - yIn;
		pixelY = yDistance / iHeight;
		mapY = pixelY * mouseY + eBottom;

/************************************************************************************/							
			 		/****DEC addition Javascript coordinate converter...
					  adjusted to work with mapX and mapY variables calculated above*/
				 ///  -07/21/05- GDC
/************************************************************************************/	

    var pi = 3.14159265358979;
    /* Ellipsoid model constants (actual values here are for WGS84) */
    var sm_a = 6378137.0;
    var sm_b = 6356752.314;
    var sm_EccSquared = 6.69437999013e-03;

    var UTMScaleFactor = 0.9996;

		var southhemi = false;		
		var txtZone = 18;
		
    /** DegToRad - Converts degrees to radians. **/
    function DegToRad (deg)
    {
        return (deg / 180.0 * pi)
    }

    /** RadToDeg - Converts radians to degrees.**/
    function RadToDeg (rad)
    {
        return (rad / pi * 180.0)
    }

    /** FootpointLatitude - Computes the footpoint latitude for use in converting Transverse Mercator coordinates to ellipsoidal coordinates.**/
    function FootpointLatitude (y)
    {
        var y_, alpha_, beta_, gamma_, delta_, epsilon_, n;
        var result;
        
        n = (sm_a - sm_b) / (sm_a + sm_b);
        	
        alpha_ = ((sm_a + sm_b) / 2.0)
            * (1 + (Math.pow (n, 2.0) / 4) + (Math.pow (n, 4.0) / 64));

        y_ = y / alpha_;
        
        beta_ = (3.0 * n / 2.0) + (-27.0 * Math.pow (n, 3.0) / 32.0)
            + (269.0 * Math.pow (n, 5.0) / 512.0);
        
        gamma_ = (21.0 * Math.pow (n, 2.0) / 16.0)
            + (-55.0 * Math.pow (n, 4.0) / 32.0);
        	
        delta_ = (151.0 * Math.pow (n, 3.0) / 96.0)
            + (-417.0 * Math.pow (n, 5.0) / 128.0);
        	
        epsilon_ = (1097.0 * Math.pow (n, 4.0) / 512.0);
        	
        result = y_ + (beta_ * Math.sin (2.0 * y_))
            + (gamma_ * Math.sin (4.0 * y_))
            + (delta_ * Math.sin (6.0 * y_))
            + (epsilon_ * Math.sin (8.0 * y_));
        
        return result;
    }

   
    /** MapXYToLatLon - Converts x and y coordinates in the Transverse Mercator projection to a latitude/longitude pair.  
			 Note that Transverse Mercator is not the same as UTM; a scale factor is required to convert between them.**/
    function MapXYToLatLon (x, y, lambda0, philambda)
    {
        var phif, Nf, Nfpow, nuf2, ep2, tf, tf2, tf4, cf;
        var x1frac, x2frac, x3frac, x4frac, x5frac, x6frac, x7frac, x8frac;
        var x2poly, x3poly, x4poly, x5poly, x6poly, x7poly, x8poly;
    
        phif = FootpointLatitude (y);
        	
        ep2 = (Math.pow (sm_a, 2.0) - Math.pow (sm_b, 2.0))
              / Math.pow (sm_b, 2.0);
        	
        cf = Math.cos (phif);
        	
        nuf2 = ep2 * Math.pow (cf, 2.0);
        	
        Nf = Math.pow (sm_a, 2.0) / (sm_b * Math.sqrt (1 + nuf2));
        Nfpow = Nf;
        	
        tf = Math.tan (phif);
        tf2 = tf * tf;
        tf4 = tf2 * tf2;
        
        x1frac = 1.0 / (Nfpow * cf);
        
        Nfpow *= Nf; 
        x2frac = tf / (2.0 * Nfpow);
        
        Nfpow *= Nf;   
        x3frac = 1.0 / (6.0 * Nfpow * cf);
        
        Nfpow *= Nf;   
        x4frac = tf / (24.0 * Nfpow);
        
        Nfpow *= Nf;  
        x5frac = 1.0 / (120.0 * Nfpow * cf);
        
        Nfpow *= Nf;   
        x6frac = tf / (720.0 * Nfpow);
        
        Nfpow *= Nf;  
        x7frac = 1.0 / (5040.0 * Nfpow * cf);
        
        Nfpow *= Nf; 
        x8frac = tf / (40320.0 * Nfpow);
        
        x2poly = -1.0 - nuf2;
        
        x3poly = -1.0 - 2 * tf2 - nuf2;
        
        x4poly = 5.0 + 3.0 * tf2 + 6.0 * nuf2 - 6.0 * tf2 * nuf2
        	- 3.0 * (nuf2 *nuf2) - 9.0 * tf2 * (nuf2 * nuf2);
        
        x5poly = 5.0 + 28.0 * tf2 + 24.0 * tf4 + 6.0 * nuf2 + 8.0 * tf2 * nuf2;
        
        x6poly = -61.0 - 90.0 * tf2 - 45.0 * tf4 - 107.0 * nuf2
        	+ 162.0 * tf2 * nuf2;
        
        x7poly = -61.0 - 662.0 * tf2 - 1320.0 * tf4 - 720.0 * (tf4 * tf2);
        
        x8poly = 1385.0 + 3633.0 * tf2 + 4095.0 * tf4 + 1575 * (tf4 * tf2);
        	
        philambda[0] = phif + x2frac * x2poly * (x * x)
        	+ x4frac * x4poly * Math.pow (x, 4.0)
        	+ x6frac * x6poly * Math.pow (x, 6.0)
        	+ x8frac * x8poly * Math.pow (x, 8.0);
        	
        philambda[1] = lambda0 + x1frac * x
        	+ x3frac * x3poly * Math.pow (x, 3.0)
        	+ x5frac * x5poly * Math.pow (x, 5.0)
        	+ x7frac * x7poly * Math.pow (x, 7.0);
        	
        return;
    }
     
    /** UTMXYToLatLon - Converts x and y coordinates in the UTM projection to a latitude/longitude pair.**/
    function UTMXYToLatLon (x, y, zone, southhemi, latlon)
    {
        	
        x -= 500000.0;
        x /= UTMScaleFactor;
        		
        y /= UTMScaleFactor;
        
        var cmeridian;
        cmeridian = DegToRad (-183.0 + (zone * 6.0));
				
        MapXYToLatLon (x, y, cmeridian, latlon);
        	
        return;
    }

    /** btnToGeographic_OnClick - Formerly called when the btnToGeographic button is clicked. 
				Now called as custom function when Get Coordinate tool is used.**/
		/************************************************************************************/	
		/*DEC NOTE:This function is now set to run when Get Coordinate clickFunction tool is used*/
		/************************************************************************************/	
                                
        latlon = new Array(2);
        var x, y, zone, southhemi;

        x = parseFloat (mapX);

        y = parseFloat (mapY);

        zone = parseFloat (txtZone);
				
        UTMXYToLatLon (x, y, zone, southhemi, latlon);
				
				var longitude = RadToDeg (latlon[1]);
				var latitude = RadToDeg (latlon[0]);

/************************************************************************************/			
								/*End added Javascript coordinate converter*/		
/************************************************************************************/	
/************************************************************************************/			
								/* 07-21-05 DEC Addition - Variables and document for text frame */		
/************************************************************************************/	

/*Create coord variables for lat/long*/
		coordXlong = longitude;
		coordYlat = latitude;				

		coordX = mapX;
		coordY = mapY;
						
		//alert(eLeft + ";" + eRight);				
						
		var Win1 = parent.TextFrame;
		Win1.document.open();
		Win1.document.writeln('<html><head><title>Coordinates</title>');
		Win1.document.writeln('<style type="text/css"> td {color:black; font-size:smaller; text-align:center;}</style></head>');
		Win1.document.writeln('<body  bgcolor="#E2ECE3" text="Black" onload="window.focus()"><center>');
		Win1.document.writeln('<center><b>The Coordinates you clicked on are:</b><br>');
		Win1.document.writeln('<table border="0" cellspacing="4" cellpadding="2" bgcolor="#E2ECE3">');
		Win1.document.writeln('<tr><td>');								
		Win1.document.writeln('<table border="1" cellpadding="2" bgcolor="White">');						
		Win1.document.writeln('<tr><td rowspan="2"><strong>&nbsp;&nbsp;&nbsp;UTM&nbsp;&nbsp;&nbsp;</strong></td><td>&nbsp;&nbsp;E : '+ parseInt(coordX)+'&nbsp;&nbsp;</td></tr>');
		Win1.document.writeln('<tr><td>&nbsp;&nbsp;N : '+ parseInt(coordY)+'&nbsp;&nbsp;</td></tr></table>');
		Win1.document.writeln('</td><td>');	
		Win1.document.writeln('<table border="1" cellpadding="2" bgcolor="White">');					
		Win1.document.writeln('<tr><td rowspan="2"><strong>Longitude/Latitude</strong></td><td>&nbsp;&nbsp;X : '+ coordXlong +'&nbsp;&nbsp;</td></tr>');
		Win1.document.writeln('<tr><td>&nbsp;&nbsp;Y : '+ coordYlat +'&nbsp;&nbsp;</td></tr></table>');
		Win1.document.writeln('</td><tr>');									
		Win1.document.writeln('</table></center>');
		Win1.document.writeln('</body></html>');
		Win1.document.close();
			
/************************************************************************************/			
								/* 07-21-05 End Addition - Variables and document for text frame */		
/************************************************************************************/	
		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 = "";
		/*customString += '<LAYER type="acetate" name="theClickPoint">\n';
		customString += '<OBJECT units="database">\n<POINT coords="' + parseInt(coordX) + coordsDelimiter + parseInt(coordY) + '';
		customString += '" >\n';
		customString += '<SIMPLEMARKERSYMBOL  type="star"';
		customString += ' color="255,255,0" outline="0,0,0" width="16" />\n</POINT>\n</OBJECT>\n';
		customString += '<OBJECT units="database">\n<TEXT coords="' + parseInt(coordX) + coordsDelimiter + parseInt(coordY) + '" label="' + parseInt(coordX) + coordsDelimiter + parseInt(coordY) + '">\n';
		customString += '<TEXTMARKERSYMBOL fontcolor="255,0,0" fontsize="12" shadow="64,64,64" glowing="255,255,0" halignment="right" valignment="top" interval="6" overlap="false" /></TEXT></OBJECT>\n';
		customString += '</LAYER>\n';*/
}

// 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]);
}



