/*
 *  Global variables
 *    formErrorMsg
 *  Global functions
 *    clearSelectedOptions(id)
 *    setAllProfileAreaOpts(optEl)
 *    toggleAvail(id1, id2)
 *    toggleDisabled(chkBox, id)
 *    tokenize(s)
 *    verifyInt(strN)
 *    verifyNatNum(input)
 *  AJAX functions
 *    makeXMLHttpRequest()
 *  Show/hide elements
 *    showElement(id)
 *    hideElement(id)
 *    hideAll()
 *    startHideAllTimer()
 *    stopHideAllTimer()
 *    doMouseOverEl(id)
 *    doMouseOutEl(id)
 *    showLogin(user) DEPRECATED
 */

var formErrorMsg = "Uh Oh! Something isn&rsquo;t right. Please " +
  "check your entries and try again."

/*
 *  Sets all option selected attributes to false. Does nothing if element does
 *  not exist or is not an object HTMLSelectElement.
 *
 *  @param  {String} id   id of select element
 *  @type   void
 */
function clearSelectedOptions(id)
{
  try {
    var el = document.getElementById(id);
    for (var i = 0; i < el.options.length; i++) {
      el.options[i].selected = false;
    }
  }
  catch (e) {}
}

/**
 *  Sets all options to selected. Used by seeker-profile area select element.
 *
 *  @param  {object HTMLOptionElement}  optEl
 *  @type   void
 */
function setAllProfileAreaOpts(optEl)
{
  var opts = optEl.parentNode.options;

  for (var i = 1; i < opts.length; i++) {
    opts[i].selected = true;
  }
}

var isAllAvail = true;
/**
 *  Toggle both availability checkboxes for a day at once.
 *  OR Toggle all availability checkboxes.
 *
 *  @param  {String} id1  id of 1st element OR special token "_all"
 *  @param  {String} id2  id of 2nd element
 *  @type   void
 */
function toggleAvail(id1, id2)
{
  if (id1 == "_all") {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
      if (inputs[i].id.indexOf("avail") != -1) {
        inputs[i].checked = isAllAvail;
      }
    }
    isAllAvail = !isAllAvail;
    return;
  }

  var el1 = document.getElementById(id1);
  var el2 = document.getElementById(id2);

  if (el1.checked && el2.checked) {
    el1.checked = false;
    el2.checked = false;
  }
  else {
    el1.checked = true;
    el2.checked = true;
  }
}

function toggleAvailBoth(id1, id2, me)
{
  var el1 = document.getElementById(id1);
  var el2 = document.getElementById(id2);

  if (el1.checked && el2.checked && !me.checked) {
    el1.checked = false;
    el2.checked = false;
	me.checked = false;
  }
  else {
    el1.checked = true;
    el2.checked = true;
	me.checked = true;
  }	
}

function checkBoth(me, e2, both)
{
	if(!me.checked)
	{
		document.getElementById(both).checked = false;
		return;
	}
	if(me.checked && document.getElementById(e2).checked)
	{
		document.getElementById(both).checked = true;
	}
}
/**
 *  Sets the disabled attribute of a form element based on the checked status
 *  of an associated checkbox: checked = enabled.
 *
 *  @param  {HTMLInputElement} chkBox   checkbox input element
 *  @param  {String} id                 id of element to toggle
 *  @type   void
 */
function toggleDisabled(chkBox, id)
{
  var el;

  if (document.getElementById) {
    el = document.getElementById(id);
  }
  else if (document.all) {
    el = document.all[id];
  }

  el.disabled = (chkBox.checked) ? false : true;
}

/**
 *  Sets the disabled attribute of a form element based on the checked status
 *  of an associated checkbox: checked = disabled.
 *
 *  @param  {HTMLInputElement} chkBox   checkbox input element
 *  @param  {String} id                 id of element to toggle
 *  @type   void
 */
function toggleDisabledOff(chkBox, id)
{
  var el;

  if (document.getElementById) {
    el = document.getElementById(id);
  }
  else if (document.all) {
    el = document.all[id];
  }

  el.disabled = (chkBox.checked) ? true : false;
}

