
//utils
function did(id){return (id!=null&&id!="")?document.getElementById(id):null;}
function dd(id){return (id!=null&&id!="")?document.getElementById(id):null;}
function shobj(n,s){var o=did(n);if(o) o.style.display=(s?'':'none');}
function hide(n){shobj(n,false);}
function show(n){shobj(n,true);}
function tog(n){var o=did(n);if(o) {var s=(o.style.display=='none');o.style.display=(s?'':'none');}}
function hd(n){shobj(n,false);}
function sw(n){shobj(n,true);}

function get_cookie(cookie_name)
{
	var results=document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
	if (results)
		return (unescape(results[2]));
	else
		return "";
}
function set_cookie(name,value,exp_y,exp_m,exp_d,path,domain,secure)
{
	var cookie_string=name+"="+escape(value);
	if (exp_y)
	{
		var expires=new Date(exp_y,exp_m,exp_d);
		cookie_string+="; expires="+expires.toGMTString();
	}
	if (path)
    cookie_string+="; path="+escape(path);
	if (domain)
    cookie_string+="; domain="+escape(domain);
	if (secure)
    cookie_string+="; secure";
	document.cookie=cookie_string;
}
function delete_cookie(cookie_name)
{
  set_cookie(cookie_name,"",1997,01,01,"/",".geoterrestrial.com");
	//var cookie_date=new Date(); //current date & time
	//cookie_date.setTime(cookie_date.getTime()-1);
	//document.cookie= cookie_name + "=; expires=" + cookie_date.toGMTString();
}
//string utils
function trim(str){
  str = str.replace(/^\s+/, '');
  for (var i = str.length - 1; i >= 0; i--) {
    if (/\S/.test(str.charAt(i))) {
      str = str.substring(0, i + 1);
      break;
    }
  }
  return str;
}
String.prototype.startsWith = function(str){
  return (this.match("^"+str)==str);}
String.prototype.endsWith = function(str){
  return (this.match(str+"$")==str);}

String.prototype.stricmp = function(str){
  return (this.toLowerCase()==str.toLowerCase() );
}

/*
function existsInString(strs,val )
{
  var arr=strs.split(",");
  for(i=0;i<arr.length;i++){
    if(trim(arr[i])!=""&&trim(arr[i])==val)
      return true;
  }
  return false;
}
*/
function addIfNotExist(strs,val)
{
  var a=strs.split(",");
  var f=false;
  var o="";
  for(i=0;i<a.length;i++ ){
    t=trim(a[i]);
    if (t!=""){
      o+=((o!="")?",":"")+t;
      if (t==val)
        f=true;
    }
  }
  if (!f)
    o+=((o!="")?",":"")+val;
  return o;
}
function removeFromList(strs,val)
{
  var a=strs.split(",");
  arrRemove(a,val);
  return makelist(a);
}
function existsNVVal(arr,n)
{
  for(i=0;i<arr.length;i++)
    if(arr[i].name==n)
      return i
  return -1;
}
function getNVVal(arr,n)
{
  var p=existsNVVal(arr,n);
  return (p>=0?arr[p].value:null);
}
function addToNV(arr,n,v)
{
  var p=existsNVVal(arr,n);
  if (p>=0)
    arr[p].value=v;
  else
    arr.push( {name:n,value:v } );
}
function removeNV(arr,n)
{
  var p=existsNVVal(arr,n);
  if(p>=0)
    arr.splice(p,1);
  return (p>=0);
}
function arrExists(a,v){
  for(i=0;i<a.length;i++)
    if (a[i]==v) return i;
  return -1;
}
function arrAdd(a,v){
  if (-1==arrExists(a,v))
    a.push(v);
  return true;
}
function arrRemove(a,v)
{
  var p=arrExists(a,v);
  if (p>=0){
    a.splice(p,1);
    return true;
  }
  return false;
}
function bsearch(arr,val)
{
  var h = arr.length, l = -1, m;
  while(h - l > 1)
    if (arr[m = h + l >> 1] < val ) l = m;
    else h = m;
  return	h >= arr.length ? -1 : ( arr[h] == val ? h : -1 );
}

function commaesc( txt )
{
  var escaped = txt.replace( /,/g, "\\\," );

  return escaped;		 
}

