function loadApplication()
{
    goTo('start.php', button_start)
}

button_start = document.getElementById('button_start');
button_poradnie = document.getElementById('button_poradnie');
button_oddzialy = document.getElementById('button_oddzialy');
button_pytanie = document.getElementById('button_pytanie');
button_punkt = document.getElementById('button_punkt');

function display($content, $divname)
{
    document.getElementById($divname).innerHTML = $content;
}

function wyświetl(co, gdzie, jak)
{
    if(jak == undefined) jak = 0;
    if(jak == 0) document.getElementById(gdzie).innerHTML = co;
    else document.getElementsByTagName(gdzie)[0].innerHTML = co;
}

function goTo(filename, topElement, vars)
{
    checkExit();
    if(vars == undefined) vars = '';
    if(exit == 1)
    {
        display('<p align="center"><br /><font size="2">Wczytywanie...</font><br /><img src="img/loadbar2.gif"/></p><br />', 'loadMessage');
        ajaxSend('post', filename, vars, goToAjax);
    }
    if(topElement != undefined)
    {
        document.getElementById('current').id = '';
        document.getElementById(topElement).id = 'current';
    }
}

function goToAjax(ajaxRequest)
{
    var response = ajaxRequest.responseText;
    display('', 'loadMessage');
    display(response, 'livecontent');
}

function loadStylesheet(path, id)
{
    document.getElementById(id).href = path;
}

function checkExit()
{
    if(document.getElementById('nidTemporary'))
    {
        if(confirm("Czy jesteś pewien, że chcesz opuścić tę stronę?"))
        {
            exit = 1;
        }
        else
        {
            exit = 0;
        }
    }
    else
    {
        exit = 1;
    }
}

function updateSection(sectionName, nid)
{
    if(nid == undefined) nid = document.getElementById('nidTemporary').value;
    if(sectionName == 'text1' || sectionName == 'text2') sectionValue = document.getElementById(sectionName).value;
    else sectionValue = document.getElementById(sectionName).value;
    ajaxSend('post', 'updateSection.php', sectionName+'='+sectionValue+'&nid='+nid, updateSectionAjax);
}

function updateSectionAjax(ajaxRequest)
{
//  alert(ajaxRequest.responseText);
}

function savePublish()
{
    window.scrollTo(0,0);
    display('<p align="center"><br /><font size="2">Wczytywanie...</font><br /><img src="img/loadbar2.gif"/></p><br />', 'loadMessage');
    ajaxSend('post', 'savePublish.php', 'nid='+document.getElementById('nidTemporary').value, savePublishAjax);
}

function savePublishAjax(ajaxRequest)
{
    var response = ajaxRequest.responseText;
    display('', 'loadMessage');
    display(response, 'livecontent');
}

function logMe()
{
    document.getElementById('login_panel').style.display = 'block';
}

function logMeIn()
{
    login = document.getElementById('loginForm');
    password = document.getElementById('passwordForm');
    document.getElementById('login_panel_loading').style.display = 'block';
    ajaxSend('post', 'logMeIn.php', 'login='+login.value+'&password='+password.value, logMeInAjax);
}

function logMeInAjax(ajaxRequest)
{
    var response = ajaxRequest.responseText;
    if(response == 'security_alert')
    {
        document.getElementById('login_panel_loading').style.display = 'none';
        window.alert('Podałeś nieprawidłowy login lub hasło.');
    }
    else if(response == 'server_error')
    {
        document.getElementById('login_panel_loading').style.display = 'none';
        window.alert('Wystąpił błąd serwera.');
    }
    else
    {
        document.getElementById('login_button').innerHTML = 'Zalogowano - <a href="javascript:void(0)" onclick="logMeOut()" id="logMeOut">Wyloguj się</a>';
        document.getElementById('login_panel').style.display = 'none';
        document.getElementById('login_panel_loading').style.display = 'none';
        loadStylesheet('css/editor.css', 'editorCSS');
        loadStylesheet('', 'userCSS');
        if(response != '') addHistory(response, '');
    }
}

