$(document).ready(function() {
  
  $('input:checkbox, input:radio').customInput();

  $.validator.addMethod('password', function(value, element) {
      return this.optional(element) || value.length > 7 && /\d{1,}/.test(value) && /\w{8,}/.test(value);
  }, "Please enter at least 8 characters with at least 1 number.");

  $('#RegisterForm').validate({
      rules: {
          EmailConfirm: {
              required: true,
              email: true,
              equalTo: "#EmailAddress"
          },
          PasswordConfirm: {
              required: true,
              password: true,
              equalTo: "#Password"
          }
      },
      messages: {
          EmailConfirm: {
              equalTo: "This must match the email address in the previous field"
          },
          PasswordConfirm: {
              equalTo: "This must match the password in the previous field"
          }
      }
  });
  
  $('a.forgotpass').afdialog({
    width: 450,
    buttons: {
      'Cancel|red': function() {
        $(this).dialog('close');
      },
      'Reset My Password|blue': function() {
        var theform = $('#ForgotPassword');
        $.ajax({
          type: 'post',
          url: theform.attr('action'),
          data: theform.serializeArray(),
          success: function(t) {
            theform.parent().html(t);
            
          }
        });
        $(this).dialog('option', 'buttons', {
          'Retrieve Password|blue': function() {
        		retrievePassword();
        		return false;
          }
        });
      }
    }
  });
  
  function retrievePassword() {
		var theform = $('#SecurityQuestion');
		$.ajax({
			type: 'post',
			url: theform.attr('action'),
			data: theform.serializeArray(),
			success: function(t) {
				theform.html(t);
				$('a.forgotpass').dialog('option', 'buttons', {
				  
				});
			}
		});
	}
});
