// Latitude and Longitude input form client side check functions
// Copyright: 2005 Delbert Matlock
// No use without permission

var LatLonFormName;
var LatLonInError = false;

function DecToDeg(DecVal) {

   var WorkVal = 0;
   var Deg = 0;
   var Min = 0;
   var Sec = 0;
   var Dir = 0;

   if (DecVal < 0) {
      Dir = -1;
      WorkVal = -DecVal;
   } else {
      Dir = 1;
      WorkVal = DecVal;
   }

   Deg = Math.floor(WorkVal);
   WorkVal = (WorkVal - Deg) * 60;

   Min = Math.floor(WorkVal);
   WorkVal = (WorkVal - Min) * 60;

   Sec = Math.round(WorkVal * 100, 2) / 100;

   return [Deg, Min, Sec, Dir];   

}

function LatLonBuildMapLink(BuildState) {
   var fo = document.forms[LatLonFormName];
   var OutTxt = '';
   var Lat = fo['lat_dec'].value;
   var Lon = fo['lon_dec'].value;

   if (BuildState && Lat.length > 0 && Lon.length > 0) {
      var GUrlM = 'http://maps.google.com/maps?q=' + Lat + ',' + Lon + '&amp;ll=' + Lat + ',' + Lon + '&amp;spn=0.1,0.1';

      OutTxt = ' Map it with ';
      OutTxt += '<a target="_blank" href="' + GUrlM + '">';
      OutTxt += 'Google</a>';
      OutTxt += ',<br>';
      OutTxt += '<a target="_blank"';
      //OutTxt += ' onMouseOver="javascript:LatLonBuildMapLink(true)"';
      OutTxt += ' href="';
      OutTxt += 'http://www.mapquest.com/maps/map.adp';
      OutTxt += '?searchtype=address&formtype=latlong&latlongtype=decimal';
      OutTxt += '&latitude=' + Lat;
      OutTxt += '&longitude=' + Lon;
      OutTxt += '">MapQuest</a>';
      OutTxt += ', or ';
      OutTxt += '<a target="_blank" href="';
      OutTxt += 'http://www.multimap.com/map/browse.cgi?icon=x';
      OutTxt += '&lat=' + Lat;
      OutTxt += '&lon=' + Lon;
      OutTxt += '">MultiMap</a>';
   }

   document.getElementById("formmaplink").innerHTML = OutTxt;
}

function LatLonPickType(PickSel) {
   var HideDMS = false;
   var HideDEC = false;
   var fo = document.forms[LatLonFormName];

   switch(PickSel) {
      case 0:
         HideDMS = true;
         HideDEC = true;
         LatLonBuildMapLink(false);   // update map link
         break;
      case 1:
         HideDMS = true;
         LatLonBuildMapLink(true);    // update map link
         break;
      case 2:
         HideDEC = true;
         LatLonBuildMapLink(true);    // update map link
         break;
   }

   fo['lat_deg'].disabled    = HideDMS;
   fo['lat_min'].disabled    = HideDMS;
   fo['lat_sec'].disabled    = HideDMS;
   fo['lat_dir'][0].disabled = HideDMS;
   fo['lat_dir'][1].disabled = HideDMS;
   fo['lon_deg'].disabled    = HideDMS;
   fo['lon_min'].disabled    = HideDMS;
   fo['lon_sec'].disabled    = HideDMS;
   fo['lon_dir'][0].disabled = HideDMS;
   fo['lon_dir'][1].disabled = HideDMS;

   fo['lat_dec'].disabled = HideDEC;
   fo['lon_dec'].disabled = HideDEC;

   return true;
}

function LatLonValDECLat() {
   var LError = false;
   var LVal;
   var fo = document.forms[LatLonFormName];

   if (isNaN(fo['lat_dec'].value))
      LError = true;
   else {
      LVal = Number(fo['lat_dec'].value);
      if (LVal < -90 || LVal > 90)
         LError = true;
   }

   if (LError) {
      LatLonInError = true;
      fo['lat_dec'].focus();
      fo['lat_dec'].select();
      alert ('Latitude must be a number between -90 and 90!');
      LatLonInError = false;
      return false;
   }

   return true;
}

