
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

gmap = "http://maps.google.com/maps?f=q&hl=en&geocode=&q=";
gmaps = "";
bmap = "http://www.bing.com/maps/default.aspx?q=";
bmaps = "&mkt=en-US&FORM=BYFD";
mapPrefix = bmap;
mapSuffix = bmaps;

clinic = [
    {
      "name":"US Healthworks Bellevue",
      "info":"http://www.ushealthworks.com/Bellevue-CenterInfo.html",
      "street":"1925 140th Ave. N.E.",
      "phone":"425-865-8060",
      "hours":"Mon-Fri: 7 a.m.-7 p.m.,		Sat: 9 a.m.-5 p.m., Sun: Closed",
      "city":"Bellevue",
      "state":"WA",
      "zip":"98005",
      "distance":-1
    },
    {
      "name":"Overlake Urgent Care Center",
      "info":"http://www.overlakehospital.org/programs.aspx?id=390",
      "street":"5708 E. Lake Sammamish Parkway S.E.",
      "phone":"425-688-5777",
      "hours":"24 hrs/7 days",
      "city":"Issaquah",
      "state":"WA",
      "zip":"98027",
      "distance":-1
    },
    {
      "name":"Valley Medical Center Newcastle Urgent Care",
      "info":"http://www.valleymed.org/Locations.htm?intClinicID=50",
      "street":"7203 129th Ave. S.E., Ste. 100",
      "phone":"253-395-1960",
      "hours":"Mon-Fri: 8 a.m.-8 p.m., 		Sat and Sun: 8 a.m.-4 p.m.",
      "city":"Newcastle",
      "state":"WA",
      "zip":"98056",
      "distance":-1
    },
    {
      "name":"Meadowbrook Urgent Care and Specialty Clinic",
      "info":"http://www.meadowbrookurgentcare.com/",
      "street":"209 Main Ave. S., Ste. 115",
      "phone":"425-831-2380",
      "hours":"Mon-Sat: 10 a.m.-8 p.m.,		Sun: 12 p.m.-5 p.m.",
      "city":"North Bend",
      "state":"WA",
      "zip":"98045",
      "distance":-1
    },
    {
      "name":"Redmond Urgent Care Evergreen Medical Group",
      "info":"http://www.evergreenhealthcare.org/primarycare/emg/redmond_urgent_care.htm",
      "street":"8301 161st Ave. N.E., Ste. 105",
      "phone":"425-883-3333",
      "hours":"Mon-Fri: 9 a.m.-8 p.m.,		Sat and Sun: 9 a.m.-5 p.m.",
      "city":"Redmond",
      "state":"WA",
      "zip":"98052",
      "distance":-1
    },
    {
      "name":"US Healthworks Redmond",
      "info":"http://www.ushealthworks.com/Redmond-CenterInfo.html",
      "street":"15937 Redmond Way",
      "phone":"425-882-0100",
      "hours":"Mon-Fri: 7 a.m.-6 p.m.,		Sat and Sun: Closed",
      "city":"Redmond",
      "state":"WA",
      "zip":"98052",
      "distance":-1
    },
    {
      "name":"Everett Clinic Walk-In Clinic at Everett",
      "info":"http://www.everettclinic.com/Walk_In_Clinic.ashx",
      "street":"3927 Rucker Ave.",
      "phone":"425-339-5441",
      "hours":"Mon-Fri: 8 a.m.-8 p.m., 		Sat and Sun: 9 a.m.-3 p.m.",
      "city":"Everett",
      "state":"WA",
      "zip":"98201",
      "distance":-1
    },
    {
      "name":"Everett Clinic Walk-In Clinic at Silver Lake",
      "info":"http://www.everettclinic.com/Walk_In_Clinic.ashx",
      "street":"1818 121st St. S.E.",
      "phone":"425-357-3305",
      "hours":"Mon-Fri: 8 a.m.-8 p.m., 		Sat and Sun: 9 a.m.-3 p.m.",
      "city":"Everett",
      "state":"WA",
      "zip":"98208",
      "distance":-1
    },
    {
      "name":"Providence Physician Group Mill Creek Walk-In Clinic",
      "info":"http://www.providence.org/Everett/ProvPG/e70Mill.htm",
      "street":"12800 Bothell Everett Hwy., Ste. 120",
      "phone":"425-316-5150",
      "hours":"Mon-Sun: 8 a.m.-7:30 p.m.",
      "city":"Everett",
      "state":"WA",
      "zip":"98208",		
      "distance":-1
    },
    {
      "name":"Sunrise Family Medical Care",
      "info":"http://www.bing.com/search?q=Sunrise+Family+Medical+Care+Everett",
      "street":"7601 Evergreen Way, Ste. B6",
      "phone":"425-353-9191",
      "hours":"Mon-Thu: 9 a.m.-7 p.m., Fri: 8 a.m.-6 p.m.,		Sat: 9 a.m.-3 p.m., Sun: Closed",
      "city":"Everett",
      "state":"WA",
      "zip":"98203",
      "distance":-1
    },
    {
      "name":"US Healthworks Lynnwood",
      "info":"http://www.ushealthworks.com/Lynnwood-CenterInfo.html",
      "street":"4320 196th St. S.W., Ste D",
      "phone":"425-774-8758",
      "hours":"Mon-Fri: 8 a.m.-8 p.m., 		Sat and Sun: 9 a.m.-5 p.m.",
      "city":"Lynnwood",
      "state":"WA",
      "zip":"98036",
      "distance":-1
    },
    {
      "name":"Walk-In Clinic at Lake Serene",
      "info":"http://www.lakesereneclinic.com/",
      "street":"3501 Shelby Road, Ste. B",
      "phone":"425-742-9119",
      "hours":"Mon-Sun: 9 a.m.-8 p.m.,		Holidays: 11 a.m.-4 p.m.",
      "city":"Lynnwood",
      "state":"WA",
      "zip":"98037",
      "distance":-1
    },
    {
      "name":"Everett Clinic Walk-In Clinic at Marysville",
      "info":"http://www.everettclinic.com/Walk_In_Clinic.ashx",
      "street":"4420 76th St. N.E.",
      "phone":"360-651-7497",
      "hours":"Mon-Fri: 8 a.m.-8 p.m., 		Sat and Sun: 9 a.m.-3 p.m.",
      "city":"Marysville",
      "state":"WA",
      "zip":"98270",
      "distance":-1
    },
    {
      "name":"Everett Clinic Mill Creek",
      "info":"http://www.everettclinic.com/Walk_In_Clinic.ashx",
      "street":"15418 Main St., Unit M303",
      "phone":"425-225-8005",
      "hours":"Mon-Fri: 8 a.m.-8 p.m., 		Sat and Sun: 9 a.m.-3 p.m.",
      "city":"Mill Creek",
      "state":"WA",
      "zip":"98012",
      "distance":-1
    },
    {
      "name":"Providence Physician Group Monroe Walk-In Clinic",
      "info":"http://www.providence.org/Everett/provpg/e50Monroe.htm",
      "street":"14692 179th Ave. S.E., Ste. 500",
      "phone":"360-805-4790",
      "hours":"Mon-Sat: 8 a.m.-7:30 p.m.,		Su: Closed",
      "city":"Monroe",
      "state":"WA",
      "zip":"98272",
      "distance":-1
    },
    {
      "name":"Everett Clinic Walk-In Clinic at Harbour Pointe",
      "info":"http://www.everettclinic.com/Walk_In_Clinic.ashx",
      "street":"4410 106th Ste. S.W.",
      "phone":"425-493-6013",
      "hours":"Mon-Fri: 8 a.m.-8 p.m. 		Sat and Sun: 9 a.m.-3 p.m.",
      "city":"Mukilteo",
      "state":"WA",
      "zip":"98275",
      "distance":-1
    },
    {
      "name":"Woodinville Urgent Care Evergreen Medical Group",
      "info":"http://www.evergreenhealthcare.org/primarycare/emg/woodinville_urgent_care.htm",
      "street":"17000 140th Ave. N.E., Unit 101",
      "phone":"425-488-2273",
      "hours":"Mon-Fri: 9 a.m.-4:30 p.m.,		Sat and Sun: 9 a.m.-4 p.m.",
      "city":"Woodinville",
      "state":"WA",
      "zip":"98072",
      "distance":-1
    },
    {
      "name":"US Healthworks Denny Way",
      "info":"http://www.ushealthworks.com/Seattle%20(Denny-Fairview)-CenterInfo.html",
      "street":"1151 Denny Way",
      "phone":"206-682-7418",
      "hours":"Mon-Fri: 7 a.m.-6 p.m.,		Sat: 9 a.m.-5 p.m., Sun: Closed",
      "city":"Seattle",
      "state":"WA",
      "zip":"98109",
      "distance":-1
    },
    {
      "name":"US Healthworks Greenlake",
      "info":"http://www.ushealthworks.com/Seattle%20(Aurora)-CenterInfo.html",
      "street":"8313 Aurora Ave. N.",
      "phone":"206-784-0737",
      "hours":"Mon-Fri: 8 a.m.-6 p.m.,		Sat: 10 a.m.-4 p.m., Sun: Closed",
      "city":"Seattle",
      "state":"WA",
      "zip":"98103",
      "distance":-1
    },
    {
      "name":"Valley Medical Center Covington Urgent Care Clinic",
      "info":"http://www.valleymed.org/Locations.htm?intClinicID=44",
      "street":"16850 SE 272nd St., Ste. 210",
      "phone":"253-395-2006",
      "hours":"Mon-Fri: 8 a.m.-8 p.m.,  		Sat and Sun: 8 a.m.-4 p.m.",
      "city":"Covington",
      "state":"WA",
      "zip":"98042",
      "distance":-1
    },
    {
      "name":"MultiCare Covington Urgent Care",
      "info":"http://www.multicare.org/home/multicare-covington-clinic",
      "street":"17700 S.E. 272nd St.",
      "phone":"253-372-7020",
      "hours":"Mon-Fri: 7 a.m.-10 p.m.,		Sat and Sun: 7 a.m.-6 p.m.",
      "city":"Covington",
      "state":"WA",
      "zip":"98042",
      "distance":-1
    },
    {
      "name":"MultiCare Kent Urgent Care",
      "info":"http://www.multicare.org/home/multicare-kent-clinic",
      "street":"222 State Ave. N.",
      "phone":"253-372-7788",
      "hours":"Mon-Fri: 9 a.m.-9 p.m.,		Sat and Sun: 9 a.m.-5 p.m.",
      "city":"Kent",
      "state":"WA",
      "zip":"98030",
      "distance":-1
    },
    {
      "name":"US Healthworks Kent",
      "info":"http://www.ushealthworks.com/Kent-CenterInfo.html",
      "street":"24031 104th Ave. S.E.",
      "phone":"253-852-1824",
      "hours":"Mon-Fri: 8 a.m.-7 p.m.,		Sat: 10a-4p, Sun: Closed",
      "city":"Kent",
      "state":"WA",
      "zip":"98030",
      "distance":-1
    },
    {
      "name":"Allcare Medical Clinic",
      "info":"http://allcaremedicalclinic.net/default.aspx",
      "street":"148 Park Ave. N.",
      "phone":"425-255-0055",
      "hours":"Mon-Fri: 9 a.m.-6 p.m.,		Sat: 10 a.m.-2 p.m., Sun: Closed",
      "city":"Renton",
      "state":"WA",
      "zip":"98055",
      "distance":-1
    },
    {
      "name":"Highlands Urgent Care Clinic",
      "info":"http://www.valleymed.org/Locations.htm?intClinicID=43",
      "street":"451 Duvall Ave. N.E., Ste. 120",
      "phone":"425-656-5518",
      "hours":"Mon-Fri: 8 a.m.-8 p.m.,		Sat and Sun: 8 a.m.-4 p.m.",
      "city":"Renton",
      "state":"WA",
      "zip":"98059",
      "distance":-1
    },
    {
      "name":"Valley Medical Center Urgent Care",
      "info":"http://www.valleymed.org/Locations.htm?intClinicID=60",
      "street":"10555 S.E. Carr Rd., Ste. M",
      "phone":"425-656-4270",
      "hours":"Mon-Fri: 8 a.m.-8 p.m.,		Sat and Sun: 8 a.m.-4 p.m.",
      "city":"Renton",
      "state":"WA",
      "zip":"98055",
      "distance":-1
    }
 ];
 
