var UI = new function()
{
    this.showWaitPopup = function()
    {
        // prepare popup's inner html
        var innerHtml = '<div id="popup_dialog_wrapper">' +
                            '<div id="popup_dialog_content">' +
                                '<img src="images/wait.gif" alt="" width="48" height="48" />' +
                            '</div>' +
                        '</div>';

        // show wait popup
        Popup.show('wait_popup', innerHtml);
    }

    this.hideWaitPopup = function()
    {
        // hide wait popup
        Popup.hide('wait_popup');
    }

    this.showInformationPopup = function(caption, content, okButtonText, onOkClick)
    {
        // prepare popup's inner html
        var innerHtml = '<div id="popup_dialog_wrapper">' +
                            '<div id="popup_dialog_caption">' +
                                '<img src="images/logo.gif" alt="" width="16" height="16" />' +
                                caption +
                            '</div>' +
                            '<div id="popup_dialog_content">' +
                                content +
                            '</div>' +
                            '<div id="popup_dialog_buttons">' +
                                '<input id="information_popup_ok" type="button" value="' + okButtonText + '" />' +
                            '</div>' +
                        '</div>';

        // show information popup
        Popup.show('information_popup', innerHtml);

        // set onOkClick event handler
        document.getElementById('information_popup_ok').onclick = function()
        {
            // hide information popup
            Popup.hide('information_popup');

            // evaluate onOkClick code
            if (onOkClick != null)
                eval(onOkClick);
        }
    }

    this.showPromptPopup = function(caption, content, yesButtonText, noButtonText, onYesClick, onNoClick)
    {
        // prepare popup's inner html
        var innerHtml = '<div id="popup_dialog_wrapper">' +
                            '<div id="popup_dialog_caption">' +
                                '<img src="images/logo.gif" alt="" width="16" height="16" />' +
                                caption +
                            '</div>' +
                            '<div id="popup_dialog_content">' +
                                content +
                            '</div>' +
                            '<div id="popup_dialog_buttons">' +
                                '<input id="prompt_popup_yes" type="button" value="' + yesButtonText + '" />' +
                                '<input id="prompt_popup_no" type="button" value="' + noButtonText + '" />' +
                            '</div>' +
                        '</div>';

        // show prompt popup
        Popup.show('prompt_popup', innerHtml);

        // set onYesClick event handler
        document.getElementById('prompt_popup_yes').onclick = function()
        {
            // hide prompt popup
            Popup.hide('prompt_popup');

            // evaluate onYesClick code
            if (onYesClick != null)
                eval(onYesClick);
        }

        // set onNoClick event handler
        document.getElementById('prompt_popup_no').onclick = function()
        {
            // hide prompt popup
            Popup.hide('prompt_popup');

            // evaluate onNoClick code
            if (onNoClick != null)
                eval(onNoClick);
        }
    }
}

