$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".buttonrec").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	var YourName = $("input#YourName").val();
	
	if (YourName == "") 
		{
			$("label#yourname_error").show();
			$("input#YourName").focus();
			return false;
		}
		
	var YourEmail = $("input#YourEmail").val();
	
	if (YourEmail == "") 
		{
			$("label#youremail_error").show();
			$("input#YourEmail").focus();
			return false;
    	}
		
	var FriendName = $("input#FriendName").val();
	
	if (FriendName == "") 
		{
			$("label#friendname_error").show();
			$("input#FriendName").focus();
			return false;
    	}
		
	var FriendEmail = $("input#FriendEmail").val();
	
	if (FriendEmail == "") 
		{
			$("label#friendemail_error").show();
			$("input#FriendEmail").focus();
			return false;
    	}		
		
	var Message = $("textarea#Message").val();
	
	if (Message == "") 
		{
			$("label#message_error").show();
			$("textarea#Message").focus();
			return false;
    	}	

	var ItemIdRe = $("#FriForm input#ItemIdRe").val(); 
			
	var dataString = 'YourName='+ YourName + '&YourEmail=' + YourEmail + '&FriendName=' + FriendName + '&FriendEmail=' + FriendEmail + '&Message=' + Message + '&ItemIdRe=' + ItemIdRe;
	
	//alert (dataString);return false;
	
	$.ajax({
    type: "POST",
    url: "/recommend.php",
    data: dataString,
    success: function() 
		{
			$('#friend_form').html("<div id='message_friend'></div>");
			$('#message_friend').html("<h2>Thankyou for sending your recommendation</h2>")
			.append("<p>Your friend should receive an email soon</p>")
			.hide()
			.fadeIn(1500, function() 
				{
					$('#message_friend').append("<img id='checkmark' src='/imgs/check.png' />");
				});
		}
    });
    return false;
});
});
runOnLoad(function(){
  $("input#YourName").select().focus();
});

