$(function(){
	$('#reviewForm').bind("invalid-form.validate", function(e, validator) {
		var errors = validator.numberOfInvalids();
		if (errors) {
			var message = errors == 1
				? 'There was a problem with one of the fields. It has been highlighted below.'
				: 'There were problems with some of the fields. They have been highlighted below.';
			$("#pagemessages").empty();
			$("#pagemessages").html('<ul class="msg_warning"><li>'+message+'</li></ul>');
		} else {
			$("ul.msg_warning").addClass('msg_confirm').removeClass('msg_warning').html('<li>All OK now!</li>');
		}
	}).validate();
	
	jQuery.validator.addMethod("maxwords", function(value, element, params) { 
	    return this.optional(element) || value.match(/\b\w+\b/g).length < params; 
	}, jQuery.format("You have exceeded the maximum of {0} words.")); 
	 
	jQuery.validator.addMethod("minwords", function(value, element, params) { 
	    return this.optional(element) || value.match(/\b\w+\b/g).length >= params; 
	}, jQuery.format("Please enter at least {0} words.")); 
	
	$("#reviewForm").submit(function(e) {
		var review = $("#Review").val();
		if(countCaps(review) >= .95) {
			e.preventDefault();
			alert('CAPITAL only text is not accepted');
		}
	});
	function countCaps(txt) {
		if(txt.length === 0) return 0;
		var matchList = "";
		txt = txt.replace(/<\S[^><]*>/g, "");
		var regexp = /[A-Z\s]/g;
		while ((match = regexp.exec(txt)) != null) {
			matchList += match;
		}
		return matchList.length / txt.length;
	}
});