function htmlesc( txt )
{
  var escaped = txt.replace( /\'/g, "\\\'" );
  escaped = escaped.replace( /\"/g, "\\\"" );
  //escaped = escaped.replace( /</g, "&lt;" );
  //escaped = escaped.replace( />/g, "&gt;" );

  return escaped;		 
}

function jsunesc( txt )
{
  var escaped = txt.replace( /\\\'/g, "\'" );
  escaped = escaped.replace( /\\\"/g, "\"" );
  //escaped = escaped.replace( /&lt;/g, "<" );
  //escaped = escaped.replace( /&gt;/g, ">" );

  return escaped;		 
}


function makelist(a){
 var t="";
 for(i=0;i<a.length;i++){
  t+=(i>0?",":"")+(typeof a[i]=="string"?commaesc(a[i]):a[i]);
 }
 return t;
}
function makelistobj(a,n)
{
  var t="";
  for(i=0;i<arr.length;i++){
    t+=(i>0?",":"")+(typeof a[i].n=="string"?commaesc(a[i].n):a[i].n);
  }
  return txt;
}
function joinWith(a,sep,tfn)
{
  var b=[];
  for(i=0;i<a.length;i++) b[i]=tfn(a[i]);
  return b.join(sep);
}
var current_url = "";
function curUrl(){
  if ( current_url=="" )
    current_url = window.location.protocol+"//"+window.location.host+window.location.pathname;
  return current_url;
}
function curUrlBase(){
  return window.location.protocol+"//"+window.location.host;
}

function getClientWidth(){
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}
function getClientHeight(){
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}
function findPos(obj)
{
  var cl=0;//left
  var ct=0;//top
  if(obj.offsetParent)
  {
    while(obj.offsetParent)
    {
      cl+=obj.offsetLeft-obj.scrollLeft;
      ct+=obj.offsetTop-obj.scrollTop;
      var position='';
      if(obj.style&&obj.style.position) position=obj.style.position.toLowerCase();
      if ((position=='absolute')||(position=='relative')) break;
      while(obj.parentNode!=obj.offsetParent) {
        obj=obj.parentNode;
        cl-=obj.scrollLeft;
        ct-=obj.scrollTop;
      }
      obj=obj.offsetParent;
    }
  }
  else{
    if(obj.x)
			cl+=obj.x;
    if (obj.y)
			ct+=obj.y;
  }
  return {left:cl,top:ct};
}

function cloneObject(what){
  for (i in what){
    if (typeof what[i] == 'object') {
      this[i] = new cloneObject(what[i]);
    }
    else
      this[i] = what[i];
  }
}

var headings=["N","NNE", "NE","E","SE","SSE", "S","SSW","SW", "W","NW", "NNW"];
function getHeading(v,nodeg)
{
  if(typeof nodeg =="undefined" || !nodeg) nodeg=false;
  if(parseInt(v)==v && v>=0&&v<=360)
  {
    if(v==360) v=0;
    var d=Math.round(v/30);
    if(d>11)d=0;
    if(nodeg)
      return headings[d];
    else 
      return v+"deg("+headings[d]+")";
  }
  return v;
}

function pad(val,l,c)
{
  if(typeof l=="undefined") l=2;
  if(typeof c=="undefined")c="0";
  if((val+"").length<l) val=c+val;
  return val;
}
var months = [ 'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec' ];

function getShortDate(dt)
{
  return months[dt.getMonth()] + " " + dt.getDate() + ", " + dt.toLocaleTimeString();
}
function getUDate(ut)
{
  return getShortDate(new Date(ut*1000));
}
function maxdim(obj,w,h)
{
  var x=obj.width;y=obj.height;
  var asp=x/y;
  if(x>w) {x=w;y=w/asp;}
  if(y>h) {y=h;x=h*asp;}
  if(x!=obj.width) obj.width=x; if(y!=obj.height) obj.height=y;
}
function getPageNav( start, count, total, backfunc, prevfunc, nextfunc )
{
  var back = "<span title='Back to Page 1' ";
  if ( start > 0 )
    back += " class='pagenav' onclick='" + backfunc +"' ";
  else
    back += " class='pagenavdis' ";
  back += " >&lt;&lt;</span>";


  var prev = "<span title='Previous Page of Results' ";
  if ( start > 0 )
    prev += " class='pagenav' onclick='" + prevfunc + "' ";
  else
    prev += " class='pagenavdis' ";
  prev += " >Prev</span>";

  var next = "<span title='Next Page of Results' ";
  if ( (start+count) < total )
    next += " class='pagenav' onclick='" + nextfunc + "' ";
  else
    next += " class='pagenavdis' ";
 
  next += " >Next Page</span>";

  if ( start == 0 && (start+count) >= total )
  {
    back = "";
    next = "";
    prev = "";
  }
  return back + "&nbsp;&nbsp;" + prev + "&nbsp;&nbsp;" + next;
}

String.prototype.stripHTML = function()
{
  // What a tag looks like
  var matchTag = /<(?:.|\s)*?>/g;
  // Replace the tag
  return this.replace(matchTag, "");
};

function latlngValid(lat,lng)
{
  return (typeof lat!="undefined" && typeof lng!="undefined") && 
  (lat > -89 && lat < 89 && lng >= -180 && lng <= 180);
}
function pnt(i){return parseInt(i);}

