// JScript source code

// *******************************************************************************
// *
// *      MODULE:  CONV_LONG_LAT_TO_GEO
// *
// *      DESCRIPTION:
// *
// *           This routine converts strings containing the latitude and longitude
// *           to a geographic X/Y coordinate.
// *******************************************************************************

//globals
var geo_x = 0;
var geo_y = 0;

function conv_long_lat_to_geo ( inp_longitude, inp_latitude, geo_x, geo_y) 
{

//alert("inp_longitude: " + inp_longitude + " , latitude: " + inp_latitude);

 var central_long = -74000000;
 var central_lat =  41000000;
 var state_plane_rotation = 0.0;
 var state_plane_flip = 0;                    //   state_plane_flip = .FALSE.
 var state_plane_offset_x = 0.0;
 var state_plane_offset_y =  0.0;
 var num_state_plane_units_in_mile = 240;

 var num_miles_in_polar_circum = 24859.82;
 var num_miles_in_equatorial_circum = 24901.55;
 var num_degrees_in_circum = 360.0;
 var num_implied_units_in_degree = 1000000.0;


inp_longitude = -1*inp_longitude*num_implied_units_in_degree;
inp_latitude = inp_latitude*num_implied_units_in_degree;

//alert("inp_longitude: " + inp_longitude + "\n" + "inp_latitude: " + inp_latitude);

//      CALCULATE THE DEGREE TO RADIAN CONVERSION FACTOR.

 var radians_in_millionth_of_degree = 3.141592654 / 180.0 / 1000000.0;

//  CALCULATE THE SLOPES AND ANGLES OF THE CTS STATE-PLANAR COORDINATES AS
//  COMPARED TO THE LATITUDE/LONGITUDE COORDINATES.  THE SLOPES OF THE TWO
//  COORDINATE SYSTEMS WERE DERIVED BY TAKING THE X-Y VALUE OF TWO POINTS AT
//  OPPOSITE ENDS OF THE METROPOLITAN AREA.

//      CONVERT THE LONGITUDE/LATITUDE STRINGS TO INTEGERS
  var temp_lat = inp_latitude;             // decode (len(latitude),'(i)',latitude) temp_lat
  var temp_long = inp_longitude;           // decode (len(longitude),'(i)',longitude) 
  
	if ((central_long > 0) != (temp_long > 0)) 
	{ 
		temp_long = -temp_long;
	}
  
	if ((central_lat > 0) != (temp_lat > 0))
	{ 
		temp_lat = -temp_lat;
	}
	
//      CALCULATE THE NUMBER OF STATE-PLANE UNITS TO A LATITUDE DEGREE UNIT
//      (WHICH IS 1/1000000 OF A DEGREE SINCE THERE IS AN IMPLIED DECIMAL POINT).

	var lat_factor = num_miles_in_polar_circum * num_state_plane_units_in_mile / num_degrees_in_circum / num_implied_units_in_degree + 0.0;

//      CALCULATE THE NUMBER OF STATE-PLANE UNITS TO A LONGITUDE DEGREE UNIT AT
//      THE EQUATOR (WHICH IS 1/1000000 OF A DEGREE SINCE THERE IS AN IMPLIED
//      DECIMAL POINT).

	var long_factor = num_miles_in_equatorial_circum * num_state_plane_units_in_mile / num_degrees_in_circum / num_implied_units_in_degree + 0.0;

//      CALCULATE THE ROTATION BETWEEN THE CTS STATE-PLANAR SYSTEM AND THE
//      EARTH'S COORDINATE SYSTEM AT THE L.A. METROPOLITAN AREA.
//      CALCULATE THE X/Y COORDINATES.  CONVERT THE LONGITUDE AND LATITUDE SO
//      THAT 33 DEGREES LATITUDE AND 118 DEGREES LONGITUDE IS THE ORIGIN.  THE
//      ORIGIN IS APPROXIMATELY IN THE CENTER OF THE L.A. METROPOLITAN REGION.

	var real_long1 = (temp_long - central_long) + 0.0;
	var real_lat1 = temp_lat + 0.0;
	var conv_y = (real_lat1 - central_lat) * lat_factor;

//      BECAUSE THE EARTH IS ROUND, THE SIZE OF A LONGITUDE DEGREE CAN VARY AT
//      DIFFERENT LATITUDES.  THE SIZE OF THIS DEGREE IS EQUAL TO THE SIZE OF
//      A LONGITUDE DEGREE AT THE EQUATOR MULTIPLIED BY THE COSINE OF THE
//      LATITUDE.

	var conv_x = real_long1 * long_factor * Math.cos(real_lat1 *  radians_in_millionth_of_degree);

//      ROTATE AND TRANSLATE THE COORDINATE SYSTEM TO MATCH THE CTS
//      STATE-PLANAR SYSTEM.

	if (state_plane_rotation == 0.0) 
	{
		geo_y = conv_y + state_plane_offset_y + 0.0;
		geo_x = conv_x + state_plane_offset_x + 0.0;
    }
	else 
	{
		sin_rotation = sin(state_plane_rotation);
		cos_rotation = cos(state_plane_rotation);
		geo_y = conv_x * sin_rotation + conv_y * cos_rotation + state_plane_offset_y;
		geo_x = conv_x * cos_rotation - conv_y * sin_rotation + state_plane_offset_x;
    }
  
	if (state_plane_flip)
	{
		geo_x = - geo_x;
	}

	
	
	geo_y = geo_y|0;
	geo_x = geo_x|0;
	
	var coordArray = new Array();
	coordArray[0] = geo_y;
	coordArray[1] = geo_x;
	
	//document.main.fromgeox.value = geo_x;
	//document.main.fromgeoy.value = geo_y;
	
	
	//alert ("geo_x: " + geo_x + "\n" + "geo_y: " + geo_y);
	

	//document.theform.startpoint.value = "Start Point";
	//document.theform.endpoint.value = "geo_x";
	//document.convert.georesults.value = "Results Calculated";
  return coordArray;
  }
  
  //*******************************************************************************
