

/**
 * format the given Date object into a String "YYYY-MM-DD HH:MM:SS"
 */
   function formatDateTimeString(dt) {
      var str = dt.getUTCFullYear()+'-';
      var t = dt.getUTCMonth()+1;
      if (t < 10) str += '0';
      str += t+'-';
      
      t = dt.getUTCDate();
      if (t < 10) str += '0';
      str += t +' '
      
      t = dt.getUTCHours();
      if (t < 10) str += '0';
      str += t + ':';
      
      t = dt.getUTCMinutes();
      if (t < 10) str += '0';
      str += t +':';
      
      t = dt.getUTCSeconds();
      if (t < 10) str += '0';
      str += t;
      
      return(str);
   }

/**
 * format the given Date object into a String "YYYY-MM-DD"
 */
   function formatDateString(dt) {
      var str = dt.getUTCFullYear()+'-';
      var t = dt.getUTCMonth()+1;
      if (t < 10) str += '0';
      str += t+'-';
      
      t = dt.getUTCDate();
      if (t < 10) str += '0';
      str += t;      
      return(str);
   }

  /**
   * return an array of Dates, in reverse chronological order, beginning w/ current DateTime
   */
   function calcDates(count) {
      var result = new Array();
      var millisecs = new Date().getTime();
      for (var i=0; i<count; i++) {
         result[i] = new Date(millisecs);
         // calc the prev day
         millisecs = millisecs - (24 * 60 * 60 * 1000);
      }
      return(result);
   }
   
/*
 * This function parses ampersand-separated name=value argument pairs from
 * the query string of the URL. It stores the name=value pairs in
 * properties of an object and returns that object. Use it like this:
 *
 * var args = getArgs( );  // Parse args from URL
 * var q = args.q || "";  // Use argument, if defined, or a default value
 * var n = args.n ? parseInt(args.n) : 10;
 *
 * taken from "JavaScript: The Definitive Guide, 5th Edition" by  David Flanagan
 */
function getArgs( ) {
    var args = new Object( );
    var query = location.search.substring(1);     // Get query string
    var pairs = query.split("&");                 // Break at ampersand
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');          // Look for "name=value"
        if (pos == -1) continue;                  // If not found, skip
        var argname = pairs[i].substring(0,pos);  // Extract the name
        var value = pairs[i].substring(pos+1);    // Extract the value
        value = decodeURIComponent(value);        // Decode it, if needed
        args[argname.toLowerCase()] = value;      // Store as a property
    }
    return args;                                  // Return the object
}


/**
 * convert coordinates from equirectangular(meters) to geographic 
 * (decimal degrees).  
 *
 * based on work by Charlton Galvarino (charlton@2creek.com)
 */  
function equirectangularToGeographic(x,y) {

   //ideally calc radius of earth based on latitude
   //R = 6378137 * (1 - 0.0033493*sin(Lat)**2)
   var R = 6378137; //radius of earth in meters
   var lat_ts = 0.0;  //latitude at projection center in radians
   var lon_0 = deg2rad(-180.0);  //longitude at projection center in radians
   
   var lon_dd = rad2deg ( lon_0 + x / ( R * Math.cos(lat_ts) ));
   var lat_dd = rad2deg ( y / R );
   if (lon_dd < -180) { lon_dd += 360; }
   return({'x':lon_dd, 'y':lat_dd});
}


/**
 * convert radians to degrees
 */
function rad2deg(rad) {
   return(rad * (180.0/Math.PI));
}


/**
 * convert degrees to radians
 */
function deg2rad(deg) {
   return(deg * (Math.PI/180.0));
}
