// Global functions

function addEvent(evt, elem, newFunction) {
    var oldFunction = elem[evt];
    
    
    if( typeof elem[evt]=='function' ) {

        elem[evt] = function() {
            oldFunction();
            newFunction(); 
        };
        
    } else {
        elem[evt] = newFunction;
    }
}

// Generate a new element (with a certain class) and wrap it around the value
function elemWrap(elem, value, className) {
    return '<' + elem + ' class="' + className + '">' + value + '</' + elem + '>';
}

function printPage() {
    window.print(); return false; 
}

// Add function to onload handler
function addPrintLink() {
    var elem = document.getElementById("print-page");
    if(elem) {
        addEvent('onclick', elem, printPage);
    }
}
addEvent('onload', window, addPrintLink);



 /**
  * Determine whether a node's text content is entirely whitespace.
  *
  * @param nod  A node implementing the |CharacterData| interface (i.e.,
  *             a |Text|, |Comment|, or |CDATASection| node
  * @return     True if all of the text content of |nod| is whitespace,
  *             otherwise false.
  */
 function is_all_ws( nod ) {
   // Use ECMA-262 Edition 3 String and RegExp features
   return !(/[^\t\n\r ]/.test(nod.data));
}

 /**
  * Determine if a node should be ignored by the iterator functions.
  *
  * @param nod  An object implementing the DOM1 |Node| interface.
  * @return     true if the node is:
  *                1) A |Text| node that is all whitespace
  *                2) A |Comment| node
  *             and otherwise false.
  */
 
 function is_ignorable( nod ) {
   return ( nod.nodeType == 8) || // A comment node
          ( (nod.nodeType == 3) && is_all_ws(nod) ); // a text node, all ws
}
    
/**
 * Version of |firstChild| that skips nodes that are entirely
 * whitespace and comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The first child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function first_child( par ) {
  var res=par.firstChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.nextSibling;
  }
  return null;
}
