/**
 *  @fileoverview ValidateInput and the other input-validation classes offer an
 *  easy way to implement client-side data validation before form submission.
 *  The format methods strip out all but the pertinent data and thus perform a
 *  first level of data sanitization.<br /><br />
 *
 *  Typical implementation in an HTML page:
 *    <pre>
 *  &lt;script type=&quot;text/javascript&quot;&gt;
 *  //&lt;![CDATA[
 *    function validateState(el)
 *    {
 *      var state = new State(el.id, el.value, 'state_err');
 *      state.setRequired();  // optional
 *      return state.verify();
 *    }
 *
 *    function verifyForm(form)
 *    {
 *      var els = form.elements;
 *      var state = validateState(els[&quot;state&quot;]);
 *
 *      if (state !== false) {
 *        els[&quot;state&quot;].value = state;
 *      }
 *      else {
 *        els[&quot;state&quot;].value = &quot;&quot;;
 *        els[&quot;state&quot;].focus();
 *        return false;
 *      }
 *    }
 *  // ]]&gt;
 *  &lt;/script&gt;
 *
 *  &lt;p&gt;
 *    &lt;label for=&quot;state&quot;&gt;State&lt;/label&gt;
 *    &lt;input id=&quot;state&quot;
 *      type=&quot;text&quot;
 *      name=&quot;state&quot;
 *      size=&quot;3&quot;
 *      maxlength=&quot;2&quot;
 *      onchange=&quot;this.value = validateState(this);&quot;
 *      onfocus=&quot;validateInput.refocus();&quot; /&gt;
 *  &lt;/p&gt;
 *  &lt;span id=&quot;state_err&quot; class=&quot;form_err&quot;&gt;
 *    &nbsp;&lt;/span&gt;
 *    </pre>
 */

/**
 *  @class  The ValidateInput object contains properties and methods used by all
 *          input-validation classes. Scripts accessing this object should use
 *          the convenience instance 'validateInput'.
 *
 *  @constructor
 *  @requires String
 *  @param  {String}  id      the id attribute of the calling element
 *  @param  {String}  value   the email as input by the user
 *  @param  {String}  errId   id of error-display element
 *  @type void
 */
function ValidateInput(id, value, errId)
{
  this.id = id;
  if ( value != null )  this.value = value.trim();
  this.errId = errId;
}

/**
 *  ID of element containing the email.
 *  @type String
 */
ValidateInput.prototype.id = null;

/**
 *  Value of element containing the email.
 *  @type String
 */
ValidateInput.prototype.value = null;

/**
 *  Is the field in this instance required?
 *  @type boolean
 */
ValidateInput.prototype.required = false;

/**
 *  Id of element in which to display error messages.
 *  @type String
 */
ValidateInput.prototype.errId = null;

/**
 *  Error messages; set by validate() method.
 *  @type String
 */
ValidateInput.prototype.errMsg = null;

/**
 *  Marks the field as required by setting this.required to true.
 *
 *  @type void
 */
ValidateInput.prototype.setRequired = function()
{
  this.required = true;
}

/**
 *  The format() method does nothing.
 *
 *  @type String
 */
ValidateInput.prototype.format = function()
{
  return this.value;
};

/**
 *  Validates an email. Input must match regx format
 *  <code>/[%\+-\.\w]+&#64;[-\.a-z0-9]+\.[\w]+/i</code>.
 *
 *  @type boolean
 */
ValidateInput.prototype.validate = function()
{
  if (this.required && this.value == "") {
    this.errMsg = "This field is required.";
    return false;
  }

  return true;
};

/**
 *  Verifies email and reports invalid input.
 *
 *  @requires ValidateInput validateInput
 *  @return if email ok then return it else false.
 *  @type mixed
 */
ValidateInput.prototype.verify = function()
{
  if (this.validate()) {
    return this.format();
  }

  validateInput.showError(this.errId, this.errMsg);
  validateInput.newFocus = this.errId;
  return false;
};

/**
 *  Contains id of element which is the target of the refocus() method. This
 *  field is set by showError() methods and cleared by refocus().
 *  @type String
 */
ValidateInput.prototype.newFocus = null;

/**
 *  Places the focus on an element if field newFocus is set.
 *
 *  @type void
 */
ValidateInput.prototype.refocus = function()
{
  if (this.newFocus) {
    var el = document.getElementById(this.newFocus);
    el.focus();
    this.newFocus = null;
  }
};

/**
 *  Displays error messages.
 *
 *  @param  {String} id     element which triggered the error
 *  @param  {String} msg    error message to display
 *  @param  {String} errId  error-message container element
 *  @type void
 */
ValidateInput.prototype.showError = function(id, msg, errId)
{
  try {
    var el = document.getElementById(id);
    el.innerHTML = msg;
    el.style.visibility = "visible";
    el.scrollIntoView();
	var top_err = document.getElementById('form_top_error');
	if ( top_err ) {
		top_err.innerHTML = "Uh Oh! Something isn&rsquo;t right. Please check your entries and try again.";
		top_err.style.display = "block";
	}
  }
  catch (e) {
    alert("DEBUG : " + id + " : " + e);
  }
};

// create convenience instance of ValidateInput
var validateInput = new ValidateInput();

