﻿$(function() {
    $('#dialog-form:ui-dialog').dialog('destroy');

    var name = $("#name"),
			email = $("#email"),
			password = $("#password"),
            repassword = $("#repassword"),
            captcha = $('#captcha'),
            lname = $('#lname'),
            lpassword = $('#lpassword'),
			allRegFields = $([]).add(name).add(email).add(password).add(repassword),
            allLoginFields = $([]).add(lname).add(lpassword),
			tips = $(".validateTips");

    function updateTips(t) {
        tips
				.text(t)
				.addClass("ui-state-highlight");
        setTimeout(function() {
            tips.removeClass("ui-state-highlight", 1500);
        }, 500);
    }

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips(n + " должен быть длиной от " +
					min + " до " + max + ".");
            return false;
        } else {
            return true;
        }
    }

    function checkSize(o, n, size) {
        if (o.val().length != size) {
            o.addClass("ui-state-error");
            updateTips(n + " должна быть длиной " +
					size + " символов.");
            return false;
        } else {
            return true;
        }
    }

    function checkEquals(val1, val2) {
        if (val1.val() !== val2.val()) {
            val2.addClass("ui-state-error");
            updateTips("Пароли должны совпадать");
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass("ui-state-error");
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }
    $("#dialog-form").show();
    $("#dialog-form").dialog({
        autoOpen: false,
        height: 550,
        width: 700,
        modal: true,
        buttons: {
            "Закрыть": function() {
                $(this).dialog("close");
            }
        },
        close: function() {
            allRegFields.val("").removeClass("ui-state-error");
            allLoginFields.val("").removeClass("ui-state-error");
        }
    });

    $('button#register').click(function() {
        var bValid = true;
        allRegFields.removeClass("ui-state-error");

        bValid = bValid && checkLength(name, "Логин", 3, 32);
        bValid = bValid && checkLength(email, "E-mail", 6, 128);
        bValid = bValid && checkLength(password, "Пароль", 5, 32);
        bValid = bValid && checkLength(password, "Пароль", 5, 32);
        bValid = bValid && checkEquals(password, repassword);
        bValid = bValid && checkSize(captcha, "Капча", 5);

        bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "напр. test@test.com");

        if (bValid) {
            $(this).closest('form').submit();
            $(this).dialog("close");
        }
    });

    $('button#login').click(function() {
        var bValid = true;
        allLoginFields.removeClass("ui-state-error");

        bValid = bValid && checkLength(lname, "Логин", 3, 32);
        bValid = bValid && checkLength(lpassword, "Пароль", 5, 32);

        if (bValid) {
            $(this).closest('form').submit();
            $(this).dialog("close");
        }
    });
    $('ul#addNav a').click(function() {
        ShowLoginOrRegister();
        return false;
    });
});

function ShowLoginOrRegister() {
    $("#dialog-form").dialog("open");
}
