/**
 *  @fileoverview   This file contains global functions used throughout site.
 *//*
 *  Variables
 *  Functions
 *    clearErrorMsgs()
 *    isBlank(value)
 *    isDate(value)
 *    isEmail(value)
 *    isNumeric(value)
 *    isTime(value)
 *    numcmp(n1, n2)
 *    openWin(uri)
 *    pop(), push(item)
 *    reloadPage()
 *    setSizes()
 *    strcmp(s1, s2)
 *    swapImg(id, img)
 *    swrite_r(a)
 *    toggleLogin()
 *    toggleOverlay(id)
 *    toggleViz(id)
 *    txtInputBlur(el)
 *    txtInputFocus(el)
 *    write_r(a)
 *
 *  AJAX function
 *    makeXMLHttpRequest()
 *
 *  String methods
 *    .replaceChar(tar, rep)
 *    .stripSpaces()
 *    .strReverse()
 *    .trimLeft()
 *    .trimRight()
 *    .trim()
 *    .ungetc(s)
 */

// VARIABLES

var flashErrMsg = "ERROR: Call to Flash failed";

var hdrLoginBg = new Image(159, 19);
hdrLoginBg.src = updir + "imgs/login_bg.gif";

var hdrLoginBgEmail = new Image(159, 19);
hdrLoginBgEmail.src = updir + "imgs/login_bg_email.gif";

var hdrLoginBgPwd = new Image(159, 19);
hdrLoginBgPwd.src = updir + "imgs/login_bg_pwd.gif";

var hdrLoginBtnOut = new Image(50, 21);
hdrLoginBtnOut.src = updir + "imgs/hdr_login_btn.png";

var hdrLoginBtnOver = new Image(50, 21);
hdrLoginBtnOver.src = updir + "imgs/hdr_login_btn_over.png";

var hdrXBtnOut = new Image(50, 21);
hdrXBtnOut.src = updir + "imgs/hdr_x_btn.png";

var hdrXBtnOver = new Image(50, 21);
hdrXBtnOver.src = updir + "imgs/hdr_x_btn_over.png";

var loginBtnOut = new Image(87, 24);
loginBtnOut.src = updir + "imgs/login_btn.gif";

var loginBtnOver = new Image(87, 24);
loginBtnOver.src = updir + "imgs/login_btn_over.gif";

var submitBtnOut = new Image(87, 24);
submitBtnOut.src = updir + "imgs/submit_btn.gif";

var submitBtnOver = new Image(87, 24);
submitBtnOver.src = updir + "imgs/submit_btn_over.gif";

var ftrAboutOut = new Image(47, 15);
ftrAboutOut.src = updir + "imgs/ftr_link_about.png";

var ftrAboutOver = new Image(47, 15);
ftrAboutOver.src = updir + "imgs/ftr_link_about_over.png";

var ftrResOut = new Image(91, 15);
ftrResOut.src = updir + "imgs/ftr_link_res.png";

var ftrResOver = new Image(91, 15);
ftrResOver.src = updir + "imgs/ftr_link_res_over.png";

var ftrTermsOut = new Image(70, 15);
ftrTermsOut.src = updir + "imgs/ftr_link_terms.png";

var ftrTermsOver = new Image(70, 15);
ftrTermsOver.src = updir + "imgs/ftr_link_terms_over.png";

var ftrPrivacyOut = new Image(83, 15);
ftrPrivacyOut.src = updir + "imgs/ftr_link_privacy.png";

var ftrPrivacyOver = new Image(83, 15);
ftrPrivacyOver.src = updir + "imgs/ftr_link_privacy_over.png";

var ftrLegalOut = new Image(77, 15);
ftrLegalOut.src = updir + "imgs/ftr_link_legal.png";

var ftrLegalOver = new Image(77, 15);
ftrLegalOver.src = updir + "imgs/ftr_link_legal_over.png";

var ftrTvspotOut = new Image(82, 15);
ftrTvspotOut.src = updir + "imgs/ftr_link_tvspot.png";

var ftrTvspotOver = new Image(82, 15);
ftrTvspotOver.src = updir + "imgs/ftr_link_tvspot_over.png";

var ftrEsOut = new Image(57, 15);
ftrEsOut.src = updir + "imgs/ftr_link_es.png";

