/*
Copyright © 2004
Author: Avery Perich
Date: 7-30-2004
Description: Code for the Swap Shop™ site
*/


//========== WARNING =========== WARNING =========== WARNING ===========
//
// Ensure that the HTML object ID is unique!
// Including META tags and other HTML elements.
// If this is not done, you may get undesired results, or no results at all!
//
//========== WARNING =========== WARNING =========== WARNING ===========




// Obtain the current length of a text box.
// Returns: The character count or -1 on error
function TextLayerLength(layerID)
{
  var oText = document.getElementById(layerID);
  if (oText)
    return oText.value.length;
  else
    return -1;
}


// Update the desired layer to show how many characters are left.
function UpdateRemainingChars(destID, txtID, maxChars)
{
  var len = TextLayerLength(txtID);
  var sHTML = "";

  // If there was an error, just show the max allowed  
  if (len >= 0)
  {
    if ((maxChars - len) >= 0)
      sHTML = ((maxChars - len) == 0 ? "No" : (maxChars - len)) + " Character" + ((maxChars - len) != 1 ? "s" : "") + " Available";
    else
      sHTML = (len - maxChars) + " Character" + ((len - maxChars) != 1 ? "s" : "") + " Over Limit";
  }
  else
  {
    sHTML = maxChars + " Characters Max.";
  }

  // Show the new text
  LayerText(destID, sHTML);

  // Return true if the length is acceptable
  // NOTE: This is an intentional off-by-one error. 
  //       When the keypress calls this function, the latest key is not added yet.
  return ((maxChars - len) > 0);
}


// See if the text is too long, and if so, show the specified message.
// Returns: true if the text length is acceptable.
function CheckTextLength(layerID, maxChars, msg)
{
  if (TextLayerLength(layerID) > maxChars)
  {
    alert(msg);
    return false;
  }
  else
    return true;
}


// A truly Swap Shop specific function.
// The element that will show the new count must have a specific ID (see function code below).
// Hook up some various event handlers to allow character tracking.
function SetupTextingEvents(layerID, maxChars)
{
  return SetupTextingEventsEx(layerID + "_char_count", layerID, maxChars);
}


// Hook up some various event handlers to allow character tracking.
function SetupTextingEventsEx(destID, txtID, maxChars)
{
  // Get the text element whose events we must look at
  var elm = document.getElementById(txtID);
  if (elm == null) return false;

  // The function to execute using eval()
  var sEval = "UpdateRemainingChars('" + destID + "', '" + txtID + "', " + maxChars + ")";

  // Create an anonymous function that merely wrap the true function call.
  // This is similar as to what the browsers do when these properties are assigned with inline HTML (ex: <a onclick="function here">)
  // Return true in the anonymous function, so as not to hinder the event from firing or whatnot.
  var fref = function(event){ return eval(sEval); }

  // Same as above, but always returns TRUE, so as not to possibly cancel the event.
  var fref_TRUE = function(event){ eval(sEval); return true; }

  // Assign the events
  elm.onkeyup = fref; // Gives a "final" count for keyboard entry
  elm.onkeypress = fref; // Gives a running total if a key is held down
  elm.onchange = fref; // Mainly here as a non-IE workaround for onpaste
  elm.onpaste = fref; // IE specific
  elm.oncut = fref_TRUE; // IE specific
  elm.ondeactivate = fref_TRUE; // IE 5.5+ specific
  return true;
}

