$( function() {
  $('form.login').submit(
    function () {
      var
      request_email = $('#request_email', this).val() || '',
      email = $('#login_email', this).val() || '',
      code = $('#login_code', this).val() || '',
      agreed = $('#login_agreed:checked', this).length,
      do_login = false;

      $.ajax({ url: 'validate_login_data',
	       dataType: 'json',
	       async: false,
	       data: { accesscode: code,
		       request_email: request_email,
		       email: email,
		       agreed: agreed },
	       success: function(data) {
		 if (data.move_email) {
		   $('#login_email').val( request_email );
		   $('#login_code').val('');
		 }
		 if (data.data) {
		   do_login = true;
		 } else {
		   alert(data.message);
		   $('#login_code').focus();
		 }
	       },
	       error: function() {
		 alert(' An error occurred. Please contact support@energyfundamentals.com.') ;
	       }
	     } );
      return do_login ;
    } ); } );