pharm = [
  {
    "name":"Walgreens",
    "info":"http://www.walgreens.com/",
    "street":"647 140th Ave NE",
    "phone":"425.603.1438",
    "hours":"Drive Thru: yes",
    "city":"Bellevue",
    "state":"WA",
    "zip":"98005",
    "distance":-1
  },
  {
    "name":"Walgreens",
    "info":"http://www.walgreens.com/",
    "street":"6300 E Lake Sammamish Pkwy SE",
    "phone":"425.369.0265",
    "hours":"Drive Thru: yes",
    "city":"Issaquah",
    "state":"WA",
    "zip":"98029",
    "distance":-1
  },
  {
    "name":"Bartell Drugs",
    "info":"http://www.bartelldrugs.com",
    "street":"653 156th Ave NE",
    "phone":"425.614.9127",
    "hours":"Drive Thru: yes",
    "city":"Bellevue",
    "state":"WA",
    "zip":"98007",
    "distance":-1
  },
  {
    "name":"Walgreens",
    "info":"http://www.walgreens.com/",
    "street":"20725 Highway 99",
    "phone":"425.712.0512",
    "hours":"Drive Thru: yes",
    "city":"Lynnwood",
    "state":"WA",
    "zip":"98036",
    "distance":-1
  },
  {
    "name":"Walgreens",
    "info":"http://www.walgreens.com/",
    "street":"6807 Evergreen Way",
    "phone":"425.438.9380",
    "hours":"Drive Thru: yes",
    "city":"Everett",
    "state":"WA",
    "zip":"98303",
    "distance":-1
  },
  {
    "name":"Walgreens",
    "info":"http://www.walgreens.com/",
    "street":"5409 15th Ave NW",
    "phone":"206.781.0056",
    "hours":"Drive Thru: yes",
    "city":"Seattle",
    "state":"WA",
    "zip":"98107",
    "distance":-1
  },
  {
    "name":"Bartell Drugs",
    "info":"http://www.bartelldrugs.com",
    "street":"600 1st Ave N",
    "phone":"206.284.1354",
    "hours":"No Drive Thru",
    "city":"Seattle",
    "state":"WA",
    "zip":"98109",
    "distance":-1
  },
  {
    "name":"Walgreens",
    "info":"http://www.walgreens.com/",
    "street":"275 Ranier Ave S",
    "phone":"425.277.0212",
    "hours":"Drive Thru: yes",
    "city":"Renton",
    "state":"WA",
    "zip":"98055",
    "distance":-1
  },
  {
    "name":"Walgreens",
    "info":"http://www.walgreens.com/",
    "street":"14656 Ambaum Blvd SW",
    "phone":"206.901.1816",
    "hours":"Drive Thru: yes",
    "city":"Burien",
    "state":"WA",
    "zip":"98166",
    "distance":-1
  },
  {
    "name":"Walgreens",
    "info":"http://www.walgreens.com/",
    "street":"23003 Pacific Hwy S",
    "phone":"206.870.1832",
    "hours":"Drive Thru: yes",
    "city":"Des Moines",
    "state":"WA",
    "zip":"98198",
    "distance":-1
  },
  {
    "name":"Walgreens",
    "info":"http://www.walgreens.com/",
    "street":"25605 104th Ave SE",
    "phone":"253.813.6968",
    "hours":"Drive Thru: yes",
    "city":"Kent",
    "state":"WA",
    "zip":"98031",
    "distance":-1
  },
  {
    "name":"Rite Aid",
    "info":"http://www.riteaid.com/pharmacy/",
    "street":"2131 SW 336th St",
    "phone":"253.952.2803",
    "hours":"Drive Thru: yes",
    "city":"Federal Way",
    "state":"WA",
    "zip":"98023",
    "distance":-1
  },
  {
    "name":"Walgreens",
    "info":"http://www.walgreens.com/",
    "street":"12405 NE 85th St",
    "phone":"425.822.9202",
    "hours":"Drive Thru: yes",
    "city":"Kirkland",
    "state":"WA",
    "zip":"98033",
    "distance":-1
  }
];
 zipLoc = {
    98005 : {
      "lng":-122.166288,
      "lat":47.614961
    },
    98007 : {
      "lng":-122.142572,
      "lat":47.617443
    },
    98012 : {
      "lng":-122.197238,
      "lat":47.840068
    },
    98023 : { 
      "lng":-122.36123,
      "lat":47.310358 
    },
    98027 : {
      "lng":-122.010699,
      "lat":47.497419 
    },
    98029 : { 
      "lng":-122.025754,
      "lat":47.565587 
    },
    98030 : {
      "lng":-122.210927,
      "lat":47.386578 
    },
    98031 : { 
      "lng":-122.193184,
      "lat":47.388004 
    },
    98033 : { 
      "lng":-122.189442,
      "lat":47.678597 
    },
    98036 : {
      "lng":-122.285481,
      "lat":47.804869 
    },
    98037 : {
      "lng":-122.282139,
      "lat":47.850532 
    },
    98040 : {
      "lng":-122.228083,
      "lat":47.56025
    },
    98042 : {
      "lng":-122.120615,
      "lat":47.368044 
    },
    98045 : {
      "lng":-121.757142,
      "lat":47.475546 
    },
    98052 : {
      "lng":-122.123242,
      "lat":47.671796 
    },
    98055 : {
      "lng":-122.207484,
      "lat":47.464759
    },
    98056 : {
      "lng":-122.181942,
      "lat":47.507336 
    },
    98059 : {
      "lng":-122.115699,
      "lat":47.505822 
    },
    98072 : {
      "lng":-122.127087,
      "lat":47.768384 
    },
    98102 : { 
      "lng":-122.320993,
      "lat":47.63025 
    },
    98103 : {
      "lng":-122.342621,
      "lat":47.67335 
    },
    98107 : { 
      "lng":-122.37626,
      "lat":47.67012
    },
    98109 : {
      "lng":-122.347615,
      "lat":47.633875 
    },
    98166 : { 
      "lng":-121.803388,
      "lat":47.432251 
    },
    98198 : { 
      "lng":-121.803388,
      "lat":47.432251 
    },
    98201 : {
      "lng":-122.200571,
      "lat":47.988431 
    },
    98203 : {
      "lng":-122.221846,
      "lat":47.941937 
    },
    98208 : {
      "lng":-122.198722,
      "lat":47.894822 
    },
    98270 : {
      "lng":-122.156168,
      "lat":48.065639 
    },
    98272 : {
      "lng":-121.947376,
      "lat":47.85853 
    },
    98275 : {
      "lng":-122.301906,
      "lat":47.919896 
    },
    98303 : { 
      "lng":-122.706706,
      "lat":47.156263 
    }
 }
 
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
/*  Latitude/longitude spherical geodesy formulae & scripts (c) Chris Veness 2002-2009            */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */

