
jQuery(document).ready( function() {
	/**
	 * Customize this function as you wish!
     * Add your validation below the email if.
     *
     * Change the behaviour of the plugin by editing this line:
     * 		$(this).blur( function() { validateField(this) } );
     * To:
     *      $(this).keyup( function() { validateField(this) } );
     *
     *  You can find it around the line number 64.
	 */
	function validateField(field) {
		var error = false;
		
		// required fields
		if (jQuery(field).attr("class").indexOf("required") != -1) {
			if (!jQuery(field).val().length)
				error = true;
		}
		// numeric fields
		if (jQuery(field).attr("class").indexOf("numeric") != -1) {
			if (!/^[0-9]*$/.test(jQuery(field).val()))
				error = true;
		}
		// characters (letters)
		if (jQuery(field).attr("class").indexOf("character") != -1) {
			if (!/^[a-zA-ZöÖäÄåÅ]*$/.test(jQuery(field).val()))
				error = true;
		}
		// emails
		if (jQuery(field).attr("class").indexOf("email") != -1) {
			if (!/^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test(jQuery(field).val()))
				error = true;
		}
		
		if (error) {
			jQuery(field).addClass("error");
		} else {
			jQuery(field).removeClass("error");
		}
		
		return !error;
	}
	
	jQuery("form").each( function() {
		// handle submissions without filling any field
		jQuery(this).submit(function () {
			var validationError = false;
			// for each field test it
			jQuery("input, select, textarea", this).each( function() {
				if (jQuery(this).attr("class")) {
					if (!validateField(this))
						validationError = true;
				}
			});
			return !validationError;
		});
	
		// handle changes on the fly
		jQuery("input, select, textarea", this).each( function() {
			if (jQuery(this).attr("class")) {
				jQuery(this).blur( function() { validateField(this) } );
    			}
		});
	});
});

