
/****************************************************************************************************************/
/*                                                 DEFINES                                                      */
  var north; 
  var south;

  var zone;
  var hemisphere;
  var easting;
  var northing;
  var zone_exists;
  var num_digits = 0;
  var Letters = new Array(); //letters
  var Precision;

  var Latitude = 0.0;
  var Longitude = 0.0;

  var TranMerc_a;
  var TranMerc_f;
  var TranMerc_Origin_Lat;
  var TranMerc_Origin_Long;
  var TranMerc_False_Northing;
  var TranMerc_False_Easting; 
  var TranMerc_Scale_Factor;

  var dummy_northing;

/*================================================================================================================*/
  var ltr2_low_value;
  var ltr2_high_value;
  var false_northing;
  var min_northing;
/*================================================================================================================*/

  var PI           = 3.14159265358979323e0;    /* PI                       */
  var MIN_LAT      = ((-80.5 * PI) / 180.0 );  /* -80.5 degrees in radians */
  var MAX_LAT_1    = ((PI * 84.5) / 180.0 );   /* 84.5 degrees in radians  */
  var MAX_LAT_2    = ((PI * 89.99) / 180.0)    /* 90 degrees in radians    */
  var MAX_LAT_3    = ((PI * 90) / 180.0)       /* 90 degrees in radians    */
  var MIN_EASTING  = 100000;
  var MAX_EASTING  = 900000;
  var MIN_NORTHING = 0;
  var MAX_NORTHING = 10000000;

  var PI_OVER          = (PI/2.0e0)             /* PI over 2 */

  var MAX_DELTA_LONG   = ((PI * 90)/180.0)      /* 90 degrees in radians */

  var MIN_SCALE_FACTOR = 0.3
  var MAX_SCALE_FACTOR = 3.0

/*================================================================================================================*/

  var strRow = new Array("a","b","c","d","e","f","g","h","j","k","l","m","n","p","q","r","s","t","u","v","w","x","y","z"); //mgrs row 문자

  //var strCol = new Array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V");    //mgrs col 문자

  var strCol = new Array("a","b","c","d","e","f","g","h","j","k","l","m","n","p","q","r","s","t","u","v");    //mgrs col 문자

/*================================================================================================================*/

  var TRANMERC_NO_ERROR           = 0x0000;
  var TRANMERC_LAT_ERROR          = 0x0001;
  var TRANMERC_LON_ERROR          = 0x0002;
  var TRANMERC_EASTING_ERROR      = 0x0004;
  var TRANMERC_NORTHING_ERROR     = 0x0008;
  var TRANMERC_ORIGIN_LAT_ERROR   = 0x0010;
  var TRANMERC_CENT_MER_ERROR     = 0x0020;
  var TRANMERC_A_ERROR            = 0x0040;
  var TRANMERC_INV_F_ERROR        = 0x0080;
  var TRANMERC_SCALE_FACTOR_ERROR = 0x0100;
  var TRANMERC_LON_WARNING        = 0x0200;

  var MGRS_NO_ERROR               = 0x0000;
  var MGRS_LAT_ERROR              = 0x0001;
  var MGRS_LON_ERROR              = 0x0002;
  var MGRS_STRING_ERROR           = 0x0004;
  var MGRS_PRECISION_ERROR        = 0x0008;
  var MGRS_A_ERROR                = 0x0010;
  var MGRS_INV_F_ERROR            = 0x0020;
  var MGRS_EASTING_ERROR          = 0x0040;
  var MGRS_NORTHING_ERROR         = 0x0080;
  var MGRS_ZONE_ERROR             = 0x0100;
  var MGRS_HEMISPHERE_ERROR       = 0x0200;

  var UTM_NO_ERROR                = 0x0000;
  var UTM_LAT_ERROR               = 0x0001;
  var UTM_LON_ERROR               = 0x0002;
  var UTM_EASTING_ERROR           = 0x0004;
  var UTM_NORTHING_ERROR          = 0x0008;
  var UTM_ZONE_ERROR              = 0x0010;
  var UTM_HEMISPHERE_ERROR        = 0x0020;
  var UTM_ZONE_OVERRIDE_ERROR     = 0x0040;
  var UTM_A_ERROR                 = 0x0080;
  var UTM_INV_F_ERROR             = 0x0100;

/******************************************************************************************************************/
/*                          GLOBAL DECLARATIONS                            */

var UTM_a = 6378137.0;         /* Semi-major axis of ellipsoid in meters  */
var UTM_f = 1 / 298.257223563; /* Flattening of ellipsoid                 */
var UTM_Override = 0;          /* Zone override flag                      */

/* Ellipsoid Parameters, default to WGS 84  */
var TranMerc_a   = 6378137.0;              /* Semi-major axis of ellipsoid i meters */
var TranMerc_f   = 1 / 298.257223563;      /* Flattening of ellipsoid  */
var TranMerc_es  = 0.0066943799901413800;  /* Eccentricity (0.08181919084262188000) squared */
var TranMerc_ebs = 0.0067394967565869;     /* Second Eccentricity squared */

/* Transverse_Mercator projection Parameters */
var TranMerc_Origin_Lat = 0.0;           /* Latitude of origin in radians */
var TranMerc_Origin_Long = 0.0;          /* Longitude of origin in radians */
var TranMerc_False_Northing = 0.0;       /* False northing in meters */
var TranMerc_False_Easting = 0.0;        /* False easting in meters */
var TranMerc_Scale_Factor = 1.0;         /* Scale factor  */

/* Isometeric to geodetic latitude parameters, default to WGS 84 */
var TranMerc_ap = 6367449.1458008;
var TranMerc_bp = 16038.508696861;
var TranMerc_cp = 16.832613334334;
var TranMerc_dp = 0.021984404273757;
var TranMerc_ep = 3.1148371319283e-005;

/* Maximum variance for easting and northing values for WGS 84. */
var TranMerc_Delta_Easting = 40000000.0;
var TranMerc_Delta_Northing = 40000000.0;

/* These state variables are for optimization purposes. The only function
 * that should modify them is Set_Tranverse_Mercator_Parameters.         */

var DEG_TO_RAD   = 0.017453292519943295; /* PI/180                */
var RAD_TO_DEG   = 57.29577951308232087; /* 180/PI                 */

