$(document).ready(function() {
	
	$('#searchsubmit').val("");
	
	/*
	*
	* Add event listeners for comment form
	*
	*/

	if($('#commentform')) {
		$('#commentform').bind('submit', validateForm);
		$('#author').bind('blur', validateFormName);
		$('#email').bind('blur', validateFormEmail);
		$('#comment').bind('blur', validateFormComment);
	}
});

/*
*
* Functions for validating comment form 
*
*/
validateForm = function() {
	var validated = validateFormName();
	validated &= validateFormEmail();
	validated &= validateFormComment();
	
	var is_valid = (validated == true);

	return is_valid;
};
validateFormName = function() {
	var isValid = true;
	var testName = checkName($('#author').val());
	
	if(!testName || $('#author').val() == "") {
		$('#name-error').show();
		isValid = false;
	}
	else {
		$('#name-error').hide();
	}
	return isValid;
};
validateFormEmail = function() {
	var isValid = true;
		
	var testEmail = checkEmail($('#email').val());

	if(!testEmail || $('#email').val() == "") {
		$('#email-error').show();
		isValid = false;
	}
	else {
		$('#email-error').hide();
	}
	
	return isValid;
};
validateFormComment = function() {
	var isValid = true;
	var checkLength = checkWordCount($('#comment').val(), 500);	
	if($('#comment').val() != "") {
		if(checkLength) {
			$('#comment-error').hide();
		}
		else {
			$('#comment-error').show();
			isValid = false;
		}
	}
	else {
		$('#comment-error').show();
		isValid = false;
	}
	return isValid;
};
checkEmail = function(input_text) {
		var emailExp = new RegExp(/^([a-zA-Z0-9_\.\-\+%])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,8})+$/);
		return emailExp.test(input_text);
};
checkName = function(input_text) {
		var nameExp = new RegExp(/^[a-zA-Z '-]+$/);
		return nameExp.test(input_text);
};
checkWordCount = function(input_text, max_words) {
	var wc = input_text.split(' ').length;
	return(wc > 2 && wc <= max_words);
};