/**
 *  Converts string to format suitable for use as an id or name token, i.e. all
 *  lowercase and any non-alphanumeric chars converted to underscore (including
 *  whitespace and chars > 127). Duplicate changes here in default.php.
 *
 *  @param  {String}  s
 *  @type   String
 */
function tokenize(s)
{
  return s.toLowerCase().replace(/[^a-z0-9_]/g, "_");
}

/**
 *  Casts String argument as a number and rounds to integer.
 *
 *  @param  {String}  strN
 *  @return If strN isNaN then false else cast as integer
 *  @type   mixed
 */
function verifyInt(strN)
{
  var intN = Math.round(strN);
  if (isNaN(intN)) {
    return false;
  }
  return intN;
}

/**
 *  Verifies numeric value of argument. Rounds argument to integer. This
 *  function is an on-change event handler for general number input fields. If
 *  arg is not numeric, alerts the user.
 *
 *  @param  {object HTMLInputElement}  input   text input field
 *  @type   void
 */
function verifyNatNum(input) {
  var s;

  var val = verifyInt(input.value);

  if (val !== false) {
    input.value = val;
  }
  else {
    window.alert("Non-numeric entry. Please try again.");
    input.value = 0;
  }
}

/* AJAX GLOBAL VARIABLE AND FACTORY FUNCTION */

var xhRequest;

/**
 *  Sets global var xhRequest to an XML-HTTP request (AJAX) instance.
 *
 *  @global {object XMLHttpRequest} xhRequest
 *  @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;
  }
  return true;
}

/* SHOW/HIDE ELEMENTS */

/**
 *  Sets element visibility to 'visible'.
 *
 *  @param  {String} id
 *  @type   void
 */
function showElement(id)
{
  if (document.getElementById) {
    document.getElementById(id).style.visibility = "visible";
  }
  else if (document.all) {
    document.all[id].style.visibility = "visible";
  }
}

/**
 *  Sets element visibility to 'hidden'.
 *
 *  @param  {String} id
 *  @type   void
 */
function hideElement(id)
{
  if (document.getElementById) {
    document.getElementById(id).style.visibility = "hidden";
  }
  else if (document.all) {
    document.all[id].style.visibility = "hidden";
  }
}

/**
 *  Sets visibility of all div elements with class 'popup' to 'hidden'.
 *
 *  @type   void
 */
function hideAll()
{
  var elements = false;

  if (document.getElementById) {
    elements = document.getElementsByTagName("div");
  }
  else if (document.all) {
    elements = document.all.tags("div");
  }

  if (elements) {
    for (var i = 0; i < elements.length; i++) {
      if (elements.item(i).className == "popup") {
        hideElement(elements.item(i).id);
        // The item method retrieves a node from the tree by index.
      }
    }
  }
}

var timerId = null;
var timecount = 200;  // milleseconds

/**
 *  Starts a timer to delay hiding elements by value of timecount.
 *
 *  @global {Number} timerId
 *  @global {Number} timecount
 */
function startHideAllTimer()
{
  if (!timerId) {
    timerId = setTimeout("hideAll()", timecount);
  }
}

/**
 *  Stops the timer started by startHideAllTimer.
 *
 *  @global {Number} timerId
 */
function stopHideAllTimer()
{
  if (timerId) {
    clearTimeout(timerId);
    timerId = null;
  }
}

/**
 *  Handles mouse-over event:
 *    (1) stops timer;
 *    (2) hides any open popups;
 *    (3) shows target element.
 *
 *  @param {String} id
 *  @type   void
 */
function doMouseOverEl(id)
{
  stopHideAllTimer();
  hideAll();
  showElement(id);
}

/**
 *  Handles mouse-out event: starts the timer to hide all open popups.
 *
 *  @param {String} id
 *  @type   void
 */
function doMouseOutEl(id)
{
  startHideAllTimer();
}
