/**
 *  standard j-insites javascript library
 */

addEvent(window, 'load', addLabelHandlers, false);

/*
 *
 */
function addLabelHandlers() {

   var aInputTags = document.getElementsByTagName('input');
   
   for (var i=0; i < aInputTags.length; i++) {
     e = aInputTags[i];
   
     if (e.type == 'checkbox' || e.type == 'radio') {
       e.onfocus = labelFocus;
       e.onblur = labelBlur;
     }
   }
}

/*
 *
 */
function labelAlterClass(e, param) {
  if (e.nodeType != 1 || e.tagName != 'LABEL' || e.htmlFor != param[0]) return;
  
  var className = param[1];
  var op = param[2];
  var pattern = new RegExp ("(^| )"+className+"( |$)");
  
  switch (op) {
    case 'add' :
      if (e.className.search(pattern) == -1) {
      e.className += " "+className;
    }
      break;
    case 'remove' :
      e.className = e.className.replace(pattern, "");
      e.className = e.className.replace(/\s+/, " ");
      break
  }
  
}

/*
 *  add a .focus class to the label of evt.target
 */
function labelFocus(evt) {
  evt = (evt) ? evt : ((event) ? event : null);
  if (evt) {
    var e = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if (e) {
    e = (e.nodeType == 1 || e.nodeType == 9) ? e : e.parentNode;

      param = new Array(e.id, 'focus', 'add');
      walkTree(e.form, labelAlterClass, param);
  }
  }
  return true;
}

/*
 *  remove any .focus class from the label of evt.target
 */
function labelBlur(evt) {
  evt = (evt) ? evt : ((event) ? event : null);
  if (evt) {
    var e = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if (e) {
    e = (e.nodeType == 1 || e.nodeType == 9) ? e : e.parentNode;

      param = new Array(e.id, 'focus', 'remove');
      walkTree(e.form, labelAlterClass, param);
  }
  }
  return true;
}


/*
 *
 */
// walk all elements of DOM tree at and below e, 
// run function fn for each element with parameters param
// not suitable for fn's which change a node  
function walkTree(e, fn, param) {

  fn(e, param);
  
  var eChild = e.firstChild
  while (eChild) {
    walkTree(eChild, fn, param);    
    eChild = eChild.nextSibling;
  }
}

/**********************************************************
Sleight for Backgrounds
Original code (c) 2001 Aaron Boodman, http://www.youngpup.net
This version (c) 2003 Drew McLellan, http://www.allinthehead.com
**********************************************************/
if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
  window.attachEvent("onload", alphaBackgrounds);
}

function alphaBackgrounds(){
  var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
  var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
  for (i=0; i<document.all.length; i++){
    var bg = document.all[i].currentStyle.backgroundImage;
    if (itsAllGood && bg){
      if (bg.match(/\.png/i) != null){
        var mypng = bg.substring(5,bg.length-2);
        document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='scale')";
        document.all[i].style.backgroundImage = "url('/images/blank.gif')";
      }
    }
  }
}

/*
 * Crossbrowser addEvent & RemoveEvent functions
 * Courtesy of http://www.soziologie.uni-halle.de/unger/scripts/workshop_internet/fr_js_322.html
 */
 function addEvent(obj, eventType, afunction, isCapture) {
    // W3C DOM
    if (obj.addEventListener) {
       obj.addEventListener(eventType, afunction, isCapture);
       return true;
    }
    // Internet Explorer
    else if (obj.attachEvent) {
       return obj.attachEvent("on"+eventType, afunction);
    }
    else return false;
 }

 function removeEvent(obj, eventType, afunction, isCapture) {
    if (obj.removeEventListener) {
       obj.removeEventListener(eventType, afunction, isCapture);
       return true;
    }
    else if (obj.detachEvent) {
       return obj.detachEvent("on"+eventType, afunction);
    }
    else return false;
 }



