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

$(document).ready(function()
{
	// Hide the errormessages from the required input fields.
	$('.commentError').hide();
	
	//Handler Event for clicking submit button.
	$(".commentSubmitBtn").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 == ""){
				$('.commentError').show();
				$("#authorFieldError").show();
				$("#author").focus();
				hasError = true;
				$('label[for=author]').css({color: "red"});
				$("#btnSidebarComment").animate({marginTop: '130px'}, 500);
			} else {
				$("#authorFieldError").hide();
				$('label[for=author]').css({color: "black"});
			}
			
			
			//Checks if someone add their email the right way.
			if(email == ""){
				$('.commentError').show();
				$("#emailFieldError").show();
				$("#email").focus();
				hasError = true;
				$('label[for=email]').css({color: "red"});
			}else if (!emailReg.test(email)){
				$('.commentError').show();
				$("#emailFieldError").show();
				$("#email").focus();
				hasError = true;
				$('label[for=email]').css({color: "red"});
				$("#btnSidebarComment").animate({marginTop: '130px'}, 500);
			} else {
				$("#emailFieldError").hide();
				$('label[for=email]').css({color: "black"});
			}
			
			
			//Checks if someone add their message to the input field.
			if(comment == ""){
				$('.commentError').show();
				$("#commentFieldError").show();
				$("#comment").focus();
				hasError = true;
				$('label[for=message]').css({color: "red"});
				$("#btnSidebarComment").animate({marginTop: '130px'}, 500);
			} else {
				$("#commentFieldError").hide();
				$('label[for=message]').css({color: "black"});
			}
			
			
			
			if (author != "" && email != "" && comment != "") {
				$('.commentError').hide();
				$("#btnSidebarComment").animate({marginTop: '5px'}, 500);
			}
			

			
			return !hasError;
	
	});
});