var ftrEsOver = new Image(57, 15);
ftrEsOver.src = updir + "imgs/ftr_link_es_over.png";

var ftrAdaOut = new Image(89, 33);
ftrAdaOut.src = updir + "imgs/ftr_logo_ada.png";

var ftrAdaOver = new Image(89, 33);
ftrAdaOver.src = updir + "imgs/ftr_logo_ada_over.png";

var ftrAdcOut = new Image(45, 33);
ftrAdcOut.src = updir + "imgs/ftr_logo_adc.png";

var ftrAdcOver = new Image(45, 33);
ftrAdcOver.src = updir + "imgs/ftr_logo_adc_over.png";

var ftrNclrOut = new Image(60, 33);
ftrNclrOut.src = updir + "imgs/ftr_logo_nclr.png";

var ftrNclrOver = new Image(60, 33);
ftrNclrOver.src = updir + "imgs/ftr_logo_nclr_over.png";

var ftrAadeOut = new Image(55, 33);
ftrAadeOut.src = updir + "imgs/ftr_logo_aade.png";

var ftrAadeOver = new Image(55, 33);
ftrAadeOver.src = updir + "imgs/ftr_logo_aade_over.png";

var ftrWidgetOut = new Image(84, 22);
ftrWidgetOut.src = updir + "imgs/get_widg_btn.png";

var ftrWidgetOver = new Image(84, 22);
ftrWidgetOver.src = updir + "imgs/get_widg_btn_over.png";

/**
 *  Clears the inner HTML and hides error-message span elements.
 *
 *  @type   void
 */
function clearErrorMsgs()
{
  var msgs = document.getElementsByTagName("span");

  for (var i = 0; i < msgs.length; i++) {
    if (msgs[i].className == "error") {
      msgs[i].innerHTML = "";
    }
  }
}

/**
 *  Tests string variable for content.
 *
 *  @param  {String} value
 *  @type   boolean
 */
function isBlank(value)
{
  return (value.trim() == "");
}

/**
 *  Tests whether a string is in a valid date format.
 *
 *  @param  {String} value
 *  @type   boolean
 */
function isDate(value)
{
  if (isNaN(Date.parse(value))) {
    return false;
  }
  return true
}

/**
 *  Tests whether a string is in a valid email format. Input must match regx format
 *  /[%\+-\.\w]+&#64;[-\.a-z0-9]+\.[\w]+/i
 *
 *  @param  {String} value
 *  @type   boolean
 */
function isEmail(value)
{
  if (value != "" && !/[%\+-\.\w]+@[-\.a-z0-9]+\.[\w]+/i.test(value)) {
    return false;
  }
  return true;
};

/**
 *  Tests whether value is numeric.
 *
 *  @param  {String} value
 *  @type   boolean
 */
function isNumeric(value)
{
  return !isNaN(stripSeparators(value.trim()));
}

/**
 *  Tests whether a string is in a valid time format. Input must match regx format
 *  /\d\d:\d\d:\d\d/
 *
 *  @param  {String} value
 *  @type   boolean
 */
function isTime(value)
{
  if (value != "" && !/\d\d:\d\d:\d\d/.test(value)) {
    return false;
  }
  return true;
};

/**
 *  Compares two numbers for sorting. See strcmp().
 *
 *  @param  {numeric} n1
 *  @param  {numeric} n2
 *  @type   int
 *  @return If n1 > n2 then (> 0) else if n1 == n2 then 0 else (< 0) (i.e. n1 < n2)
 */
function numcmp(n1, n2)
{
  return n1 - n2;
}

var popupWin;

/**
 *  Opens a popup window and loads the uri in it.
 *
 *  @param  {String} uri
 *  @type   void
 */
function openWin(uri)
{
  try {
    popupWin.close();
  }
  catch (e) {}

  var width = (document.all) ? 504 : 486;
  var height = (window.screen.availHeight > 684) ? 684 : window.screen.availHeight;

  var features = "scrollbars,width=" + width + ",height=" + height;

  var htmlObj = document.getElementsByTagName("html")[0];

  var puX = htmlObj.clientWidth / 2 - width / 2;
  var puY = window.screen.availTop;

  popupWin = window.open(uri, "popup_win", features);
  popupWin.moveTo(puX, puY);
}

