/* Append events to the onLoad event */


/// Helper method to allow us to add multiple events to the Body onload event
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/// Helper method to insert a new element after a provided element
function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

/// Helper method to add a html class to the provided element
function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}

// RemoveClass
function removeClass(el,cl){
    if(typeof el=='undefined')return;
    if(el.className===null)return;
    var classes=el.className.split(" ")
    var result=[]
    for(i in classes){
        if(classes[i] !=cl) result[result.length]=classes[i];
    }
    el.className=result.join(" ")
}

/// Method to set form focus on the supplied element
function setFocus(elementId){
    if (!document.getElementById) return false;
    
    var textField;
    textField  = document.getElementById(elementId);

    if (!textField)
    {
        return false; 
    }
    else
    {
        textField.focus();
    } 
}

///Method to stripe tables
function stripeTables() {

  if (!document.getElementsByTagName) return false;

  var tables = document.getElementsByTagName("table");
  
  for (var i=0; i<tables.length; i++) {
   
      if(tables[i].className.indexOf("noStripe") == -1)
      {
            var odd = false;
            var rows = tables[i].getElementsByTagName("tr");
                      
            for (var j=0; j<rows.length; j++) 
            {  
              if (odd == true) 
              {
                addClass(rows[j],"even");
                odd = false;
              } 
              else 
              {
	            addClass(rows[j],"odd");        
	            odd = true;
               }
            }
       }
      else
      {
      } 
  }
}

///Helper method to find elements by Class Name - pass the classname to search for, the tag to look for (* for all) and the element to search under
function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

// this is a function to test whether the browser is javascript enabled
// use results cautiously
function TestJavaScript()
{

    if(document.getElementById('testJavaScript') != null)
    {
        document.getElementById('testJavaScript').value = '1';
    }
     if(document.getElementById('testJavaScriptSubform') != null)
    {
        document.getElementById('testJavaScriptSubform').value = '1';
    }
}

///Method to show the Back button container and attach the JS back functionality to the OnClick event
function showBackButton(){
    
    var buttonContainer = document.getElementById("dvBack");
    var backButton =  document.getElementById("btnBack");

    if(buttonContainer.style.display == 'none')
       {
            buttonContainer.style.display = 'block'; 
        }

        // attach a simple back function to the button
        backButton.onclick = function() {   
            history.go(-1);
            } 
    }

///Method to show the print button attach the JS print functionality to the OnClick event
function showPrintButton(newDisplay){

    var printButton =  document.getElementById("btnPrint");

    if(printButton.style.display == 'none')
       {
            printButton.style.display = newDisplay; 
        }

        // attach a simple print function to the button
        printButton.onclick = function() {   
            window.print();
            } 
    }

///Method to show the Refine search container and attach the JS back functionality to the OnClick event of the link
function showRefineSearchButton()
{

   // get all list items on the page with a class of jsRefineButton 
    var buttonContainers = getElementsByClassName("jsRefineButton", "li", document);
    
    for (var i=0; i<buttonContainers.length; i++)
    {
        // rate a new link inside the matching list items
        var newLink = document.createElement("A");
        var defaultLinkText = document.createTextNode("[Back]");
        // append the text node to the link
        newLink.appendChild(defaultLinkText);
        newLink.setAttribute("href", "#");
        // attach a simple back function to the link
        newLink.onclick = function() 
        {   
        history.go(-1); return false;
        }
        buttonContainers[i].appendChild(newLink);
        
        if(buttonContainers[i].style.display == 'none')
        {
            buttonContainers[i].style.display = 'inline'; 
         }
       }   
    }