function LatLonValDECLon() {
   var LError = false;
   var LVal;
   var fo = document.forms[LatLonFormName];

   if (isNaN(fo['lon_dec'].value))
      LError = true;
   else {
      LVal = Number(fo['lon_dec'].value);
      if (LVal < -180 || LVal > 180)
         LError = true;
   }

   if (LError) {
      LatLonInError = true;
      fo['lon_dec'].focus();
      fo['lon_dec'].select();
      alert ('Longitude must be a number between -180 and 180!');
      LatLonInError = false;
      return false;
   }

   return true;
}

function LatLonValDMSLatDeg() {
   var LError = false;
   var LVal;
   var fo = document.forms[LatLonFormName];

   if (isNaN(fo['lat_deg'].value))
      LError = true;
   else {
      LVal = Number(fo['lat_deg'].value);
      if (LVal < 0 || LVal > 90 || LVal != Math.floor(LVal))
         LError = true;
   }

   if (LError) {
      LatLonInError = true;
      fo['lat_deg'].focus();
      fo['lat_deg'].select();
      alert ('Latitude degrees must be a whole number between 0 and 90!');
      LatLonInError = false;
      return false;
   }

   if (LVal == 90) {
      fo['lat_min'].value = '0';
      fo['lat_sec'].value = '0';
   }

   return true;
}

function LatLonValDMSLatMin() {
   var LError = false;
   var LVal;
   var fo = document.forms[LatLonFormName];

   if (isNaN(fo['lat_min'].value))
      LError = true;
   else {
      LVal = Number(fo['lat_min'].value);
      if (LVal < 0 || LVal > 59 || LVal != Math.floor(LVal))
         LError = true;
   }

   if (LError) {
      LatLonInError = true;
      fo['lat_min'].focus();
      fo['lat_min'].select();
      alert ('Latitude minutes must be a whole number between 0 and 59!');
      LatLonInError = false;
      return false;
   }

   return true;
}

function LatLonValDMSLatSec() {
   var LError = false;
   var LVal;
   var fo = document.forms[LatLonFormName];

   if (isNaN(fo['lat_sec'].value))
      LError = true;
   else {
      LVal = Number(fo['lat_sec'].value);
      if (LVal < 0 || LVal > 59.99)
         LError = true;
   }

   if (LError) {
      LatLonInError = true;
      fo['lat_sec'].focus();
      fo['lat_sec'].select();
      alert ('Latitude seconds must be a number between 0 and 59.99!');
      LatLonInError = false;
      return false;
   }

   return true;
}

function LatLonValDMSLonDeg() {
   var LError = false;
   var LVal;
   var fo = document.forms[LatLonFormName];

   if (isNaN(fo['lon_deg'].value))
      LError = true;
   else {
      LVal = Number(fo['lon_deg'].value);
      if (LVal < 0 || LVal > 180 || LVal != Math.floor(LVal))
         LError = true;
   }

   if (LError) {
      LatLonInError = true;
      fo['lon_deg'].focus();
      fo['lon_deg'].select();
      alert ('Longitude degrees must be a whole number between 0 and 180!');
      LatLonInError = false;
      return false;
   }

   if (LVal == 180) {
      fo['lon_min'].value = '0';
      fo['lon_sec'].value = '0';
   }

   return true;
}

function LatLonValDMSLonMin() {
   var LError = false;
   var LVal;
   var fo = document.forms[LatLonFormName];

   if (isNaN(fo['lon_min'].value))
      LError = true;
   else {
      LVal = Number(fo['lon_min'].value);
      if (LVal < 0 || LVal > 59 || LVal != Math.floor(LVal))
         LError = true;
   }

   if (LError) {
      LatLonInError = true;
      fo['lon_min'].focus();
      fo['lon_min'].select();
      alert ('Longitude minutes must be a whole number between 0 and 59!');
      LatLonInError = false;
      return false;
   }

   return true;
}

