showDialog = function(title, body, focus)
{
    $("#dialog").dialog({
        modal: true,
        position: 'center',
        bgiframe: true,
        title: title,

        close: function(event, ui) {
                $('#dialog').dialog('destroy');
                if (focus)
                    $(focus).focus();
        },
        open: function(event, ui) {
            $('#dialog-body').html(body);
        },
        buttons: {
            "Ok": function() {$(this).dialog("close")}
        }

    });

    $('#dialog').dialog('open');
}

showDiscussionDialog = function(replyId, subject, loggedIn)
{

    var btns = {};
    btns[$.i18n('Add')] = function() {
        if (validateDiscussionForm(loggedIn))
            $('#discussionForm').submit();
    };

    btns[$.i18n('Close')] = function() {$(this).dialog("close");};

    $("#discussion-dialog").dialog({
        modal: true,
        position: 'center',
        bgiframe: true,
        title: $.i18n('Add new entry'),
        
        close: function(event, ui) {
                $('#discussion-dialog').dialog('destroy');
        },

        open: function(event, ui) {

            if (!loggedIn) {
                $("#discussion-dialog input[type=text]").val('');
                reloadCaptcha('dsc_captcha');
            }

            $("#discussion-dialog textarea").val('');
            $("#discussion-dialog input[type=checkbox]").removeAttr('checked');

            if (subject != undefined)
                $("#discussion-dialog input[name=dsc_subject]").val(subject);
            else {
                $("#discussion-dialog input[name=dsc_subject]").val('');
            }

            if (replyId != undefined)
                $("#dsc_replyId").val(replyId);
            else
                $("#dsc_replyId").val('');
        },
        buttons: btns

    });

}

openResetPassDialog = function()
{

    var btns = {};
    
    var _validateForm = function()
    {
        if (!validatorRequire('#r_email', $.i18n('Please enter email'), $.i18n('Error'))) return false;
        if (!validatorEmail('#r_email', $.i18n('Please enter valid email'), $.i18n('Error'))) return false;
        if (!validatorCaptcha('#resetPassCaptcha_word', '#resetPassCaptcha_id', $.i18n('Please enter valid code'), $.i18n('Error'))) return false;
        return true;
    }

    btns[$.i18n('Send')] = function() {
        if (_validateForm()) {
            $('#dialog-reset').dialog('destroy');
            $.ajax({
                type: "POST",
                url:"/system/user-reset-pass/",
                async: false,
                data: {
                    captcha_word: $('#resetPassCaptcha_word').val(),
                    captcha_id: $('#resetPassCaptcha_id').val(),
                    email: $('#r_email').val(),
                    lang: pageLang
                },
                dataType: "json",
                success: function(data){
                    showDialog($.i18n('Info'), '<p>'+data.msg+'</p>');
                }
            });
        }
    };

    btns[$.i18n('Close')] = function() {$(this).dialog("close");};

    $("#dialog-reset").dialog({
        modal: true,
        position: 'center',
        bgiframe: true,
        title: $.i18n('Reset password'),

        close: function(event, ui) {
                $('#dialog-reset').dialog('destroy');
        },
        open: function(event, ui) {
            var body = '<form method="post" action="/system/reset-pass/request/" name="resetPassForm" id="resetPassForm" onsubmit="return false;"><dl>';
            body += '<dt><label for="r_email">Email</label></dt><dd><input type="text" class="txt4" name="email" id="r_email" /></dd>';
            body += '<dt><label for="resetPassCaptcha_word">'+$.i18n('Rewrite code')+':</label></dt><dd><div id="resetPassCaptcha-loading" class="captcha-img-txt5" style="float: left;"><img src="" id="resetPassCaptcha-img" title="'+$.i18n('Click to reload captcha image')+'" alt="Captcha" onclick="reloadCaptcha(\'resetPassCaptcha\');"/><input type="hidden" name="captcha_id" id="resetPassCaptcha_id"  /></div>';
            body += '<div style="float:left; margin-left: 10px;"><input type="text" class="txt5" name="captcha_word" id="resetPassCaptcha_word" maxlength="3" /></div><div class="cleaner"></div></dd>';
            body += '</dl></form>';

            $('div#dialog-reset div#dialog-body1').html(body);

            reloadCaptcha('resetPassCaptcha');
        },
        buttons: btns

    });

}