var LETTER_A     = 0;    /* ARRAY INDEX FOR LETTER A               */
var LETTER_B     = 1;    /* ARRAY INDEX FOR LETTER B               */
var LETTER_C     = 2;    /* ARRAY INDEX FOR LETTER C               */
var LETTER_D     = 3;    /* ARRAY INDEX FOR LETTER D               */
var LETTER_E     = 4;    /* ARRAY INDEX FOR LETTER E               */
var LETTER_F     = 5;    /* ARRAY INDEX FOR LETTER E               */
var LETTER_G     = 6;    /* ARRAY INDEX FOR LETTER H               */
var LETTER_H     = 7;    /* ARRAY INDEX FOR LETTER H               */
var LETTER_I     = 8;    /* ARRAY INDEX FOR LETTER I               */
var LETTER_J     = 9;    /* ARRAY INDEX FOR LETTER J               */
var LETTER_K     = 10;   /* ARRAY INDEX FOR LETTER J               */
var LETTER_L     = 11;   /* ARRAY INDEX FOR LETTER L               */
var LETTER_M     = 12;   /* ARRAY INDEX FOR LETTER M               */
var LETTER_N     = 13;   /* ARRAY INDEX FOR LETTER N               */
var LETTER_O     = 14;   /* ARRAY INDEX FOR LETTER O               */
var LETTER_P     = 15;   /* ARRAY INDEX FOR LETTER P               */
var LETTER_Q     = 16;   /* ARRAY INDEX FOR LETTER Q               */
var LETTER_R     = 17;   /* ARRAY INDEX FOR LETTER R               */
var LETTER_S     = 18;   /* ARRAY INDEX FOR LETTER S               */
var LETTER_T     = 19;   /* ARRAY INDEX FOR LETTER S               */
var LETTER_U     = 20;   /* ARRAY INDEX FOR LETTER U               */
var LETTER_V     = 21;   /* ARRAY INDEX FOR LETTER V               */
var LETTER_W     = 22;   /* ARRAY INDEX FOR LETTER W               */
var LETTER_X     = 23;   /* ARRAY INDEX FOR LETTER X               */
var LETTER_Y     = 24;   /* ARRAY INDEX FOR LETTER Y               */
var LETTER_Z     = 25;   /* ARRAY INDEX FOR LETTER Z               */

var MGRS_LETTERS = 3;                       /* NUMBER OF LETTERS IN MGRS      */
var ONEHT        = 100000.e0;               /* ONE HUNDRED THOUSAND           */
var TWOMIL       = 2000000.e0;              /* TWO MILLION                    */
var TRUE         = 1;                       /* CONSTANT VALUE FOR TRUE VALUE  */
var FALSE        = 0;                       /* CONSTANT VALUE FOR FALSE VALUE */
var PI           = 3.14159265358979323e0;   /* PI                             */
var PI_OVER_2    = (PI / 2.0e0);

var MIN_EASTING    = 100000;
var MAX_EASTING    = 900000;
var MAX_NORTHING   = 10000000;
var MAX_PRECISION  = 5;                      /* Maximum precision of easting & northing */
var MIN_UTM_LAT    =  ((-80 * PI) / 180.0 ); /* -80 degrees in radians    */
var MAX_UTM_LAT    =  ((84 * PI) / 180.0 );  /* 84 degrees in radians     */

var MIN_EAST_NORTH = 0;
var MAX_EAST_NORTH = 4000000;

/* Ellipsoid parameters, default to WGS 84 */
var MGRS_a = 6378137.0;         /* Semi-major axis of ellipsoid in meters */
var MGRS_f = 1 / 298.257223563; /* Flattening of ellipsoid           */
var MGRS_recpf = 298.257223563;
var MGRS_Ellipsoid_Code = new Array("W","E",0); //MGRS_Ellipsoid_Code

var CLARKE_1866 = "CC";
var CLARKE_1880 = "CD";
var BESSEL_1841 = "BR";
var BESSEL_1841_NAMIBIA = "BN";

/*****************************************************************************************************************/

//MGRS -> WGS84
function Convert_MGRS_To_Geodetic(MGRS) { /* Convert_MGRS_To_Geodetic */

  var MGRS = MGRS;

  //alert(MGRS);
 
  var error_code = MGRS_NO_ERROR;

  error_code = Check_Zone(MGRS);

  if (!error_code){
    if (zone_exists)
    {
      error_code = Convert_MGRS_To_UTM (MGRS);

			Set_UTM_Parameters (MGRS_a, MGRS_f, 0);

      error_code = Convert_UTM_To_Geodetic2(zone, hemisphere, easting, northing, Latitude, Longitude);
    }
  }	
	
  Latitude  = Latitude * 180/3.14159265358979323e0;
	Longitude = Longitude * 180/3.14159265358979323e0;

	Latitude   = Latitude.toFixed(5);
	Longitude  = Longitude.toFixed(5);

  	//strLat.Format("%.6fN", Latitude*180/3.14159265358979323e0);
	//strLong.Format("%.6fE", Longitude*180/3.14159265358979323e0);

    //alert(Latitude + " " + Longitude);
	
  return (Latitude + "/" +  Longitude);
	//alert(latitude + "/" + longitude);
} /* END OF Convert_MGRS_To_Geodetic */

function Convert_MGRS_To_UTM(MGRS){ /* Convert_MGRS_To_UTM */
	
  var scaled_min_northing;
  var min_northing;

  var upper_lat_limit;     /* North latitude limits based on 1st letter  */
  var lower_lat_limit;     /* South latitude limits based on 1st letter  */
  var grid_easting;        /* Easting for 100,000 meter grid square      */
  var grid_northing;       /* Northing for 100,000 meter grid square     */  
  //var letters[MGRS_LETTERS];
  var in_precision;

  var divisor = 1.0;
  var error_code = MGRS_NO_ERROR;

   error_code = Break_MGRS_String (MGRS);

  if (!Zone){
    error_code |= MGRS_STRING_ERROR;

  }else{

		if (!error_code){

		  if ((Letters[0] == LETTER_X) && ((Zone == 32) || (Zone == 34) || (Zone == 36))){

			error_code = MGRS_STRING_ERROR;

		  }else{

				if (Letters[0] < LETTER_N){
					Hemisphere = 'S';	 
				}else{
					Hemisphere = 'N';
				}

				//alert(Zone);

				Get_Grid_Values(Zone);

				//alert(Letters[1] + " " + Letters[2]);

				if ((Letters[1] < ltr2_low_value) || (Letters[1] > ltr2_high_value) || (Letters[2] > LETTER_V))

				  error_code = MGRS_STRING_ERROR;

				if (!error_code)
				{
				  grid_northing = (Letters[2]) * ONEHT + false_northing;
				  grid_easting  = ((Letters[1]) - ltr2_low_value + 1) * ONEHT;

				  if ((ltr2_low_value == LETTER_J) && (Letters[1] > LETTER_O))
					grid_easting = grid_easting - ONEHT;

				  if (Letters[2] > LETTER_O)
					grid_northing = grid_northing - ONEHT;

				  if (Letters[2] > LETTER_I)
					grid_northing = grid_northing - ONEHT; 

				  if (grid_northing >= TWOMIL)
					grid_northing = grid_northing - TWOMIL;

				  //alert(grid_northing + " " + grid_easting);

				  min_northing = 3500000.0;

				  //error_code = Get_Latitude_Band_Min_Northing(Letters[0]);

				  //alert(min_northing);

				  if (!error_code)
				  {
					scaled_min_northing = min_northing;

					while (scaled_min_northing >= TWOMIL)
					{
					  scaled_min_northing = scaled_min_northing - TWOMIL;
					}

					grid_northing = grid_northing - scaled_min_northing;

					if (grid_northing < 0.0){
					  grid_northing = grid_northing + TWOMIL;
					}

					grid_northing = min_northing + grid_northing;

					Easting  = grid_easting + Easting;
					Northing = grid_northing + Northing;

					//alert(Easting + " " +Northing);

					error_code = Set_UTM_Parameters(MGRS_a, MGRS_f, Zone);

					//alert(UTM_a + " " + UTM_f + " "+ UTM_Override);

					if (!error_code)
					{
					  error_code = Convert_UTM_To_Geodetic(Zone,Hemisphere,Easting,Northing);

					  //alert(error_code);

					  if (!error_code)
					  {
						//divisor = Math.pow(10.0, in_precision);
						divisor = Math.pow(10.0, Precision);

						//alert(Precision);

						//alert(divisor);

						error_code = Get_Latitude_Range(Letters[0]);

					  var upper_lat_limit = north;
						var lower_lat_limit = south;

						//alert(upper_lat_limit + " " + lower_lat_limit);

						if (!error_code)
						{
						  if (!(((lower_lat_limit - DEG_TO_RAD/divisor) <= Latitude) && (Latitude <= (upper_lat_limit + DEG_TO_RAD/divisor))))

							error_code = MGRS_LAT_ERROR;
						}
					  }
					}
				  }
				}
			 }
		}
  }
  
  //alert(error_code);

  return error_code;
} /* END OF Convert_MGRS_To_UTM */


