
  //   Update the JavaScript String object to add 
  //   trim, startsWith, and endsWith methods
  String.prototype.trim = function(){return(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}
  String.prototype.startsWith = function(str){if ((str != null) && (str.length <= this.length) && (this.substring(0, str.length) == str)) {return(true)} else {return(false)}}
  String.prototype.endsWith = function(str){if ((str != null) && (str.length <= this.length) && (this.substring(this.length-str.length) == str)) {return(true)} else {return(false)}}


  //   Add load event should be used to build up a list of functions to run onload
  function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
      window.onload = func;
    } else {
      window.onload = function() {
        if (oldonload) {
          oldonload();
        }
        func();
      }
    }
  }


  //   Get cookie value by name
  function getCookie(strCookieName) {
    if (typeof document.cookie == "undefined")
      return("");
    var strAllCookies = document.cookie;
    var cookieValue = "";
    var i, j, k = 0;
    while ((i = strAllCookies.indexOf(strCookieName + "="), j) > 0) {
      if ((i == 0) || (strAllCookies.substr((i - 1), 1) == ";"))
        break;
      j = i + 1;
    }
    if (i < 0)
      return(cookieValue);
    if ((k = strAllCookies.indexOf(";", (i + strCookieName.length))) > 0) {
      cookieValue = strAllCookies.substring((i + strCookieName.length + 1), k);
    }
    else  {
      cookieValue = strAllCookies.substring(i + strCookieName.length + 1);
    }
    if (cookieValue.startsWith("\"") && cookieValue.endsWith("\"") && (cookieValue.length > 1)) {
      cookieValue = cookieValue.substring(1, (cookieValue.length - 1));
    }
    return(cookieValue);
  }


  //   Put cookie
  function putCookie(strCookieName, strCookieValue, expirationDate) {
    var isDebug = false;
    if (typeof document.cookie == "undefined")
      return;
    var expirationDateGMT = null;
    if ((typeof expirationDate == "object") && (expirationDate != null)) {
      expirationDateGMT = expirationDate.toGMTString();
    }
    if ((typeof document.domain != "undefined") && (document.domain != null) && (document.domain.length > 0)) {
      var cookieDomain = document.domain;
      dotIdx = cookieDomain.indexOf(".");
      if (dotIdx >= 0) {
	cookieDomain = cookieDomain.substring(dotIdx);
      }
      if ((typeof expirationDateGMT != "undefined") && (expirationDateGMT != null)) {
	document.cookie = strCookieName + "=" + escape(strCookieValue) + "; expires=" + expirationDateGMT 
			  + "; path=/; domain=" + cookieDomain;
      }
      else {
	document.cookie = strCookieName + "=" + escape(strCookieValue) + "; path=/; domain=" + cookieDomain;
      }
      if (isDebug) {
	alert("Wrote cookie \"" + strCookieName + "\" with value = \"" + strCookieValue + "\" for domain = \""
		 + cookieDomain + "\" expires = " + expirationDateGMT);
      }
    }
    else {    
      if ((typeof expirationDateGMT != "undefined") && (expirationDateGMT != null)) {
	document.cookie = strCookieName + "=" + escape(strCookieValue) + "; expires=" + expirationDateGMT + "; path=/";
      }
      else {
	document.cookie = strCookieName + "=" + escape(strCookieValue) + "; path=/";
      }
      if (isDebug) {
	alert("Wrote cookie \"" + strCookieName + "\" with value = \"" + strCookieValue + "\" expires = " + expirationDateGMT);
      }
    }
  }


  function clearSiteMinderCookies() {
    putCookie("REASON", "", new Date(0));
    putCookie("SMTRYNO", "", new Date(0));
    putCookie("SMSESSION", "", new Date(0));
    putCookie("sm_email", "", new Date(0));
  }


  function incrementTryCount() {
    var isDebug = false;
    var tryCnt = getCookie("SMTRYNO");
    if (isDebug) {
      alert("From cookie SMTRYNO = " + tryCnt + ", length = " + tryCnt.length + ", type = " + typeof tryCnt);
    }
    if ((typeof tryCnt != "undefined") && (tryCnt != null) && (tryCnt.length > 0)) {
      tryCnt = unescape(tryCnt);
      tryCnt = parseInt(tryCnt);
      if (isNaN(tryCnt)) {
	tryCnt = 0;
      }
    }
    else {
      tryCnt = 0;
    }
    if (typeof tryCnt == "number") {
      tryCnt++;
    }
    else {
      tryCnt = 1;
    }
    if (isDebug) {
      alert("Incremented SMTRYNO = " + tryCnt);
    }
    if (tryCnt > 5) {
      if (isDebug) {
        alert("Redirecting to Unauthenticated page.");
      }
      window.location = "/am/opn/login.unauth";
      return(false);
    }
    else {
      putCookie("SMTRYNO", tryCnt, null);
    }
    return(true);
  }


  //   Get SiteMinder TARGET parameter
  function getSmTarget() {
    var isDebug = false;
    var rawTarget = "";
    var target = "";
    var qs = window.location.search.substring(1, window.location.search.length);
    if ((qs == null) || (qs == "")) {
      return(null);
    }
    //   Extract the TARGET query string value
    var targetMatch = /(?:\&|\?)TARGET=([^&]*)/i.exec(qs);
    if ((typeof targetMatch != "undefined") && (targetMatch != null) && (targetMatch.length > 1)) {
      rawTarget = unescape(targetMatch[1]);
      if (isDebug) {
	alert("TARGET from query string = \"" + rawTarget  + "\"");
      }
    }
    if ((typeof rawTarget == "undefined") || (rawTarget == null) || (rawTarget.length <= 0)) {
      //   If the TARGET wasn't available in the query string, try to get it from a cookie
      rawTarget = getCookie("smTargetUrl");
      if (rawTarget.startsWith("\"") && rawTarget.endsWith("\"") && (rawTarget.length > 1)) {
        rawTarget = rawTarget.substring(1, (rawTarget.length - 1));
      }
      if (isDebug) {
	alert ("TARGET from cookie (smTargetUrl) = " + rawTarget);
      }
    }
    if ((typeof rawTarget != "undefined") && (rawTarget != null) && (rawTarget.length > 0)) {
      //   Determine if it is SiteMinder Escaped
      var smEscapeChar = "";
      if (rawTarget.startsWith("$SM$")) {
	//   Older style SiteMinder escaping
	smEscapeChar = "$";
	smEscapedTarget = rawTarget.substring(4);
      }
      else if (rawTarget.startsWith("-SM-")) {
	//   Newer style SiteMinder escaping
	smEscapeChar = "-";
	smEscapedTarget = rawTarget.substring(4);
      }
      else {
	smEscapeChar = "";
	smEscapedTarget = "";
	target = rawTarget;
      }
      if (isDebug) {
	alert("SM escape char = " + smEscapeChar + "\nSM escaped URL = " + smEscapedTarget);
      }
      //   SM Unescape the value, if it is SM escaped
      if ((smEscapedTarget != "") && (smEscapeChar != "")) {
	var idxEscapeChar = smEscapedTarget.indexOf(smEscapeChar);
	while (idxEscapeChar >= 0) {
	  if (idxEscapeChar > 0) {
	    target += smEscapedTarget.substring(0, idxEscapeChar);
	    smEscapedTarget = smEscapedTarget.substring(idxEscapeChar + 1);
	  }
	  else {
	    smEscapedTarget = smEscapedTarget.substring(1);
	  }
	  if (isDebug) {
	    alert("Target fragment = " + target + "\nSM Escaped Target fragment = " + smEscapedTarget);
	  }
	  if (smEscapedTarget.startsWith("%")) {
	    //   Escaped preexisting (i.e., before SM escaping) URL encoding
	    target += smEscapedTarget.substring(0, 3);
	    smEscapedTarget = smEscapedTarget.substring(3);	    
	  }
	  else {
	    target += escape(smEscapedTarget.substring(0, 1));
	    smEscapedTarget = smEscapedTarget.substring(1);	    
	  }
	  if (isDebug) {
	    alert("Target fragment = " + target + "\nSM Escaped Target fragment = " + smEscapedTarget);
	  }
	  idxEscapeChar = smEscapedTarget.indexOf(smEscapeChar);
	}
	if (smEscapedTarget != "") {
	  target  += unescape(smEscapedTarget);
	}
      }
      //   Clean up the resulting URL
      if ((target != null) && (target.length > 0)) {
        //   Clean up the resulting URL
        if (target.startsWith("HTTP:")) {
	  target = "http:" + target.substring(5);
        }
        else if (target.startsWith("HTTPS:")) {
  	  target = "https:" + target.substring(6);
        }
        if ((target.length > 1) && (target.endsWith("?"))) {
  	  target = target.substring(0, target.length-1);
        }
      }
      if (isDebug) {
	if (smEscapeChar != "") {
	  alert("TARGET SM unescaped =  \"" + target + "\"");
	}
	else {
	  alert("TARGET not SM escaped =  \"" + target + "\"");
	}
      }
    }
    else if (isDebug) {
	alert("TARGET not found");
    }

    return(target);
  }


  //   Get TARGET parameter and set the TARGET form field value to it.
  //   NOTE: SiteMinder unescape the value of TARGET, if it's SiteMinder escaped.
  function setTarget() {
    var isDebug = false;
    var target = getSmTarget();
    if (target != null) {
      document.loginForm.target.value = target;
    }
    if (isDebug) {
        alert("Login Target field value = " + document.loginForm.target.value);
    }
    incrementTryCount();
  }


  //   Get TARGET parameter and set the TARGET form field value to it.
  //   If all goes well, return true, so the submission goes through.
  //   NOTE: SiteMinder unescape the value of TARGET, if it's SiteMinder escaped.
  function setTargetOnSubmit(formObj) {
    var isDebug = false;
    if ((formObj == null) || (typeof formObj != "object")) {
        if (isDebug) {
	  alert("formObj parameter is null or wrong type - should be \"object\"");
        }
	return(false);
    }
    if ((formObj.target == null) || (formObj.target == "undefined")) {
        if (isDebug) {
	  alert("formObj field \"target\" not found");
        }
	return(false);
    }
    var target = getSmTarget();
    if (target != null) {
      formObj.target.value = target;
    }
    else {
      if (isDebug) {
        alert("Target field value = null");
      }
      return(false);
    }   
    if (isDebug) {
        alert("Login Target field value = " + formObj.target.value);
    }
    return(incrementTryCount());
  }


  //   The display error function makes the failed login message visable.
  function displayError() {
    var isDebug = false;
    var errorCode = getCookie("REASON");
    if (isDebug) {
      alert("REASON = " + errorCode);
    }
    if ((typeof errorCode != "undefined") && (errorCode != null) && (errorCode.length > 0)) {
      errorCode = parseInt(errorCode);
      if (isNaN(errorCode)) {
        errorCode = 0;
      }
      if (errorCode > 0) {
	var errorMsgObj = document.getElementById('errorMsg');
	if ((typeof errorMsgObj != "undefined") && (typeof errorMsgObj.style != "undefined")) {
	  errorMsgObj.style.display = "";
	} 
	var noErrorMsgObj = document.getElementById('noErrorMsg');
	if ((typeof noErrorMsgObj != "undefined") && (typeof noErrorMsgObj.style != "undefined")) {
	  noErrorMsgObj.style.display = "none";
	} 
      }
    }
  }


  //   This function sets the originUrl cookie based on the current Web page and
  //   redirects the vistor to the target URL.
  function setOriginAndRedirect(targetUrl) {
    var isDebug = false;
    if (isDebug) {
      alert("Creating originUrl cookie with value of window.location = " + window.location);
    }
    putCookie("originUrl", window.location, null);
    if (isDebug) {
      alert("Redirecting to: " + targetUrl);
    }
    window.location = targetUrl;
  }

