﻿$(function() {
    // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
    $("#dialog").dialog("destroy");

    var name = $("#name"),
		email = $("#email"),
		password = $("#password"),
		allFields = $([]).add(name).add(email).add(password),
		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("Length of " + n + " must be between " + min + " and " + max + ".");
            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").dialog({
        autoOpen: false,
        height: 450,
        width: 480,
        modal: true,
        buttons: {
            'Отправить': function() {
                $('form').submit();
                return false;
            }
        },
        close: function() {
            allFields.val('').removeClass('ui-state-error');
        }
    });

    $("#dialog-form").parent().appendTo($("form:first"));


    $('a.create-user')
		.button()
		.click(function() {
		    $('#dialog-form').dialog('open');
		});

});