/*
 * Use Haversine formula to calculate distance (in km) between two points specified by 
 * latitude/longitude (in numeric degrees)
 *
 * from: Haversine formula - R. W. Sinnott, "Virtues of the Haversine",
 *       Sky and Telescope, vol 68, no 2, 1984
 *       http://www.census.gov/cgi-bin/geo/gisfaq?Q5.1
 *
 * example usage from form:
 *   result.value = LatLon.distHaversine(lat1.value.parseDeg(), long1.value.parseDeg(), 
 *                                       lat2.value.parseDeg(), long2.value.parseDeg());
 * where lat1, long1, lat2, long2, and result are form fields
 */
LatLon.distHaversine = function(lat1, lon1, lat2, lon2) {
  var R = 6371; // earth's mean radius in km
  var dLat = (lat2-lat1).toRad();
  var dLon = (lon2-lon1).toRad();
  lat1 = lat1.toRad(), lat2 = lat2.toRad();

  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
          Math.cos(lat1) * Math.cos(lat2) * 
          Math.sin(dLon/2) * Math.sin(dLon/2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  var d = R * c;
  return d;
}


/*
 * Use Law of Cosines to calculate distance (in km) between two points specified by latitude/longitude 
 * (in numeric degrees).
 */
LatLon.distCosineLaw = function(lat1, lon1, lat2, lon2) {
  var R = 6371; // earth's mean radius in km
  var d = Math.acos(Math.sin(lat1.toRad())*Math.sin(lat2.toRad()) +
                    Math.cos(lat1.toRad())*Math.cos(lat2.toRad())*Math.cos((lon2-lon1).toRad())) * R;
  return d;
}


/*
 * calculate (initial) bearing between two points
 *   see http://williams.best.vwh.net/avform.htm#Crs
 */
LatLon.bearing = function(lat1, lon1, lat2, lon2) {
  lat1 = lat1.toRad(); lat2 = lat2.toRad();
  var dLon = (lon2-lon1).toRad();

  var y = Math.sin(dLon) * Math.cos(lat2);
  var x = Math.cos(lat1)*Math.sin(lat2) -
          Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
  return Math.atan2(y, x).toBrng();
}


/*
 * calculate midpoint of great circle line between p1 & p2.
 *   see http://mathforum.org/library/drmath/view/51822.html for derivation
 */
LatLon.midPoint = function(lat1, lon1, lat2, lon2) {
  lat1 = lat1.toRad(); 
  lat2 = lat2.toRad();
  var dLon = (lon2-lon1).toRad();

  var Bx = Math.cos(lat2) * Math.cos(dLon);
  var By = Math.cos(lat2) * Math.sin(dLon);

  lat3 = Math.atan2(Math.sin(lat1)+Math.sin(lat2),
                    Math.sqrt((Math.cos(lat1)+Bx)*(Math.cos(lat1)+Bx) + By*By ) );
  lon3 = lon1.toRad() + Math.atan2(By, Math.cos(lat1) + Bx);

  if (isNaN(lat3) || isNaN(lon3)) return null;
  return new LatLon(lat3.toDeg(), lon3.toDeg());
}


/*
 * calculate destination point given start point, initial bearing (deg) and distance (km)
 *   see http://williams.best.vwh.net/avform.htm#LL
 */
LatLon.prototype.destPoint = function(brng, d) {
  var R = 6371; // earth's mean radius in km
  var lat1 = this.lat.toRad(), lon1 = this.lon.toRad();
  brng = brng.toRad();

  var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + 
                        Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
  var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), 
                               Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));
  lon2 = (lon2+Math.PI)%(2*Math.PI) - Math.PI;  // normalise to -180...+180

  if (isNaN(lat2) || isNaN(lon2)) return null;
  return new LatLon(lat2.toDeg(), lon2.toDeg());
}


