
var LoginValidator;
var LoginSuccessful = function(response) {
    location.reload(true);
}
$().ready(function() {
    var options = jQuery.extend(true, {}, DefaultAjaxOptions);
    options.url = ServicesPath + 'AuthenticateUser.ajax';
    options.type = 'POST';
    
    options.success = function(response, status) {
        response = text2xml(response);
        if(HasErrors(response)) {
            HandleErrors(LoginValidator, response);
            $("ul", ErrorsElement).show();
            ErrorsElement.show();
        }
        else {
            LoginSuccessful(response);
        }
    }
    
    var validationOptions = {
        rules: {
            "email": {
                required: true,
                email: true
            },
            "password": {
                required: true,
                minlength: 6,
                maxlength: 14
            }
        },
        messages: {
            "email": {
                required: "Email required",
                email: "Email invalid"
            },
            "password": {
                required: "Password required",
                minlength: "Password > 5 chars",
                maxlength: "Password < 15 chars"
            }
        },
        errorContainer: ErrorsElement,
        errorLabelContainer: $("ul", ErrorsElement),
        wrapper: 'li',
        submitHandler: function(form) {
            $("ul", ErrorsElement).show();
            $("ul", ErrorsElement).empty();
            $(form).ajaxSubmit(options);
            $("ul", ErrorsElement).show();
            ErrorsElement.show();
            return false;
        }
    };
    
    ErrorHandlersMap["Incorrect email/password combination, could not authenticate"] = function(error) {
         $("ul", ErrorsElement).show();
         $("ul", ErrorsElement).append('<li>' + 'Incorrect' + '</li>');
         $("ul", ErrorsElement).show();
    };    
    
    /*
    ErrorHandlersMap.Ineligible = function(error) {
        $('#errors').show();
        $('#errors').append('<li>' + validationOptions.messages.ineligible + '</li>');
    }*/
        
    LoginValidator = $('#login').validate(validationOptions);
});

