$(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"});
  });

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	  var name = $("input#name").val();
		if (name == "") {
      $("label#name_error").show();
      $("input#name").focus();
      return false;
    }
		var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
		var emailbody = $("textarea#emailbody").val();
		if (emailbody == "") {
      $("label#emailbody_error").show();
      $("input#emailbody").focus();
      return false;
    }
		
		var dataString = 'name='+ name + '&email=' + email + '&emailbody=' + emailbody;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "bin/process.php",
      data: dataString,
      success: function() {
   		
       
        $('#PTContactform').append("<div id='message'></div>");
       	$('#message').html("<img id='checkmark' src='images/messageconfirmation.png' />")
        .hide()
        .fadeIn(1500,function(){resetForm();} );
          $('#message').delay(5000);
          
          $('#message').animate( {opacity:'0'}, 250 , function(){$('#message').remove();} );
          
    
      	}
     });
    return false;
	});
});

//runOnLoad(function(){
  //$("input#name").select().focus();
//});

function resetForm(){
	// This resets the form after submission
		var myInputAreas = document.getElementsByTagName('input');
		var myTextArea = document.getElementsByTagName('textarea');
		myInputAreas[0].value = "full name";
		myInputAreas[1].value = "e-mail address";
		myTextArea[0].value = "What do you want to talk about?";
}
