

	

$(document).ready(function(){

	errornotice = $(".error");
	errornotice.hide();
	// The text to show up within a field when it is incorrect
	emptyerror = "Required";
	emailerror = "Please enter a valid e-mail.";
checkboxerror = "Choose One"; //make sure any checkboxes have a value, otherwise validation won't work on them.
thankyou = "Thank you, your message has been received. We will reply shortly.";

$('.validate').submit(function() {
	ourForm = $(this);
	sendform = ourForm.attr('action');
	


	//Validate required fields
	ourForm.find('.crequired').each(function() {
		var input = $(this);
		if ((input.val() == "") || (input.val() == emptyerror)) {
			input.addClass("needsfilled");
			input.val(emptyerror);
		// Validate the e-mail.
		} else if (input.hasClass('cemail')) {
			if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(input.val())) {
				input.addClass("needsfilled");
				input.val(emailerror);
			}	
		// validates checkboxes
		} 	else if ((input.attr('type') == 'checkbox') && (input.is(':checked') == false)) {
			input.addClass("needsfilled");
			if ($('.checkboxerror').length == 0) {
				input.before('<span class="checkboxerror">'+checkboxerror+' </span>');
			}
			}	
			else {
			input.removeClass("needsfilled");
			$('.checkboxerror').remove();
			}
	});
	

		//if any inputs on the page have the class 'needsfilled' the form will not submit
		if ($(":input").hasClass("needsfilled")) {
		$('html, body').animate({ scrollTop: 0 }, 'slow');
		errornotice.show();
			return false;
		} else {
				/*var names = {};
				$('input:radio').each(function() { // find unique names
      			names[$(this).attr('name')] = true;
				});
				for (group in names) {
                var radioButttons = $(":radio[name=" + group + "]:checked");
                isChecked = !!radioButttons.length;
                if (!isChecked) {
                    $(":radio[name=" + group + "]").next("label").css("border", "solid 1px red");
                }
                else {
                    $(":radio[name=" + group + "]").next("label").css("background-color", "white");
                }
				var count = 0;
				$.each(names, function() { // then count them
     			 count++;
     			 
				});
				if($('input:radio:checked').length == count) {*/
      			errornotice.hide();
				return true;
				/*}
				else {$('html, body').animate({ scrollTop: 0 }, 'slow');
				errornotice.show();
				return false;
				}
				}*/
			
		}
	});
	
	// Clears any fields in the form when the user clicks on them
	$(":input").focus(function(){		
	   if ($(this).hasClass("needsfilled") ) {
			$(this).val("");
			$(this).removeClass("needsfilled");
	   }
	});
});
