/*
  Misc odds and ends utility functions. 
  Steaven Woyan
*/

//safely get text value for an element's child, blank if the element has no children
function getElementChildValue(node) {
  if(!node) {
    return "";
  }
  if(node.firstChild) {
    return node.firstChild.nodeValue;
  }    
  else {
    return "";
  }
}

//remove all blank text elements from a DOM node
function removeDOMWhitespace(node) {
  if(!node || !node.childNodes) {
    return;
  }
  var notWhitespace = /\S/;
  for (var x = 0; x < node.childNodes.length; x++) {
    var childNode = node.childNodes[x];
    if ((childNode.nodeType == 3) && (!notWhitespace.test(childNode.nodeValue))) {
      node.removeChild(node.childNodes[x]);
      x--;
    }
    if (childNode.nodeType == 1) {
      //elements can have text child nodes of their own
      removeDOMWhitespace(childNode);
    }
  }
}

//get today as an ISO string
function getToday() {
  return getTodayISO();
}

//get today as USA string
function getTodayUSA() {
  var today = new Date();
  var m = "" + (today.getMonth() + 1);
  var d = "" + today.getDate();
  var y = "" + today.getFullYear();
  if(m.length == 1) {m = "0" + m;}
  if(d.length == 1) {d = "0" + d;}
  
  return m + "/" + d + "/" + y;
}

//get today as ISO string
function getTodayISO() {
  var today = new Date();
  var m = "" + (today.getMonth() + 1);
  var d = "" + today.getDate();
  var y = "" + today.getFullYear();
  if(m.length == 1) {m = "0" + m;}
  if(d.length == 1) {d = "0" + d;}
  
  return y + "-" + m + "-" + d;
}

//is a year a leap year
function isLeap(y) {
  var r = false;
  if((y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0))) {
    r = true;
  }
  return r;
}