function LatLonValDMSLonSec() {
   var LError = false;
   var LVal;
   var fo = document.forms[LatLonFormName];

   if (isNaN(fo['lon_sec'].value))
      LError = true;
   else {
      LVal = Number(fo['lon_sec'].value);
      if (LVal < 0 || LVal > 59.99)
         LError = true;
   }

   if (LError) {
      LatLonInError = true;
      fo['lon_sec'].focus();
      fo['lon_sec'].select();
      alert ('Longitude seconds must be a number between 0 and 59.99!');
      LatLonInError = false;
      return false;
   }

   return true;
}

function LatLonValForm() {
   var fo = document.forms[LatLonFormName];

   if(fo['LatLonEntryType'][1].checked)
      return (LatLonValDECLon() && LatLonValDECLon());
   if(fo['LatLonEntryType'][2].checked)
      return LatLonValDMS();
   return true;
}

function LatLonLeaveDECLat() {
   var DMSlat;
   var fo = document.forms[LatLonFormName];

   // Perform error checks on DEC values
   if (LatLonInError || !LatLonValDECLat())
      return false;   

   // Update DMS entries for current DEC values
   DMSlat = DecToDeg(fo['lat_dec'].value);

   fo['lat_deg'].value = DMSlat[0];   
   fo['lat_min'].value = DMSlat[1];   
   fo['lat_sec'].value = DMSlat[2];
   if (DMSlat[3] >= 0) {
      fo['lat_dir'][0].checked = true;
   } else {
      fo['lat_dir'][1].checked = true;
   }

   LatLonBuildMapLink(true);   // update map link

   return true;
}

function LatLonLeaveDECLon() {
   var DMSlon;
   var fo = document.forms[LatLonFormName];

   // Perform error checks on DEC values
   if (LatLonInError || !LatLonValDECLon())
      return false;   

   // Update DMS entries for current DEC values
   DMSlon = DecToDeg(fo['lon_dec'].value);

   fo['lon_deg'].value = DMSlon[0];   
   fo['lon_min'].value = DMSlon[1];   
   fo['lon_sec'].value = DMSlon[2];   
   if (DMSlon[3] >= 0)
      fo['lon_dir'][0].checked = true;
   else
      fo['lon_dir'][1].checked = true;

   LatLonBuildMapLink(true);   // update map link

   return true;
}

function LatLonLeaveDMSPopLat () {
   var DEClat;
   var fo = document.forms[LatLonFormName];

   DEClat = Number(fo['lat_deg'].value) + (Number(fo['lat_min'].value) / 60) + (Number(fo['lat_sec'].value) / 3600);
   if (fo['lat_dir'][1].checked)
      DEClat = -DEClat;

   fo['lat_dec'].value = Math.round(DEClat * 100000) / 100000;
   LatLonBuildMapLink(true);   // update map link
}

function LatLonLeaveDMSPopLon () {
   var DEClon;
   var fo = document.forms[LatLonFormName];

   DEClon = Number(fo['lon_deg'].value) + (Number(fo['lon_min'].value) / 60) + (Number(fo['lon_sec'].value) / 3600);
   if (fo['lon_dir'][1].checked)
      DEClon = -DEClon;

   fo['lon_dec'].value = Math.round(DEClon * 100000) / 100000;
   LatLonBuildMapLink(true);   // update map link
}

function LatLonLeaveDMSLatDeg () {
   if (LatLonInError || !LatLonValDMSLatDeg())
      return false;   

   LatLonLeaveDMSPopLat();
}

function LatLonLeaveDMSLatMin () {
   if (LatLonInError || !LatLonValDMSLatMin())
      return false;   

   LatLonLeaveDMSPopLat();
}

