﻿/*	 *	jQuery validVal version 2.0.1 *	demo's and documentation: *	validval.frebsite.nl * *	Copyright (c) 2010 Fred Heusschen *	www.frebsite.nl * *	Dual licensed under the MIT and GPL licenses. *	http://en.wikipedia.org/wiki/MIT_License *	http://en.wikipedia.org/wiki/GNU_General_Public_License */(function($) {	$.fn.validVal = function( o ) {		if (this.length > 1) {			return this.each(function() {				$(this).validVal( o );			});		}		var form = this,			opts = $.extend({}, $.fn.validVal.defaults, o),			inputSelector = 'input:not(:button|:submit|:reset), textarea, select',			clss = {				//	key			:  class				'placeholder'	: 'placeholder',				'formatted'		: 'formatted',							'focus'			: 'focus',				'autofocus'		: 'autofocus',				'autotab'		: 'autotab',				'invalid'		: 'invalid',				'inactive'		: 'inactive'			},			vlds = {				//	function	:  class				'corresponding'	: 'corresponding',				'number'		: 'number',				'email'			: 'email',				'required'		: 'required'			};		if ( $.fn.validVal.customValidations ) {			opts.customValidations = $.extend({}, $.fn.validVal.customValidations, opts.customValidations);		}		$(inputSelector, form).each(function() {			var $ff = $(this);			//	overwrite HTML5 attributes			var atr = [ 'placeholder', 'required', 'autofocus' ],				typ = [ 'number', 'email'/*, url*/ ];			if ( $ff.attr( 'placeholder' ) ) $ff.val( $ff.attr( 'placeholder' ) );			for ( var a in atr ) {				if ( vv_test_html5_attr( $ff, atr[ a ] ) ) {					$ff.addClass( vv_get_class( atr[ a ] ) );					$ff.removeAttr( atr[ a ] );				}			}			for ( var t in typ ) {				if ( $ff.is( '[type=' + typ[ t ] + ']' ) ) {					$ff.addClass( vv_get_class( typ[ t ] ) );//					$ff.attr( 'type', 'text' );				}			}			//	save defaults			$ff.data( 'vv_placeholdervalue', $ff.val() )				.data( 'vv_format', $ff.val() )				.data( 'vv_format_text', '' )				.data( 'vv_type', $ff.attr( 'type' ) );			//	bind events			$ff.bind('focus', function() {				vv_clear_placeholdervalue( $ff );				vv_clear_formatvalue( $ff );				$ff.addClass( vv_get_class( 'focus' ) );			}).bind('blur', function() {				$ff.removeClass( vv_get_class( 'focus' ) );				$ff.trigger( 'test', opts.validate.onBlur );				vv_restore_formatvalue( $ff );				vv_restore_placeholdervalue( $ff );			}).bind('test', function( event, onEvent ) {				if ( onEvent === false ) return;				$ff.data( 'vv_isValid', 'valid' );				if ( $ff.is( ':hidden' ) && !opts.validate.hiddenFields ) return;				if ( $ff.is( ':disabled' ) && !opts.validate.disabledFields ) return;				for ( var k in vlds ) {					var v = vlds[ k ];					if ( $ff.hasClass( v ) ) {						if ( !eval( 'vv_is_' + k + '( $ff )' ) ) {							$ff.data( 'vv_isValid', 'NOT' );							break;						}					}				}				for ( var v in opts.customValidations ) {					if ( $ff.hasClass( v ) ) {						var f = opts.customValidations[ v ];						if ( typeof f == 'function' ) {							if ( !f( $ff ) ) {								$ff.data( 'vv_isValid', 'NOT' );								break;							}						}					}				}				if ( $ff.data( 'vv_isValid' ) == 'valid' ) {					if ( onEvent !== 'invalid' ) {						vv_set_valid( $ff, form, opts );					}				} else {					if ( onEvent !== 'valid' ) {						vv_set_invalid( $ff, form, opts );					}				}			});			//	placeholder			if ( vv_is_placeholderfield( $ff ) ) {				$ff.addClass( vv_get_class( 'inactive' ) );				if ( $ff.is( 'select' ) ) {					$ff.find( 'option:selected' ).addClass( vv_get_class( 'inactive' ) );					$ff.change(function() {						if ( vv_trim( $ff.val() ) == $ff.data( 'vv_placeholdervalue' ) ) {							$ff.addClass( vv_get_class( 'inactive' ) );						} else {							$ff.removeClass( vv_get_class( 'inactive' ) );						}					});				} else if ( $ff.data( 'vv_type' ) == 'password' ) {//					$ff[0].attr( 'type', 'text' );				}			}			if ( $ff.hasClass( vv_get_class( 'corresponding' ) ) ) {				$('[name=' + $ff.attr( 'alt' ) + ']').blur(function() {					if ( vv_trim( $ff.val() ).length > 0 ) {						vv_clear_formatvalue( $ff );						vv_clear_placeholdervalue( $ff );						$ff.trigger( 'test', opts.validate.onBlur );						vv_restore_formatvalue( $ff );						vv_restore_placeholdervalue( $ff );					}				});			}			//	autotabbing			if ( $ff.hasClass( vv_get_class( 'autotab' ) ) ) {				var max = $ff.attr( 'maxlength' ),					tab = $ff.attr( 'tabindex' ),					$next = $('[tabindex=' + ( parseInt( tab ) + 1 ) + ']');				if ( $ff.is( 'select' ) ) {					if ( tab ) {						$ff.change(function() {							if ( $next.length ) $next.focus();						});					}				} else {					if ( max && tab ) {						$ff.keyup(function() {							if ( $ff.val().length == max ) {								if ( $next.length ) $next.focus();								$ff.trigger( 'blur' );							}						});					}				}			}			//	autofocus			if ( $ff.hasClass( vv_get_class( 'autofocus' ) ) ) {				$ff.focus();			}		}).filter( 'select, :checkbox, :radio' ).change(function() {			$(this).trigger( 'blur' );		});		form.submitform = function() {			var miss_arr = [],				data_obj = {};			$(inputSelector, form).each(function() {				var $ff = $(this);				vv_clear_placeholdervalue( $ff );				vv_clear_formatvalue( $ff );				$ff.trigger( 'test', opts.validate.onSubmit );								var n = $ff.attr( 'name' ),					v = $ff.val();								vv_restore_placeholdervalue( $ff );				vv_restore_formatvalue( $ff );				if ( $ff.data( 'vv_isValid' ) == 'valid' ) {					if ( $ff.is( ':radio' ) || $ff.is( ':checkbox' ) ) {						if ( !$ff.is( ':checked' ) ) v = '';					}					if ( v.length > 0 ) {						data_obj[ n ] = v;					}				} else if ( opts.validate.onSubmit !== false ) {												miss_arr.push( $ff );				}			});			if ( miss_arr.length > 0 ) {				if ( opts.invalidFormFunc ) {					opts.invalidFormFunc( miss_arr, form, opts.language );				}				miss_arr[0].focus();				return false;			} else {				$('input:text', form ).each(function() {					var $ff = $(this);					vv_clear_placeholdervalue( $ff );					vv_clear_formatvalue( $ff );				});				return data_obj;			}		};		form.resetform = function() {			$(inputSelector, form).each(function() {				var $ff = $(this);				if ( vv_is_placeholderfield( $ff ) ) {					$ff.addClass( vv_get_class( 'inactive' ) );				}				vv_set_valid( $ff, form, opts );			});			return true;		};		if ( form.is( 'form' ) ) {			form[0].onsubmit = function() {				return form.submitform();			};			form[0].onreset = function() {				return form.resetform();			};		}		return this;	};	$.fn.validVal.defaults = {		language : 'en',		customValidations : {},		validate: {			onBlur: true,			onSubmit: true,			hiddenFields: false,			disabledFields: false		},		invalidFieldFunc : function( $field, $form, language ) {			if ( $field.is( ':radio' ) || $field.is( ':checkbox' ) ) {				$field.parent().addClass( vv_get_class( 'invalid' ) );			}			$field.addClass( vv_get_class( 'invalid' ) );		},		validFieldFunc : function( $field, $form, language ) {			if ( $field.is( ':radio' ) || $field.is( ':checkbox' ) ) {				$field.parent().removeClass( vv_get_class( 'invalid' ) );			}			$field.removeClass( vv_get_class( 'invalid' ) );		},		invalidFormFunc : function( field_arr, $form, language ) { 			switch (language) {				case 'nl':					msg = 'Let op, niet alle velden zijn correct ingevuld.';					break;								case 'de':					msg = 'Achtung, nicht alle Felder sind korrekt ausgefuellt.';					break;					default:					msg = 'Por favor llene los campos correctamente, gracias.';					break;			}			alert( msg );		}	};	//	validations	function vv_is_required( $f ) {		var v = vv_trim( $f.val() );		if ( $f.is( ':radio' ) || $f.is( ':checkbox' ) ) {			var attr = ( $f.is( ':checkbox' ) ) ? 'alt' : 'name';			if ( typeof $f.attr( attr ) == 'undefined' ) {				return true;			}			if ( !$( 'input[' + attr + '=' + $f.attr( attr ) + ']:checked' ).length ) {				return false;			}		} else if ( $f.is( 'select' ) ) {			if ( vv_is_placeholder( $f ) ) {				return false;			}		} else {			if ( v.length == 0 ) {				return false;		 	}		}	 	return true;	}	function vv_is_number( $f ) {		var v = vv_strip_whitespace( $f.val() );		if ( v.length > 0 && isNaN( v ) ) {			return false;		}		return true;	}	function vv_is_email( $f ) {		var v = vv_trim( $f.val() );		if ( v.length > 0 &&			( v.indexOf("@") == -1 || v.indexOf(".") == -1 || v.length < 7 )		) {			return false;		}		return true;	}	function vv_is_corresponding( $f ) {		if ( $f.val() != $('[name=' + $f.attr( 'alt' ) + ']').val() ) {			return false;		}		return true;	}	function vv_is_placeholder( $f ) {		if ( vv_trim( $f.val() ) == $f.data( 'vv_placeholdervalue' ) ) {			return true;		}		return false;	}	//	placeholder functions	function vv_is_placeholderfield( $f ) {		if ( $f.hasClass( vv_get_class( 'placeholder' ) ) ) {			return true;		} else {			return false;		}	}	function vv_clear_placeholdervalue( $f ) {		if ( vv_is_placeholderfield( $f ) ) {			if ( vv_is_placeholder( $f ) && !$f.is( 'select' )  ) {				$f.val( '' );				$f.removeClass( vv_get_class( 'inactive' ) );								if ( $f.data( 'vv_type' ) == 'password' ) {//					$f.attr( 'type', 'password' );				}			}		}	}	function vv_restore_placeholdervalue( $f ) {		if ( vv_is_placeholderfield( $f ) ) {			if ( vv_trim( $f.val() ) == '' && !$f.is( 'select' ) ) {				$f.val(  $f.data( 'vv_placeholdervalue' ) );				$f.addClass( vv_get_class( 'inactive' ) );							if ( $f.data( 'vv_type' ) == 'password' ) {//					$f.attr( 'type', 'text' );				}			}		}	}	//	formatted functions	function vv_is_formattedfield( $f ) {		if ( $f.hasClass( vv_get_class( 'formatted' ) ) ) {			return true;		} else {			return false;		}	}	function vv_clear_formatvalue( $f ) {		if ( vv_is_formattedfield( $f ) ) {			$f.val( $f.data( 'vv_format_text' ) );		}	}	function vv_restore_formatvalue( $f ) {		if ( vv_is_formattedfield( $f ) ) {			var o = vv_strip_whitespace( $f.val() ),				v = $f.data( 'vv_format' );			$f.data( 'vv_format_text', o );			for ( var a = 0; a < o.length && a < v.length; a++ ) {				v = v.replace( '_', o[ a ] );			}			$f.val( v );		}	}	function vv_set_valid( $f, f, o ) {		if ( o.validFieldFunc ) {			o.validFieldFunc( $f, f, o.language );		}	}	function vv_set_invalid( $f, f, o ) {		if ( o.invalidFieldFunc ) {			o.invalidFieldFunc( $f, f, o.language );		}	}		function vv_test_html5_attr( $f, a ) {		var i = document.createElement( 'input' );				//	HTML5 supported		if ( a in i ) {			return $f.attr( a );		}				//	no HTML5		if ( typeof $f.attr( a ) != 'undefined' ) {			return true;		}		return false;	}	function vv_get_class( cl ) {		if ( typeof clss != 'undefined' && typeof clss[ cl ] != 'undefined' ) return clss[ cl ];		return cl;	}	function vv_trim( str ) {		if ( typeof str == 'undefined' ) return '';		return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');	}	function vv_strip_whitespace( str ) {		str = vv_trim( str );		var r = [ ' ', '-', '+', '(', ')', '/', '\\' ];		for ( var i in r ) {			str = str.split( r[ i ] ).join('');		}		return str;	}})(jQuery);
