// ========== WAS THIS HELPFUL
// This code creates the slideout div that is displayed when the Was This Helpful sidebar button is clicked.

$('body').append('<div class="wthcontainer">' +
'<div class="wth-one" onclick="hideWTH()"><i class="glyphicon glyphicon-question-sign"></i></a></div>' +
    '<div class="wth-two">' +
    '<div id="wth-twotitlebar">' +
        '<div id="wth-twotitle">Was this helpful?</div>' +
        '<div id="wth-close" onclick="hideWTH()">' +
            '<a href="#"><i class="glyphicon glyphicon-remove" style="color:#fff;"></i></a>' +
        '</div>' +
    '</div>' +
    '<div id="wth-twobody">' +
        '<div id="wth-main">' +
            '<div class="btn-group btn-group-toggle" data-toggle="buttons">' +
                '<label class="btn btn-secondary active">' +
                    '<input type="radio" name="wth-options" id="option1" autocomplete="off" value="Yes" checked> Yes' +
                '</label>' +
                '<label class="btn btn-secondary">' +
                    '<input type="radio" name="wth-options" id="option2" autocomplete="off"  value="No"> No' +
                '</label>' +
            '</div>' +
            '<div id="wth-input">' +
                '<p id="wth-intropara">Glad we could help!</p>' +
                '<p>If you\'d care to, please send us a comment!</p>' +
                '<div class="form-group">' +
                    '<label for="comment">Comment:</label>' +
                    '<textarea id="wth-textbox" class="form-control" rows="5" id="comment"></textarea>' +
                '</div>' +
            '</div>' +
            '<button type="button" id="wth-send" class="btn btn-primary" onclick="submitWTH();">Submit</button>' +
        '</div>' +
        '<div id="wth-ty-container">' +
            '<p id="wth-ty-head">Thank you for letting us know!</p>' +
            '<p id="wth-ty-body">We truly value your input!</p>' +
            '<button type="button" id="wth-ty-close" class="btn btn-primary" onclick="closeWTH();">Close</button>' +
        '</div>' +
    '</div>' +
'</div>' +
'</div>');
$('.btn-group').css('margin-bottom', '10px');
$(function() {
    $('input:radio[name="wth-options"]').change(function() {
        if ($(this).val() == 'Yes') {
            $('#wth-intropara').html("Glad we could help!");
        } else {
            $('#wth-intropara').html("Sorry you didn't find what you needed.");
        }
    });
});
// ========== END WAS THIS HELPFUL


function showWTH() {
    $('.wthcontainer').css("display", "block");
    // $('.wth-one').trigger('click');
    $(".wthcontainer").animate({right: '400px'});
    $(".wth-one").css("display", "block");
}

function hideWTH() {
    $(".wthcontainer").animate({right: '-400px'});
    $(".wth-one").css("display", "none");
}

function helpfulYes() {
    alert("Yes");
}

function helpfulNo() {
    alert("No");
}


function submitWTH() {
    var textComment = $('#wth-textbox').val().trim();
    var url = window.location.href;
    var helpful = $('input[name=wth-options]:radio:checked').val();
    var data = { "text": textComment, "helpful": helpful,"url": url }

    console.log(JSON.stringify(data));
    $.ajax({
        type: 'POST',
        contentType: 'application/json',
        url: '/resources/webhelp-includes/WasThisHelpfulHandler.php',
        dataType: 'json',
        data: JSON.stringify(data),
        success: function(result) {
            console.log("Response: " + result);
            // $('#searchForm').submit();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log("Error: " + thrownError);			}
    });

    // return false;

    showWthThankYou();
}

function showWthThankYou() {
    $('#wth-main').hide();
    hideWthMain();
    $('#wth-ty-container').fadeIn();
    $('#wth-textbox').val("");
}

function hideWthMain() {
    $('#wth-main').css('display','none');
}

function closeWTH() {
    $('#wth-ty-container').fadeOut();
    hideWTH();
    setTimeout(function(){
        $('#wth-textbox').css('height', '100px');
        $('#wth-main').css('display', 'block');
        $('#wth-ty-container').css('display', 'none');
        $('input:radio[name=wth-options][value=Yes]').click();
    }, 2000);
}



function myFunction(){
    $('#wthFrame').attr('src', "/resources/feedback/wasthishelpful/helpfulyes.php?wth=true");
}

function showIframeYes() {
    $("#iframe-container-yes").fadeIn(300);
}

function showIframeNo() {
    $("#iframe-container-no").fadeIn(300);
}

function hideIframe() {
    $("#iframe-container-yes").fadeOut(300);
    $("#iframe-container-no").fadeOut(300);
}

function hideHelpfulPodShowThankyou() {
    $("#helpful-pod").fadeOut(300);
    $("#helpful-thankyou").fadeIn(300);
    $("#helpful-thankyou").delay(3000).fadeOut(300);
}

function showThanks() {
    document.getElementById("#helpful-thankyou").fadeIn(300);
    document.getElementById("#helpful-thankyou").delay(3000).fadeOut(300);
}