function onRegistrationRequestComplete(responseText)
{
    // check response text
    switch (responseText)
    {
        case 'login_empty':
            UI.hideWaitPopup();
            UI.showInformationPopup('Rejestracja', 'Wprowadź swój login.', 'OK', 'document.getElementById(\'registration_login\').focus(); document.getElementById(\'registration_login\').select();');
            break;

        case 'login_invalid':
            UI.hideWaitPopup();
            UI.showInformationPopup('Rejestracja', 'Twój login zawiera niedozwolone znaki.<br /><br />Dozwolone znaki to: &quot;a-z 0-9 . - _&quot;.', 'OK', 'document.getElementById(\'registration_login\').focus(); document.getElementById(\'registration_login\').select();');
            break;

        case 'login_too_long':
            UI.hideWaitPopup();
            UI.showInformationPopup('Rejestracja', 'Twój login jest zbyt długi.<br /><br />Maksymalna długość loginu to 255 znaków.', 'OK', 'document.getElementById(\'registration_login\').focus(); document.getElementById(\'registration_login\').select();');
            break;

        case 'login_exists':
            UI.hideWaitPopup();
            UI.showInformationPopup('Rejestracja', 'Użytkownik o podanym loginie już istnieje.', 'OK', 'document.getElementById(\'registration_login\').focus(); document.getElementById(\'registration_login\').select();');
            break;

        case 'email_empty':
            UI.hideWaitPopup();
            UI.showInformationPopup('Rejestracja', 'Wprowadź swój adres e-mail.', 'OK', 'document.getElementById(\'registration_email\').focus(); document.getElementById(\'registration_email\').select();');
            break;

        case 'email_invalid':
            UI.hideWaitPopup();
            UI.showInformationPopup('Rejestracja', 'Nieprawidłowy adres e-mail.', 'OK', 'document.getElementById(\'registration_email\').focus(); document.getElementById(\'registration_email\').select();');
            break;

        case 'email_too_long':
            UI.hideWaitPopup();
            UI.showInformationPopup('Rejestracja', 'Twój adres e-mail jest zbyt długi.<br /><br />Maksymalna długość adresu e-mail to 255 znaków.', 'OK', 'document.getElementById(\'registration_email\').focus(); document.getElementById(\'registration_email\').select();');
            break;

        case 'email_exists':
            UI.hideWaitPopup();
            UI.showInformationPopup('Rejestracja', 'Podany adres e-mail jest przypisany do innego konta.', 'OK', 'document.getElementById(\'registration_email\').focus(); document.getElementById(\'registration_email\').select();');
            break;

        case 'email_not_send':
            UI.hideWaitPopup();
            UI.showInformationPopup('Rejestracja', 'Błąd podczas wysyłania wiadomości na Twój adres email.', 'OK', 'document.getElementById(\'registration_email\').focus(); document.getElementById(\'registration_email\').select();');
            break;

        case 'temporary_not_possible':
            UI.hideWaitPopup();
            UI.showInformationPopup('Rejestracja', 'Rejestracja jest tymczasowo niemożliwa.', 'OK', null);
            break;

        case 'ok':
            var contentElement = document.getElementById('content_left');
            contentElement.innerHTML = '<h1>Rejestracja zakończona pomyślnie</h1>';
            contentElement.innerHTML += '<h2>Dziękujemy za zarejestrowanie się w naszym serwisie</h2>';
            contentElement.innerHTML += 'Wiadomość z danymi niezbędnymi do zalogowania się została przesłana na Twój adres e-mail.';
            UI.hideWaitPopup();
            break;

        case 'reload':
        default:
            window.location.reload();
            break;
    }
}

function onRegisterClick()
{
    // show "please wait" popup
    UI.showWaitPopup();

    // prepare parameters which will be sent to the server
    var parameters = new Array();
    parameters['login'] = document.getElementById('registration_login').value;
    parameters['email'] = document.getElementById('registration_email').value;
    parameters['action'] = 'register';

    // send request to the server
    Ajax.sendRequest('registration', parameters, onRegistrationRequestComplete);
}

//------------------------------------------------------------------------------