function Get_Latitude_Range(letter) { /* Get_Latitude_Range */

  var error_code = MGRS_NO_ERROR;

 /* if ((letter >= LETTER_C) && (letter <= LETTER_H))
  {
    north = Latitude_Band_Table[letter-2].north * DEG_TO_RAD;
    south = Latitude_Band_Table[letter-2].south * DEG_TO_RAD;
  }
  else if ((letter >= LETTER_J) && (letter <= LETTER_N))
  {
    north = Latitude_Band_Table[letter-3].north * DEG_TO_RAD;
    south = Latitude_Band_Table[letter-3].south * DEG_TO_RAD;
  }
  else if ((letter >= LETTER_P) && (letter <= LETTER_X))
  {*/
  if ((letter >= LETTER_P) && (letter <= LETTER_X))
  {
    north = 40.0 * DEG_TO_RAD;
    south = 32.0 * DEG_TO_RAD;
  }

  else

    error_code = MGRS_STRING_ERROR;

  return error_code;
} /* Get_Latitude_Range */



function Convert_UTM_To_Geodetic(Zone, Hemisphere, Easting, Northing) {

  var Error_Code       = UTM_NO_ERROR;
  var Origin_Latitude  = 0;
  var Central_Meridian = 0;
  var False_Easting    = 500000;
  var False_Northing   = 0;
  var Scale            = 0.9996;

  if ((Zone < 1) || (Zone > 60))
    Error_Code = UTM_ZONE_ERROR;

  if ((Hemisphere != 'S') && (Hemisphere != 'N'))
    Error_Code = UTM_HEMISPHERE_ERROR;

  if ((Easting < MIN_EASTING) || (Easting > MAX_EASTING))
    Error_Code = UTM_EASTING_ERROR;

  if ((Northing < MIN_NORTHING) || (Northing > MAX_NORTHING))
    Error_Code = UTM_NORTHING_ERROR;

  
  if (!Error_Code){ /* no errors */

    if (Zone >= 31)
      Central_Meridian = ((6 * Zone - 183) * PI / 180.0 + 0.00000005);
    else
      Central_Meridian = ((6 * Zone + 177) * PI / 180.0 + 0.00000005);

    if (Hemisphere == 'S')

      False_Northing = 10000000;

	  //alert(Central_Meridian + " " + False_Northing);

    Set_Transverse_Mercator_Parameters(UTM_a, UTM_f, Origin_Latitude,Central_Meridian, False_Easting, False_Northing, Scale);

    if (Convert_Transverse_Mercator_To_Geodetic(Easting,Northing))

		Error_Code = UTM_NORTHING_ERROR;	

     if ((Latitude < MIN_LAT) || (Latitude > MAX_LAT_1))
     { 
		Error_Code = UTM_NORTHING_ERROR;
	
     }

  }

  //alert(Error_Code);
	
  return (Error_Code);
} /* END OF Convert_UTM_To_Geodetic */

                               
function Convert_UTM_To_Geodetic2(Zone, Hemisphere, Easting, Northing, Latitude, Longitude) {

  var Error_Code       = UTM_NO_ERROR;
  var Origin_Latitude  = 0;
  var Central_Meridian = 0;
  var False_Easting    = 500000;
  var False_Northing   = 0;
  var Scale            = 0.9996;

  //alert(Latitude + " " +Longitude);

  if ((Zone < 1) || (Zone > 60))
    Error_Code = UTM_ZONE_ERROR;

  if ((Hemisphere != 'S') && (Hemisphere != 'N'))
    Error_Code = UTM_HEMISPHERE_ERROR;

  if ((Easting < MIN_EASTING) || (Easting > MAX_EASTING))
    Error_Code = UTM_EASTING_ERROR;

  if ((Northing < MIN_NORTHING) || (Northing > MAX_NORTHING))
    Error_Code = UTM_NORTHING_ERROR;

  
  if (!Error_Code){ /* no errors */

    if (Zone >= 31)
      Central_Meridian = ((6 * Zone - 183) * PI / 180.0 + 0.00000005);
    else
      Central_Meridian = ((6 * Zone + 177) * PI / 180.0 + 0.00000005);

    if (Hemisphere == 'S')

      False_Northing = 10000000;

	  //alert(Central_Meridian + " " + False_Northing);

    Set_Transverse_Mercator_Parameters(UTM_a, UTM_f, Origin_Latitude,Central_Meridian, False_Easting, False_Northing, Scale);

    if (Convert_Transverse_Mercator_To_Geodetic(Easting,Northing))

		Error_Code = UTM_NORTHING_ERROR;	

     if ((Latitude < MIN_LAT) || (Latitude > MAX_LAT_1))
     { 
		Error_Code = UTM_NORTHING_ERROR;
	
     }

  }

  //alert(Error_Code);
	
  return (Error_Code);
} /* END OF Convert_UTM_To_Geodetic */