/**
 *  Creates a FIFO stack.
 */
var stack = new Array();

function pop()
{
  return stack.pop();
}

function push(item)
{
  stack.push(item);
}

/**
 *  Wrapper for location.reload method.
 *
 *  @type   void
 */
function reloadPage()
{
  location.reload();
}

/**
 *  Event handler for window.onload and .onresize. Adjusts the height and width of the the mask,
 *  content background and flash container.
 *
 *  @global {String} page
 *  @type   void
 */
function setSizes()
{
  var minFlashWidth = 1000;
  var minFlashHeight = 600;

  // viewport size
  var html = document.getElementsByTagName("HTML")[0];
  var vpWidth = html.clientWidth;
  var vpHeight = html.clientHeight;

  // overlay mask
  var mask = document.getElementById("mask");

  var headerHeight = document.getElementById("header").offsetHeight;
  var footer = document.getElementById((page == "mea") ? "footer_mea" : "footer");
  var footerHeight = footer.offsetHeight;
  var dumpHeight = (document.getElementById("dump_div")) ?
    document.getElementById("dump_div").offsetHeight : 0;

  var headFootHeight = headerHeight + footerHeight + dumpHeight;

  var flashContainer = document.getElementById("flash_container");
  var contentBgId = "content_bg";
  var contentBg, flashWidth, flashHeight;

  switch (page) {
    case "mea":
      contentBgId = "mea_bg";
      break;

    default:
      break;
  }

  contentBg = document.getElementById(contentBgId);

  
  if ((flashWidth = vpWidth) < minFlashWidth) {
    if (flashContainer) {
      flashContainer.style.width = minFlashWidth + "px";
    }

    contentBg.style.width = minFlashWidth + "px";
    mask.style.width = minFlashWidth + "px";
  }
  else {
    if (flashContainer) {
      flashContainer.style.width = flashWidth + "px";
    }

    contentBg.style.width = flashWidth + "px";
    mask.style.width = flashWidth + "px";
  }

  if ((flashHeight = vpHeight - headFootHeight) < minFlashHeight) {
    if (flashContainer) {
      flashContainer.style.height = minFlashHeight + "px";
    }
  }
  else {
    if (flashContainer) {
      flashContainer.style.height = flashHeight + "px";
    }
  }

  mask.style.height = html.scrollHeight + "px";
}

window.onload = setSizes;
window.onresize = setSizes;

/**
 *  Compares two strings for sorting. See numcmp().
 *
 *  @param  {String} s1
 *  @param  {String} s2
 *  @type   int
 *  @return If s1 > s2 then 1 else if s1 == s2 then 0 else -1 (i.e. s1 < s2)
 */
function strcmp(s1, s2)
{
  if (s1 > s2) {
    return 1;
  }

  if(s1 == s2) {
    return 0;
  }

  return -1;
}

/**
 *  Swaps an image by changing its src.
 *
 *  @param  {String} id   ID attribute of image element to replace
 *  @param  {Image} img   replacement Image object
 *  @type   void
 */
function swapImg(id, img)
{
  if (document.getElementById) {
    document.getElementById(id).src = img.src;
  }
  else if (document.all) {
    document.all[id].src = img.src;
  }
}

/**
 *  DEBUG FUNCTION: Parses an array and converts the elements to a string.
 *
 *  @param  {Array} a
 *  @type   String
 */
function swrite_r(a)
{
  var s = "array [key] : value\n";

  for (item in a) {
    s += "  [" + item + "] : " + a[item] + "\n";
  }

  return s;
}

/**
 *  Reverses the visibility of the login form. Hides the underlying elements.
 *
 *  @type   void
 */
function toggleLogin()
{
  clearErrorMsgs();
  toggleViz("hdr_login_form");
  toggleViz("hdr_login");
  toggleViz("hdr_forgot_pwd");
}

/**
 *  Reverses the visibility of an overlay and the mask. Also clears previous error messages.
 *
 *  @param  {String} id   id of overlay to show/hide
 *  @type   void
 */
function toggleOverlay(id)
{
  clearErrorMsgs();
  toggleViz("mask");
  toggleViz(id);
}

