$(document).ready(function() {
	function submitComment() {
		var name = $('input[name=name]').val();
		var email = $('input[name=email]').val();
		var phone = $('input[name=phone]').val();
		var comment = $('textarea#ta_comment').val();
		
		var html = $.ajax({
			type: 'POST',
			url: 'php/ajax.contact.php',
			data: 'name=' + name + '&email=' + email + '&phone=' + phone + '&comment=' + comment, 
			async: false
		}).responseText;
	 
		return true;
	}
	
	function validateName() {
		if (jQuery.trim($('input[name=name]').val()) == '') {
			return false;
		}
		
		return true;
	}	
	
	function validateEmail() {
		if (jQuery.trim($('input[name=email]').val()) == '') {
			return false;
		}
		
		return true;
	}
	
	function validateComment() {
		if (jQuery.trim($('textarea#ta_comment').val()) == '') {
			return false;
		}
		
		return true;
	}
	
	$('#error_dialog').dialog({
		bgiframe: true,			
		modal: true,
		autoOpen: false,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});
	
	$('input[name=submit]').click(function(e){
		e.preventDefault();
		
		$('#error_dialog_msgs').empty();	

		if (!validateName()) {
			$('#error_dialog_msgs').append('<div class="dialog_warning">You need to provide a contact name.</div>');				
		}
		if (!validateEmail()) {
			$('#error_dialog_msgs').append('<div class="dialog_warning">You need to provide an email address.</div>');
		}
		if (!validateComment()) {
			$('#error_dialog_msgs').append('<div class="dialog_warning">You need to provide a comment or question.</div>');				
		}
		
		if ($('#error_dialog_msgs').text() != '') {
			$('#error_dialog').dialog('option', 'title', 'Error');
			$('#error_dialog_msgs').addClass('ui-state-error ui-corner-all');
			$('#error_dialog').dialog('open');
		} else {
			submitComment();	
			
			$('input[name=name]').val('');
			$('input[name=email]').val('');
			$('input[name=phone]').val('');
			$('textarea#ta_comment').val('');						
				
			$('#error_dialog').dialog('option', 'title', 'Thank you!');
			$('#error_dialog_msgs').removeClass('ui-state-error ui-corner-all');
			$('#error_dialog_msgs').append('<div class="dialog_thanks">Thank you for contacting me. I will respond as soon as possible.</div>');
			$('#error_dialog').dialog('open');
		}		
	});
});