function LatLonLeaveDMSLatSec () {
   if (LatLonInError || !LatLonValDMSLatSec())
      return false;   

   LatLonLeaveDMSPopLat();
}

function LatLonLeaveDMSLatDir () {

   LatLonLeaveDMSPopLat();
}

function LatLonLeaveDMSLonDeg () {
   if (LatLonInError || !LatLonValDMSLonDeg())
      return false;   

   LatLonLeaveDMSPopLon();
}

function LatLonLeaveDMSLonMin () {
   if (LatLonInError || !LatLonValDMSLonMin())
      return false;   

   LatLonLeaveDMSPopLon();
}

function LatLonLeaveDMSLonSec () {
   if (LatLonInError || !LatLonValDMSLonSec())
      return false;   

   LatLonLeaveDMSPopLon();
}

function LatLonLeaveDMSLonDir () {

   LatLonLeaveDMSPopLon();
}

function LatLonValForm() {
   var fo = document.forms[LatLonFormName];

   //if(fo['LatLonEntryType'][1].checked)
   if(fo['latlon_type'][1].checked)
      return (LatLonValDECLon() && LatLonValDECLon());
   if(fo['latlon_type'][2].checked) {
      if (!LatLonValDMSLatDeg() || !LatLonValDMSLatMin() || !LatLonValDMSLatSec())
         return false;
      if (!LatLonValDMSLonDeg() || !LatLonValDMSLonMin() || !LatLonValDMSLonSec())
         return false;
   }
   return true;
}

// Start out on the decimal value
function LatLonLoadSetup(FormName) {
   var fo = document.forms[FormName];
   var pt = fo['latlon_type'];
   
   LatLonFormName = FormName;     // Save in a global variable for other functions
   
   if (pt[1].checked) {           // Handle if decimal is selected
      LatLonPickType(1);
      LatLonLeaveDECLat();
      LatLonLeaveDECLon();
   } else if (pt[2].checked) {    // Handle if deg/min/sec is selected
      LatLonPickType(2);
      LatLonLeaveDMSLatDeg();
      LatLonLeaveDMSLonDeg();
   } else {                       // Default to no position selected
      LatLonPickType(0);
   }
   
   return true;
}

// FASTCLICK.COM INTERSTITIAL HEAD CODE v1.0 for worldbeachlist.com 
function FCx(x){
   var min=15; // minimum minutes between interstitials (needs to be >15)
   if(x.indexOf('get.media')>0){
      x=unescape(x.substring(x.indexOf('&url=')+5,x.length));
   }else{
      if(document.cookie.indexOf('CxIC=1')<=0){
         x='http://media.fastclick.net/w/get.media?sid=20132&m=5&tp=6&url='+escape(x);
         var date_ob=new Date();
         date_ob.setTime(date_ob.getTime()+min*1000*60);
         document.cookie='FCxIC=1; path=/; expires='+date_ob.toGMTString();
      }
   }
   return x;
}
// FASTCLICK.COM INTERSTITIAL HEAD CODE v1.0 for worldbeachlist.com

// FastClick Popunder code placed in a function
// NO CHANGES WERE MADE TO THE CODE
function FCPopUnder() {

   var dc=document; var date_ob=new Date();
   dc.cookie='h2=o; path=/;';var bust=date_ob.getSeconds();
   if(dc.cookie.indexOf('e=llo') <= 0 && dc.cookie.indexOf('2=o') > 0){
      dc.write('<scr'+'ipt language="javascript" src="http://media.fastclick.net');
      dc.write('/w/pop.cgi?sid=20132&m=2&tp=2&v=1.8&c='+bust+'"></scr'+'ipt>');
      date_ob.setTime(date_ob.getTime()+43200000);
      dc.cookie='he=llo; path=/; expires='+ date_ob.toGMTString();
   }
   return true;
}
