﻿(function($) {
    var $usernameWebservice;

    $.fn.required = function(params) {

        var $message = params.message;
        if (this.val() === "" || this.val() === "undefined") {
            this.showError({ control: this });

            if ($message !== undefined && $message !== "") {
                this.setErrorMessage({ control: this, message: $message });
            }
            return false;
        }

        this.hideError({ control: this });
        return true;
    };

    $.fn.date = function() {
        var exp = /^([01][1-9]|3[0-1])\/(0?[1-9]|1[0-2])\/(\d{4})$/;

        if (!exp.test(this.val())) {
            this.showError({ control: this });
            return false;
        }
        else {
            this.hideError({ control: this });
            return true;
        }
    };

    $.fn.username = function() {
        var $valid = true;

        $.WebServiceCall({
            url: $usernameWebservice,
            data: "{username: '" + $(this).val() + "'}"
        }, function(response) {
            if (response.d) {
                $valid = false;
            }
        });

        if (!$valid) {
            this.showError({ control: $(this) });
            this.setErrorMessage({ control: $(this), message: "The username you have chosen already exists" });
            return false;
        }
        else {
            this.hideError({ control: $(this) });
            return true;
        }
    };

    $.fn.telephone = function() {
        var num = this.val().replace(" ", "");

        var exp = /^[0-9]{10,11}$/;

        if (!exp.test(num)) {
            this.showError({ control: this });
            return false;
        }
        else {
            this.hideError({ control: this });
            return true;
        }
    };

    $.fn.password = function() {
        var $confirmPasswordTextBox = $(".confirmPassword");
        var $password = $(".mainPassword");

        if (($confirmPasswordTextBox.val() !== ""
            && $password.val() !== "")
            && ($confirmPasswordTextBox.validPassword({})
            && $password.validPassword({}))) {
            if ($password.val() === $confirmPasswordTextBox.val()) {

                this.hideError({ control: $password });
                this.hideError({ control: $confirmPasswordTextBox });
                return true;
            }

            this.showError({ control: $password });
            this.showError({ control: $confirmPasswordTextBox });
            this.setErrorMessage({ control: $password, message: "Passwords must match" });
            this.setErrorMessage({ control: $confirmPasswordTextBox, message: "Passwords must match" });
            return false;
        }
    };

    $.fn.validPassword = function(params) {
        var $message = params.message;

        if ($message === undefined)
            $message = "Password must be between 7 and 15 characters long and contain no spaces";


        var regExp = new RegExp("^([a-zA-Z0-9_@*#]{7,15})$");

        if (regExp.test(this.val())) {
            this.hideError({ control: this });
            return true;
        }

        this.setErrorMessage({ control: this, message: $message });
        this.showError({ control: this });
        return false;
    };

    $.fn.email = function() {
        var regExp = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        if (regExp.test(this.val())) {
            this.hideError({ control: this });
            return true;
        }

        this.showError({ control: this });
        return false;
    };

    $.fn.validate = function() {
        var valid = true;

        this.find(".required").each(function(name, element) {
            if (!$(this).required({})) {
                valid = false;
            }
        });

        this.find(".username").each(function(name, element) {
            if (!$(this).username()) {
                valid = false;
            }
        });

        this.find(".email").each(function(name, element) {
            if (!$(this).email()) {
                valid = false;
            }
        });

        this.find(".telephone").each(function(name, element) {
            if (!$(this).telephone()) {
                valid = false;
            }
        });

        this.find(".mainPassword").each(function(name, element) {
            if ($(this).required({ message: "Please enter a password" })) {
                if ($(this).validPassword({})) {
                    if (!$(this).password()) {
                        valid = false;
                    }
                }
            }
        });

        this.find(".confirmPassword").each(function(name, element) {
            if ($(this).required({ message: "Please confirm your password" })) {
                if ($(this).validPassword({})) {
                    if (!$(this).password()) {
                        valid = false;
                    }
                }
            }
        });

        this.find(".date").each(function(name, element) {
            if (!$(this).date()) {
                valid = false;
            }
        });

        this.find(".dateRequired").each(function(name, element) {
            if ($(this).required({})) {
                if (!$(this).date()) {
                    valid = false;
                }
            }
        });

        return valid;
    };

    $.fn.InitialiseValidation = function(params) {
        $usernameWebservice = params.usernameWebservice;

        $(".required").live("blur", function() {
            $(this).required({});
        });

        $(".mainPassword").live("blur", function() {
            if ($(this).required({ message: "Please enter a password" })) {
                if ($(this).validPassword({})) {
                    $(this).password();
                }
            }
        });

        $(".confirmPassword").live("blur", function() {
            if ($(this).required({ message: "Please confirm your password" })) {
                if ($(this).validPassword({})) {
                    $(this).password();
                }
            }
        });

        $(".telephone").live("blur", function() {
            $(this).telephone();
        });

        $(".email").live("blur", function() {
            $(this).email();
        });

        $(".username").live("blur", function() {
            var $isValidUsername = $(this).validPassword({ message: "Username must be between 7 and 15 characters long and contain no spaces" });
            if ($isValidUsername) {
                $(this).username();
            }
        });

        $(".date").live("blur", function() {
            $(this).date();
        });

        $(".dateRequired").live("blur", function() {
            if ($(this).required({})) {
                $(this).date();
            }
        });
    };

    $.fn.showError = function(params) {
        var $control = params.control;
        var $errorControl = $control.parent().find(".errorControl");

        var $controlClass = $control.attr("class");

        if ($controlClass.indexOf("2deep") > 0)
            $errorControl = $control.parent().parent().find(".errorControl");

        $control.css("border", "2px inset red");
        $errorControl.addClass("inlineError");
        $errorControl.show();
    };

    $.fn.hideError = function(params) {
        var $control = params.control;
        var $errorControl = $control.parent().find(".errorControl");

        var $controlClass = $control.attr("class");

        if ($controlClass.indexOf("2deep") > 0)
            $errorControl = $control.parent().parent().find(".errorControl");

        $control.css("border", "thin inset");
        $errorControl.removeClass("inlineError");
        $errorControl.hide();
    };

    $.fn.setErrorMessage = function(params) {
        var $control = params.control;
        var $message = params.message;
        var $errorControl = $control.parent().find(".errorControl");
        $errorControl.html($message);
        $errorControl.innerHTML = $message;
    };
})(jQuery);