function logMeOut(ajaxRequest)
{
    if(ajaxRequest == undefined)
    {
        ajaxSend('post', 'logMeOut.php', '', logMeOut);
    }
    else
    {
        var response = ajaxRequest.responseText;
        if(response == 'server_error') window.alert('Wystąpił błąd serwera.');
        else
        {
            document.getElementById('login_button').innerHTML = '<a href="javascript:void(0)" onclick="logMe()" id="logMe">Zaloguj się</a>';
            goTo('start.php');
            loadStylesheet('', 'editorCSS');
            loadStylesheet('css/user.css', 'userCSS');
            window.alert('Wylogowano z serwisu.');
        }
    }
}

function askQuestion(form)
{
    display('<p align="center"><br /><font size="2">Wczytywanie...</font><br /><img src="img/loadbar2.gif"/></p><br />', 'loadMessage');
    ajaxSend('get', 'zadajPytanie.ajax.php', 'mailtoperson='+form.mailtoperson.value+'&name='+form.name.value+'&sendermail='+form.sendermail.value+'&message='+form.message.value, askQuestionAjax);
}

function askQuestionAjax(ajaxRequest)
{
    var response = ajaxRequest.responseText;
    display('', 'loadMessage');
    display(response, 'response');
}

function saveAttachments()
{
    if(window.addEventListener) document.getElementById('hid_form').addEventListener('load',savePublish,false);
    else document.getElementById('hid_form').attachEvent('onload',savePublish);
}

function deleteNews(nid)
{
    if(nid != undefined) {
        if(confirm('Czy na pewno chcesz usunąć?'))
        {
            ajaxSend('post', 'deleteNews.php', 'nid='+nid, deleteNewsAjax);
        }
    }
}

function deleteNewsAjax(ajaxRequest)
{
    goTo('news.php', this, 'cid=2');
}

function wczytajNowiny(strona, id)
{
    display('<p align="center"><br /><font size="2">Wczytywanie...</font><br /><img src="img/loadbar2.gif"/></p><br />', 'loadMessage');
    if(strona == undefined)
    {
        if(id == undefined)
        {
            strona = 0;
            strona_nowin = strona;
        }
    }
    if(id == undefined) ajaxSend('post', '../nowiny/wczytajNowiny.php', 'archiwum=1&strona='+strona, wczytajNowinyAjax);
    else ajaxSend('post', 'wczytajNowiny.php', 'archiwum=1&id='+id, wczytajNowinyAjax);
}

var strona_nowin;

function wczytajNowinyAjax(ajaxRequest)
{
    var response = ajaxRequest.responseText;
    response = response.split('!@#$%^&*');
    if(response[2] != undefined) strona_nowin = response[2];
    stronaplus = strona_nowin*1+1;
    stronaminus = strona_nowin*1-1;
    if(response[1] == 0) wyświetl('&lt;&lt; Nowsze | Starsze &gt;&gt;', 'nawigacja');
    else if(response[1] == 1) wyświetl('&lt;&lt; Nowsze | <a href="#nowina/'+obecne_id+'/'+stronaplus+'" onclick="addHistory(\'nowina/'+obecne_id+'/'+stronaplus+'\'">Starsze &gt;&gt;</a>', 'nawigacja');
    else if(response[1] == 2) wyświetl('<a href="#nowina/'+obecne_id+'/'+stronaminus+'" onclick="addHistory(\'nowina/'+obecne_id+'/'+stronaminus+'\'">&lt;&lt; Nowsze</a> | <a href="#nowina/'+obecne_id+'/'+stronaplus+'" onclick="addHistory(\'nowina/'+obecne_id+'/'+stronaplus+'\'">Starsze &gt;&gt;</a>', 'nawigacja');
    else if(response[1] == 3) wyświetl('<a href="#nowina/'+obecne_id+'/'+stronaminus+'" onclick="addHistory(\'nowina/'+obecne_id+'/'+stronaminus+'\'">&lt;&lt; Nowsze</a> | Starsze &gt;&gt;', 'nawigacja');
    display('', 'loadMessage');
    document.getElementById('ul-lista').innerHTML = response[0];
}