// lib_core.js - Main library of common javascript code and "global" variables

/*** Global Variable Declarations ***/
  /* KEY:
   * WR: Writable - You can specify custom values for these.
   * RO: Read Only - Do NOT attempt to set these directly!
   */

/*** end global variables ***/


/*** String manipulation functions (common to most libraries) ***/
  // ltrim(string) : Returns a copy of a string without leading spaces.
  function ltrim(str) {
    str = str.replace(/^\s+/g, "");
    return str;
  }

  // rtrim(string) : Returns a copy of a string without trailing spaces.
  function rtrim(str) {
    str = str.replace(/\s+$/g, "");
    return str;
  }

  // trim(string) : Returns a copy of a string without leading or trailing spaces.
  function trim(str) {
    // do a left trim first
    str = ltrim(str);
    // then do a right trim
    str = rtrim(str);
    return str;
  }

  // doTrim(thisElement) : Reassigns a trimmed string back to the form element.
  function doTrim(thisElement) {
    thisElement.value = trim(thisElement.value);
  }

  // removeLinks(thisString) : Remove HREF tags from a link, and return only the inner text.
  function removeLinks(thisString) {
    str = thisString.replace(/<a.+>(.+)<\/a>/gi, "$1");
    return str;
  }

  // removeScripts(thisString) : Remove all SCRIPT tags and their contents.
  function removeScripts(thisString) {
    str = thisString.replace(/((<[\s\/]*script\b[^>]*>)([^>]*)(<\/script>))/gi, "");
    return str;
  }

  // removeComments(thisString) : Remove all COMMENT (<!-- -->) tags and their contents.
  function removeComments(thisString) {
    str = thisString.replace(/<\!--(.+)-->/g, "");
    return str;
  }

  // removeWhitespace(thisString) : Remove all excess whitespace (replace with single space).
  function removeWhitespace(thisString) {
    str = thisString.replace(/\r+/g, " ");
    str = thisString.replace(/\n+/g, " ");
    str = thisString.replace(/\s+/g, " ");
    return str;
  }

  // fixNumber(obj) : Strips commas and non-numeric values, returns float value
  function fixNumber(obj) {
    val = obj.value;
    if (val=='') return '';
    return parseFloat(val.split(',').join(''));
  }

  // regexCheck(string,pattern) : Returns whether a given string matches the given pattern.
  function regexCheck(teststring,pattern) {
    var returnVar = true;
    if (teststring != null && teststring != undefined && !pattern.test(teststring)) {
      returnVar = false;
    }
    return returnVar;
  }

  // trimCommas(myString) : Commonly used to clean up a comma-delimited list.
  function trimCommas(myString) {
    // Remove leading commas
    myString = myString.replace(/^\,+/g, "");
    // Remove trailing commas
    myString = myString.replace(/\,+$/g, "");
    return myString;
  }
/*** end of string manipulation functions ***/


/*** Cookie manipulation functions (includes supporting functions) ***/
  // Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
  if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(v,b,s) {
      for( var i = +b || 0, l = this.length; i < l; i++ ) {
        if( this[i]===v || s && this[i]==v ) { return i; }
      }
      return -1;
    }
  }

  // Array.unique( strict ) - Remove duplicate values
  if (!Array.prototype.unique) {
    Array.prototype.unique = function(b) {
      var a = [], i, l = this.length;
      for( i=0; i<l; i++ ) {
        if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
      }
      return a;
    }
  }

  // Simple function for setting a cookie
  function createCookie(cookieName, cookieValue, expDays) {
  	var path = "/";
  	var newdate = new Date();
  	newdate.setTime(newdate.getTime()+(1000*60*60*24*expDays));
  	var expires = newdate.toGMTString();
  	document.cookie = cookieName + "=" + cookieValue + "; expires=" + expires + "; path=" + path + ";";
  }

  // Delete a cookie by setting an expired cookie by the same name
  function deleteCookie(cookieName) {
    createCookie(cookieName,"",-1);
  }

  // Read all the values from a given cookie, by name
  function readCookie(cookieName) {
    var nameEQ = cookieName + "=";
    var cookieArray = document.cookie.split(";");
    for(var i=0;i < cookieArray.length;i++) {
      var thisCookie = trim(cookieArray[i]);
      if (thisCookie.indexOf(nameEQ) == 0) {
        return thisCookie.substring(nameEQ.length,thisCookie.length);
      }
    }
    return null;
  }
/*** end of cookie manipulation functions ***/


