/**
 * JavaScript Validations framework.
 * tyler@boat-ed.com
 *
 * It's regex based. You call the constructor
 * with a hash of field IDs and regex patterns.
 * (See format below)
 *
 * When you call you call the korrect() method,
 * it iterates through every ID in the pValidations
 * hash and runs it against the regex. If the
 * regex fails, it attempts to set display:block
 * on {$id}_errorText and sets the text of the
 * errorText object to whatever was specified.
 *
 * An alternative to regex is customValidation.
 * You can set a customValidation property instead
 * of a regex, and give it an anonymous function
 * that returns true or false. 
 *
 * I should probably note that it also creates
 * the little errorText boxes by inserting them
 * after the relevant form fields.
 *
 * If you want to define them yourself, pass
 * false as the second parameter.
 *
 * Format for constructing:
 *
 * new Korrector( 
 * 		{
 *			location_name: [
 *					{
 *						regex: '.+',
 *						errorText: 'Location Name is required.'
 *					},
 *					{
 *						regex: '[A-Za-z0-9_]+',
 *						errorText: 'Location Name is an alphanumeric field.'
 *					}
 *				],
 *	
 *			location_capacity: [
 *					{
 *						regex: '.+',
 *						errorText: 'Location Capacity is required.'
 *					},
 *					{
 *						regex: '\d+',
 *						errorText: 'Location Capacity is a numeric field.'
 *					},
 *					{
 *						customValidation: function ( ) { return ( prompt( 'Confirm location capacity:' ) == document.getElementById('location_capacity') ); },
 *						errorText: 'Confirmation failed.'
 *				]
 * 		}
 * );
 *
*/
function Korrector ( pValidations, pAutoGenerate )
{
  var obj;
  var errorDiv;
  
  this.validations = pValidations;

  if ( typeof pAutoGenerate == 'undefined' || pAutoGenerate != false )
  {
    for ( var fieldName in this.validations )
    {
      obj = document.getElementById(fieldName);
      
      if ( obj )
      {
        errorDiv = document.createElement( "DIV" );
        errorDiv.setAttribute( 'id', fieldName + '_errorText' );
        errorDiv.className = "validationText";
        obj.parentNode.insertBefore( errorDiv, obj.nextSibling );
      }
    }
  }
}

/**
 * This method puts the delicious cream filling inside the
 * twinkie.
 * return boolean
*/
Korrector.prototype.korrect = function ( )
{
  var fieldValidations;
  var thisValidation;
  var pattern;
  var passedValidation = true;
  
  for( var fieldName in this.validations )
  {
    if ( ! document.getElementById( fieldName + '_errorText' ) )
    {
      alert( 'No error field for ' + fieldName );
      break;;
    }
    
    document.getElementById( fieldName + '_errorText' ).style.display = 'none';
    fieldValidations = this.validations[fieldName];
    
    for( var i = 0; i < fieldValidations.length; i++ )
    {
      thisValidation = fieldValidations[i];
      if ( thisValidation.regex )
      {
        pattern = new RegExp( thisValidation.regex, 'g' );
        if ( document.getElementById(fieldName) )
        {
          if ( ! pattern.test( document.getElementById(fieldName).value ) )
          {
            passedValidation = false;
            document.getElementById( fieldName + '_errorText' ).innerHTML = thisValidation.errorText;
            document.getElementById( fieldName + '_errorText' ).style.display = 'block';
            break;
          }
        }
      }
      else if ( thisValidation.customValidation )
      {
        if ( document.getElementById(fieldName) )
        {
          if ( ! thisValidation.customValidation() )
          {
            passedValidation = false;
            document.getElementById( fieldName + '_errorText' ).innerHTML = thisValidation.errorText;
            document.getElementById( fieldName + '_errorText' ).style.display = 'block';
            break;
          }
        }
      }
    }
  }
  
  return passedValidation;
}