function onRecoveryRequestComplete(responseText)
{
    // check response text
    switch (responseText)
    {
        case 'login_empty':
            UI.hideWaitPopup();
            UI.showInformationPopup('Odzyskiwanie hasła', 'Wprowadź swój login.', 'OK', 'document.getElementById(\'recovery_login\').focus(); document.getElementById(\'recovery_login\').select();');
            break;

        case 'login_invalid':
            UI.hideWaitPopup();
            UI.showInformationPopup('Odzyskiwanie hasła', 'Twój login zawiera niedozwolone znaki.<br /><br />Dozwolone znaki to: &quot;a-z 0-9 . - _&quot;.', 'OK', 'document.getElementById(\'recovery_login\').focus(); document.getElementById(\'recovery_login\').select();');
            break;

        case 'login_too_long':
            UI.hideWaitPopup();
            UI.showInformationPopup('Odzyskiwanie hasła', 'Twój login jest zbyt długi.<br /><br />Maksymalna długość loginu to 255 znaków.', 'OK', 'document.getElementById(\'recovery_login\').focus(); document.getElementById(\'recovery_login\').select();');
            break;

        case 'email_empty':
            UI.hideWaitPopup();
            UI.showInformationPopup('Odzyskiwanie hasła', 'Wprowadź swój adres e-mail.', 'OK', 'document.getElementById(\'recovery_email\').focus(); document.getElementById(\'recovery_email\').select();');
            break;

        case 'email_invalid':
            UI.hideWaitPopup();
            UI.showInformationPopup('Odzyskiwanie hasła', 'Nieprawidłowy adres e-mail.', 'OK', 'document.getElementById(\'recovery_email\').focus(); document.getElementById(\'recovery_email\').select();');
            break;

        case 'email_too_long':
            UI.hideWaitPopup();
            UI.showInformationPopup('Odzyskiwanie hasła', 'Twój adres e-mail jest zbyt długi.<br /><br />Maksymalna długość adresu e-mail to 255 znaków.', 'OK', 'document.getElementById(\'recovery_email\').focus(); document.getElementById(\'recovery_email\').select();');
            break;

        case 'email_not_send':
            UI.hideWaitPopup();
            UI.showInformationPopup('Odzyskiwanie hasła', 'Błąd podczas wysyłania wiadomości na Twój adres email.', 'OK', 'document.getElementById(\'recovery_email\').focus(); document.getElementById(\'recovery_email\').select();');
            break;

        case 'invalid_login_or_email':
            document.getElementById('recovery_login').value = '';
            document.getElementById('recovery_email').value = '';
            UI.hideWaitPopup();
            UI.showInformationPopup('Odzyskiwanie hasła', 'Nieprawidłowy login lub adres e-mail.', 'OK', 'document.getElementById(\'recovery_login\').focus(); document.getElementById(\'recovery_login\').select();');
            break;

        case 'temporary_not_possible':
            UI.hideWaitPopup();
            UI.showInformationPopup('Odzyskiwanie hasła', 'Odzyskanie hasła jest tymczasowo niemożliwe.', 'OK', null);
            break;

        case 'ok':
            var contentElement = document.getElementById('content_left');
            contentElement.innerHTML = '<h1>Odzyskiwanie hasła zakończone</h1>';
            contentElement.innerHTML += '<h2>Procedura odzyskiwania hasła do Twojego konta konta została pomyślnie zakończona.</h2>';
            contentElement.innerHTML += 'Nowe hasło do Twojego konta zostało przesłane na Twój adres e-mail.';
            UI.hideWaitPopup();
            break;

        case 'reload':
        case 'defualt':
            window.location.reload();
            break;
    }
}

function onRecoverPasswordClick()
{
    // show "please wait" popup
    UI.showWaitPopup();

    // prepare parameters which will be sent to the server
    var parameters = new Array();
    parameters['login'] = document.getElementById('recovery_login').value;
    parameters['email'] = document.getElementById('recovery_email').value;
    parameters['action'] = 'recover_password';

    // send request to the server
    Ajax.sendRequest('recovery', parameters, onRecoveryRequestComplete);
}

//------------------------------------------------------------------------------

function onLogInRequestComplete(responseText)
{
    // check response text
    switch (responseText)
    {
        case 'invalid_login_or_password':
            document.getElementById('login').value = 'login';
            document.getElementById('password').value = 'hasło';
            UI.hideWaitPopup();
            UI.showInformationPopup('Logowanie', 'Nieprawidłowa nazwa użytkownika lub hasło.', 'OK', null);
            break;

        case 'ok':
        case 'reload':
        default:
            window.location.reload();
            break;
    }
}

function onLogInClick()
{
    // show "please wait" popup
    UI.showWaitPopup();

    // prepare parameters which will be sent to the server
    var parameters = new Array();
    parameters['login'] = document.getElementById('login').value;
    parameters['password'] = document.getElementById('password').value;
    parameters['action'] = 'log_in';

    // send request to the server
    Ajax.sendRequest('login', parameters, onLogInRequestComplete);
}