/*** Location functions (find mouse/field position on-screen) ***/
  // Form field geometry detection functions (finds position of an object on the screen)
  function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
      while (obj.offsetParent) {
        curleft += obj.offsetLeft;
        obj = obj.offsetParent;
      }
    } else if (obj.x) {
      curleft += obj.x;
    }
    return curleft;
  }
  function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
      while (obj.offsetParent) {
        curtop += obj.offsetTop;
        obj = obj.offsetParent;
      }
    } else if (obj.y) {
      curtop += obj.y;
    }
    return curtop;
  }

  // Set the browser to run captureMousePosition whenever the mouse is moved
  document.onmousemove = captureMousePosition;

  // Global variables
  var xMousePos = 0; // Horizontal position of the mouse on the screen
  var yMousePos = 0; // Vertical position of the mouse on the screen
  var xMousePosMax = 0; // Width of the page
  var yMousePosMax = 0; // Height of the page

  function captureMousePosition(evt) {
    // When the page scrolls in Netscape, the event's mouse position
    // reflects the absolute position on the screen. innerHight/Width
    // is the position from the top/left of the screen that the user is
    // looking at. pageX/YOffset is the amount that the user has
    // scrolled into the page. So the values will be in relation to
    // each other as the total offsets into the page, no matter if
    // the user has scrolled or not.

    // When the page scrolls in IE, the event's mouse position
    // reflects the position from the top/left of the screen the
    // user is looking at. scrollLeft/Top is the amount the user
    // has scrolled into the page. clientWidth/Height is the height/
    // width of the current page the user is looking at. So, to be
    // consistent with Netscape (above), add the scroll offsets to
    // both so we end up with an absolute value on the page, no
    // matter if the user has scrolled or not.

    if (evt) {
      xMousePos = evt.pageX;
      yMousePos = evt.pageY;
      xMousePosMax = window.innerWidth + window.pageXOffset;
      yMousePosMax = window.innerHeight + window.pageYOffset;
    } else {
      xMousePos = window.event.x + document.body.scrollLeft;
      yMousePos = window.event.y + document.body.scrollTop;
      xMousePosMax = document.body.clientWidth + document.body.scrollLeft;
      yMousePosMax = document.body.clientHeight + document.body.scrollTop;
    }

    // Comment out the line below to HIDE the coords in the status bar.
    // window.status = "xMousePos=" + xMousePos + ", yMousePos=" + yMousePos + ", xMousePosMax=" + xMousePosMax + ", yMousePosMax=" + yMousePosMax + ", Width=" + screen.width + ", Height=" + screen.height;
  }
/*** end location functions ***/


/*** Element Manipulations Functions ***/
  // Allow easy access to properties of an HTML element, and perform certain actions using a single call.
  function initElement(elementID) {
    this.handle = document.getElementById(elementID); // Handle to the element, within this object.

    this.hidden = function() {
      if (this.handle) { this.handle.style.visibility = "hidden"; }
    } // this.makeHidden()

    this.visible = function() {
      if (this.handle) { this.handle.style.visibility = "visible"; }
    } // this.makeVisible()

    this.collapse = function() {
      if (this.handle) { this.handle.style.display = "none"; }
    } // this.collapse()

    this.expand = function() {
      if (this.handle) { this.handle.style.display = "block"; }
    } // this.expand()

    this.inline = function() {
      if (this.handle) { this.handle.style.display = "inline"; }
    } // this.inline()
  } // initElement
/*** end element manipulations ***/


/*** Special Functions ***/
  // Stop all processing in the browser
  function stopProcessing(evt) {
    try { evt.stopPropagation(); } catch (e) { /* Die quietly if not supported. */ }
    try { evt.preventDefault(); } catch (e) { /* Die quietly if not supported. */ }
    try { event.cancelBubble = true; } catch (e) { /* Die quietly if not supported. */ }
    try { event.returnValue = false; } catch (e) { /* Die quietly if not supported. */ }
    return false;
  }

  // Hide any messages in the status bar
  function hideStatus() {
    window.status = "";
    return true;
  }

  // Stop processing during an onsubmit (event handle is passed)
  function handleSubmit(evt) {
    stopProcessing(evt);
  }

  // Define the message display routine, which we depend on for various features
  function showErrMsg(myMessage) {
    var textType = "error";
    var thisAlertBox = document.getElementById("alertBox");
    if (myMessage != "") { // Text is present, so display as needed
      if (arguments.length >= 2) { textType=arguments[1]; }
      if (thisAlertBox) {
        thisAlertBox.innerHTML = "<span class='" + textType + "'>" + myMessage + "</span><br/>" + thisAlertBox.innerHTML;
        thisAlertBox.style.display = "block";
        thisAlertBox.scrollIntoView();
      } else {
        alert(myMessage);
      }
    } else { // Text is empty, so hide the box and clear any prior messages
      if (thisAlertBox) {
        thisAlertBox.innerHTML = "";
        thisAlertBox.style.display = "none";
      }
    }
    return true;
  }
/*** end special functions ***/

// Set a global flag that indicates this library has been loaded by the browser.
var LIB_CORE_LOADED = true;

// End library