//Sees if an element contains another element
function elementContains(elmOuter, elmInner)
{
  while (elmInner && elmInner != elmOuter)
  {
    elmInner = elmInner.parentNode;
  }
  if (elmInner == elmOuter)
  {
    return true;
  }
  return false;
}

//Get the absolute coordinates of an element on the page
function getPageXY(elm)
{
  var point = { x: 0, y: 0 };
  while (elm)
  {
    point.x += elm.offsetLeft;
    point.y += elm.offsetTop;
    elm = elm.offsetParent;
  }
  return point;
}

//Get the child link of a particular element
function getFirstChildLink(theParent)
{
  var childLink = null;
  
  //See if the parent has children
  if(theParent.childNodes)
  {
    //Loop through the children
    for(i=0; i<theParent.childNodes.length; i++)
    {
      //Check if we found an anchor tag
      if(theParent.childNodes[i].nodeName == "A")
      {
        //Set the link to this node
        childLink = theParent.childNodes[i];
      }
    }
  }
  
  return childLink;
  
}

//Set the absolute coordinates of an element on the page
function setPageXY(elm, x, y)
{
  var parentXY = {x: 0, y: 0 };

  if (elm.offsetParent)
  {
    parentXY = getPageXY(elm.offsetParent);
  }

  elm.style.left = (x - parentXY.x) + 'px';
  elm.style.top  = (y - parentXY.y) + 'px';
}

//Clear a text box
function clearTextBox(aTextBox)
{
	if(aTextBox.value == aTextBox.defaultValue)
	{
	  aTextBox.value="";
	}
}

//Places data into ektron search field
function placeDataIntoEktronSearchField(searchTextBox)
{
  var ektronSearchField = document.getElementById("ecmBasicKeywords");
  ektronSearchField.value = searchTextBox.value;
}

//Sets search fields for proper execution of a search
function setSearchFields(searchTextBox)
{
  var postField
  placeDataIntoEktronSearchField(searchTextBox);
  postField = document.getElementById("__ekSearchPost");
  postField.value = 1;
}


//Assign a button to the enter key press
function clickEnterButton(e, aButtonId)
{
  var keyCode;
  if (window.event) keyCode = window.event.keyCode;
  else if (e) keyCode = e.which;
  else return true;

  if(keyCode == 13)
  {
    document.getElementById(aButtonId).click();
    return false;
  }
  else
  {
    return true;
  }
}

//Turns an enter into a tab
function enterToTab(input, e)
{
 var keyCode;
  if (window.event) keyCode = window.event.keyCode;
  else if (e) keyCode = e.which;
  else return true;

  if (keyCode == 13)
  { 
    input.form[(getIndex(input)+1) % input.form.length].focus(); 
    return false 
  }
  else
  { 
    return true;
  }

}

//Turns an enter into a tab for checkboxes

function supressEnter(input, e)
{
 var keyCode;
  if (window.event) keyCode = window.event.keyCode;
  else if (e) keyCode = e.which;
  else return true;

  if (keyCode == 13)
  { 
    //if (window.event.keyCode == 13)
	//{
		//window.event.keyCode = 9;
	//}
    
    return false 
 }
  else
  { 
    return true;
  }

}



//Add a class to an element
function addClassToElement(className, element)
{
  if(element.className.search(new RegExp(className+"\\b"))==-1)
  {
    if(element.className == "")
      element.className = className;
    else
      element.className += " " + className;
  }
}

//Remove a class from an element
function removeClassFromElement(className, element)
{
  if(element.className == className)
    element.className = "";
  else
    element.className = element.className.replace(new RegExp(" "+className+"\\b"), "");
}

//Set a class on a particular element
function setClassOnElement(className, element)
{
  element.className = className; 
}

//Get the index of a specific input
function getIndex(input)
{
  var index = -1, i = 0, found = false;
  while (i < input.form.length && index == -1)
  {
    if (input.form[i] == input)
      index = i;
    else i++;
  }
    return index;
}

//Used to check the maxlength of a multiline textbox
function checkTextMaxLength(text, maxLength)
{
    if ( text.value.length > maxLength )
    {
         text.value = text.value.substring(0, maxLength);
         alert("Only " + maxLength + " characters allowed in this field.");
    }
}

//Check all checkboxes in a checkbox list
function checkAllBoxesInList(checkBoxList)
{
    for (i = 0; i < checkBoxList.length; i++)
        checkBoxList[i].checked = true;
}

//Uncheck all checkboxes in a checkbox list
function uncheckAllBoxesInList(checkBoxList)
{
    for (i = 0; i < checkBoxList.length; i++)
    checkBoxList[i].checked = false;
}