﻿//Get bounding box.
function GetElementRectangle(element) {
   var rect = null;
   if (isIE)
      rect = element.getBoundingClientRect();
   else {
      var bounds = Sys.UI.DomElement.getBounds(element);
      rect = { "left": bounds.x, "top": bounds.y, "right": bounds.x + bounds.width, "bottom": bounds.y + bounds.height };
   }
   return rect;
}
//Radio checked value.
function GetRadioValue(obj) {
   if (obj) {
      for (var i = 0; i < obj.length; i++) {
         if (obj[i].checked) {
            return obj[i].value;
         }
      }
   }
   return "";
}
// Compute bearing.
function getBearing(x1,y1,x2,y2) {
   var beg,end;
   var bear;
   var xD = x1 - x2;
   var yD = y1 - y2;
   
   if ((Math.abs(xD) < 1) && (Math.abs(yD) < 1)) {
      return "";
   }
   var rad = Math.atan2(yD,xD);
   var deg = rad * (180 / Math.PI) + 90;
   end = "E";

   if (deg > -90 && deg <= 90) {
      beg = "N";
      if (deg > 0) {
         end = "W";
      }
      bear= Math.abs(deg);

   } else {
      beg = "S";
      if (deg < 180) {
         end = "W";
      }
      bear = Math.abs(180 - deg);
   }
   bear = DDToDMS(bear,true,false);
   bear = beg + bear + end;    
   return bear;
}
// DD to DM.
function DDToDM(dd,addZero) {
   var d = parseInt(dd);
   var dF = Math.abs(dd - d);
   var m = dF * 60;
   var u = 1000;
   m = parseInt(m * u + (5/10)) / u;

   if (addZero) {
      if (d < 10) {
         d = "0" + d;
      }
      if (m < 10) {
         m = "0" + m;
      }
   }
   var dm = (d + '\xB0 ' + m + '\'');
   return dm;
}
// DD to DMS.
function DDToDMS(dd,addZero,addDecimal) {
   var d = parseInt(dd);
   var dF = Math.abs(dd - d);
   var m = parseInt(dF * 60);
   var s = dF * 3600 - m * 60;

   if (addDecimal) {
      var u = 10;
      s = parseInt(s * u + (5/10)) / u;
   } else {
      s = Math.round(s);
   }
   if (addZero) {
      if (d < 10) {
         d = "0" + d;
      }
      if (m < 10) {
         m = "0" + m;
      }
      if (s < 10) {
         s = "0" + s;
      }
   }
   var dms = (d + '\xB0 ' + m + '\' ' + s + '\"');
   return dms;
}
// Compute area.
function computeArea(numPnt,x,y) {
   var area = 0;
   var i,j;
   var xjyi, xiyj;
   var xydiff;
   j = numPnt;

   for (i=1; i<=numPnt; i++) {
      xiyj = x[i] * y[j];
      xjyi = x[j] * y[i];
      xydiff = (xiyj - xjyi);
      area += xydiff;
      j = i;
   }
   area = Math.abs(area / 2);
   return area;  
}
// Compute 2 point distance.
function twoPntDistance(x1,y1,x2,y2) {
   var dist;
   var xD = Math.abs(x1 - x2);
   var yD = Math.abs(y1 - y2);
   dist = Math.sqrt(Math.pow(xD,2) + Math.pow(yD,2));
   return dist;  
}
// Compute multi-point distance.
function multiPntDistance(numPnt,x,y) {
   var dist = 0;
   var i;
   for (i=1; i<numPnt; i++) {
      dist += twoPntDistance(x[i], y[i], x[i+1], y[i+1]);
   }
   return dist;  
}
// Get query string arg.
function getQSArg(name) {
   var qsValue = "";
   var qs = document.location.search.substring(1).toUpperCase();
   var i;
   var nv;
   var pairs = qs.split("&");

   for (i=0; i<pairs.length; i++) {
      nv = pairs[i].split("=");
      if (nv[0] == name.toUpperCase()) {
         qsValue = nv[1];
      }
   }
   return qsValue;
}
// Set a cookie.
function setCookie(name, value, years) {
   var nextYear = new Date();
   nextYear.setFullYear(nextYear.getFullYear() + years);
   document.cookie = name + "=" + escape(value) + "; expires=" + nextYear.toGMTString();
}
// Delete cookie.
function deleteCookie(name) {
   document.cookie = name + "=" + escape("") + "; expires=" + now();
}
// Get a cookie.
function getCookie(name) {
   var value = "";
   var allCookies = document.cookie;
   var begPos = allCookies.indexOf(name);
   
   if (begPos != -1) {
      begPos += name.length + 1;
      var endPos = allCookies.indexOf(";", begPos);
      if (endPos == -1) {
         endPos = allCookies.length;
      }
      value = allCookies.substring(begPos, endPos);
      value = unescape(value);
   }
   return value;
}
// Current time.
function now() {
   return new Date().getTime();
}
// Get lat/lon.
function getLatLon(uX,uY,outFormat) {
   var a = 20925604.48;   		//major radius of ellipsoid, map units (NAD 83)
   var ec = 0.08181905782;  	//eccentricity of ellipsoid (NAD 83)
   var angRad = 0.01745329252; //number of radians in a degree
   var pi4 = Math.PI / 4;     //Pi / 4

   var p0 = LatOrigin * angRad;
   var p1 = Lat1stParallel * angRad;
   var p2 = Lat2ndParallel * angRad;
   var m0 = CentralMeridian * angRad;
   var x0 = CentralMeridianEasting;
   var y0 = FalseNorthing;

   // Calculate the coordinate system constants.
   with (Math) {
      var m1 = cos(p1) / sqrt(1 - (pow(ec,2)) * pow(sin(p1),2));  
      var m2 = cos(p2) / sqrt(1 - (pow(ec,2)) * pow(sin(p2),2));
      var t0 = tan(pi4 - (p0 / 2));
      var t1 = tan(pi4 - (p1 / 2));
      var 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);
      var n = log(m1 / m2) / log(t1 / t2);
      var f = m1 / (n * pow(t1,n)); 
      var rho0 = a * f * pow(t0,n);

      // Calculate the Longitude.
      var uX = uX - x0;
      var uY = uY - y0;
      var pi2 = pi4 * 2;

      var rho = sqrt(pow(uX,2) + pow((rho0 - uY),2));  
      var theta = atan(uX / (rho0 - uY));
      var txy = pow((rho / (a * f)),(1 / n));
      var lon = (theta / n) + m0;
      uX = uX + x0;

      // Estimate the Latitude
      var lat0 = pi2 - (2 * atan(txy));

      // Substitute the estimate into the iterative calculation that
      // converges on the correct Latitude value.
      var part1 = (1 - (ec * sin(lat0))) / (1 + (ec * sin(lat0)));
      var 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.
   var lat = lat1 / angRad;
   lon = lon / angRad;

   // Format output.
   latDM = DDToDM(lat,false);
   lonDM = DDToDM(-lon,false);
   LatDMS = DDToDMS(lat,false,true);
   LonDMS = DDToDMS(-lon,false,true);
   var u = 100000
	lat = parseInt(lat * u + (5/10)) / u;
	lon = parseInt(lon * u + (5/10)) / u;

   if (outFormat == "DM") {
      return "Lat, Long  DM: " + latDM + " N,  " + lonDM + " W";
   } else {
      return "Lat, Long  DMS: " + LatDMS + " N,  " + LonDMS + " W";
   }
   //return "Lat, Long  DD: " + lat + "\xB0 N,  " + -lon + "\xB0 W" + "     DMS: " + LatDMS + " N,  " + LonDMS + " W";
}
//Clean user input.
function cleanUp(txt) {
   txt = txt.replace(/\%/g, "");
   txt = txt.replace(/\#/g, "");
   txt = txt.replace(/\?/g, "");
   txt = txt.replace(/\&/g, "");
   txt = txt.replace(/\|/g, "");
   return txt;
}
//Round function.
function roundNumber(num, dec) {
	var result = Math.round(num * Math.pow(10,dec)) / Math.pow(10,dec);
   return result;
}

