/* Generic double form submission prevention. use: $('#my_form').preventDoubleSubmit(); */
/*/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////*/
jQuery.fn.preventDoubleSubmit = function() {
	jQuery(this).submit(function() {
		if (this.beenSubmitted)
			return false;
		else
			this.beenSubmitted = true;
		});
};

/* Custom validator method: select */
/*/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////*/
jQuery.validator.addMethod('selectnone', function(value, element) {
    if (element.value == "")
		return false;
	else
		return true;
}, "Please select an option.");

/* Custom validator method: UK postcode validation */
/*/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////*/
jQuery.validator.addMethod(
    "postcode",
    function(value, element) {
        // uk postcode regex – all straight forward apart from that last bit – apparently
        // uk postocdes don’t have the letters [CIKMOV] in the last 2
        var regex = /^[A-Z]{1,2}[0-9R][0-9A-Z]? ?[0-9][ABD-HJLNP-UW-Z]{2}$/i; //after – no need for space
        return regex.test(value);
    },
    "Please specify a valid postcode."
);

/* Custom validator method: hardens email validation */
/*/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////*/
jQuery.validator.addMethod('email2', function(value, element, params) {
    var pattern1 = /^\S+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/;
    var pattern2 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
    return this.optional(element) || (value.match(pattern1) && !(value.match(pattern2))); 
}, "Enter a valid email address.");

/* Custom validator method: checks that the amount is in the correct format */
/*/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////*/
jQuery.validator.addMethod('amount_format', function(value, element, params) {
    return this.optional(element) || value.match(/^\d+\.\d{2}$/); 
}, "Format should be 'xx.xx' (e.g. 21.87).");

/* Custom validator method: checks that the amount is in the correct format */
/*/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////*/
jQuery.validator.addMethod('amount_gt0', function(value, element, params) {
    return this.optional(element) || (value > 0 && value < 1000000); 
}, "Please enter a valid amount.");

/* Custom validator method: letters only */
/*/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////*/
jQuery.validator.addMethod('lettersonly', function(value, element, params) { 
	return this.optional(element) || /^[a-zA-Z]+$/i.test(value);
}, "Letters only please."); 

/* Custom validator method: letters only */
/*/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////*/
jQuery.validator.addMethod('alpha_dotspacedash', function(value, element, params) { 
	return this.optional(element) || /^[A-Z][a-zA-Z \-\.]+$/i.test(value);
}, "Letters, dashes, and spaces only please."); 

/* Custom validator method: maxWords */
/*/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////*/
jQuery.validator.addMethod('maxWords', function(value, element, params) {
    return this.optional(element) || value.match(/\b\w+\b/g).length < params;
}, jQuery.validator.format("Please enter {0} words or less."));  

/* Custom validator method: minWords */
/*/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////*/
jQuery.validator.addMethod('minWords', function(value, element, params) {
    return this.optional(element) || value.match(/\b\w+\b/g).length >= params;
}, jQuery.validator.format("Please enter at least {0} words.")); 

/* Custom validator method: rangeWords */
/*/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////*/
jQuery.validator.addMethod('rangeWords', function(value, element, params) {
    return this.optional(element) || value.match(/\b\w+\b/g).length >= params[0] && value.match(/bw+b/g).length < params[1];
}, jQuery.validator.format("Please enter between {0} and {1} words."));

/* Custom validator method: checks if the age of the user is valid */
/* http://dev-tips.com/featured/how-to-validate-date-of-birth-with-jquery */
/*/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////*/
jQuery.validator.addMethod('dob_select', function(value, element) {
	var day = $("#day").val();
	var month = $("#month").val();
	var year = $("#year").val();

	var dob = day+month+year;
	
	return (dob = 8 ? true : false);
});