/*
 * calculate final bearing arriving at destination point given start point, initial bearing and distance
 */
LatLon.prototype.finalBrng = function(brng, d) {
  var p1 = this, p2 = p1.destPoint(brng, d);
  // get reverse bearing point 2 to point 1
  var rev = LatLon.bearing(p2.lat, p2.lon, p1.lat, p1.lon);
  // & reverse it by adding 180°
  var brng = (rev + 180) % 360;
  return brng;
}


/*
 * calculate distance, bearing, destination point on rhumb line
 *   see http://williams.best.vwh.net/avform.htm#Rhumb
 */
LatLon.distRhumb = function(lat1, lon1, lat2, lon2) {
  var R = 6371; // earth's mean radius in km
  var dLat = (lat2-lat1).toRad(), dLon = Math.abs(lon2-lon1).toRad();
  var dPhi = Math.log(Math.tan(lat2.toRad()/2+Math.PI/4)/Math.tan(lat1.toRad()/2+Math.PI/4));
  var q = (Math.abs(dLat) > 1e-10) ? dLat/dPhi : Math.cos(lat1.toRad());
  // if dLon over 180° take shorter rhumb across 180° meridian:
  if (dLon > Math.PI) dLon = 2*Math.PI - dLon;
  var d = Math.sqrt(dLat*dLat + q*q*dLon*dLon); 
  return d * R;
}


