
function backToForm(){
	$('#responseContain').hide();
	$('#formContain').show();
	$("#btnSubmit").attr("disabled", false);
	$("#theForm input").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 gender = $('#theForm #gender_m').attr('checked') ? "M" : ($('#theForm #gender_f').attr('checked') ? "F" : "");
			var dob = $('#theForm #dob_d').attr('value')+"/"+$('#theForm #dob_m').attr('value')+"/"+$('#theForm #dob_y').attr('value');
			var email = $('#theForm #email').attr('value');
			var phone = $('#theForm #phone').attr('value');
			var address = $('#theForm #address').attr('value');
			var suburb = $('#theForm #suburb').attr('value');
			var post_code = $('#theForm #post_code').attr('value');
			var state = $('#theForm #stateSelect').attr('value');
			var country = $('#theForm #countrySelect').attr('value');
			var store = $('#theForm #store').attr('value');
			var token = $('#theForm #token').attr('value');
			var sign_up = $('#theForm #sign_up').attr('checked') ? "1" : "0";
			
			var ts = $('#postTS').attr('value');
			
			if (gender.length == 0) errorMessage += "Please specify your gender. \n";
			if (dob.length == 0 || dob == "//") errorMessage += "Please specify your date of birth. \n";
			if (!validateEmail("theForm","email")) errorMessage += "Please specify a valid email. \n";
			if (phone.length == 0) errorMessage += "Please specify your phone number. \n";
			if (address.length == 0) errorMessage += "Please specify your address. \n";
			if (state.length == 0 || state == undefined) errorMessage += "Please specify your state/province/region. \n";
			if (country.length == 0 || country == undefined) errorMessage += "Please specify your country. \n";
			if (store.length == 0) errorMessage += "Please specify your store of purchase. \n";
			if (token.length == 0) errorMessage += "Please enter your token. \n";
			
			if (errorMessage == "") {
			 
				$("#theForm input").attr("disabled", "disabled");
				$("#btnSubmit").attr("disabled", "disabled");
				$.ajax({
					type: "POST",
					url: "/common_gl/inc_wax-and-win_post.php",
					data: "first_name=" + URLEncode(firstName) + 
							"&last_name=" + URLEncode(lastName) + 
							"&gender=" + gender + 
							"&dob=" + URLEncode(dob) + 
							"&phone=" + URLEncode(phone) + 
							"&email=" + URLEncode(email) + 
							"&address=" + URLEncode(address) + 
							"&suburb=" + URLEncode(suburb) + 
							"&post_code=" + URLEncode(post_code) + 
							"&state=" + URLEncode(state) + 
							"&country=" + URLEncode(country) + 
							"&store=" + URLEncode(store) + 
							"&token=" + URLEncode(token) + 
							"&sign_up=" + sign_up + 
							"&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;
	});
});