$(function() {
	
	$("#contact-btn").bind("click",scrollToContact);
	
	function scrollToContact()
	{
		$('html, body').stop().animate({scrollTop: $('#contact_form').offset().top}, 1000,'easeInOutExpo');
	}
	
	$("input#name").blur(function()
	{
		if($(this).val() == "")
		{
			$(this).val("name");
		}
	});
	
	$("input#profession").blur(function()
	{
		if($(this).val() == "")
		{
			$(this).val("profession");
		}
	});
	
	$("input#email").blur(function()
	{
		if($(this).val() == "")
		{
			$(this).val("email address");
		}
	});
	
  $('#contact span.error').hide();
  $('#contact input[type=text]').css({backgroundColor:"#FFFFFF"});
  $('#contact input[type=text]').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
	if($(this).val() == "name" || $(this).val() == "profession" || $(this).val() == "email address")
	{
		$(this).val("");
	}
  });
  
  $('#contact input[type=text]').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
	/*if($("input#name").val() == "") 
	{
		$("input#name").val("name");
	}
	if($("input#profession").val() == "")
	{
		$("input#profession").val("profession");
	}
	if($("input#email").val() == "")
	{
		$("input#email").val("email address");
	}*/
  });

  $("#submit_btn").click(function() {
		// validate and process form
		// first hide any error messages
		
    $('#contact span.error').hide();
		
	  var name = $("input#name").val();
		if (name == "" || name == "name") {
      $("span#name_error").show();
      $("input#name").focus();
      return false;
    }
		var email = $("input#email").val();
		if (email == "" || validEmail() == false || email == "email address") {
      $("span#email_error").show();
      $("input#email").focus();
      return false;
    }
		var profession = $("input#profession").val();
		if (profession == "" || profession == "profession") {
      $("span#profession_error").show();
      $("input#profession").focus();
      return false;
    }
		
		$('#contact_form').html('<h4 style="text-align:center; font-weight:bold;">Sending email. please wait...</h4>');
		
		var dataString = 'name='+ name + '&email=' + email + '&profession=' + profession;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "../script/process.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>Thanks for the email!</h2>")
        .append("<p>We will be in touch soon.</p>")
        .hide()
        .fadeIn(1500);
      }
     });
    return false;
	});
	
	var testresults;
function validEmail()
{
	var str = $("input#email").val();
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
	{
		testresults=true;
	}
	else{
		testresults=false;
	}
	return (testresults)
}
	
});

