//==============================================================================
//    FUNCTIONS
//==============================================================================

// Send an HTTP request to the server
//  Requires that params be a two-dimensional array, or a string in the proper
//  format.
function AJAXmakeRequest(params, handler)
{
  // First things first, we create an HTTPRequest object
  var http_request = false;
  
  if (window.XMLHttpRequest)
  {
    // Mozilla, Safari, ...
    http_request = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    // IE
    try
    {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e){}
    }
  }
  
  if (!http_request)
  {
    alert("Giving up - Cannot create an XMLHTTP instance");
    return false;
  }
  
  // Now we need to process the parameters
  if (hasValue(params))
  {
    if (params.join)
    {
      // Array was passed - check if it's two-dimensional
      if (params[0].join)
      {
        for(i = 0; i < params.length; i++)
        {
          params[i] = params[i].join("=");
        }
      }
      // One-dimensional array - assume that it's name & value pairs
      params = params.join("&");
    }
  }
  
  http_request.onreadystatechange = function() { handler(http_request); };
  http_request.open("POST", "ajaxrequest.php", true);
  http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  http_request.send(params);
}


// Maxlength function
function checkLength(obj, maxlen, e)
{
  if(obj.value.length > maxlen)
  {
    c = e.keyCode;  // Grab the character pressed
    // When at maxlen, allow Bksp, Del, and arrows (Del == 46 || 127)
    if(c == 9 || c == 8 || (c >= 37 && c <= 40) || c == 127 || c == 46)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  else
  {
    return true;
  }
}

// Returns a number or string in US Currency format
//    Based on "Currency Format" by Cyanide_7 (leo7278@hotmail.com)
//        Web Site:  http://www7.ewebcity.com/cyanide7
function formatCurrency(num, omit)
{
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
  {
    num = "0";
  }
  sign = (num < 0);
  num = Math.round(num * 100);
  cents = num % 100;
  num = Math.floor(num / 100).toString();
  if(cents < 10)
  {
    cents = "0" + cents;
  }
  for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
  {
    num = num.substring(0, num.length - (4 * i + 3)) + ',' + 
        num.substring(num.length - (4 * i + 3));
  }
  return (
      ((sign) ? '-' : '') + 
      ((hasValue(omit) && omit) ? '' : '$ ') + num + '.' + cents
      );
}

// Return whether the variable has a value (is not undefined or an empty string)
function hasValue(variable)
{
  return (variable != '' && variable != undefined);
}

// Rounds a number, x, to the specified number of decimal places, y.
function roundDecimal(x, y)
{
  return (Math.round(x * Math.pow(10, y)) / Math.pow(10, y));
}

// Repeats a given string the specified number of times
function strRepeat(str, num)
{
  ret_str = "";
  if(!hasValue(str) || !hasValue(num))
  {
    return "";
  }
  else
  {
    for(var i = 0; i < num; i++)
    {
      ret_str += str;
    }
    return ret_str;
  }
}

// Submit the form with the given pagestate and/or action
function submitForm(ps, action)
{
  if(hasValue(action))
  {
    document.forms[0].action = action;
  }
  if(hasValue(ps))
  {
    document.forms[0].pagestate.value = ps;
  }
  document.forms[0].submit();
}


//==============================================================================
//  THIRD-PARTY FUNCTIONS
//==============================================================================

/* Script by: www.jtricks.com
 * Version: 20060314
 * Latest version:
 * www.jtricks.com/javascript/window/box.html
 * Edited by: Turonah
 */
// Moves the box object to be directly beneath an object.
function popup_move_box(elem, box)
{
  var cleft = 0;
  var ctop = 0;
  var obj = elem;

  while (obj.offsetParent)
  {
      cleft += obj.offsetLeft;
      ctop += obj.offsetTop;
      obj = obj.offsetParent;
  }
  // cleft += elem.offsetLeft;
  box.style.left = ((isNaN(cleft))? 0 : cleft) + 'px';

  ctop += elem.offsetHeight + 8;
  // Handle Internet Explorer body margins,
  // which affect normal document, but not
  // absolute-positioned stuff.
  if (document.body.currentStyle && document.body.currentStyle['marginTop'])
  {
    ctop += parseInt(
        document.body.currentStyle['marginTop']);
  }
  box.style.top = ((isNaN(ctop))? 0 : ctop) + 'px';
}

// Shows a box if it wasn't shown yet or is hidden
// or hides it if it is currently shown
function popup_show_hide_box(elem, width, height, content, boxid)
{
  // Try to grab the boxdiv
  var boxdiv = document.getElementById(boxid);
  
  // If it exists, toggle its visibility
  if (boxdiv != null)
  {
    if (boxdiv.style.display == "none")
    {
      popup_move_box(elem, boxdiv);
      boxdiv.style.display = "block";
    }
    else
    {
      boxdiv.style.display = "none";
    }
    return false;
  }
  else
  {
    // boxdiv doesn't exist yet - create it
    boxdiv = document.createElement("div");
    boxdiv.setAttribute("id", boxid);
    boxdiv.style.display = "block";
    boxdiv.style.position = "absolute";
    boxdiv.style.width = width + "px";
    boxdiv.style.height = height + "px";
    boxdiv.style.textAlign = "left";
    boxdiv.style.padding = "4px";
    boxdiv.style.overflow = "auto";
    boxdiv.style.border = "1px solid black";
    boxdiv.style.backgroundColor = "#FFFFFF";

    // Remove the following code if 'Close' hyperlink
    // is not needed.
    var close_href = document.createElement("a");
    close_href.href = "javascript:void(0);";
    close_href.setAttribute("onclick", "document.getElementById('" + 
        boxid + "').style.display='none';");
    close_href.appendChild(document.createTextNode("Close"));
    boxdiv.appendChild(close_href);
    // End of 'Close' hyperlink code.
    
    boxdiv.innerHTML += "<br />" + content;
    document.body.appendChild(boxdiv);
    popup_move_box(elem, boxdiv);

    // The script has successfully shown the box,
    // prevent hyperlink navigation.
    return false;
  }
}

// This function is used to change the content of the popup div
function popup_change_content(newcontent, boxid)
{
  var boxdiv = document.getElementById(boxid);
  
  // Remove the following code if 'Close' hyperlink
  // is not needed.
  var close_href = boxdiv.removeChild(boxdiv.firstChild);
  boxdiv.innerHTML = "";
  boxdiv.appendChild(close_href);
  // End of 'Close' hyperlink code.
  
  boxdiv.innerHTML += "<br />" + newcontent;
}
