/**
 * checkForm : jQuery form's fields validation plug-in.
 *
 * Copyright (c) 2007 Université Lumière Lyon 2 (www.univ-lyon2.fr)
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
 /**
 * @author Mehdi Kabab mehdi.kabab@univ-lyon2.fr
 * @copyright (c) 2007 Université Lumière Lyon 2 (www.univ-lyon2.fr)
 */
jQuery.fn.checkForm.rules = {
	/**
	 * Check if we have a text field.
	 * @return {Boolean}
	 */
	text: function() {
		return true;
	},
	
	/**
	 * Check if we have a numeric field.
	 * @return {Boolean}
	 * @todo Add a range validation
	 */
	numeric: function() {
		return !isNaN(e.value);
	},
	
	/**
	 * Check if the field contains a right email.
	 * 
	 * @example "mail": "email, univ-lyon2.fr"
	 * @desc Restrict email's domain to 'univ-lyon2.fr'.
	 * 
	 * @example "mail": "email, all"
	 * @desc All emails are granted
	 * 
	 * @return {Boolean}
	 */
	email: function() {
		// Return true if the pattern doesn't match
		var _bRegexResult = e.value.match( /^([A-Z0-9._%-]+)@([A-Z0-9.-]+\.[A-Z]{2,4})$/i );
		
		if( !_bRegexResult || ( ( RegExp.$2 != e.param[0] ) && ( e.param[0] != "all" )) )
		{
			return false;
		}
		else
		{
			return true;
		}
	},
	
	/**
	 * Check if we have a date field and if it format matches with the pattern defined in options.
	 * 
	 * @example "date": "date, 00/00/0000"
	 * @desc JJ/MM/YYYY
	 * 
	 * @example "date": "date, 0000/00/00"
	 * @desc YYYY/MM/JJ
	 * 
	 * @return {Boolean}
	 * @todo management of common patterns.
	 */
	date: function() {
		var _pattern = e.param[0];
		var _date = e.value;
		var _patternDate = null;
		var _patternDate1 = /^\d{2}\/\d{2}\/\d{4}$/;
		var _patternDate2 = /^\d{4}-\d{2}-\d{2}$/;
		
		if( _pattern.search( _patternDate1 ) != -1 )
		{
			_patternDate = _patternDate1;
		}
		else if( _pattern.search( _patternDate2 ) != -1 )
		{
			_patternDate = _patternDate2;
		}
		
		if( _patternDate == null )
		{
			return false;
		}
		else if( !_patternDate.test( _date ) )
		{
			return false;
		}
		else
		{
			return true;
		}
	},
	
	/**
	 * Check if we have a phone number field
	 * (00 00 00 00 00 or 00-00-00-00-00 or 00/00/00/00/00).
	 * @return {Boolean}
	 */
	phone: function() {
		_pattern = /^\d{2}([.\/\- ]?)\d{2}\1\d{2}\1\d{2}\1\d{2}$/;
		if( !_pattern.test( e.value ) )
			return false;
		else
			return true;
	},
	
	/**
	 * Check if the current field match with the one given as argument.
	 * 
	 * @example "passConfirm": "password, pass, required"
	 * @desc Compare passConfirm and pass fields.
	 * 
	 * @return {Boolean}
	 * @todo Manage min and max length.
	 */
	password: function() {
		if( ( jQuery( 'input[@name="' + e.param[0] + '"]:password', jQuery.context ).val() === e.value ) )
		{
			return true;
		}
		else
		{
			return false;
		}
	},
	
	/**
	 * Check if the number of checked checkbox is superior or equal than the value given in second argument.
	 * 
	 * @example "checkbox[]": "checkbox, 2"
	 * @desc Set the number of needed checked checkbox to 2.
	 * 
	 * @example "checkbox[]": "checkbox, , required"
	 * @desc No constraints, but required => 1 checkbox must be checked.
	 * 
	 * @return {Boolean}
	 */
	checkbox: function() {
		if( jQuery( 'input[@name="' + e.id + '"]:checked:checkbox' ).length >= e.param[0] )
			return true;
		else
			return false;
	},
	
	/**
	 * @see #checkbox
	 */
	radio: function() {
		if( jQuery('input[@name="' + e.id + '"]:checked:radio').length > 0 )
			return true;
		else
			return false;
	},
	
	/**
	 * Select field.
	 * @return {Boolean}
	 */
	select: function() {
		if( e.value != '' )
			return true;
		else
			return false;
	},
	
	/**
	 *  File field.
	 * @return {Boolean}
	 */
	file: function() {
		if( e.value == '' )
			return false;
		else
			return true;
	}
};
