/**
 * --------------------------------------------------------------------
 * jQuery-Plugin "Validate"
 * Version: 1.0, 19.05.2009
 * by Pete Crewdson, pete@wnwdesign.co.uk
 *                      http://www.wnwdesign.co.uk/
 *
 * Copyright (c) 2000 Pete Crewdson
 * Licensed for use on the Mazurka E-commerce software only
 *
 * Changelog:
 *    19.05.2009 initial Version 1.0
 * --------------------------------------------------------------------
 */

(function($) {
	
	jQuery.fn.validate = function() {
	
		$(this).submit(function() {
			
			count=0;
			message = "<strong>Sorry, an error has occured, please see below for more details:</strong><br />";
										
			// Remove the "highlighted" class from all inputs in the form we are validating.
			$("form[name="+ $(this).attr("name") +"] :input").each(function() { $(this).removeClass("highlighted"); });
						
			// Validate a field which must have content in any form.
			$("form[name="+ $(this).attr("name") +"] :input.val_anything").each(function() {
				if($(this).val().length == 0) {
					count += 1;
					message += "<strong>"+ $(this).attr("alt") +"</strong> is a required field.<br />";
					$(this).addClass("highlighted");
				}
			});

			// Validate the field as a whole number (allowing spaces eg, a credit card number).
			$("form[name="+ $(this).attr("name") +"] :input.val_number").each(function() {
				if($(this).val().length == 0 || isNaN($(this).val().replace(/ /, ""))) {
					count += 1;
					message += "<strong>"+ $(this).attr("alt") +"</strong> is required as a whole number.<br />";
					$(this).addClass("highlighted");
				}
			});
				
			// Validate a phone number which can include space and an area code.
			$("form[name="+ $(this).attr("name") +"] :input.val_phone").each(function() {
				if($(this).val().length == 0 || isNaN($(this).val().replace(/[+() ]/g, ""))) {
					count += 1;
					message += "<strong>"+ $(this).attr("alt") +"</strong> is required as a telephone number.<br />";
					$(this).addClass("highlighted");
				}
			});
					
			// Validate an email address to see if it contains the required elements.
			$("form[name="+ $(this).attr("name") +"] :input.val_email").each(function() {
				if($(this).val().length == 0 || !$(this).val().match(".") || !$(this).val().match("@")) {
					count += 1;
					message += "<strong>"+ $(this).attr("alt") +"</strong> is required as an email address.<br />";
					$(this).addClass("highlighted");
				}
			});
				
			// Check to see if a required checkbox has been checked or not.
			$("form[name="+ $(this).attr("name") +"] :input.val_checkbox").each(function() {
				if($(this).is(":checked") === false) {
					count += 1;
					message += "<strong>"+ $(this).attr("alt") +"</strong> is required to be checked.<br />";
					$(this).addClass("highlighted");
				}
			});
			
			if(count > 0) {						// return our message so it can be echoed out to the page if needs be.
				$("p#info_message").html(message).removeClass("error").removeClass("message").removeClass("tip").addClass("error").fadeIn();
				window.scroll(0,0);
				return false;
			} else {							// return the script as true, to show that no errors were returned.
				return true;
				//$("form[name="+ $(this).attr("name") +"] input[type=submit]").click();
			}
																					  
		});
	
	}

})(jQuery);