function Convert_Transverse_Mercator_To_Geodetic (Easting, Northing){ /* BEGIN Convert_Transverse_Mercator_To_Geodetic */

  var c;       /* Cosine of latitude                                */
  var de;      /* Delta easting - Difference in Easting (Easting-Fe)*/
  var dlam;    /* Delta longitude - Difference in Longitude         */
  var eta;     /* constant - TranMerc_ebs *c *c                     */
  var eta2;
  var eta3;
  var eta4;
  var ftphi;   /* Footpoint latitude                              */
  var i;       /* Loop iterator                                   */
  var s;       /* Sine of latitude                                */
  var sn;      /* Radius of curvature in the prime vertical       */
  var sr;      /* Radius of curvature in the meridian             */
  var t;       /* Tangent of latitude                             */
  var tan2;
  var tan4;
  var t10;     /* Term in coordinate conversion formula - GP to Y */
  var t11;     /* Term in coordinate conversion formula - GP to Y */
  var t12;     /* Term in coordinate conversion formula - GP to Y */
  var t13;     /* Term in coordinate conversion formula - GP to Y */
  var t14;     /* Term in coordinate conversion formula - GP to Y */
  var t15;     /* Term in coordinate conversion formula - GP to Y */
  var t16;     /* Term in coordinate conversion formula - GP to Y */
  var t17;     /* Term in coordinate conversion formula - GP to Y */
  var tmd;     /* True Meridional distance                        */
  var tmdo;    /* True Meridional distance for latitude of origin */
  var Error_Code = TRANMERC_NO_ERROR;

	  if ((Easting < (TranMerc_False_Easting - TranMerc_Delta_Easting))||(Easting > (TranMerc_False_Easting + TranMerc_Delta_Easting)))
	  { /* Easting out of range  */
		Error_Code = TRANMERC_EASTING_ERROR;
	  }

	  if ((Northing < (TranMerc_False_Northing - TranMerc_Delta_Northing))|| (Northing > (TranMerc_False_Northing + TranMerc_Delta_Northing)))
	  { /* Northing out of range */
		Error_Code = TRANMERC_NORTHING_ERROR;
	  }

	  if (!Error_Code)
	  {
			/* True Meridional Distances for latitude of origin */
			tmdo = SPHTMD(TranMerc_Origin_Lat);

			/*  Origin  */
			tmd = tmdo +  (Northing - TranMerc_False_Northing) / TranMerc_Scale_Factor; 

			/* First Estimate */
			sr    = SPHSR(0.e0);
			ftphi = tmd/sr;

			for (i = 0; i < 5 ; i++)
			{
			  t10   = SPHTMD(ftphi);
			  sr    = SPHSR(ftphi);
			  ftphi = ftphi + (tmd - t10) / sr;
			}

			/* Radius of Curvature in the meridian */
			sr = SPHSR(ftphi);

			/* Radius of Curvature in the meridian */
			sn = SPHSN(ftphi);

			/* Sine Cosine terms */
			s = Math.sin(ftphi);
			c = Math.cos(ftphi);

			/* Tangent Value  */
			t    = Math.tan(ftphi);
			tan2 = t * t;
			tan4 = tan2 * tan2;
			eta  = TranMerc_ebs * Math.pow(c,2);
			eta2 = eta * eta;
			eta3 = eta2 * eta;
			eta4 = eta3 * eta;
			de   = Easting - TranMerc_False_Easting;

			//if (fabs(de) < 0.0001)
			//  de = 0.0;

			/* Latitude */
			t10 = t / (2.e0 * sr * sn * Math.pow(TranMerc_Scale_Factor, 2));
			t11 = t * (5.e0  + 3.e0 * tan2 + eta - 4.e0 * Math.pow(eta,2) - 9.e0 * tan2 * eta) / (24.e0 * sr * Math.pow(sn,3) * Math.pow(TranMerc_Scale_Factor,4));
			t12 = t * (61.e0 + 90.e0 * tan2 + 46.e0 * eta + 45.E0 * tan4 - 252.e0 * tan2 * eta  - 3.e0 * eta2 + 100.e0 * eta3 - 66.e0 * tan2 * eta2 - 90.e0 * tan4 * eta + 88.e0 * eta4 + 225.e0 * tan4 * eta2 + 84.e0 * tan2* eta3 - 192.e0 * tan2 * eta4) / ( 720.e0 * sr * Math.pow(sn,5) * Math.pow(TranMerc_Scale_Factor, 6) );

			t13 = t * ( 1385.e0 + 3633.e0 * tan2 + 4095.e0 * tan4 + 1575.e0 * Math.pow(t,6))/ (40320.e0 * sr * Math.pow(sn,7) * Math.pow(TranMerc_Scale_Factor,8));

			Latitude = ftphi - Math.pow(de,2) * t10 + Math.pow(de,4) * t11 - Math.pow(de,6) * t12 + Math.pow(de,8) * t13;

			t14 = 1.e0 / (sn * c * TranMerc_Scale_Factor);

			t15 = (1.e0 + 2.e0 * tan2 + eta) / (6.e0 * Math.pow(sn,3) * c * Math.pow(TranMerc_Scale_Factor,3));

			t16 = (5.e0 + 6.e0 * eta + 28.e0 * tan2 - 3.e0 * eta2 + 8.e0 * tan2 * eta + 24.e0 * tan4 - 4.e0 * eta3 + 4.e0 * tan2 * eta2 + 24.e0 * tan2 * eta3) / (120.e0 * Math.pow(sn,5) * c  * Math.pow(TranMerc_Scale_Factor,5));

			t17 = (61.e0 +  662.e0 * tan2 + 1320.e0 * tan4 + 720.e0 * Math.pow(t,6)) / (5040.e0 * Math.pow(sn,7) * c * Math.pow(TranMerc_Scale_Factor,7));

			/* Difference in Longitude */
			dlam = de * t14 - Math.pow(de,3) * t15 + Math.pow(de,5) * t16 - Math.pow(de,7) * t17;

			/* Longitude */
			Longitude = TranMerc_Origin_Long + dlam;

			//alert(Latitude + " " +dlam+ " " +Longitude);

			while (Latitude > (90.0 * PI / 180.0))
			{
			  Latitude = PI - Latitude;
			  Longitude += PI;

			  if (Longitude > PI)
				Longitude -= (2 * PI);
			}

			while (Latitude < (-90.0 * PI / 180.0))
			{
			  Latitude = - (Latitude + PI);
			  Longitude += PI;

			  if (Longitude > PI)
				Longitude -= (2 * PI);
			}

			if (Longitude > (2*PI))
			  Longitude -= (2 * PI);

			if (Longitude < -PI)
			  Longitude += (2 * PI);

			//if (fabs(dlam) > (9.0 * PI / 180))
			//{ /* Distortion will result if Longitude is more than 9 degrees from the Central Meridian */
			//  Error_Code = TRANMERC_LON_WARNING;
			//}
	}

	//alert(Error_Code);

  return Error_Code;
} /* END OF Convert_Transverse_Mercator_To_Geodetic */


