/*
 * jQuery Gigi Form Validation
 * version: 0.1.1 (02/07/2010)
 *  - full compatibility with default values
 *    Event are now namespaced -> no more side effect
 *
 * version: 0.1.0 (30/12/2009)
 *  - check of default values
 *
 * version: 0.1.0 (24/11/2008)
 * @requires jQuery v1.2.6 or later
 *
 * No use and no modification whitout express authorisation
 *
 * Revision: $Id$
 */
(
function($)
{
 $.GigiFormValidation =
 {
  invalidInputClass: 'invalidInput',
  invalidInputLabelClass: 'invalidInputLabel',
  masks:
  {
   email     : /^([\w.\-])+\@(([\w.\-])+\.)[a-zA-Z0-9]{2,}$/g,
   date      : /^[0-9]{2,2}\/[0-9]{2,2}\/[0-9]{4,4}$/,
   domain    : /^(http(s)?:\/\/)([\w]+\.){1,}[A-Z]{2,4}\b/gi,
   ftp       : /^(ftp(s)?:\/\/)(([\w\@\.-]+)\:([\w$&]+)\@)?((([\w-]+\.){1,}[A-Z]{2,4})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(\:[0-9]+)?\b/gi,
   phone_fr  : /^(\+[0-9]{2,4}|[0-9]{2,})(\.[0-9]{1,2}){4,5}$/gi,
   zip       : /^[0-9]{0,5}$/gi,
   numeric   : /^[0-9]+(\.[0-9]+)?$/gi,
   image     : /[\w]+\.(gif|jpg|bmp|png|jpeg)$/gi,
   ewvt      : /[\w]+\.(htm|html|php|txt)$/gi,
   media     : /[\w]+\.(avi|mov|mpeg|wmv)$/gi,
   pdf       : /[\w]+\.(pdf)$/gi
  },
  defaultValueAttribute: 'alt'
 };
 
 
 $.fn.validForm = function( requiredLevel, options )
 {
  var bReturn = true;
  
  $(this).find( ":input" ).not( "*[disabled]" ).each( function()
                                {
                                 if( !$(this).validInput( requiredLevel, options ) )
                                  bReturn = false;
                                } );
  
  return bReturn;
 };
 
 $.fn.validInput = function( requiredLevel, options )
 {
  var bReturn = false;
  var requirementValidated = false;
  var maskValidated = false;
  var options = $.extend( {}, jQuery.GigiFormValidation, options || {} );
  
  if( ( $(this).is( "input" ) || $(this).is( "select" ) || $(this).is( "textarea" ) ) && !$(this).is( "button" ) )
  {
   requirementValidated = $(this).validInputRequirement( requiredLevel );
   maskValidated = $(this).validInputMask( requiredLevel );
   
   $(this).unbind( ".formValidation" );
   
   if( requirementValidated == false || maskValidated == false )
   {
    bReturn = false;
    
    $(this).bind( 'keyup.formValidation', function(){ $(this).validInput( requiredLevel ); } )
           .bind( 'click.formValidation', function(){ $(this).validInput( requiredLevel ); } )
           .bind( 'blur.formValidation', function(){ $(this).validInput( requiredLevel ); } );
   }
   else
   {
    bReturn = true;
   }
   
   if( bReturn == false )
   {
    $(this).addClass( jQuery.GigiFormValidation.invalidInputClass );
    $("label[for=" + $(this).attr("id") + "]").addClass( options.invalidInputLabelClass );
   }
   else
   {
    $(this).removeClass( jQuery.GigiFormValidation.invalidInputClass );
    $("label[for=" + $(this).attr("id") + "]").removeClass( options.invalidInputLabelClass );
   }
  }
  else
  {
   bReturn = true;
  }
  
  return bReturn;
 };
 
 $.fn.inputHasRequirement = function( requiredLevel )
 {
  requiredLevel = ( typeof requiredLevel != "string" ? "true" : requiredLevel );
  
  var bReturn = false;
  var levels = requiredLevel.split( " " );
  var requiredFor = ( typeof $(this).attr( "required" ) != "string" ? "" : $(this).attr( "required" ) ).split( " " );
  
  $.each( requiredFor,
          function( i, value )
          {
           if( $.trim(value) != "" )
           {
            if( $.inArray( $.trim(value), levels ) >= 0 )
             bReturn = true;
           }
          });
  
  return bReturn;
 };
 
 $.fn.validInputRequirement = function( requiredLevel )
 {
  var bReturn = false;
  
  if( $(this).inputHasRequirement( requiredLevel ) === true )
  {
   if( $(this).is( "input" ) && $(this).attr( "type" ) == "checkbox" )
   {
    if( $(this).attr( "checked" ) )
     bReturn = true;
    else
     bReturn = false;
   }
   else if( $(this).is( "input" ) && $(this).attr( "type" ) == "radio" )
   {
    if( $( "input[name=" + $(this).attr("name") + "]:checked" ).length == 1 )
     bReturn = true;
    else
     bReturn = false;
   }
   else if( ( $(this).is( "input" ) || $(this).is( "select" ) || $(this).is( "textarea" ) ) && !$(this).is( "button" ) )
   {
    if( $(this).getValue() != '' )
     bReturn = true;
    else
     bReturn = false;
   }
   else
   {
    bReturn = true;
   }
  }
  else
  {
   bReturn = true;
  }
  
  return bReturn;
 };
 
 $.fn.validInputMask = function( requiredLevel )
 {
  var bReturn = false;
  
  if( ( $(this).is( "input" ) || $(this).is( "select" ) || $(this).is( "textarea" ) ) && !$(this).is( "button" ) )
  {
   var mask = $(this).attr( "mask" );
   
   if( $(this).is( "input" ) )
   {
    if( mask == "undefined" || mask == "" )
     bReturn = true;
    else
    {
     if( $(this).inputHasRequirement( requiredLevel ) === false && $(this).getValue() == "" )
      bReturn = true;
     else
     {
      if( typeof jQuery.GigiFormValidation.masks[mask] == "undefined" )
       bReturn = true;
      else
      {
       bReturn = jQuery.GigiFormValidation.masks[mask].test( $(this).getValue() );
       result = jQuery.GigiFormValidation.masks[mask].exec( $(this).getValue() );
      }
     }
    }
   }
   else
   {
    bReturn = true;
   }
  }
  else
  {
   bReturn = true;
  }
  
  return bReturn;
 };
 
 $.fn.getValue = function()
 {
  var value = $(this).val();
  
  if( typeof $(this).attr( jQuery.GigiFormValidation.defaultValueAttribute ) == "string" )
  {
   if( $(this).attr( jQuery.GigiFormValidation.defaultValueAttribute ) == value )
    value = "";
  }
  
  return value;
 }
}
)(jQuery);