LatLon.brngRhumb = function(lat1, lon1, lat2, lon2) {
  var dLon = (lon2-lon1).toRad();
  var dPhi = Math.log(Math.tan(lat2.toRad()/2+Math.PI/4)/Math.tan(lat1.toRad()/2+Math.PI/4));
  if (Math.abs(dLon) > Math.PI) dLon = dLon>0 ? -(2*Math.PI-dLon) : (2*Math.PI+dLon);
  return Math.atan2(dLon, dPhi).toBrng();
}


LatLon.prototype.destPointRhumb = function(brng, dist) {
  var R = 6371; // earth's mean radius in km
  var d = parseFloat(dist)/R;  // d = angular distance covered on earth's surface
  var lat1 = this.lat.toRad(), lon1 = this.lon.toRad();
  brng = brng.toRad();

  var lat2 = lat1 + d*Math.cos(brng);
  var dLat = lat2-lat1;
  var dPhi = Math.log(Math.tan(lat2/2+Math.PI/4)/Math.tan(lat1/2+Math.PI/4));
  var q = (Math.abs(dLat) > 1e-10) ? dLat/dPhi : Math.cos(lat1);
  var dLon = d*Math.sin(brng)/q;
  // check for some daft bugger going past the pole
  if (Math.abs(lat2) > Math.PI/2) lat2 = lat2>0 ? Math.PI-lat2 : -(Math.PI-lat2);
  lon2 = (lon1+dLon+Math.PI)%(2*Math.PI) - Math.PI;
 
  if (isNaN(lat2) || isNaN(lon2)) return null;
  return new LatLon(lat2.toDeg(), lon2.toDeg());
}


