/**
  General cross-browser functions used by most pages and function libraries.
  Last Update 12/8/2003 M2
  (c) 2000-2003 Expeditors International of Washington, Inc. Business confidential and 
  proprietary, this information may not be reproduced in any form without advance written 
  consent of an authorized officer of the copyright holder.
*/

// bit 'o browser sniffing
var agt=navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);
var iePos  = appVer.indexOf('msie');
if (iePos !=-1) {
  is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)));
  is_major = parseInt(is_minor);
}
var isMinNS4 = ((navigator.appName.indexOf("Netscape") >= 0 || agt.indexOf("gecko") >= 0) && is_major >= 4) ? 1 : 0;
var isMinNS6 = (agt.indexOf("gecko") >=0 && is_major >= 5) ? 1 : 0;
var isMinIE4 = (document.all) ? 1 : 0;
var isMinIE5 = (isMinIE4==1 && is_major >= 5) ? 1 : 0;
var isMinIE6 = (isMinIE4 && is_major >= 6) ? 1 : 0;
var isDOM = (document.getElementById) ? 1 : 0;

/**
 Returns a single named DOM element or null if not found.
 @param elementName String _ID_ of element you want to retrieve
 @return element if found or null.
*/
function get(elementName) {
  var ret = null;
  if (isDOM == 1) {
    if(document.getElementById(elementName)) {
      ret = document.getElementById(elementName);
    }
  }  else if (isMinIE4 == 1) {
    if(document.all(elementName)[0]) {
      ret = document.all(elementName)[0];
    }  else {
      ret = document.all(elementName);
    }
  }
  return ret;
}

/** 
 Supports fwdSubmit requests that occur on pages that have more than one 
 function in more than one form.  Returns null if no element with NAME or ID 
 Attributes exists within the passed form.
 @param form object refrence to form you want to look in.  Form must have ID attribute and 
 that ID must be unique to the page 
 @param elementName - Normally "ACTION" but could be anything. Must be either the NAME or ID
   Attribute value in the form.
 @ return javascript object element or null if not found
 @see fwdSubmit() below
*/
function getObjectInForm(form, elementName) {
  var ret = null;
  if (isDOM == 1) {
    if(form.elements[elementName]) {
      ret = form.elements[elementName];
    }
  }
  return ret;
}

/**
 Returns a collection of DOM elements with the specified tagname.
 @param source a DOM Element, typically document, but may be any container
 @param tagName String HTML tag to look for, CAPITALIZED 'cause its picky
 @return parent element or zero-length array if not found
*/
function getAllByName(source, tagName) {
  var ret = new Array();
  if (isDOM){
    ret = source.getElementsByTagName(tagName);
  }
  else if (isMinIE4) {
    ret = source.all.tags(tagName)
  }
  return ret;
}

/**
 Return the parent element of type tag where found.  If tag equals source.tagName, 
 returns original source value - this means you can't search for a div tag
 parent of a div tag using this function.  Unsafe to change longstanding behavior.
 @param source DOM element node to return parent of
 @param tag String HTML tag to look for, CAPITALIZED
 @return parent element of requested type or null if not found
*/
function getParent(source, tag) {
  // prevent going off the top of the document
  while((source) && source.tagName != tag) {
    source = source.parentNode ? source.parentNode : null;
  }
  return source;
}

/**
 Return width of the window's content area.  May differ between browsers.
 @return int width of window content area or 0 if something weird happens
*/
function windowWidth(){
  if (window.innerWidth) {
    return parseInt(window.innerWidth);
  } else if (document.body.parentNode.clientWidth) {
    return parseInt(document.body.parentNode.clientWidth); 
  } else if (document.body.clientWidth) {
    return parseInt(document.body.clientWidth);
  } else {
    return 0;
  }
}

/**
 Return height of the window's content area.  May differ between browsers.
 @return int height of window content area or 0
*/
function windowHeight(){
  if (window.innerHeight) {
    return parseInt(window.innerHeight);
  } else if (document.body.parentNode.clientHeight) {
    return document.body.parentNode.clientHeight;
  } else if (document.body.clientHeight) {
    return document.body.clientHeight;
  } else {
    return 0;
  }
}

