/*  util.js 
  this file provides library routines used by other js files
*/

//  cross browser events by Scott Andrew
function addEvent(obj, evType, fn, useCapture) {
  if (obj.addEventListener) {
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent) {
    var r = obj.attachEvent('on' + evType, fn);
    return r;
  } else {
    obj['on' + evType] = fn;
    return true;
  }
}

function getElementsByClass(theNode, theClass) {
  var cArray = [];
  var z = new RegExp('((^)|(\ ))' + theClass + '((\ )|($))');
  var q = ".//*[ contains(concat(' ', @class, ' '), ' " + theClass + " ')]";
  
  function doTree(theNode) {
    if (theNode.className && z.test(theNode.className)) {
      cArray.push(theNode);
    }
    for (var i=0, len=theNode.childNodes.length; i<len; i++) {
      doTree(theNode.childNodes[i]);
    }
  }
  
  if ( document.evaluate ) {  // firefox (much faster way)
    var res = document.evaluate(q, theNode, null, XPathResult.ANY_TYPE, null);
    theEl = res.iterateNext();
    while ( theEl ) {
      cArray.push(theEl);        
      theEl = res.iterateNext();
    }
  } else {  // everyone else
    doTree(theNode);
  }
  
  return cArray;
}

function findParentWithClass(theNode, theClass) {
  var z = new RegExp('((^)|(\ ))' + theClass + '((\ )|($))');
  if ( theNode.className && z.test(theNode.className) ) return theNode;
  else {
    if ( theNode.tagName.toLowerCase() == "body" ) return null;
    else return findParentWithClass(theNode.parentNode, theClass);
  }
}

function findParentWithTagName(theNode, tagName) {
  if ( theNode.tagName.toLowerCase() == tagName.toLowerCase() ) return theNode;
  else {
    if ( theNode.tagName.toLowerCase() == "body" ) return null;
    else return findParentWithTagName(theNode.parentNode, tagName);
  }
}

function showProps(obj, skiptype) {
  var data = "";
  for(var i in obj) {
    if (( typeof obj[i] != skiptype ) && 
        ( obj[i] !== null ))
      data += i + " - " + obj[i] + "\n";
  }
  alert(data);
}

function makeDict(data, rSep, fSep) {
  if ( typeof rSep == "undefined") rSep = "&";
  if ( typeof fSep == "undefined") fSep = "=";
  var retVal = {};
  var aLine = [];
  var fName;
  var aData = data.split(rSep);
  for ( var i = 0; i < aData.length; i++ ) {
    aLine = aData[i].split(fSep);
    fName = aLine.shift();
    retVal[fName] = aLine.join(fSep);
  }
  return retVal;
}

//   ie/win needs an iframe 'shim' underneath dynamically shown content
//  to keep selects and other 'windowed controls' from poking thru
//   these functions create, hide and place the shim
function needShim() {
  if ( document.all && document.getElementById ) {
    if (navigator.appVersion.substr(22,3)!="5.0") {
      var aBodies = document.getElementsByTagName("body");
      var shim = document.getElementById("shim");
      if ( !shim ) {
        var el = document.createElement("iframe");
        el.id = "shim";
        el.src = "about:blank";
        el.className = "hide";
        el.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
        aBodies[0].appendChild(el);
      }
      return true;
    }
  }
  return false;
}

function placeShim(t, l, w, h) {
  var shim = document.getElementById("shim");
  if ( shim ) {
    shim.style.top = t;
    shim.style.left = l;
    shim.style.width = w + "px";
    shim.style.height = h + "px";
    shim.className = "show";
  }
}

function hideShim() {
  var shim = document.getElementById("shim");
  if ( shim ) {
    shim.className = "hide";
  }
}
//  end shim routines

// string related utilities
String.prototype.toTitleCase = function() {
  val = this.toString().split(' ');
  for(var c=0; c < val.length; c++) 
    val[c] = val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length);
  return val.join(" ");
}