/*
 * construct a LatLon object: arguments in numeric degrees
 *
 * note all LatLong methods expect & return numeric degrees (for lat/long & for bearings)
 */
function LatLon(lat, lon) {
  this.lat = lat;
  this.lon = lon;
}


/*
 * represent point {lat, lon} in standard representation
 */
LatLon.prototype.toString = function() {
  return this.lat.toLat() + ', ' + this.lon.toLon();
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */

// extend String object with method for parsing degrees or lat/long values to numeric degrees
//
// this is very flexible on formats, allowing signed decimal degrees, or deg-min-sec suffixed by 
// compass direction (NSEW). A variety of separators are accepted (eg 3º 37' 09"W) or fixed-width 
// format without separators (eg 0033709W). Seconds and minutes may be omitted. (Minimal validation 
// is done).

String.prototype.parseDeg = function() {
  if (!isNaN(this)) return Number(this);                 // signed decimal degrees without NSEW

  var degLL = this.replace(/^-/,'').replace(/[NSEW]/i,'');  // strip off any sign or compass dir'n
  var dms = degLL.split(/[^0-9.]+/);                     // split out separate d/m/s
  for (var i in dms) if (dms[i]=='') dms.splice(i,1);    // remove empty elements (see note below)
  switch (dms.length) {                                  // convert to decimal degrees...
    case 3:                                              // interpret 3-part result as d/m/s
      var deg = dms[0]/1 + dms[1]/60 + dms[2]/3600; break;
    case 2:                                              // interpret 2-part result as d/m
      var deg = dms[0]/1 + dms[1]/60; break;
    case 1:                                              // decimal or non-separated dddmmss
      if (/[NS]/i.test(this)) degLL = '0' + degLL;       // - normalise N/S to 3-digit degrees
      var deg = dms[0].slice(0,3)/1 + dms[0].slice(3,5)/60 + dms[0].slice(5)/3600; break;
    default: return NaN;
  }
  if (/^-/.test(this) || /[WS]/i.test(this)) deg = -deg; // take '-', west and south as -ve
  return deg;
}
// note: whitespace at start/end will split() into empty elements (except in IE)


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */

// extend Number object with methods for converting degrees/radians

Number.prototype.toRad = function() {  // convert degrees to radians
  return this * Math.PI / 180;
}

Number.prototype.toDeg = function() {  // convert radians to degrees (signed)
  return this * 180 / Math.PI;
}

Number.prototype.toBrng = function() {  // convert radians to degrees (as bearing: 0...360)
  return (this.toDeg()+360) % 360;
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */

// extend Number object with methods for presenting bearings & lat/longs

Number.prototype.toDMS = function() {  // convert numeric degrees to deg/min/sec
  var d = Math.abs(this);  // (unsigned result ready for appending compass dir'n)
  d += 1/7200;  // add ½ second for rounding
  var deg = Math.floor(d);
  var min = Math.floor((d-deg)*60);
  var sec = Math.floor((d-deg-min/60)*3600);
  // add leading zeros if required
  if (deg<100) deg = '0' + deg; if (deg<10) deg = '0' + deg;
  if (min<10) min = '0' + min;
  if (sec<10) sec = '0' + sec;
  return deg + '\u00B0' + min + '\u2032' + sec + '\u2033';
}

Number.prototype.toLat = function() {  // convert numeric degrees to deg/min/sec latitude
  return this.toDMS().slice(1) + (this<0 ? 'S' : 'N');  // knock off initial '0' for lat!
}

Number.prototype.toLon = function() {  // convert numeric degrees to deg/min/sec longitude
  return this.toDMS() + (this>0 ? 'E' : 'W');
}

Number.prototype.toPrecision = function(fig) {  // override toPrecision method with one which displays 
  if (this == 0) return 0;                      // trailing zeros in place of exponential notation
  var scale = Math.ceil(Math.log(this)*Math.LOG10E);
  var mult = Math.pow(10, fig-scale);
  return Math.round(this*mult)/mult;
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */


  var results;
  var moreResults;
  var pharmResults;
  var morePharmResults;
  
  function onloadinit()
  {
    var z = readCookie("zip");
    if (z)
    {
      $("zipCode").value = z;
    }
    
    results = document.getElementById("results");
    moreResults = document.getElementById("moreResults");
    pharmResults = document.getElementById("pharmResults");
    morePharmResults = document.getElementById("morePharmResults");

    actOnZip(document.getElementById("zipCode"));
  }
  
  function actOnZip(src)
  {
    var re = new RegExp(/\b\d{5}\b/);
    var zip = src.value;
    var m = re.exec(zip);
    if (m != null)
    {
      createCookie("zip", src.value, 365);
      addScript("http://ws.geonames.org/postalCodeLookupJSON?postalcode=" + zip + "&country=US&callback=zipresult");
    }
  }
  
  function zipresult(place)
  {
    var p = place.postalcodes.first();
    
    if (place.postalcodes.length == 0)
    {
      results.innerHTML = "<p>No results were found for " + document.getElementById("zipCode").value + ".";
      return;
    }
 
    results.innerHTML = "<p>These urgent care facilities are near  " + p.placeName + ".</p>";
    moreResults.innerHTML = "&nbsp;";
   
    var here = new LatLon(p.lat, p.lng);
    
    for (var i=0; i < clinic.length; i++)
    {
      var c = clinic[i];
      var cl = zipLoc[c.zip]
      c.distance = LatLon.distHaversine(p.lat, p.lng, cl.lat, cl.lng);
    }
    
    clinic.sort(function(a, b) { return a.distance - b.distance; });
    
    for (var i=0; i < clinic.length; i++)
    {
      var c = clinic[i];
      var te = i<3 ? results : moreResults;
 
      te.innerHTML +=
        "<p class=\"navloc\"><a href=\"" + mapPrefix + c.street + ", " + c.city + " " + c.state + " " + c.zip + mapSuffix +
        "\" class=\"dist\">" + kmToMiles(c.distance) + " miles</a> " + 
        "<a href=\"" + c.info + "\">" + c.name + "</a></p><p class=\"navhours\"> " + c.hours + "</p>";
                              
    }
    
    pharmResults.innerHTML = "<p>These all hours pharmacies are near  " + p.placeName + ".</p>";
    morePharmResults.innerHTML = "&nbsp;";
    
    for (var i=0; i < pharm.length; i++)
    {
      var c = pharm[i];
      var cl = zipLoc[c.zip]
      c.distance = LatLon.distHaversine(p.lat, p.lng, cl.lat, cl.lng);
    }
    
    pharm.sort(function(a, b) { return a.distance - b.distance; });
    
    for (var i=0; i < pharm.length; i++)
    {
      var c = pharm[i];
      var te = i<3 ? pharmResults : morePharmResults;
 
      te.innerHTML +=
        "<p class=\"navloc\"><a href=\"" + mapPrefix + c.street + ", " + c.city + " " + c.state + " " + c.zip + mapSuffix +
        "\" class=\"dist\">" + kmToMiles(c.distance) + " miles</a> " + 
        "<a href=\"" + c.info + "\">" + c.name + "</a></p><p class=\"navhours\"> " + c.hours + "</p>";
    }
  }
 
  function kmToMiles(km)
  {
    return (km * 0.621371192234848).toFixed(1);
  }
  
  function addScript(url) {
    var script = document.createElement('script');
    script.src = url;
    document.body.appendChild(script);
  }
  
  
  function toggleDisplayAfter(element)
	{
	  var el = Element.nextSiblings(element)[0];
		if (el.className == 'hide')
		{
			el.className = 'show';
			element.className = 'showing';
		}
		else
		{
			el.className = 'hide';
			element.className = 'hiding';
		}
	}