/**
 For working with EI app framework, set a hidden field named "Action" to
 the supplied 'action' param value, then submit the form.  Note that the
 form.Action hidden input field is different from the form.action attribute.
 @form Form object reference
 @action String to place in the form.Action hidden field
*/
function fwdSubmit(form, action){
  var correctAction = getObjectInForm(form, "Action");
  correctAction.setAttribute("value", action);
  form.submit();
}
  

/* attachMouseBehavior function to attach mouse behavior to any set of objects
** @author Matt Shrader
** @param source - the javascript object that is the container for the tags you wish to attach events to
** @param blockBegin - tells the method where in the collection to begin the event attachment
** @param tag - the tag name of the objects to be altered - must be upper case for some reason
** @param mouseover - the name of the script object used for the mouseover event ( pass the function name with no parens or quotes )
** @param mouseout - see mouseover
** @param click - see above
*/
function attachMouseBehavior(source, tag, blockBegin, mouseover, mouseout, click){
  var objCollection = getAllByName(source,tag);
  for (x=blockBegin; x<=objCollection.length -1;x++){
    obj = objCollection.item(x);
    if(document.all){
        if (isMinIE4 ==1 && isMinIE5 != 1 &&  isMinIE6 != 1){
          //no op for IE4 - must be written into the table
        }
        else{
          //IE 5+
          if(mouseover != null){ var overEvent = obj.attachEvent("onmouseover", mouseover);}
          if(mouseout != null ){ var outEvent = obj.attachEvent("onmouseout", mouseout);}
          if (click != null){
            source.style.cursor=(isMinIE5==1)?"hand":"pointer";
            var outEvent = obj.attachEvent("onclick", click);
            obj.title= "Click to edit";
          }//if
        }//if
      }//if
      else{
        //NS6+
        if(mouseover != null){ obj.addEventListener("mouseover", mouseover, false);}
        if(mouseout != null ){ obj.addEventListener("mouseout", mouseover, false);}
        if (click != null){
          source.style.cursor="pointer"
          obj.addEventListener("click", click, false);
          obj.title= "Click to edit";
        }//if
      }//if
    }//for
  }//attachMouseBehavior
  
  /*arraySwap swaps the position of target1 and target2 in the specified array
  * @param target1 int
  * @param target2 int
  * @param  array  Array - the array to operate on
  */
  function arraySwap(target1, target2, array){
    var tmpNode = array[target2];
    array[target2] = array[target1];
    array[target1] = tmpNode;
  }

/** Convience method to resolve the correct reference to the browsers event object.  Access
  ** to this object is critical and (of course) varies between browsers. Should not be needed by 
  ** Java Developers but put here as it is used everywhere.
**/
  function resolveEventTargetObject(e){
  var whichInput; 
    
    if(document.all){
      whichInput = window.event.srcElement;
    }
    else{
      whichInput = e.currentTarget;
    }
    return whichInput;  
  }//function
  
/** Used to determine if the keyCode passed in is in the alphanumeric part of the keyboard
    * Arrow Keys, enter key, tab key etc return false.  
    * @param keyCode Integer value of the ascii code of the key to be checked. Obtainable from event.keyCode
    * @return boolean.
*/ 
function isAlphaNumericKey(keyCode){
// Do I need to deal with function keys?
  switch(keyCode){
  case 8:
  case 9:
  case 13:
  case 16:
  case 17:
  case 18:
  case 19:
  case 20:
  case 27:
  case 33:
  case 34:
  case 35:
  case 36:
  case 37:
  case 38:
  case 39:
  case 40:
  case 45: 
  case 46:
  case 91:
  case 92:
  case 93:
  case 144:
    return false;
    break;
default:
return true;
break;

  }
}

/**
 Returns a reference to the element that triggered the event.
 Originally in eventHandling.js
 @param evt Event object reference (IE or NS)
*/
function getElementFromEvent(evt) {
  evt = (evt) ? evt : ((event) ? event : null);
  if (evt) {
    var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    return elem;
  }
  return null;
}
