<!--
// $Id: Common.js,v 1.4 2006/09/25 15:44:25 lsmith Exp $
/**
 * OO AJAX helper methods for Entice sites
 * collected from misc resources and brought together as a collection here
 *
 * @category   HTML
 * @package    ENTICE_E_PBX
 * @author     Lance Smith
 * @copyright  2006 Emergent Network Solutions
 * @license    Emergent Network Solutions
 * @version    Release: @package_version@
 */

var bDOM = (document.getElementById) ? true : false;
var bNS4 = (document.layers) ? true : false;
var bIE = (document.all) ? true : false;
var bIE4 = bIE && !bDOM;
var bIE5 = (navigator.appVersion.indexOf("MSIE 5.") != -1);
var bNS6 = bDOM && !bIE;
var bMac = (navigator.appVersion.indexOf("Mac") != -1);
var bOpera = (navigator.userAgent.indexOf("Opera")!=-1);
var bKonqueror = (navigator.userAgent.indexOf("Konqueror")!=-1);
var bCanPrint = (window.print) ? 1 : 0;


function mouseOnMenu(td){
	mouseOnRow(td);
}

function mouseOffMenu(td){
	mouseOffRow(td);
}

function mouseOnRow(td){
	td.oldclass=td.className;
	td.className=td.className+'_hotrow';
}

function mouseOffRow(td){
	td.className=td.oldclass;
}

function gotoPage(url){

	top.location.href=url;
}


/**
 * Find the element in the current HTML document with the given id or ids
 * once in a while hard to write equates to hard to read:-)
 */
var $;
if (!$ && document.getElementById) {
  $ = function() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
      var element = arguments[i];
      if (typeof element == 'string') {
        element = document.getElementById(element);
      }
      if (arguments.length == 1) {
        return element;
      }
      elements.push(element);
    }
    return elements;
  }
}
else if (!$ && document.all) {
  $ = function() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
      var element = arguments[i];
      if (typeof element == 'string') {
        element = document.all[element];
      }
      if (arguments.length == 1) {
        return element;
      }
      elements.push(element);
    }
    return elements;
  }
}


/**
 * Set the value an HTML element to the specified value.
 */
function setValue (ele, val, escapeHtml) {
 if (val == null) val = "";
 if (escapeHtml) {
      val = val.replace(/&/g, "&amp;");
      val = val.replace(/'/g, "&apos;"); //' fix quote problem
      val = val.replace(/</g, "&lt;");
      val = val.replace(/>/g, "&gt;");
 }

  var orig = ele;
  var nodes, node, i;

  ele = $(ele);

 // We can work with names and need to sometimes for radio buttons
  if (ele == null) {
    nodes = document.getElementsByName(orig);
    if (nodes.length >= 1) {
      ele = nodes.item(0);
    }
  }
  if (ele == null) {
    alert("setValue() can't find an element with id/name: " + orig + ".");
    return;
  }

  if (_isHTMLElement(ele, "select")) {
    if (ele.type == "select-multiple" && _isArray(val)) {
      _selectListItems(ele, val);
    }
    else {
      _selectListItem(ele, val);
    }
    return;
  }

  if (_isHTMLElement(ele, "input")) {
    if (ele.type == "radio") {
      // Some browsers match names when looking for ids, so check names anyway.
      if (nodes == null) nodes = document.getElementsByName(orig);
      if (nodes != null && nodes.length > 1) {
        for (i = 0; i < nodes.length; i++) {
          node = nodes.item(i);
          if (node.type == "radio") {
            node.checked = (node.value == val);
          }
        }
      }
      else {
        ele.checked = (val == true);
      }
    }
    else if (ele.type == "checkbox") {
      ele.checked = val;
    }
    else {
      ele.value = val;
    }
    return;
  }

  if (_isHTMLElement(ele, "textarea")) {
    ele.value = val;
    return;
  }

  // If the value to be set is a DOM object then we try importing the node
  // rather than serializing it out
  if (val.nodeType) {
    if (val.nodeType == 9 /*Node.DOCUMENT_NODE*/) {
      val = val.documentElement;
    }

    val = _importNode(ele.ownerDocument, val, true);
    ele.appendChild(val);
    return;
  }

  // Fall back to innerHTML
  ele.innerHTML = val;
};

/**
 * Is the given node an HTML element (optionally of a given type)?
 * @param ele The element to test
 * @param nodeName eg "input", "textarea" - check for node name (optional)
 *         if nodeName is an array then check all for a match.
 */
function _isHTMLElement(ele, nodeName) {
  if (ele == null || typeof ele != "object" || ele.nodeName == null) {
    return false;
  }

  if (nodeName != null) {
    var test = ele.nodeName.toLowerCase();

    if (typeof nodeName == "string") {
      return test == nodeName.toLowerCase();
    }

    if (_isArray(nodeName)) {
      var match = false;
      for (var i = 0; i < nodeName.length && !match; i++) {
        if (test == nodeName[i].toLowerCase()) {
          match =  true;
        }
      }
      return match;
    }

    alert("_isHTMLElement was passed test node name that is neither a string or array of strings");
    return false;
  }

  return true;
};

function _importNode(doc, importedNode, deep) {
  var newNode;

  if (importedNode.nodeType == 1 /*Node.ELEMENT_NODE*/) {
    newNode = doc.createElement(importedNode.nodeName);

    for (var i = 0; i < importedNode.attributes.length; i++) {
      var attr = importedNode.attributes[i];
      if (attr.nodeValue != null && attr.nodeValue != '') {
        newNode.setAttribute(attr.name, attr.nodeValue);
      }
    }

    if (typeof importedNode.style != "undefined") {
      newNode.style.cssText = importedNode.style.cssText;
    }
  }
  else if (importedNode.nodeType == 3 /*Node.TEXT_NODE*/) {
    newNode = doc.createTextNode(importedNode.nodeValue);
  }

  if (deep && importedNode.hasChildNodes()) {
    for (i = 0; i < importedNode.childNodes.length; i++) {
      newNode.appendChild(_importNode(doc, importedNode.childNodes[i], true));
    }
  }

  return newNode;
}

/**
 * Find an item in a select list and select it. Used by setValue()
 * @param ele The select list item
 * @param val The value to select
 */
function _selectListItem (ele, val) {
  // We deal with select list elements by selecting the matching option
  // Begin by searching through the values
  var found  = false;
  var i;
  for (i = 0; i < ele.options.length; i++) {
    if (ele.options[i].value == val) {
      ele.options[i].selected = true;
      found = true;
    }
    else {
      ele.options[i].selected = false;
    }
  }

  // If that fails then try searching through the visible text
  if (found) return;

  for (i = 0; i < ele.options.length; i++) {
    if (ele.options[i].text == val) {
      ele.options[i].selected = true;
    }
    else {
      ele.options[i].selected = false;
    }
  }
}



/**
 * Array detector. Work around the lack of instanceof in some browsers.
 */
function _isArray (data) {
  return (data && data.join) ? true : false;
};

-->