function Set_Transverse_Mercator_Parameters(a, f, Origin_Latitude, Central_Meridian, False_Easting, False_Northing, Scale_Factor) { 

  var tn;         /* True Meridianal distance constant       */
  var tn2;
  var tn3;
  var tn4;
  var tn5;
  var TranMerc_b; /* Semi-minor axis of ellipsoid, in meters */
  var inv_f = 1 / f;
  var Error_Code = TRANMERC_NO_ERROR;

  if (a <= 0.0)
  { /* Semi-major axis must be greater than zero */
    Error_Code = TRANMERC_A_ERROR;
  }
  if ((inv_f < 250) || (inv_f > 350))
  { /* Inverse flattening must be between 250 and 350 */
    Error_Code = TRANMERC_INV_F_ERROR;
  }
  if ((Origin_Latitude < - MAX_LAT_2) || (Origin_Latitude > MAX_LAT_2))
  { /* origin latitude out of range */
    Error_Code = TRANMERC_ORIGIN_LAT_ERROR;
  }
  if ((Central_Meridian < -PI) || (Central_Meridian > (2*PI)))
  { /* origin longitude out of range */
    Error_Code = TRANMERC_CENT_MER_ERROR;
  }
  if ((Scale_Factor < MIN_SCALE_FACTOR) || (Scale_Factor > MAX_SCALE_FACTOR))
  {
    Error_Code = TRANMERC_SCALE_FACTOR_ERROR;
  }

  if (!Error_Code)
  { /* no errors */
	 
	TranMerc_a = a;
	TranMerc_f = f;
    TranMerc_Origin_Lat     = 0;
    TranMerc_Origin_Long    = 0;
    TranMerc_False_Northing = 0;
    TranMerc_False_Easting  = 0; 
    TranMerc_Scale_Factor   = 1;

    /* Eccentricity Squared */
    TranMerc_es = 2 * TranMerc_f - TranMerc_f * TranMerc_f;
    /* Second Eccentricity Squared */
    TranMerc_ebs = (1 / (1 - TranMerc_es)) - 1;

    TranMerc_b = TranMerc_a * (1 - TranMerc_f);    
    /*True meridianal constants  */
    tn = (TranMerc_a - TranMerc_b) / (TranMerc_a + TranMerc_b);
    tn2 = tn * tn;
    tn3 = tn2 * tn;
    tn4 = tn3 * tn;
    tn5 = tn4 * tn;

    TranMerc_ap = TranMerc_a * (1.e0 - tn + 5.e0 * (tn2 - tn3)/4.e0 + 81.e0 * (tn4 - tn5)/64.e0 );
    TranMerc_bp = 3.e0 * TranMerc_a * (tn - tn2 + 7.e0 * (tn3 - tn4)/8.e0 + 55.e0 * tn5/64.e0 )/2.e0;
    TranMerc_cp = 15.e0 * TranMerc_a * (tn2 - tn3 + 3.e0 * (tn4 - tn5 )/4.e0) /16.0;
    TranMerc_dp = 35.e0 * TranMerc_a * (tn3 - tn4 + 11.e0 * tn5 / 16.e0) / 48.e0;
    TranMerc_ep = 315.e0 * TranMerc_a * (tn4 - tn5) / 512.e0;

	//alert(TranMerc_ap + " " + TranMerc_bp + " " + TranMerc_cp + " " + TranMerc_dp + " " + TranMerc_ep);

    Convert_Geodetic_To_Transverse_Mercator(MAX_LAT_2,MAX_DELTA_LONG);

    Convert_Geodetic_To_Transverse_Mercator2(0,MAX_DELTA_LONG);

    TranMerc_Origin_Lat = Origin_Latitude;

    if (Central_Meridian > PI)
      Central_Meridian -= (2*PI);

	TranMerc_Origin_Long    = Central_Meridian;
    TranMerc_False_Northing = False_Northing;
    TranMerc_False_Easting  = False_Easting; 
    TranMerc_Scale_Factor   = Scale_Factor;

	//alert(TranMerc_Origin_Long + " " +TranMerc_False_Northing + " " + TranMerc_False_Easting + " "+TranMerc_Scale_Factor);
  } /* END OF if(!Error_Code) */

  return Error_Code;
}  /* END of Set_Transverse_Mercator_Parameters  */



