// Gets the desired XML element.
// If the element is empty or null, returns blank string
function getXMLElement(parent, elementName) {
    if (parent.getElementsByTagName(elementName)[0] == null) {
        return '';
    }
    if (parent.getElementsByTagName(elementName)[0].firstChild == null) {
        return '';
    }
    return (parent.getElementsByTagName(elementName)[0].firstChild.nodeValue);
}

// Textarea limit function
// Trims a text area if it's too long
function textCounter(field, countfield, maxlimit) 
{
    if (field.value.length > maxlimit) {
        // If it's too long, trim
        field.value = field.value.substring(0, maxlimit);
    }
    else  {
        // otherwise, update 'characters left' counter
        countfield.value = maxlimit - field.value.length;
    }    
}

// Toggles layers between hidden and visible.
function toggleLayer( whichLayer ) {
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
      elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
      elem = document.layers[whichLayer];
  vis = elem.style;  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
      vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

// Explicitly hides a layer.
function hideLayer( whichLayer ) {
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
      elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
      elem = document.layers[whichLayer];
  vis = elem.style;  // if the style.display value is blank we try to figure it out here
  vis.display = 'none';
}

// Explicitly shows a layer.
function showLayer( whichLayer ) {
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
      elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
      elem = document.layers[whichLayer];
  vis = elem.style;  // if the style.display value is blank we try to figure it out here
  vis.display = 'block';
}

// Adds an option to a select box not using jquery.
function addOptionNormal(selectboxName,id,text)
{
    var optn = document.createElement('option')
    optn.value = id;
    optn.text = text;
    document.getElementById(selectboxName).options.add(optn);
}

// Adds an option to a select box using jquery.
function addOption(selectboxName,id,text,isSelected)
{
    //var optn = document.createElement('option')
    if (isSelected) {
        var $option = $("<option></option>").text(text).attr("id",id).attr("selected", true);
    } else {
        var $option = $("<option></option>").text(text).attr("id",id);
    }
    $("#"+selectboxName).append($option).change();
    //optn.value = id;
    //optn.text = text;
    //selectbox.options.add(optn);
}

function getSelectedValue(formElement) {
    for (var i = 0; i < formElement.options.length; i++) {
        if (formElement.options[i].selected) {
            return (parseInt(formElement.options[i].value));
        }
    }
    return 0;
}

function getSelectedText(formElement) {
    for (var i = 0; i < formElement.options.length; i++) {
        if (formElement.options[i].selected) {
            return (formElement.options[i].text);
        }
    }
    return 0;
}

function setSelectedValue(formElement, valueToSelect) {
    for (var i = 0; i < formElement.options.length; i++) {
        if (formElement.options[i].value == valueToSelect) {
            formElement.options[i].selected = true;
            return;
        }
    }
    return;
}

// executes an onchange function after 500ms (or specified delay)
// global timer ID for the safeOnChange1 function.
var soc_id = null;
function delayOnChange(functionToRun, delay ){
  delay = delay || 500;
  window.clearTimeout(soc_id);
  soc_id = window.setTimeout(functionToRun,delay);
} 

// returns an array of selected items
function getMultiple(multiSelectBox) {
  var selected = new Array();
  for (var i = 0; i < multiSelectBox.options.length; i++) {
    if (multiSelectBox.options[i].selected) {
      var currId = multiSelectBox.options[i].id;
      while (currId.indexOf('asm') != -1) {
        currId = currId.substring(currId.indexOf('asm')+3,currId.length);
      }
      selected.push(currId);
    }
  }
  return selected;
}

// Deselect everything in a select box
function deselectAll(multiSelectBox) {
  for (var i = 0; i < multiSelectBox.options.length; i++) {
    if (multiSelectBox.options[i].selected) {
      multiSelectBox.options[i].selected = false;
    }
  }
}

// A "do nothing" function for links designed to use onclick events
function doScriptLink() {

}

// Disable the specified object
function disableObject(theObject) {
    theObject.disabled = true;
}

// Enable the specified object
function enableObject(theObject) {
    theObject.disabled = false;
}