//*
//*      MODULE:  CONV_GEO_TO_LONG_LAT
//*
//*      DESCRIPTION:
//*
//*           This routine converts a geographic X/Y coordinate to strings
//*           containing the latitude and longitude.
//*
//*******************************************************************************

function conv_geo_to_long_lat ( geo_x, geo_y, inp_longitude, inp_latitude) 
{

 var central_long = -74000000;
 var central_lat =  41000000;
 var state_plane_rotation = 0.0;
 var state_plane_flip = 0;                           //  state_plane_flip = .FALSE.
 var state_plane_offset_x = 0.0;
 var state_plane_offset_y =  0.0;
 var num_state_plane_units_in_mile = 240;

 var num_miles_in_polar_circum = 24859.82;
 var num_miles_in_equatorial_circum = 24901.55;
 var num_degrees_in_circum = 360.0;
 var num_implied_units_in_degree = 1000000.0;
//      CALCULATE THE DEGREE TO RADIAN CONVERSION FACTOR.

 var radians_in_millionth_of_degree = 3.141592654 / 180.0 / num_implied_units_in_degree;

//      CALCULATE THE SLOPES AND ANGLES OF THE CTS STATE-PLANAR COORDINATES AS
//      COMPARED TO THE LATITUDE/LONGITUDE COORDINATES.  THE SLOPES OF THE TWO
//      COORDINATE SYSTEMS WERE DERIVED BY TAKING THE X-Y VALUE OF TWO POINTS AT
//      OPPOSITE ENDS OF THE METROPOLITAN AREA.

//      CALCULATE THE NUMBER OF STATE-PLANE UNITS TO A LATITUDE DEGREE UNIT
//      (WHICH IS 1/1000000 OF A DEGREE SINCE THERE IS AN IMPLIED DECIMAL POINT).

	var lat_factor = num_miles_in_polar_circum / num_degrees_in_circum / num_implied_units_in_degree * num_state_plane_units_in_mile + 0.0;

//      CALCULATE THE NUMBER OF STATE-PLANE UNITS TO A LONGITUDE DEGREE UNIT AT
//      THE EQUATOR (WHICH IS 1/1000000 OF A DEGREE SINCE THERE IS AN IMPLIED
//      DECIMAL POINT).

	var long_factor = num_miles_in_equatorial_circum / num_degrees_in_circum / num_implied_units_in_degree *  num_state_plane_units_in_mile + 0.0;

//      ROTATE AND TRANSLATE THE COORDINATE SYSTEM TO MATCH THE EARTH'S
//      COORDINATE SYSTEM AT THE L.A. METROPOLITAN AREA.
 if (state_plane_flip)
 { 
   geo_x = -geo_x;
 }
 if (state_plane_rotation == 0.0) 
 {
	var conv_y = geo_y - state_plane_offset_y + 0.0;
	var conv_x = geo_x - state_plane_offset_x + 0.0;
   }
 else 
 {
	//        CALCULATE THE ROTATION BETWEEN THE CTS STATE-PLANAR SYSTEM AND THE
	//        EARTH'S COORDINATE SYSTEM AT THE L.A. METROPOLITAN AREA.
	var sin_rotation = Math.sin(state_plane_rotation);
	var cos_rotation = Math.cos(state_plane_rotation);
	var conv_y = ((geo_y - state_plane_offset_y)/sin_rotation + (state_plane_offset_x - geo_x)/cos_rotation) / (cos_rotation/sin_rotation + sin_rotation/cos_rotation);
	var conv_x = (geo_y - conv_y * cos_rotation - state_plane_offset_y) / sin_rotation;
   }

//      CALCULATE THE LATITUDE DEGREE.

	var real_lat1 = conv_y / lat_factor + central_lat;

//      BECAUSE THE EARTH IS ROUND, THE SIZE OF A LONGITUDE DEGREE CAN VARY AT
//      DIFFERENT LATITUDES.  THE SIZE OF THIS DEGREE IS EQUAL TO THE SIZE OF
//      A LONGITUDE DEGREE AT THE EQUATOR MULTIPLIED BY THE COSINE OF THE
//      LATITUDE.

	var real_long1 = conv_x / long_factor / Math.cos(real_lat1 * radians_in_millionth_of_degree) + central_long;
	var temp_lat = real_lat1;
	var temp_long = real_long1;
  
	if (temp_long < 0)
	{ 
		temp_long = -temp_long;
	}

	if (temp_lat < 0)
	{ 
		temp_lat = -temp_lat;
	}

//      CONVERT THE LONGITUDE/LATITUDE INTEGERS TO STRINGS

	//inp_latitude = temp_lat|0;     // encode (len(latitude),'(i<len(latitude)>)',latitude) temp_lat
	//inp_longitude = temp_long|0;   // encode (len(longitude),'(i<len(longitude)>)',longitude) temp_long

	var t_lat = temp_lat|0;
	var t_long = temp_long|0;
	
	inp_latitude = t_lat/num_implied_units_in_degree;     // encode (len(latitude),'(i<len(latitude)>)',latitude) temp_lat
	inp_longitude = -1*t_long/num_implied_units_in_degree;   // encode (len(longitude),'(i<len(longitude)>)',longitude) temp_long
	//alert("inp_latitude: " + inp_latitude + "\n" + "inp_longitude: " + inp_longitude);
	
	//truncate
	//int intInp_Latitude = inp_latitude;
	//int intInp_Longitude = inp_longitude;
	
	//inp_latitude = inp_latitude|0;
	//inp_longitude = inp_longitude|0;
	
	document.ItinOptions.origLat.value = inp_latitude;
	document.ItinOptions.origLong.value = inp_longitude;
	//alert("inp_longitude:" + inp_longitude + ", inp_latitude: " + inp_latitude);
	//document.convert.latlongresults.value = "Results Calculated";
 return 
}



