/**
* utility.js
* Miscellaneous utility functions.
*/

/**
* A non-destructive means of adding an event to an object.
*/
function addEvent(objObject, strEventName, fnHandler) {
  if (objObject.addEventListener) {
    objObject.addEventListener(strEventName, fnHandler, false);
  } else if (objObject.attachEvent) {
    objObject.attachEvent("on" + strEventName, fnHandler);
  }
}

/**
* Remove an event from an object. This will only remove events added using 
* addEvent().
*/
function removeEvent(objObject, strEventName, fnHandler) {
  if (objObject.removeEventListener) {
    objObject.removeEventListener(strEventName, fnHandler, false);
  } else if (objObject.detachEvent) {
//alert('ie removing');
    objObject.detachEvent("on" + strEventName, fnHandler);
  }
}

/**
* Since IE doesn't support the 'this' keyword for event sources, this function 
* figures it out, cross browser
*/
function getEventSrc(e) {
  if (!e) e = window.event;
  if (e.target) {
    return e.target;
  } else if (e.srcElement) {
    return e.srcElement;
  }
}

/**
* Test a variable to see if it contains an empty value.
* return boolean
*/
function isEmpty(val) {   
	return ((val == null) || (val.length == 0) || (val == ""));
}

/**
 * Blank String Test
 * Tests a string to determine if it contains any non-whitespace characters. 
 * This test will return boolean true if the string consists of whitespace  
 * characters only, if any non-whitespace characters exist it will return boolean 
 * false.
 * return boolean
 */
function isBlank(string) {
// returns true if the string contains only white space characters
  if (string.search(/[^\S]/) == -1) {
    return true;
  } else {
    return false;
  }
}

/**
* Open a page in a new window.
*/
function newWindow() {
  var width = 300;
  var height = 300;
  var left = 25;
  var top = 25;
  if (size) {
    width = size[0];
    height = size[1];
  }
  if (pos) {
    left = pos[0];
    top = pos[1];
  }
  if (null == scroll || true == scroll) scroll = ",scrollbars";
  else scroll = "";
  if (! name) name = new Date().getTime();
  window.open(page,name,"width="+width+",height="+height+",top="+top+",left="+left+",screenX="+top+",screenY="+left+scroll)
}

/**
* Loads a page into the parent window.
* var page string url to be loaded in the parent window
* var closer boolean whether or not to close the current window, default won't
*/
function loadParentWindow(page, closer) {
  self.opener.location = page;
  if (true == closer) {
    window.close();
  }
}

/**
* Strip characters from a string value.
* var str string value to be filtered
* var filter string of characters to be stripped
* return string
*/
function strip(str, filter) {
  var retVal = '';
  for (i=0; i<str.length; i++) {
    if (filter.indexOf(str.charAt(i)) == -1) {
      retVal += str.charAt(i);
    }
  }
  return retVal;
}

/**
* Select a random value from the supplied array.
* var list array of values
* return mixed
*/
function selectRandom(list) {
  return list[Math.ceil(Math.random() * list.length) - 1];
}

/**
* Determines if a value fits within a specified range.
*
* This function is used to test the length of a string, array or numeric 
* quantity. If a string is passed the length of the string will be tested, if 
* a number is passed the value will be tested.
* Arguments:
*   val: The value to be tested. Required
*   min: The minimum length allowed. Required
*   max: Optional length not to be exceeded.
* return boolean
*/
function valueBetween(val, min, max) {
  if ( isNaN ( val ) ) {
    return ( ( val.length >= ( min != null ? min : 0 ) )
              &&  ( val.length <= ( max != null ? max : Number.MAX_VALUE ) ) );
  } else {
    return ( ( val >= ( min != null ? min : Number.MIN_VALUE ) )
              &&  ( val <= ( max != null ? max : Number.MAX_VALUE ) ) );
  }
}

/**
 * Alpha Numeric Test
 * Tests a string to determine if it contains only alpha numeric characters. 
 * Returns boolean true if string contains only alpha numeric characters, false 
 * otherwise.
 * return boolean
 */
function alphaNumeric(str) {
alert('function in strings.js');
  // uses a grep pattern to test for non-alpha numeric characters
  if (str.search(/[^a-z0-9]/i) == -1) {
    return true;
  } else {
    return false;
  }
}

/**
 * If the supplied value is an integer this function will return true, otherwise 
 * it will return false.
 * return boolean
 */
function isInt(str) {
  var retVal = true;
  if (! isDigit(str)) {
    // character other than a number found
    if (str.charAt(0) != '-') {
      // not a negative number
      retVal = false;
    } else if (str.substring(1).search(/[^0-9]/) != -1) {
      // additional non-numberic character(s) found
        retVal = false;
    }
  }
  return retVal;
}

/**
* Checks for any non-numberic characters.
* return boolean
*/
function isDigit(val) {
  if (val.search(/[^0-9]/) != -1) {
    return false;
  }
  return true;
}

/**
 * Removes all white space on the beginning and/or end of a string.
 * Arguments:
 *  str:  String to be trimmed. Required
 *  side: Optional parameter declaring which side to trim, default is to trim 
 *    off both sides.
 * @return string
 */
function trim(str, side) {
  var left = new RegExp(/^\s*/);
  var right = new RegExp(/\s*$/);
  switch (side) {
    case 'left':
    case 'start':
    case 'beginning':
      str = str.replace(left, '');
      break;
    case 'right':
    case 'end':
      str = str.replace(right, '');
      break;
    default:
      str = str.replace(left, '');
      str = str.replace(right, '');
  }
  return str;
}

/**
 * IE does not support document.importNode(), so we can create it's functionality 
 * here. This function is important for bringing objects from one document 
 * (frame) into another.
 */
if(typeof document.importNode != "function"){
  document.importNode = function(importedNode, deep){
    var newNode;

    if(importedNode.nodeType == 1) { // Node.ELEMENT_NODE
      newNode = document.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 = document.createTextNode(importedNode.nodeValue);
    }
    
    if(deep && importedNode.hasChildNodes()){
      for(var i = 0; i < importedNode.childNodes.length; i++) {
        newNode.appendChild(
          document.importNode(importedNode.childNodes[i], true)
        );
      }
    }
    
    return newNode;
  }
}

function getFileNameFromURI(str) {
  var comps = str.split('/');
  return comps[comps.length -1];
}

function stripExtension(str) {
  return str.substring(0, str.lastIndexOf('.'));
}