function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   return temp;
}

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, true); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

function initHighlight() {
    if (!document.getElementsByTagName){ return; }
    var allfields = document.getElementsByTagName("input");

    // loop through all input tags and add events
    for (var i=0; i<allfields.length; i++){
        var field = allfields[i];
        if ((field.getAttribute("type") == "text") || (field.getAttribute("type") == "password") ) {
            field.onfocus = function () {this.className += ' highlightActiveField';}
            field.onblur = function () {this.className += ' highlightInactiveField';}
        }
    }
}
function initBtnHover() {
    if (!document.getElementsByTagName){ return; }
    var allfields = getElementsByClassName(document, "input", "btnSubmit"); //document.getElementsByTagName("input");
    var allfields2 = getElementsByClassName(document, "input", "btnSubmitSmall"); //document.getElementsByTagName("input");
    var allfields3 = getElementsByClassName(document, "input", "submit"); //document.getElementsByTagName("input");
    // loop through all input tags and add events
    for (var i=0; i<allfields.length; i++){
            allfields[i].onmouseover = function () {this.className = this.className + 'Hover';}
            allfields[i].onmouseout = function () {this.className = this.className.replace(/Hover/g, "");}
    }
     for (var i=0; i<allfields3.length; i++){
            allfields3[i].onmouseover = function () {this.className = this.className + 'Hover';}
            allfields3[i].onmouseout = function () {this.className = this.className.replace(/Hover/g, "");}
    }
      // loop through all input tags and add events
    for (var i=0; i<allfields2.length; i++){
            allfields2[i].onmouseover = function () {this.className = this.className + 'Hover';}
            allfields2[i].onmouseout = function () {this.className = this.className.replace(/Hover/g, "");}
    }
}
	   
// Nifty function to add onload events without overwriting
// ones already there courtesy of the lovely and talented
// Simon Willison http://simon.incutio.com/
function addTPLoadEvent(func) {  

    var oldonload = window.onload;
    if (typeof window.onload != 'function'){
        window.onload = func;
    } else {
        window.onload = function(){
        oldonload();
        func();
        }
    }
}

/* newer version below
 document.getElementsByClassName = function (needle)
 {
     var my_array = document.getElementsByTagName("*");
     var retvalue = new Array();
     var i, j;
     for (i = 0, j = 0; i < my_array.length; i++)
     {
         var c = " " + my_array[i].className + " ";
         if (c.indexOf(" " + needle + " ") != -1)
             retvalue[j++] = my_array[i];
     }
     return retvalue;
 }
 */
function getEventSrc(e) {
       var targ;
// get a reference to the IE/windows event object
if (!e) e = window.event;

// DOM-compliant name of event source property
if (e.target)
  targ = e.target;
// IE/windows name of event source property
else if (e.srcElement)
  targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
       targ = targ.parentNode;
return targ;
}

var Color= new Array();
Color[1] = "ff";
Color[2] = "ee";
Color[3] = "dd";
Color[4] = "cc";
Color[5] = "bb";
Color[6] = "aa";
Color[7] = "99";

function waittofade(id) {
if (document.getElementById(id)) {
  setTimeout("fadeIn(7, '"+id+"')", 100);
 }
}

function fadeIn(where, id) {
if (where >= 1) {
    document.getElementById(id).style.backgroundColor = "#ffff" + Color[where];
  if (where > 1) {
    where -= 1;
    setTimeout("fadeIn("+where+", '"+id+"')", 200);
  } else {
    where -= 1;
    setTimeout("fadeIn("+where+", '"+id+"')", 200);
    document.getElementById(id).style.backgroundColor = "transparent";
    //document.getElementById('docContainer').id = '';
  }
}
}

function hideAll(){
  var obj,nextspan,anchor,content;
  // get all spans
  obj=document.getElementsByTagName('span')
  // run through them
  for (var i=0;i<obj.length;i++){
    // if it has a class of helpLink
    if(/helpLink/.test(obj[i].className)){
      // get the adjacent span
      nextspan=obj[i].nextSibling;
      while(nextspan.nodeType!=1) nextspan=nextspan.nextSibling;
       // hide it
      nextspan.style.display='none';
      //create a new link
      anchor=document.createElement('a');
      // copy original helpLink text and add attributes
      content=document.createTextNode(obj[i].firstChild.nodeValue);
      anchor.appendChild(content);
      anchor.href='#help';
      anchor.title='Click to show help';
      anchor.className=obj[i].className;
      anchor.nextspan=nextspan;
      anchor.onclick=function(){showHide(this.nextspan);changeTitle(this);return false;}
     // anchor.onfocus=function(){showHide(this.nextspan);changeTitle(this);return false;}

      // replace span with created link
      obj[i].replaceChild(anchor,obj[i].firstChild);
    }
  }
}
// used to flip helpLink title
function changeTitle(obj){
  if(obj)
    obj.title = obj.title=='Click to show help' ? 'Click to hide help' : 'Click to show help';
}



// used to flip the display property
function showHide(obj){
  if(obj)
    obj.style.display = obj.style.display=='none' ? 'inline' : 'none';
}

 if(typeof Array.prototype.push != "function"){
    Array.prototype.push = ArrayPush;
    function ArrayPush(value){
        this[this.length] = value;
    }
}  
	/*
    usage:
	getElementsByClassName(document.getElementById("container"), "div", "col");
	*/
	
	function getElementsByClassName(oElm, strTagName, oClassNames){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var arrRegExpClassNames = new Array();
    if(typeof oClassNames == "object"){
        for(var i=0; i<oClassNames.length; i++){
            arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
        }
    }
    else{
        arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
    }
    var oElement;
    var bMatchesAll;
    for(var j=0; j<arrElements.length; j++){
        oElement = arrElements[j];
        bMatchesAll = true;
        for(var k=0; k<arrRegExpClassNames.length; k++){
            if(!arrRegExpClassNames[k].test(oElement.className)){
                bMatchesAll = false;
                break;                      
            }
        }
        if(bMatchesAll){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

function doHover(obj, doIt){
			   if(doIt == true){
					obj.className = "trHover";
			   }else{
				 obj.className = "none";
			   }
			  }
	   function initTrHover(){
			var nextspan;
			var arrContainers;
			var arrCheckboxes;
			arrContainers = getElementsByClassName(document, "tr", "doHover");
			for(var i=0; i<arrContainers.length; i++){
			arrContainers[i].onmouseover=function(){doHover(this, true);}
			arrContainers[i].onmouseout=function(){doHover(this, false);}
			}
	  }
	  function doHover2(obj, doIt){
			   if(doIt == true){
					obj.className = "trHover2";
			   }else{
				 obj.className = "none";
			   }
			  }
	   function initTrHover2(){
			var nextspan;
			var arrContainers;
			var arrCheckboxes;
			arrContainers = getElementsByClassName(document, "tr", "doHover2");
			for(var i=0; i<arrContainers.length; i++){
			arrContainers[i].onmouseover=function(){doHover2(this, true);}
			arrContainers[i].onmouseout=function(){doHover2(this, false);}
			}
	  }


//sorts out all text inputs to have a highlight on focus
addTPLoadEvent(initHighlight);
//adds the expandable help sections for form fields
addTPLoadEvent(hideAll);

addTPLoadEvent(initTrHover);
addTPLoadEvent(initTrHover2);
addTPLoadEvent(initBtnHover);