/*
 * comment.js
 * Copyright (C) 2009
 * Author: Vanity Stuurland <vanity.stuurland@lightmaker.com>
 * Created: 2009-12-08
 */

$(document).ready(function()
{
	
	//Handler Event for clicking submit button.
	$(".form-submit #submit").click(function(event)
	{
			
			//For matching email and phone number.
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

			//Making variables of the id's from the input fields.
			var author = $("#author").val();
			var email = $("#email").val();
			var comment = $("#comment").val();
			
			var hasError = false;
			
			
			//Trimming the spaces from the input fields.
			$('input, textarea').each(function(){
				$(this).val(jQuery.trim($(this).val()));
			});
			
			
			
			//Checks if someone add their name to the input field.
			if(author == ""){
				$("#author").focus();
				$("#authorFieldError").show();
				//$('label[for=author]').css({color: "red"});
				hasError = true;	
			} else {
				$("#authorFieldError").css({opacity: "0"});
				//$('label[for=author]').css({color: "black"});
			}
			
			
			//Checks if someone add their email the right way.
			if(email == ""){
				$("#email").focus();
				$("#emailFieldError").show();	
				//$('label[for=email]').css({color: "red"});
				hasError = true;
			}else if (!emailReg.test(email)){
				$("#email").focus();
				$("#emailFieldError").show();
				//$('label[for=email]').css({color: "red"});
				hasError = true;
			} else {
				$("#emailFieldError").css({opacity: "0"});
				//$("#emailFieldError").hide();
				//$('label[for=email]').css({color: "black"});
			}
			
			
			//Checks if someone add their message to the input field.
			if(comment == ""){
				$("#comment").focus();
				$("#messageFieldError").show();
				$("#comment").animate({marginTop: '5px'}, 500);
				//$('label[for=message]').css({color: "red"});
				hasError = true;
			} else {
				$("#messageFieldError").hide();
				$("#comment").animate({marginTop: '0px'}, 500);
				//$('label[for=message]').css({color: "black"});
			}
			
			
			if (author != "" || email != "" || (!emailReg.test(email))) {
				$("#author").animate({marginTop: '0px'}, 500);
				$("#email").animate({marginTop: '0px'}, 500);
			} else {
				$("#author").animate({marginTop: '5px'}, 500);
				$("#email").animate({marginTop: '5px'}, 500);
			}
			
			return !hasError;
	
	});
});