/**
 *  Reverses the visibility of an element. A hidden element's visibility must be defined in its
 *  style attribute.
 *
 *  @param  {String} id   id of element to show/hide
 *  @type   void
 */
function toggleViz(id)
{
  var el = document.getElementById(id);

  el.style.visibility = (el.style.visibility == "hidden") ? "visible" : "hidden";
}

/**
 *  On-blur event handler for text inputs: changes the background image.
 *
 *  @param  {HTMLInputElement} el
 *  @param  {HTMLImageElement} bgImg
 *  @type   void
 */
function txtInputBlur(el, bgImg)
{
  if (el.value == "") {
    el.style.backgroundImage = "url(" + bgImg.src + ")";
  }
}

/**
 *  On-focus event handler for text inputs: changes the background image.
 *
 *  @param  {HTMLInputElement} el
 *  @param  {HTMLImageElement} bgImg
 *  @type   void
 */
function txtInputFocus(el, bgImg)
{
  el.style.backgroundImage = "url(" + bgImg.src + ")";
}

/**
 *  DEBUG FUNCTION: Prints array items.
 *
 *  @param  {Array} a
 *  @type   void
 */
function write_r(a)
{
  document.write(swrite_r(a));
}

/**
 *  Declare AJAX global variable.
 */
var xhRequest;

/**
 *  AJAX factory function. Sets global var xhRequest to an XML-HTTP request instance.
 *
 *  @type   boolean
 */
function makeXMLHttpRequest()
{
  xhRequest = null;

  // Mozilla, Safari, ...
  if (window.XMLHttpRequest) {
    xhRequest = new XMLHttpRequest();
    if (xhRequest.overrideMimeType) {
      xhRequest.overrideMimeType('text/xml');
    }
  }
  // IE
  else if (window.ActiveXObject) {
    try {
      xhRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xhRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {}
    }
  }

  if (!xhRequest) {
    alert("Error: this browser cannot create an XMLHttpRequest instance");
    return false;
  }
  else {
    xhRequest.post = function (reqFile, reqData)
    {
      xhRequest.onreadystatechange = handleResponse;
      xhRequest.open('POST', reqFile, true);
      xhRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xhRequest.send(reqData);
    }
  }

  return true;
}

// STRING PROTOTYPE DEFINITIONS ================================================

/**
 *  Replaces all target characters with the replacement character. Works by
 *  splitting it into an array and then rejoining it. Method added to String object.
 *
 *  @param  {String} str    String in which to replace characters
 *  @param  {String} tar    target character to replace
 *  @param  {String} rep    replacement character
 *  @return String with target characters replaced
 *  @type   String
 */
String.prototype.replaceChar = function(tar, rep)
{
  return this.split(tar).join(rep);
}

/**
 *  Strips spaces from a string. Method added to String object.
 *
 *  @requires String
 *  @return   String with spaces removed.
 *  @type     String
 */
String.prototype.stripSpaces = function()
{
  return this.replaceChar(" ", "");
}

/**
 *  Reverses the characters in a string. Method added to String object.
 *
 *  @requires String
 *  @return   String in reversed order
 *  @type     String
 */
String.prototype.strReverse = function()
{
  return this.split("").reverse().join("");
}

/**
 *  Trims leading whitespace from a string. Method added to String object.
 *
 *  @requires String
 *  @return   New string with spaces removed from the left.
 *  @type     String
 */
String.prototype.trimLeft = function()
{
  return this.replace(/^\s+/, "");
}

/**
 *  Trims trailing whitespace from a string. Method added to String object.
 *
 *  @requires String
 *  @return   New string with spaces removed from the right.
 *  @type     String
 */
String.prototype.trimRight = function()
{
  return this.replace(/\s+$/, "");
}

/**
 *  Trims leading and trailing whitespace from a string. Method added to String
 *  object.
 *
 *  @requires String
 *  @return   New string with leading and trailing spaces removed.
 *  @type     String
 */
String.prototype.trim = function()
{
  return this.trimLeft().trimRight();
}

/**
 *  Removes the last char from a string.
 *
 *  @requires String
 *  @return   New string with one less char than the original.
 *  @type     String
 */
String.prototype.ungetc = function()
{
  return this.substr(0, this.length - 1);
}
