// ---------------------------------------------------------------------
//
//  Beautiful Forms
//  
//  A simple set of functions to remove form labels and place their
//  description inside the form field
//
//  Source:
//    http://ennuidesign.com/blog/Beautiful+Forms+with+JavaScript/
//
//  Usage:
//    <code>
//      var formId = "formName"; // id attribute of the form
//      var reqFields = new Array('field1','field2',...); // id of required fields
//      if ( document.getElementById(formId) ) {
//        prettyForms(formId);
//      }
//    </code>
//
//  Author:
//    Jason Lengstorf <answers@ennuidesign.com>
//    www.EnnuiDesign.com
//
//  License:
//    This software is distributed under the MIT License
//
//  Copyright (c) 2009 Jason Lengstorf
//  
//  Permission is hereby granted, free of charge, to any person
//  obtaining a copy of this software and associated documentation
//  files (the "Software"), to deal in the Software without
//  restriction, including without limitation the rights to use,
//  copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following
//  conditions:
//  
//  The above copyright notice and this permission notice shall be
//  included in all copies or substantial portions of the Software.
//  
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//  OTHER DEALINGS IN THE SOFTWARE.
//
// ---------------------------------------------------------------------

function prettyForms(formid, reqFields) {
  if ( document.getElementById ) {
    document.forms[formId].onsubmit = function() { return validateForm(reqFields); }
    var labels = document.forms[formid].getElementsByTagName("label");
    for ( i = 0; i < labels.length; i++ ) {
      if ( labels[i].htmlFor != '' ) {
        formInput = document.getElementById(labels[i].htmlFor);
        formInput.label = labels[i];
        formInput.onfocus = function() { textFocus(this); }
        formInput.onblur = function() { textBlur(this); }
      } else return false;
      if ( formInput.type != 'checkbox' && formInput.type != 'radio' ) {
        try {
          labels[i].currentStyle.display = "none";
        } catch(e) {
          labels[i].style.display = "none";
        }
        if(formInput.value == '') {
          formInput.value = labels[i].innerHTML;
        }
      } else return false;
    }
  } else {
    return false;
  }
}

function textFocus(input) {
  if ( input.value == input.label.innerHTML ) {
    input.value = '';
  } else {
    return false;
  }
}

function textBlur(input) {
  if ( input.value == '' ) {
    input.value = input.label.innerHTML;
  } else {
    return false;
  }
}

function validateForm(inputArray) {
  var x;
  for ( x in inputArray ) {
    var inputElm = document.getElementById(inputArray[x]);
    if ( (inputElm.value == inputElm.label.innerHTML) || (inputElm.value == '') ) {
      alert('Please fill out all required fields before submitting this form.');
      inputElm.focus();
      return false;
    }
  }
  return true;
}