//------------------------------------------------------------------------------

function onLogOutRequestComplete(responseText)
{
    // check response text
    switch (responseText)
    {
        case 'ok':
            window.location.href = "";
            break;

        case 'reload':
        default:
            window.location.reload();
            break;
    }
}

function onLogOutClick()
{
    // show "please wait" popup
    UI.showWaitPopup();

    // prepare parameters which will be sent to the server
    var parameters = new Array();
    parameters['action'] = 'log_out';

    // send request to the server
    Ajax.sendRequest('account', parameters, onLogOutRequestComplete);
}

//------------------------------------------------------------------------------

function onChangePasswordRequestComplete(responseText)
{
    // check response text
    switch (responseText)
    {
        case 'password_empty':
            UI.hideWaitPopup();
            UI.showInformationPopup('Zmiana hasła', 'Podaj nowe hasło.', 'OK', 'document.getElementById(\'password\').focus(); document.getElementById(\'password\').select();');
            break;

        case 'password_too_short':
            UI.hideWaitPopup();
            UI.showInformationPopup('Zmiana hasła', 'Nowe hasło jest zbyt krótkie.<br /><br />Minimalna długość hasła to 6 znaków.', 'OK', 'document.getElementById(\'password\').focus(); document.getElementById(\'password\').select();');
            break;

        case 'invalid_password_confirmation':
            UI.hideWaitPopup();
            UI.showInformationPopup('Zmiana hasła', 'Nieprawidłowe potwierdzenie hasła.', 'OK', 'document.getElementById(\'password_confirmation\').focus(); document.getElementById(\'password_confirmation\').select();');
            break;

        case 'temporary_not_possible':
            UI.hideWaitPopup();
            UI.showInformationPopup('Zmiana hasła', 'Zmiana hasła jest tymczasowo niemożliwa.', 'OK', null);
            break;

        case 'ok':
            document.getElementById('password').value = '';
            document.getElementById('password_confirmation').value = '';
            UI.hideWaitPopup();
            UI.showInformationPopup('Zmiana hasła', 'Twoje hasło zostało zmienione.', 'OK', null);
            break;

        case 'reload':
        default:
            window.location.reload();
            break;
    }
}

function onChangePasswordClick()
{
    // show "please wait" popup
    UI.showWaitPopup();

    // prepare parameters which will be sent to the server
    var parameters = new Array();
    parameters['password'] = document.getElementById('password').value;
    parameters['password_confirmation'] = document.getElementById('password_confirmation').value;
    parameters['action'] = 'change_password';

    // send request to the server
    Ajax.sendRequest('account', parameters, onChangePasswordRequestComplete);
}

//------------------------------------------------------------------------------

function onChangeEmailRequestComplete(responseText)
{
    // check response text
    switch (responseText)
    {
        case 'email_empty':
            UI.hideWaitPopup();
            UI.showInformationPopup('Zmiana adresu e-mail', 'Wprowadź swój adres e-mail.', 'OK', 'document.getElementById(\'email\').focus(); document.getElementById(\'email\').select();');
            break;

        case 'email_invalid':
            UI.hideWaitPopup();
            UI.showInformationPopup('Zmiana adresu e-mail', 'Nieprawidłowy adres e-mail.', 'OK', 'document.getElementById(\'email\').focus(); document.getElementById(\'email\').select();');
            break;

        case 'email_too_long':
            UI.hideWaitPopup();
            UI.showInformationPopup('Zmiana adresu e-mail', 'Twój adres e-mail jest zbyt długi.<br /><br />Maksymalna długość adresu e-mail to 255 znaków.', 'OK', 'document.getElementById(\'email\').focus(); document.getElementById(\'email\').select();');
            break;

        case 'email_exists':
            UI.hideWaitPopup();
            UI.showInformationPopup('Zmiana adresu e-mail', 'Podany adres e-mail jest przypisany do innego konta.', 'OK', 'document.getElementById(\'email\').focus(); document.getElementById(\'email\').select();');
            break;

        case 'temporary_not_possible':
            UI.hideWaitPopup();
            UI.showInformationPopup('Zmiana adresu e-mail', 'Zmiana adresu e-mail jest tymczasowo niemożliwa.', 'OK', null);
            break;

        case 'ok':
            UI.hideWaitPopup();
            UI.showInformationPopup('Zmiana adresu e-mail', 'Twój adres e-mail został zmieniony.', 'OK', null);
            break;

        case 'reload':
        default:
            window.location.reload();
            break;
    }
}

