$(
	function() {
		/* Log in */
		$('#login_form').submit(
			function() {
				var is_login_successful = false;
				var error_messages = {};
				//var is_aafes_updated = false;

				$.ajax({
					async: false,
					url: '_system/controllers/user.cfc',
					data: { method: 'log_in', returnFormat: 'json', email_address: $('#login_email_address', this).val(), password: $('#login_password', this).val() },
					success: function(data) { is_login_successful = data; },
					dataType: 'json'
				});
				
				if (is_login_successful === true) {
					/*    read session.user.retailer_id and look for 27176 AAFES and dUpdated value GT some value tbd*/					
					$.ajax({
						async: false,
						url: '_system/controllers/user.cfc',
						data: { method: 'check_aafes', returnFormat: 'json'},
						success: function(data) { is_aafes_updated = data; },
						dataType: 'json'
					});	
					//alert(is_aafes_updated);					
					
						if (is_aafes_updated === true) {
							parent.location.reload();
						}
						else {
							//alert('Please update your profile to continue.');
							parent.location = '?user/update_profile.cfm';
					}

				} else {
					if ($('.error_messages', $('#global_login_overlay')).length == 0) $('#global_login_overlay >div:first').after($('<ul />').addClass('error_messages'));
					error_messages = $('.error_messages', $('#global_login_overlay')).empty();
					error_messages.append($('<li />').html('Username and/or password is incorrect. Please try again.'));
					$('#login_password', this).val('').focus();
				}
				return false;
			}
		);
		$('#feed_login_form').submit(
			function() {
				if ($('#understand_participation input:checked').val() == 1) return true;
				alert('You need to understand these conditions to proceed');
				return false;
			}
		);

		/* Migrate */
		$('#home_migrate_profile_form .form_control input[type=submit], #migrate_profile_form .form_control input[type=submit]').click(
			function() {
				user_profile = { username: $('#login_overlay #legacy_username').val(), system_type: 'legacy' }
				$.ajax({
					async: false,
					url: '_system/controllers/user.cfc',
					data: $.extend({ method: 'is_existing_user', returnFormat: 'json' }, user_profile),
					success: function(data) { is_existing_user = data; },
					dataType: 'json'
				});

				if (is_existing_user === true) {
				} else {
					if ($('.error_messages', $('#login_overlay')).length == 0) $('#login_overlay >div:first').after($('<ul />').addClass('error_messages'));
					error_messages = $('.error_messages', $('#login_overlay')).empty();
					error_messages.append($('<li />').html('Username is incorrect. Please try again.'));
					$('#login_password', this).val('');
					$('#legacy_username').focus();
				}
				return is_existing_user;
			}
		);

		$('#login_overlay').addClass('jqmWindow').jqm({ trigger: '#login_form_link' }).jqmAddClose('#login_overlay .close_overlay');
		$('body').append($('.jqmWindow'));
	}
);