/*
 *  Naam      : R. Wacanno  Skip Harks  Chris Bras  Bodi Boelé
 *  UvAnetID  : 11741163    12002038    11917318    11904089
 *  Studie    : BSc Informatica
 *
 *  changeReply.js
 *  - This file makes it possible for an user to edit his own replies.
 */
 /* get the text from the database */
function pullText2()
{
    var replyId = <?php echo json_encode($reactieId); ?>;

    $.get("replyContent.php",
    {
        id: replyId
    },
    function(data)
    {
        $('#replyMessage').html(data);
    }).fail(function()
    {
        alert( "Er ging iets mis bij het laden." );
    });
}

/* get the text from the database without parsedown */
function pullTextP2()
{
    var replyId = <?php echo json_encode($reactieId); ?>;

    $.post("replyContentP.php",
    {
        reactie_id: replyId
    },
    function(data)
    {
        $('#textfield2').val(data);
    }).fail(function()
    {
        alert( "Er ging iets mis bij het laden." );
    });
}

/* push the text into the database */
function pushText2()
{
    var text = $('#textfield2').val();
    var replyId = <?php echo json_encode($reactieId); ?>;

    $.post("changeReply.php",
    {
        reactie_id: replyId,
        reactie_text: text
    },
    function(data)
    {
        showText2();
    }).fail(function()
    {
        alert( "Er ging iets mis bij het opslaan." );
    });
}

/* show the text */
function showText2()
{
    pullText2();
    pullTextP2();
    $('#textWrapper2').css('display', 'none');
    $('#saveButton2').css('display', 'none');
    $('#textButton2').html('Veranderen');
    $('#textButton2').attr('onclick', 'showField()');
    $('#textButton2').attr('class', 'formButton');
}

/* show the textfield */
function showField2()
{
    pullText2();
    pullTextP2();
    $('#textWrapper2').css('display', 'inline');
    $('#saveButton2').css('display', 'inline');
    $('#textfield2').focus();
    $('#textButton2').html('Annuleren');
    $('#textButton2').attr('onclick', 'showText()');
    $('#textButton2').attr('class', 'formButtonRed');
}

/* save the changes */
function saveText2()
{
    pushText2();
}

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

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