function pullWelcomeText()
{
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        var xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            $('#welcomeText').html(this.responseText);
        }
    };
    xmlhttp.open("POST", "welcometext.php");
    xmlhttp.send();
}

function pullWelcomeTextP()
{
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        var xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            $('#welcomeTextfield').val(this.responseText);
        }
    };
    xmlhttp.open("POST", "welcometextP.php");
    xmlhttp.send();
}

function pushWelcomeText()
{
    var text = $('#welcomeTextfield').val();
    var dataString = "text=" + text;

    $.ajax({
        type: "POST",
        url: "changewelcometext.php",
        data: dataString,
        cache: false
    })
    .done(function(data){
        showWelcomeText()
    })
    .fail(function() {
        alert( "Er ging iets mis bij het opslaan." );
    });
}

function showWelcomeText()
{
    pullWelcomeText();
    pullWelcomeTextP();
    $('#welcomeText').css('display', 'inline');
    $('#welcomeTextWrapper').css('display', 'none');
    $('#welcomeSaveButton').css('display', 'none');
    $('#welcomeTextButton').html('Veranderen');
    $('#welcomeTextButton').attr('onclick', 'showWelcomeField()');
    $('#welcomeTextButton').attr('class', 'formButton');
}

function showWelcomeField()
{
    pullWelcomeText();
    pullWelcomeTextP();
    $('#welcomeText').css('display', 'none');
    $('#welcomeTextWrapper').css('display', 'inline');
    $('#welcomeSaveButton').css('display', 'inline');
    $('#welcomeTextfield').focus();
    $('#welcomeTextButton').html('Annuleren');
    $('#welcomeTextButton').attr('onclick', 'showWelcomeText()');
    $('#welcomeTextButton').attr('class', 'formButtonRed');
}

function saveWelcomeText()
{
    pushWelcomeText();
}

$('body').ready(showWelcomeText());

/* Adapt textarea hight based on its contents */
$('#welcomeTextWrapper').on( 'focus change keyup keydown paste cut', 'textarea', function (){
    $(this).height(0).height(this.scrollHeight);
}).find( 'textarea' ).change();
