
function backToForm(){
	$('#responseContain').hide();
	$('#formContain').show();
	$("#btnSubmit").attr("disabled", false);
	$("#theForm input, #theForm textarea").attr("disabled", false);
}

$().ready(function(){

	$('#theForm .warning').remove();
	
	$.get("/common_gl/inc_comments_form_token.php",function(txt){
		$("#theForm").append('<input type="hidden" id="postTS" name="ts" value="'+txt+'" />');
	});
	
	$("#theForm").submit(function() {
	
		if ($("#btnSubmit").attr("disabled") != true) {
			
			var errorMessage = "";
			
			var firstName = $('#theForm #first_name').attr('value');
			var lastName = $('#theForm #last_name').attr('value');
			var companyName = $('#theForm #company_name').attr('value');
			var email = $('#theForm #email').attr('value');
			var streetAddress = $('#theForm #street_address').attr('value');
			var suburb = $('#theForm #suburb').attr('value');
			var state = $('#theForm #stateSelect').attr('value');
			var country = $('#theForm #countrySelect').attr('value');
			var post_code = $('#theForm #post_code').attr('value');
			var phone = $('#theForm #phone').attr('value');
			var contactMethod = $('#theForm #contact_method').attr('value');
			var contactTime = $('#theForm #contact_time').attr('value');
			var message = $('#theForm #message').attr('value');
			
			var ts = $('#postTS').attr('value');
			
			if (firstName.length == 0) errorMessage += "Please specify your first name. \n";
			if (lastName.length == 0) errorMessage += "Please specify your last name. \n";
			if (!validateEmail("theForm","email")) errorMessage += "Please specify a valid email. \n";
			if (phone.length == 0) errorMessage += "Please specify your phone number. \n";
			if (message.length == 0) errorMessage += "Please enter a message. \n";
			
			if (errorMessage == "") {
			 
				$("#theForm input, #theForm textarea").attr("disabled", "disabled");
				$("#btnSubmit").attr("disabled", "disabled");
				$.ajax({
					type: "POST",
					url: "/common_gl/inc_contact_post.php",
					data: "first_name=" + URLEncode(firstName) + 
							"&last_name=" + URLEncode(lastName) + 
							"&company_name=" + URLEncode(companyName) + 
							"&email=" + URLEncode(email) + 
							"&street_address=" + URLEncode(streetAddress) + 
							"&suburb=" + URLEncode(suburb) + 
							"&state=" + URLEncode(state) + 
							"&country=" + URLEncode(country) + 
							"&post_code=" + URLEncode(post_code) + 
							"&phone=" + URLEncode(phone) + 
							"&contact_method=" + URLEncode(contactMethod) + 
							"&contact_time=" + URLEncode(contactTime) + 
							"&message=" + URLEncode(message) + 
							"&ts=" + ts,
					success: function(data){
						$("#formContain").hide();
						$("#responseContain").show();
						$("#responseCell").html(data);
					}
				});
				
			} else {
				alert("The following errors need to be fixed: \n"+errorMessage);
			}
		
		}
		
		return false;
	});
});
