/**
 *  @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
 *  @type void
 */
function ValidateInput() { }

/**
 *  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();
  }
  catch (e) {
    alert("DEBUG : " + id + " : " + e);
  }
};

// create convenience instance of ValidateInput
var validateInput = new ValidateInput();