function Convert_Geodetic_To_Transverse_Mercator (Latitude, Longitude){ /* BEGIN Convert_Geodetic_To_Transverse_Mercator */
 
  var c;       /* Cosine of latitude                              */
  var c2;
  var c3;
  var c5;
  var c7;
  var dlam;    /* Delta longitude - Difference in Longitude       */
  var eta;     /* constant - TranMerc_ebs *c *c                   */
  var eta2;
  var eta3;
  var eta4;
  var s;       /* Sine of latitude                                */
  var sn;      /* Radius of curvature in the prime vertical       */
  var t;       /* Tangent of latitude                             */
  var tan2;
  var tan3;
  var tan4;
  var tan5;
  var tan6;
  var t1;      /* Term in coordinate conversion formula - GP to Y */
  var t2;      /* Term in coordinate conversion formula - GP to Y */
  var t3;      /* Term in coordinate conversion formula - GP to Y */
  var t4;      /* Term in coordinate conversion formula - GP to Y */
  var t5;      /* Term in coordinate conversion formula - GP to Y */
  var t6;      /* Term in coordinate conversion formula - GP to Y */
  var t7;      /* Term in coordinate conversion formula - GP to Y */
  var t8;      /* Term in coordinate conversion formula - GP to Y */
  var t9;      /* Term in coordinate conversion formula - GP to Y */
  var tmd;     /* True Meridional distance                        */
  var tmdo;    /* True Meridional distance for latitude of origin */
  var Error_Code = TRANMERC_NO_ERROR;
  var temp_Origin;
  var temp_Long;

 if ((Latitude < - MAX_LAT_2) || (Latitude > MAX_LAT_2))
  {  
    Error_Code = TRANMERC_LAT_ERROR;
  }

  if (Longitude > PI)
    Longitude -= (2 * PI);

  if ((Longitude < (TranMerc_Origin_Long - MAX_DELTA_LONG))|| (Longitude > (TranMerc_Origin_Long + MAX_DELTA_LONG)))
  {
    if (Longitude < 0)
      temp_Long = Longitude + 2 * PI;
    else
      temp_Long = Longitude;

    if (TranMerc_Origin_Long < 0)
      temp_Origin = TranMerc_Origin_Long + 2 * PI;
    else
      temp_Origin = TranMerc_Origin_Long;

    if ((temp_Long < (temp_Origin - MAX_DELTA_LONG))|| (temp_Long > (temp_Origin + MAX_DELTA_LONG)))

      Error_Code = TRANMERC_LON_ERROR;
  }

  //alert(TranMerc_Origin_Long);
  //alert(temp_Long + " " +temp_Origin);

  if (!Error_Code){ 

    dlam = Longitude - TranMerc_Origin_Long;

   /* if (fabs(dlam) > (9.0 * PI / 180))
    { 
      Error_Code = TRANMERC_LON_WARNING;
    }*/

    if (dlam > PI)
      dlam -= (2 * PI);

    if (dlam < -PI)
      dlam += (2 * PI);

    //if (fabs(dlam) < 2.e-10)
    //  dlam = 0.0;

    s    = Math.sin(Latitude);
    c    = Math.cos(Latitude);
    c2   = c * c;
    c3   = c2 * c;
    c5   = c3 * c2;
    c7   = c5 * c2;
    t   = Math.tan (Latitude);
    tan2 = t * t;
    tan3 = tan2 * t;
    tan4 = tan3 * t;
    tan5 = tan4 * t;
    tan6 = tan5 * t;
    eta  = TranMerc_ebs * c2;
    eta2 = eta * eta;
    eta3 = eta2 * eta;
    eta4 = eta3 * eta;

	//alert(t);
 
    sn   = SPHSN(Latitude);
    tmd  = SPHTMD(Latitude);
    tmdo = SPHTMD(TranMerc_Origin_Lat);

	//alert(sn + " " + tmd + " " + tmdo);
	//alert(sn);

    t1 = (tmd - tmdo) * TranMerc_Scale_Factor;
    t2 = sn * s * c * TranMerc_Scale_Factor/ 2.e0;
    t3 = sn * s * c3 * TranMerc_Scale_Factor * (5.e0 - tan2 + 9.e0 * eta + 4.e0 * eta2) /24.e0; 

    t4 = sn * s * c5 * TranMerc_Scale_Factor * (61.e0 - 58.e0 * tan2 + tan4 + 270.e0 * eta - 330.e0 * tan2 * eta + 445.e0 * eta2 + 324.e0 * eta3 -680.e0 * tan2 * eta2 + 88.e0 * eta4 -600.e0 * tan2 * eta3 - 192.e0 * tan2 * eta4) / 720.e0;

    t5 = sn * s * c7 * TranMerc_Scale_Factor * (1385.e0 - 3111.e0 * tan2 + 543.e0 * tan4 - tan6) / 40320.e0;

    Northing = TranMerc_False_Northing + t1 + Math.pow(dlam,2.e0) * t2 + Math.pow(dlam,4.e0) * t3 + Math.pow(dlam,6.e0) * t4 + Math.pow(dlam,8.e0) * t5; 


    t6 = sn * c * TranMerc_Scale_Factor;
    t7 = sn * c3 * TranMerc_Scale_Factor * (1.e0 - tan2 + eta ) /6.e0;
    t8 = sn * c5 * TranMerc_Scale_Factor * (5.e0 - 18.e0 * tan2 + tan4 + 14.e0 * eta - 58.e0 * tan2 * eta + 13.e0 * eta2 + 4.e0 * eta3 - 64.e0 * tan2 * eta2 - 24.e0 * tan2 * eta3 )/ 120.e0;
    t9 = sn * c7 * TranMerc_Scale_Factor * ( 61.e0 - 479.e0 * tan2 + 179.e0 * tan4 - tan6 ) /5040.e0;

    Easting = TranMerc_False_Easting + dlam * t6 + Math.pow(dlam,3.e0) * t7 + Math.pow(dlam,5.e0) * t8 + Math.pow(dlam,7.e0) * t9;

	//alert(Northing + " " +Easting);
  }

  return Error_Code;

} /* END of Convert_Geodetic_To_Transverse_Mercator  */



function Convert_Geodetic_To_Transverse_Mercator2 (Latitude, Longitude){ /* BEGIN Convert_Geodetic_To_Transverse_Mercator */
 
  var c;       /* Cosine of latitude                              */
  var c2;
  var c3;
  var c5;
  var c7;
  var dlam;    /* Delta longitude - Difference in Longitude       */
  var eta;     /* constant - TranMerc_ebs *c *c                   */
  var eta2;
  var eta3;
  var eta4;
  var s;       /* Sine of latitude                                */
  var sn;      /* Radius of curvature in the prime vertical       */
  var t;       /* Tangent of latitude                             */
  var tan2;
  var tan3;
  var tan4;
  var tan5;
  var tan6;
  var t1;      /* Term in coordinate conversion formula - GP to Y */
  var t2;      /* Term in coordinate conversion formula - GP to Y */
  var t3;      /* Term in coordinate conversion formula - GP to Y */
  var t4;      /* Term in coordinate conversion formula - GP to Y */
  var t5;      /* Term in coordinate conversion formula - GP to Y */
  var t6;      /* Term in coordinate conversion formula - GP to Y */
  var t7;      /* Term in coordinate conversion formula - GP to Y */
  var t8;      /* Term in coordinate conversion formula - GP to Y */
  var t9;      /* Term in coordinate conversion formula - GP to Y */
  var tmd;     /* True Meridional distance                        */
  var tmdo;    /* True Meridional distance for latitude of origin */
  var Error_Code = TRANMERC_NO_ERROR;
  var temp_Origin;
  var temp_Long;

 if ((Latitude < - MAX_LAT_2) || (Latitude > MAX_LAT_2))
  {  
    Error_Code = TRANMERC_LAT_ERROR;
  }

  if (Longitude > PI)
    Longitude -= (2 * PI);

  if ((Longitude < (TranMerc_Origin_Long - MAX_DELTA_LONG))|| (Longitude > (TranMerc_Origin_Long + MAX_DELTA_LONG)))
  {
    if (Longitude < 0)
      temp_Long = Longitude + 2 * PI;
    else
      temp_Long = Longitude;

    if (TranMerc_Origin_Long < 0)
      temp_Origin = TranMerc_Origin_Long + 2 * PI;
    else
      temp_Origin = TranMerc_Origin_Long;

    if ((temp_Long < (temp_Origin - MAX_DELTA_LONG))|| (temp_Long > (temp_Origin + MAX_DELTA_LONG)))

      Error_Code = TRANMERC_LON_ERROR;
  }

//	alert(TranMerc_Origin_Long);
//  alert(temp_Long + " " +temp_Origin);

  if (!Error_Code){ 

    dlam = Longitude - TranMerc_Origin_Long;

   /* if (fabs(dlam) > (9.0 * PI / 180))
    { 
      Error_Code = TRANMERC_LON_WARNING;
    }*/

    if (dlam > PI)
      dlam -= (2 * PI);

    if (dlam < -PI)
      dlam += (2 * PI);

    //if (fabs(dlam) < 2.e-10)
    //  dlam = 0.0;

    s    = Math.sin(Latitude);
    c    = Math.cos(Latitude);
    c2   = c * c;
    c3   = c2 * c;
    c5   = c3 * c2;
    c7   = c5 * c2;
    t   = Math.tan (Latitude);
    tan2 = t * t;
    tan3 = tan2 * t;
    tan4 = tan3 * t;
    tan5 = tan4 * t;
    tan6 = tan5 * t;
    eta  = TranMerc_ebs * c2;
    eta2 = eta * eta;
    eta3 = eta2 * eta;
    eta4 = eta3 * eta;

	//alert(t);
 
    sn   = SPHSN(Latitude);
    tmd  = SPHTMD(Latitude);
    tmdo = SPHTMD(TranMerc_Origin_Lat);

	//alert(sn + " " + tmd + " " + tmdo);

	//alert(sn);

 
    t1 = (tmd - tmdo) * TranMerc_Scale_Factor;
    t2 = sn * s * c * TranMerc_Scale_Factor/ 2.e0;
    t3 = sn * s * c3 * TranMerc_Scale_Factor * (5.e0 - tan2 + 9.e0 * eta + 4.e0 * eta2) /24.e0; 

    t4 = sn * s * c5 * TranMerc_Scale_Factor * (61.e0 - 58.e0 * tan2 + tan4 + 270.e0 * eta - 330.e0 * tan2 * eta + 445.e0 * eta2 + 324.e0 * eta3 -680.e0 * tan2 * eta2 + 88.e0 * eta4 -600.e0 * tan2 * eta3 - 192.e0 * tan2 * eta4) / 720.e0;

    t5 = sn * s * c7 * TranMerc_Scale_Factor * (1385.e0 - 3111.e0 * tan2 + 543.e0 * tan4 - tan6) / 40320.e0;

    dummy_northing = TranMerc_False_Northing + t1 + Math.pow(dlam,2.e0) * t2 + Math.pow(dlam,4.e0) * t3 + Math.pow(dlam,6.e0) * t4 + Math.pow(dlam,8.e0) * t5; 


    t6 = sn * c * TranMerc_Scale_Factor;
    t7 = sn * c3 * TranMerc_Scale_Factor * (1.e0 - tan2 + eta ) /6.e0;
    t8 = sn * c5 * TranMerc_Scale_Factor * (5.e0 - 18.e0 * tan2 + tan4 + 14.e0 * eta - 58.e0 * tan2 * eta + 13.e0 * eta2 + 4.e0 * eta3 - 64.e0 * tan2 * eta2 - 24.e0 * tan2 * eta3 )/ 120.e0;
    t9 = sn * c7 * TranMerc_Scale_Factor * ( 61.e0 - 479.e0 * tan2 + 179.e0 * tan4 - tan6 ) /5040.e0;

    Easting = TranMerc_False_Easting + dlam * t6 + Math.pow(dlam,3.e0) * t7 + Math.pow(dlam,5.e0) * t8 + Math.pow(dlam,7.e0) * t9;

	//alert(Northing + " " +Easting);
  }

  return Error_Code;

} /* END of Convert_Geodetic_To_Transverse_Mercator  */



