function validateEmail(elementValue){  
  var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
  return emailPattern.test(elementValue);  
} 


function sendValues(theName, theEmail, theComments)
{
	//remove existing error msg if any
	$('#error-msg').html('');
	$('#success-msg').html('');
	//disable submit btn
	$('#submit-btn').fadeOut(300);
	//add a loading icon
	$('#error-msg').append('<img src="assets/slices/loading.gif" border="0" />');
	$.post('contact.php', {uname:theName, email:theEmail, txt:theComments, rnd:'g_391109'},
		function(data) {
			if(data == 'Fail') 
			{
				$('#error-msg').html('');
				$('#error-msg').append('Something went wrong while recording your comment, please try again :(');
				$('#error-msg').fadeOut(0);
				$('#error-msg').fadeIn(300);
				$('#submit-btn').fadeIn(300);
			}else{
				$('#error-msg').html('');
				$('#success-msg').append('Thanks for writing to us! We will get back to you with an appropriate response at the earliest :)');
				$('#success-msg').fadeOut(0);
				$('#success-msg').fadeIn(300);
			}
		}
	);
}

$(document).ready(
	function()
	{
		//uname focus/blur
		$('#uname').focus(function()
			{
				if($(this).val() == 'YOUR NAME') $(this).val('');
			}
		);
		
		$('#uname').blur(function()
			{
				if($(this).val() == '')
				{
					$(this).val('YOUR NAME');
				}
			}
		);
		
		//email focus/blur
		$('#email').focus(function()
			{
				if($(this).val() == 'VALID EMAIL ADDRESS') $(this).val('');
			}
		);
		
		$('#email').blur(function()
			{
				if($(this).val() == '')
				{
					$(this).val('VALID EMAIL ADDRESS');
				}
			}
		);
		
		//comments
		$('#comments').focus(function()
			{
				if($(this).val() == 'YOUR COMMENTS') $(this).val('');
			}
		);
		
		$('#comments').blur(function()
			{
				if($(this).val() == '')
				{
					$(this).val('YOUR COMMENTS');
				}
			}
		);
		
	
		$('#submit-btn').click(
			function()
			{
				//get the values in the form
				var uname = $('#uname').val();
				var email = $('#email').val();
				var txt = $('#comments').val();
				
				if(uname.toLowerCase().indexOf('your') > -1)
				{
					alert('That surely cant be your name! :D');
					$('#uname').focus();
					return false;
				}
				
				else if(!validateEmail(email))
				{
					alert('Please enter a valid email address!');
					$('#email').focus();
					return false;
				}
				
				else
				{
					sendValues(uname,email,txt);
					return false;
				}
			}
		);
	}
);

