/* contact us  and Thankyou */
function isBlank(input) {
    return ($(input).val() == "");
}

function isValidEmail(input) {
    var filter  = /^([a-zA-Z0-9_.-/+])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,6})$/;
    return (filter.test($(input).val()));
}

function isChecked(input) {
	return ($(input).is(":checked"));
}
/* contact us */
function validateContactForm() {
	var error = new Array();
	if (isBlank($("#email"))) {
		error.push("Please fill in your e-mail address.");
	}
	else if (!isValidEmail($("#email"))) {
		error.push("Your e-mail address must be in the format name@domain.com");
	}
	if (isBlank($("#subject option:selected"))) {
		error.push("Please select a subject.");
	}
	if (isBlank($("#msg"))) {
		error.push("Please enter a message.");
	}
	if (error.length > 0) {
		$(".errorBox").remove();
		var errorBox = "<div class='errorBox'><ul>";
		for (var i=0; i < error.length; i++) {
			errorBox += "<li>" + error[i] + "</li>";
		}
		errorBox += "</ul></div>";
		$("#contactForm").before(errorBox);
		return false;
	}
	return true;
}
/* thank you form*/
function validateThankyouForm() {
	var error = new Array();
	if (isBlank($("#email"))) {
		error.push("Please fill in your e-mail address.");
	}
	else if (!isValidEmail($("#email"))) {
		error.push("Your e-mail address must be in the format name@domain.com");
	}
    if (!isChecked($('input:checkbox'))) {
		error.push("Please choose a newsletter.");
	}
	if (error.length > 0) {
		$(".errorBox").remove();
		var errorBox = "<div class='errorBox'><ul>";
		for (var i=0; i < error.length; i++) {
			errorBox += "<li>" + error[i] + "</li>";
		}
		errorBox += "</ul></div>";
		$("#thankyouForm").before(errorBox);
		return false;
	}
	return true;
}

$(document).ready(function(){
	//character counter
	$("#charCount").text("(1000 characters remaining)");
	$("#contactForm textarea").bind("click change keydown keyup keypress blur focus", function(){
		var count = $(this).val().length;
		$("#charCount").text("(" + (1000 - count) + " characters remaining)");
	});

    /*validate form on submit*/
    $("#contactForm").bind("submit", function(){
        return validateContactForm();
    });

	/*validate form on submit*/
    $("#thankyouForm").bind("submit", function(){
        return validateThankyouForm();
    });
});