function SPHTMD(Latitude){

	var SPHTMD_value = ((TranMerc_ap * Latitude  - TranMerc_bp * Math.sin(2.e0 * Latitude) + TranMerc_cp * Math.sin(4.e0 * Latitude) - TranMerc_dp * Math.sin(6.e0 * Latitude) + TranMerc_ep * Math.sin(8.e0 * Latitude) ) );

  return SPHTMD_value;

}

function SPHSN(Latitude) {

	var SPHSN_value = ((TranMerc_a / Math.sqrt( 1.e0 - TranMerc_es *  Math.pow(Math.sin(Latitude), 2))));

	return SPHSN_value;
}

function SPHSR(Latitude) {

	var SPHSR_value = ((TranMerc_a * (1.e0 - TranMerc_es) /  Math.pow(DENOM(Latitude), 3)));

	return SPHSR_value;
}

function  DENOM(Latitude) {

	var DENOM_value = ((Math.sqrt(1.e0 - TranMerc_es * Math.pow(Math.sin(Latitude),2))));

	return DENOM_value;
}


function Set_UTM_Parameters(a,f,override) {

  var inv_f = 1 / f;
  var Error_Code = UTM_NO_ERROR;

  if (a <= 0.0)
  { /* Semi-major axis must be greater than zero      */
    Error_Code = UTM_A_ERROR;
  }
  if ((inv_f < 250) || (inv_f > 350))
  { /* Inverse flattening must be between 250 and 350 */
    Error_Code = UTM_INV_F_ERROR;
  }
  if ((override < 0) || (override > 60))
  {
    Error_Code = UTM_ZONE_OVERRIDE_ERROR;
  }
  if (!Error_Code)
  { /* no errors */
    UTM_a = a;
    UTM_f = f;
    UTM_Override = override;
  }

  return Error_Code;
} /* END OF Set_UTM_Parameters */


function Get_Latitude_Band_Min_Northing(letter) { /* Get_Latitude_Band_Min_Northing */

  var error_code = MGRS_NO_ERROR;

 /* if ((letter >= LETTER_C) && (letter <= LETTER_H))
		min_northing = Latitude_Band_Table[letter-2].min_northing;
  else if ((letter >= LETTER_J) && (letter <= LETTER_N))
		min_northing = Latitude_Band_Table[letter-3].min_northing;
  else if ((letter >= LETTER_P) && (letter <= LETTER_X))*/
	
  //alert(letter);

  if (letter == 18)
 
		min_northing = 3500000.0;
  else

    error_code = MGRS_STRING_ERROR;

  return error_code;
} /* Get_Latitude_Band_Min_Northing */


function Get_Grid_Values(zone) { /* BEGIN Get_Grid_Values */

  var set_number;    /* Set number (1-6) based on UTM zone number */
  var aa_pattern;    /* Pattern based on ellipsoid code           */

  set_number = zone % 6; //zone 52

	  if (!set_number)
		set_number = 6;

	 /* if (!strcmp(MGRS_Ellipsoid_Code,CLARKE_1866) || !strcmp(MGRS_Ellipsoid_Code, CLARKE_1880) || !strcmp(MGRS_Ellipsoid_Code,BESSEL_1841) || !strcmp(MGRS_Ellipsoid_Code,BESSEL_1841_NAMIBIA))
	  {
			aa_pattern = FALSE;
	  }else{
			aa_pattern = TRUE;
	  }*/

	  aa_pattern = TRUE;

	  if ((set_number == 1) || (set_number == 4)){

		ltr2_low_value  = LETTER_A;
		ltr2_high_value = LETTER_H;

	  }else if ((set_number == 2) || (set_number == 5)){

		ltr2_low_value  = LETTER_J;
		ltr2_high_value = LETTER_R;

	  }else if ((set_number == 3) || (set_number == 6)){

		ltr2_low_value = LETTER_S;
		ltr2_high_value = LETTER_Z;
	  }

	  /* False northing at A for second letter of grid square */
	  if (aa_pattern){

		if ((set_number % 2) ==  0)
		  false_northing = 1500000.0; //<- 1500000.0000000000
		else
		  false_northing = 0.0;
	  }else{
		if ((set_number % 2) == 0)
		  false_northing =  500000.0;
		else
		  false_northing = 1000000.00;
	  }

	  //alert(ltr2_low_value + " " + ltr2_high_value + " " + false_northing);

} /* END OF Get_Grid_Values */