function onChangeEmailClick()
{
    // show "please wait" popup
    UI.showWaitPopup();

    // prepare parameters which will be sent to the server
    var parameters = new Array();
    parameters['email'] = document.getElementById('email').value;
    parameters['action'] = 'change_email';

    // send request to the server
    Ajax.sendRequest('account', parameters, onChangeEmailRequestComplete);
}

//------------------------------------------------------------------------------

function onDeregisterRequestComplete(responseText)
{
    // check response text
    switch (responseText)
    {
        case 'administrator_account_cannot_be_deleted':
            UI.hideWaitPopup();
            UI.showInformationPopup('Usuwanie konta', 'Nie można usunąć wbudowanego konta administratora systemu.', 'OK', null);
            break;

        case 'ok':
            window.location.href = "";
            break;

        case 'reload':
        default:
            window.location.reload();
            break;
    }
}

function onDeregisterClick()
{
    var action = 'UI.showWaitPopup();';
    action += 'var parameters = new Array();';
    action += 'parameters[\"action\"] = \"deregister\";';
    action += 'Ajax.sendRequest(\"account\", parameters, onDeregisterRequestComplete);';
    UI.showPromptPopup('Usuwanie konta', 'Na pewno chcesz usunąć swoje konto z naszego systemu?', 'Tak', 'Nie', action, null);
}

//------------------------------------------------------------------------------

function onPollSubmitRequestComplete(responseText)
{
    // check response text
    switch (responseText)
    {
        case 'poll_item_empty':
            UI.hideWaitPopup();
            UI.showInformationPopup('Wysyłanie ankiety', 'Zaznacz jedną z dostępnych odpowiedzi.', 'OK', null);
            break;

        case 'temporary_not_possible':
            UI.hideWaitPopup();
            UI.showInformationPopup('Wysyłanie ankiety', 'Wysłanie ankiety jest tymczasowo niemożliwe.', 'OK', null);
            break;

        case 'ok':
        case 'reload':
        default:
            window.location.reload();
            break;
    }
}

function onPollSubmitClick()
{
    // show "please wait" popup
    UI.showWaitPopup();

    // get selected poll item
    var selectedPollItem = '';
    var pollItems = document.getElementsByName('poll_item');
    for (var i = 0; i < pollItems.length; i++)
    {
        if (pollItems[i].checked)
        {
            selectedPollItem = pollItems[i].value;
            break;
        }
    }

    // prepare parameters which will be sent to the server
    var parameters = new Array();
    parameters['name'] =  document.getElementById('name').value;
    parameters['poll_item'] = selectedPollItem;
    parameters['action'] = 'submit';

    // send request to the server
    Ajax.sendRequest('poll', parameters, onPollSubmitRequestComplete);
}

//------------------------------------------------------------------------------

function onSearchClick()
{
    // show "please wait" popup
    UI.showWaitPopup();

    // get the query
    var query = document.getElementById('query').value;

    // check if query is provided
    if (query == 'wpisz szukaną frazę...')
    {
        UI.hideWaitPopup();
        UI.showInformationPopup('Wyszukiwarka', 'Wpisz szukaną frazę.', 'OK', null);
        return;
    }

    // perform the search
    window.location.href = 'search/' + encodeURIComponent(query);
}
