//*******************************************************************************
//***                                                                         ***
//***  This script uses latitude and longitude to calculate the distance      ***
//***  between two Postal Codes using javascript database array of ZIP        ***
//***  codes and their corresponding coordinates based on the Census 2000     ***
//***  U.S. Gazetteer Files from the U.S. Census  Bureau.                     ***
//***                                                                         ***
//***  PLEASE NOTE:                                                           ***
//***  South latitudes are negative, east longitudes are positive             ***
//***  In the arrays, longitude is listed first and latitude seconde          ***
//***                                                                         ***
//***  lon1, lat1 = Latitude and Longitude of point 1 (in decimal degrees)    ***
//***  lon2, lat2 = Latitude and Longitude of point 2 (in decimal degrees)    ***
//***  m_unit = desired unit of measurement for calculation                   ***
//***           M = miles                                                     ***
//***           K = kilometers                                                ***
//***           N = nautical miles                                            ***
//***                                                                         ***
//*******************************************************************************

var pi = 3.14159265358979323846;
codes = new Array();

function distance(lat1, lon1, lat2, lon2, m_unit) {
  	theta = lon1 - lon2;
  	dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
	dist = Math.acos(dist);
  	dist = rad2deg(dist);
  	final_distance = dist * 60 * 1.1515;
  	switch(m_unit.toUpperCase()) {
    	case "K":
      		final_distance = final_distance * 1.609344;
	  		break;
    	case "N":
      		final_distance = final_distance * 0.8684;
	  		break;
		}
	return final_distance.toFixed(2);
	}

//*******************************************************************************
//***           This function converts decimal degrees to radians             ***
//*******************************************************************************
function deg2rad(degrees) {
	return Number(degrees * pi / 180);
	}

//*******************************************************************************
//***           This function converts radians to decimal degrees             ***
//*******************************************************************************
function rad2deg(radians) {
	return Number(radians * 180 / pi);
	}

function get_coord(zip) {
	for (j=0;j<codes.length;j++) {
		if (zip == codes[j][0]) return j;
		}
	}

function find_it(zip1,zip2) {
	// Get location 1
	lon1 = codes[get_coord(zip1)][1];
	lat1 = codes[get_coord(zip1)][2];
	
	lon2 = codes[get_coord(zip2)][1];
	lat2 = codes[get_coord(zip2)][2];
	
	dist = distance(lat1, lon1, lat2, lon2, "M")
	dist = (isNaN(dist)) ? 0 : dist;
	
	return dist;
	}

function url_prep(txt) {
	while (txt.indexOf(" ")>0) {
  		txt = txt.replace(" ","+");
  		}
	return txt;
	}

function zip_info(code) {
	for (k=0;k<codes.length;k++) {
		if (codes[k][0]==code) return codes[k];
		}
	}

//*******************************************************************************
//***           This function cleans HTML code from the provided string       ***
//*******************************************************************************

function encode_html(str) {
    	 encodedHtml = escape(str);
    	 encodedHtml = encodedHtml.replace(/\//g,"%2F");
    	 encodedHtml = encodedHtml.replace(/\?/g,"%3F");
    	 encodedHtml = encodedHtml.replace(/=/g,"%3D");
    	 encodedHtml = encodedHtml.replace(/&/g,"%26");
    	 encodedHtml = encodedHtml.replace(/@/g,"%40");
    	 return encodedHtml;
  	 } 

//*******************************************************************************
//***         These functions retrieves variables from the querystring        ***
//*******************************************************************************

var argName;
var argValue;
var pairs;
var args;

function GetQuery()
	{
	args = new Array();
	query = location.search.substring(1);
	pairs = query.split("&");
	for (i=0;i<pairs.length;i++)
		{
		args[i] = pairs[i].split('=');
		}
	}
	
function Request(myVariable)
	{
	GetQuery();
	for (i=0;i<pairs.length;i++)
		{
		if (args[i][0]==myVariable) return encode_html(args[i][1]);
		}
	}

//*******************************************************************************
//***          This function adds a new JS function to the DOM object         ***
//*******************************************************************************

function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

// Undocument the lines below to test the code.
//
//document.writeln(distance(32.9697, -96.80322, 29.46786, -98.53506, "M") + " Miles<br>");
//document.writeln(distance(32.9697, -96.80322, 29.46786, -98.53506, "K") + " Kilometers<br>");
//document.writeln(distance(32.9697, -96.80322, 29.46786, -98.53506, "N") + " Nautical Miles<br>");