//*********************** UTIL ***********************

//coverts a decimal lat/lon and returns a string in the form of dd mm.ssss[N|S|E|W]
function convertLatLonToStr (value, islat) {
	if (value == "") { value = 0; }
	d = Math.floor(parseFloat(value));
	m = (parseFloat(value) - Math.floor(parseFloat(value))) * 60;
	m = roundOff(m, 2);
	if (islat) {
		if (d < 0) {dir = 'S'} else {dir = 'N'};
	} else {
		if (d < 0) {dir = 'W'} else {dir = 'E'};
	}
	return d + ' ' + m + dir;
}

//safely extract value from xml node
function getElement(e) {
	if ((e) && (e.firstChild)) {
		return e.firstChild.nodeValue; 
	} else {
		return "";
	} 
}

//display a status window
function showStatus(html) {
	var status = document.getElementById('status');
	if (status) {
		if (html > '') {
			status.style.display = '';
			status.innerHTML = html;
		} else {
			status.style.display = 'none';
		}
	}
}

//return passed parameters in args[]
function getQueryString() {
  var args = new Object();
  var query = location.search.substring(1);
  // Split query at the comma
  var pairs = query.split("&");
  var counter = 0;
  
  // Begin loop through the querystring
  for(var i = 0; i < pairs.length; i++) {
    // Look for "name=value"
    var pos = pairs[i].indexOf('=');

    // if not found, skip to next
    if (pos == -1) continue;

    // Extract the name
    var argname = pairs[i].substring(0, pos);
    
    // Extract the value
    var value = pairs[i].substring(pos+1); 

    // Store as a property
    if (!args[argname]) {
      args[argname] = unescape(value);
    }
    else {
      args[argname] += ("&" + argname + "=" + unescape(value));
    }
  }
  return args; // Return the Object
}

//insert commas in string version of a number
function Commaize(value) {
	var delimiter = ","; // replace comma if desired
	var a = value.split('.', 2);
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3) {
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if (d > '') { value = n + '.' + d; }
	else { value = n; }
	value = minus + value;
	return value;
}

//remove sid from end of callsign
function StripSID(callsign) {
  var i = callsign.indexOf('-', 0);
  if (i > -1) 
	return callsign.slice(0, i)
  else 
	return callsign;
}

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
