// JavaScript Document


function dms2dd(deg_in, min_in, sec_in, sign_in)

  {

	deg = parseFloat(Math.abs(deg_in));

	mint = parseFloat(Math.abs(min_in));

      sec = parseFloat(Math.abs(sec_in));

	if (isNaN(deg))

		deg = 0;

	if (isNaN(mint))

		mint = 0;

      if (isNaN(sec))

            sec = 0;



	angle = deg + (mint/60.) + (sec/3600.);



      sign = 1;



	if (sign_in == "South" || sign_in == "West")

		{
			sign = -1;
		}



	angle = angle * sign



	return(angle);

  }



function deg2rad(deg)

  {

	conv_factor = (2.0 * Math.PI)/360.0;

	return(deg * conv_factor);

  }



function rad2deg(rad)

  {

	conv_factor = 360.0/(2.0 * Math.PI);

	return(rad * conv_factor);

  }


// Main function for computing course and distance

function compute(start_ken,end_ken)

  {

   // Set initial value of the vertex to 0,0



   Lv = 0;

   Lov = 0;


   lat1_deg = start_ken.degLat;

   lat1_min = start_ken.minLat;

	lat1_sec = 0;

   lon1_deg = start_ken.degLong;

   lon1_min = start_ken.minLong;

	lon1_sec = 0;


   lat2_deg = end_ken.degLat;

   lat2_min = end_ken.minLat;

	lat2_sec = 0;

   lon2_deg = end_ken.degLong;

   lon2_min = end_ken.minLong;

	lon2_sec = 0;



   lat1_sign = "North";

   lat2_sign = "North";

   lon1_sign = "West";

   lon2_sign = "West";



   // Get angles & convert to radians

   lat1 = deg2rad(dms2dd(lat1_deg, lat1_min, lat1_sec, lat1_sign));

   lat2 = deg2rad(dms2dd(lat2_deg, lat2_min, lat2_sec, lat2_sign));

   lon1 = dms2dd(lon1_deg, lon1_min, lon1_sec, lon1_sign);

   lon2 = dms2dd(lon2_deg, lon2_min, lon2_sec, lon2_sign);


   dlon = lon2 - lon1;

   if (dlon > 180)

   	{dlon = dlon - 360;}

   if (dlon< -180)

   	{dlon = 360 + dlon;}

   rdlon = deg2rad(dlon);



   // Calculate Course
   tancourse = Math.sin(rdlon) /((Math.cos(lat1)*Math.tan(lat2)) -

                 (Math.sin(lat1)*Math.cos(rdlon)))



   course = rad2deg(Math.atan(tancourse));

   if (dlon == 0 && lat1 > lat2)

      {course = 180;}

   if (dlon < 0 && course < 0)

      {course = course + 360;}

   else 

      {if ((dlon< 0 && course > 0) || (dlon > 0 && course< 0))

         {course = course + 180;}

      }

	return Math.round(course);

  }