function Break_MGRS_String (MGRS){  /* Break_MGRS_String */

  var num_letters;
  var i = 0;
  var j = 0;
  var k = 0;
  var error_code = MGRS_NO_ERROR;

  var MGRS_string1, MGRS_string2, MGRS_string3, MGRS_string4, MGRS_string5, MGRS_string6, MGRS_string7, MGRS_string8, MGRS_string9, MGRS_string10, MGRS_string11, MGRS_string12, MGRS_string13, MGRS_string14, MGRS_string15

  	MGRS_string1   = MGRS.substring(0,1);
	MGRS_string2   = MGRS.substring(1,2);
	MGRS_string3   = MGRS.substring(2,3);
	MGRS_string4   = MGRS.substring(3,4);
	MGRS_string5   = MGRS.substring(4,5);
	MGRS_string6   = MGRS.substring(5,6);
	MGRS_string7   = MGRS.substring(6,7);
	MGRS_string8   = MGRS.substring(7,8);
	MGRS_string9   = MGRS.substring(8,9);
	MGRS_string10  = MGRS.substring(9,10);
	MGRS_string11  = MGRS.substring(10,11);
	MGRS_string12  = MGRS.substring(11,12);
	MGRS_string13  = MGRS.substring(12,13);
	MGRS_string14  = MGRS.substring(13,14);
	MGRS_string15  = MGRS.substring(14,15);

	//alert(MGRS_string1+" "+MGRS_string2+" "+MGRS_string3+" "+MGRS_string4+" "+MGRS_string5+" "+MGRS_string6+" "+MGRS_string7+" "+MGRS_string8+" "+MGRS_string9+" "+MGRS_string10+" "+MGRS_string11+" "+MGRS_string12+" "+MGRS_string13+" "+MGRS_string14+" "+MGRS_string15);
				
   if (num_digits <= 2){

		if (num_digits > 0){

		  //char zone_string[3];
		  var zone_string = new Array();
		  //strncpy (zone_string, MGRS+j, 2);
		  zone_string[0] = MGRS_string1
		  zone_string[1] = MGRS_string2
		  zone_string[2] = 0;

		  Zone = zone_string[0] +  zone_string[1]

		  //자료형변환 문자 -> 숫자
		  //sscanf (zone_string, "%ld", Zone); //문자형을 숫자형으로 변환
		  Zone  = Number(Zone);

			if ((Zone < 1) || (Zone > 60))
				error_code = MGRS_STRING_ERROR;
			
		}else{
				Zone = 0;
		}
	
	  //alert(Zone);

  }else{
		error_code = MGRS_STRING_ERROR;
  }

  //j = 2;
  num_letters = 3;

  if (num_letters == 3){

  	var m_areanum1;
	for (j=0; strRow.length; j++){
		if (MGRS_string4 == strRow[j]){
			m_areanum1 = j;
			break;
		} 			
	}

	var m_areanum2;
	for (k=0; strCol.length; k++){
		if (MGRS_string5 == strCol[k]){
			m_areanum2 = k;
			break;
		}
	}
  
		Letters[0] = 18;

		if ((Letters[0] == LETTER_I) || (Letters[0] == LETTER_O))
				 error_code = MGRS_STRING_ERROR;

		Letters[1] = m_areanum1;
		if ((Letters[1] == LETTER_I) || (Letters[1] == LETTER_O))
				  error_code = MGRS_STRING_ERROR;

		Letters[2] = m_areanum2;
		if ((Letters[2] == LETTER_I) || (Letters[2] == LETTER_O))
				  error_code = MGRS_STRING_ERROR;

		//alert(Letters[0] + " " + Letters[1] +" "+ Letters[2]);

  }else{
		error_code = MGRS_STRING_ERROR;
  }

  //j = 5;

  num_digits = 10;

  if ((num_digits <= 10) && (num_digits%2 == 0))
  {
		var n;
		var east_string  = new Array(); //LOC10 앞 다섯자리
		var north_string = new Array(); //LOC10 뒷 다섯자리

		//var east_string[6];
		//var north_string[6];
		var east;
		var north;
		var multiplier;
	   
		n = num_digits/2;
		Precision = n;

		//alert(Precision);

		if (n > 0){

		  //strncpy (east_string, MGRS+j, n);
		  //east_string[n] = 0;
		  //sscanf (east_string, "%ld", &east);
		  //strncpy (north_string, MGRS+j+n, n);

		  east_string[0] = MGRS_string6;
		  east_string[1] = MGRS_string7;
		  east_string[2] = MGRS_string8;
		  east_string[3] = MGRS_string9;
		  east_string[4] = MGRS_string10;
		  east_string[5] = 0;

		  east  = east_string[0] + east_string[1] + east_string[2] + east_string[3] + east_string[4];
		  east  = Number(east);

		  //north_string[n] = 0;
		  //sscanf (north_string, "%ld", &north);

		  north_string[0] = MGRS_string11;
		  north_string[1] = MGRS_string12;
		  north_string[2] = MGRS_string13;
		  north_string[3] = MGRS_string14;
		  north_string[4] = MGRS_string15;
		  north_string[5] = 0;

		  north  = north_string[0] + north_string[1] + north_string[2] + north_string[3] + north_string[4];
		  north  = Number(north);

		  //alert(east + " " + north);

		  multiplier = Math.pow(10.0, 5 - n);
		  multiplier = multiplier.toFixed(10); 
		  //multiplier = parseFloat(1.1000000000);
	      //multiplier2 = 10.0007 * 0.006;

		  //alert(multiplier);
		  Easting = east * multiplier;
		  Northing = north * multiplier;

		  //alert(Easting + " " + Northing);

		}else{

		  Easting = 0.0;
		  Northing = 0.0;
		 
		}

  }else{
    error_code = MGRS_STRING_ERROR;
  }
	
  return error_code;
} /* END OF Break_MGRS_String */


function Check_Zone(MGRS) { /* Check_Zone */

  var MGRS = MGRS;
  var i = 0;
  var j = 0;
  var error_code = MGRS_NO_ERROR;

	//alert(MGRS);

	/* skip any leading blanks  
	while (MGRS_string[i] == ' '){
		i++;  
	}

	j = i;
	
 	while (isdigit(MGRS_string[i])){
		i++;
	}*/

	num_digits = 2;
	
	if (num_digits <= 2){
		if (num_digits > 0){
			zone_exists = TRUE;
		}else{
			zone_exists = FALSE;
		}
	}else{

		error_code = MGRS_STRING_ERROR;
	}
	
  return error_code;
} /* Check_Zone */

