// Use the following glyphicons for the toolbar buttons
// Feedback: glyphicon glyphicon-envelope
// Chat: 

// #region Feedback slideout box
$('body').append('<div id="FeedbackBox" class="feedbackBox">' +
'<div class="container">' +
    '<div class="row" id="FeedbackBoxTitleBar" style="display:inline-block">' +
        'Feedback' + '<a href="#" id="closeFB">' + 
            '<i class="bi bi-box-arrow-right closeFBPane" style="position:absolute; right:10px;"></i>' +
        '</a>' +
    '</div>' +
    '<div class="row" id="FeedbackBoxBody">' +
        '<p id="confEnvTitle">Send us your feedback!</p>' + 
		'<p id="FeedbackBodyText">We would love to hear from you about your experience with our documentation.<br/>' +
		'Click the following link to send us an email.</p>' +
		'<p><a target="_black" href="mailto: WKFS-InfoDevSupport <WKFS-InfoDevSupport@wolterskluwer.com>?subject = Documentation Feedback&body = Message">Send Feedback</a></p>' +
		'<p class="FeedbackPostMsg"><b>NOTE:</b> If the link above does not display your email client, please copy and paste the following address to a new email message:<br/>' +
		'WKFS-InfoDevSupport@wolterskluwer.com</p>' +
    '</div>' +
'</div>');
$("#feedback").click(function(){
	$(".feedbackBox").addClass("show");
});
$("#closeFB").click(function(){
	$(".feedbackBox").removeClass("show");
});
// #endregion

// #region Did You Know slideout box
$('body').append('<div id="DykBox" class="dykBox">' +
'<div class="container">' +
    '<div class="row" id="DykBoxTitleBar" style="display:inline-block">' + 
        'Did you know?' + '<a href="#" id="closeDYK">' + 
            '<i class="bi bi-box-arrow-right closeDYKPane" style="position:absolute; right:10px;"></i>' +
        '</a>' +
    '</div>' +
    '<div class="row" id="DykBoxBody">' +
        '<ul class="list-group list-group-flush DYKList"></ul>' + 
    '</div>' +
'</div>');
$("#closeDyk").click(function(){
	$(".dykBox").removeClass("show");
});
$('.closeDYKPane').on('click', function(event){
    $('.dykBox').removeClass('show');
});
// #endregion

// #region Chat slideout box
$('body').append('<div id="ChatBox" class="chatBox">' +
'<div class="container">' +
    '<div class="row" id="ChatBoxTitleBar" style="display:inline-block">' + 
        'Live Chat' + '<a href="#" id="closeCLPane">' + 
            '<i class="bi bi-box-arrow-right closeChat" style="position:absolute; right:10px;"></i>' +
            '</a>' +
    '</div>' +
    '<div class="row" id="ChatBoxBody">' +
        '<p id="confEnvTitle">Have a Production Support question?<br/><span style="font-size:11px">(Available 8am-5pm CT, Mon-Fri)</span></p>' +
        
        '<div>' +
            '<input class="chatTextbox" type="text" name="firstname" value="Name (required)" id="firstname" autocomplete="off">' +
        '</div>' +
        '<div id="provideName"><span class="NameReminder">Please provide your name.</span></div>' +

        '<div>' +
            '<input class="chatTextbox" type="text" name="customernum" value="Customer #  (required)" id="customernum" autocomplete="off" onclick="CustNumKeyPress()">' +
        '</div>' +
        '<div id="provideCustNum"><span class="NumReminder">Please provide your customer number.</div>' +

        '<div id="ContinueButton">' +
            '<button type="button" id="ChatBoxContinue" class="btn btn-primary active"  onclick="submitLiveChat()";>Continue <i class="bi bi-chevron-right"></i></button>' +
        '</div>' + 
    '</div>' + 
'</div>');

$("#chat").click(function(){
	$(".chatBox").addClass("show");
});
$(".closeChat").click(function(){
	$(".chatBox").removeClass("show");
});

var enableDisable = function(){
    var currentTime = new Date().getHours();
    var currentDay = new Date().getDay();
    if (currentDay >= 1 && currentDay <= 5 && currentTime >= 4 && currentTime < 17){
        $('#firstname').attr("disabled", false);
        $('#customernum').attr("disabled", false);
        $('#ChatBoxContinue').attr("disabled", false);
    }
    else
    {
        $('#firstname').attr("disabled", true);
        $('#customernum').attr("disabled", true);
        $('#ChatBoxContinue').attr("disabled", true);
    }
};
enableDisable();

// Show chat button
var chatAssumptions 	   = window.location.href.indexOf("Assumptions") != -1;
var chatDocViewer 		= window.location.href.indexOf("DocViewer") != -1;
var chatExpere 	  = window.location.href.indexOf("Expere") != -1;
var chatMortgage 		= window.location.href.indexOf("Mortgage") != -1;
var chatOPPSA 		= window.location.href.indexOf("OPPSA") != -1;

if ( chatDocViewer || chatExpere || chatAssumptions || chatMortgage || chatOPPSA) {
    $('#chat').css('display', 'block');

    // // Create Chat notice popup div
    // var elm = '<div id="ChatBoxNotice" class="chatBoxNotice">' +
    // '<div class="confirmEnv">' +
    // 	'<div id="ChatBoxNoticeTitleBar">' +
    // 		'<div style="display:inline-block;" onclick="hideLiveChatNotice()">' +
    // 			'<a href="#" id="closeChatNotice" style=" color: black;"><i class="glyphicon glyphicon-remove-circle"></i><span style="font-size:10px">Don\'t show again</span></a>' +
    // 		'</div>' +
    // 		'<div style="display:inline-block; float: right">' +
    // 			'<i class="glyphicon glyphicon-arrow-up"></i>' +
    // 		'</div>' +
    // 	'</div>' +
    // 	'<div id="ChatBoxNoticeBody">' +
    // 		'<p id="ChatBoxNoticeTitle">Hi!     I noticed you\'ve been on the page a while.</p>' +
    // 		'<p id="ChatBoxNoticeBodyMsg">If you ever have questions and would like to speak with one of ' +
    // 		'our representatives, you can click the Chat button above!</p>' +
    // 	'</div>' +
    // '</div>';
    // $(elm).insertAfter("#ChatBox");
    
    $("#chat,#chatNoticeBtn").click(function(){
    	$('.chatBoxNotice').css('display', 'none');
    	$(".chatBox").addClass("show");
    });
    $("#closeChat").click(function(){
    	$(".chatBox").removeClass("show");
    });

    // Comment the next line if you do NOT want the Live Chat notice to appear.
    // AutoShowLiveChatNotice();
}

var SelectedOption = "prod";
$('.btn-group input[type=radio]').change(function() {
    SelectedOption = (this.value);
});

$("#firstname, #customernum").on('mouseup', function() { 
    $(this).select();
});

$("#customernum").keypress(function() {
    var name = $('#firstname').val();
    var custnum = $('#customernum').val();
    if(event.keyCode == 13 && (name != "Name (required)" || name != "") && (custnum != "Customer #  (required)" || custnum != "")) {
        $("#ChatBoxContinue").click();
    }
});

$("#ChatBoxContinue").click(function(){
    var switchval = 0;
    var name = $('#firstname').val();
    var custnum = $('#customernum').val();
    if (name === "Name (required)" || name === "") {
        switchval = switchval + 3;
    }
    if (custnum == "Customer #  (required)" || custnum == "") {
        switchval = switchval + 5;
    }
    // console.log("switchval is: " + switchval);

    switch (switchval) {
        case 0: //no problems
            var baseurl = "https://grcchat.wolterskluwer.com/i3root/grc/target.html";
            var fn = "?chatUsername=" + $('#firstname').val();
            var cn = "&customInfo=" +$('#customernum').val();
            var currentUrl = "&url=" + window.location.href;

            if (SelectedOption == "prod") {
                // Capture url location to decide where to route
                if( chatDocViewer || chatExpere || chatOPPSA) {
                    if (navigator.userAgent.match(/msie/i) || navigator.userAgent.match(/trident/i) ){
                        var targetProduct = "&t=FS_BMI_SL+Expere+IE+Support+Chat";
                    } else {
                        var targetProduct = "&t=FS_BMI_SL%2520Expere%2520IE%2520Support%2520Chat";
                        // var targetProduct = "FS_BMI_SL_zTest_Chat";
                    }
                }
                if ( chatMortgage) {
                    // console.log(chatMortgage);
                    if (navigator.userAgent.match(/msie/i) || navigator.userAgent.match(/trident/i) ){
                        var targetProduct = "&t=FS_BMI_SL+ComplianceOne+Mortgage+Chat";
                    } else {
                        var targetProduct = "&t=FS_BMI_SL%2520ComplianceOne%2520Mortgage%2520Chat";
                        // var targetProduct = "FS_BMI_SL_zTest_Chat";
                    }
                }
                if (chatAssumptions) {
                    if (navigator.userAgent.match(/msie/i) || navigator.userAgent.match(/trident/i) ){
                        var targetProduct = "&t=FS_BMI_SL+ComplianceOne+Assumptions+Chat";
                    } else {
                        var targetProduct = "&t=FS_BMI_SL%2520ComplianceOne%2520Assumptions%2520Chat";
                        // var targetProduct = "FS_BMI_SL_zTest_Chat";
                    }
                }

                //FS_R&C_SFCS for SaaS Wiz Chat

                // if (navigator.userAgent.match(/msie/i) || navigator.userAgent.match(/trident/i) ){
                // 	var targetProduct = "&t=FS_BMI_SL+Expere+IE+Support+Chat";
                // } else {
                // 	var targetProduct = "&t=FS_BMI_SL%2520Expere%2520IE%2520Support%2520Chat";
                // 	// var targetProduct = "FS_BMI_SL_zTest_Chat";
                // }
                newUrl = (baseurl + fn + cn + targetProduct + currentUrl);
                localStorage.setItem("i3ChatUrl", newUrl);
                // console.log("newUrl is: " + newUrl);
                window.open(newUrl);
            }
            if (SelectedOption == "ct") {
                alert('Coming soon!');
            }
            $(".chatBox").removeClass("show");
            $('#provideName').css('display','none');
            $('#provideCustNum').css('display','none');
            $('#ChatBox').css('height','290px');
            break;

        case  3: //name is missing
            // $('#ChatBox').css('height','320px');
            $('.NameReminder').css('display','block');
            $('.NumReminder').css('display','none');
            switchval = null;
            break;

        case 5: //customer num is missing
            // $('#ChatBox').css('height','320px');
            $('.NumReminder').css('display','block');
            $('.NameReminder').css('display','none');
            switchval = null;
            break;
            
        case 8: // name AND cust num are missing
        // console.log('show both');
            // $('#ChatBox').css('height','350px');
            $('.NameReminder').css('display','block');
            $('.NumReminder').css('display','block');
            switchval = null;
            break;
    }		
});

function submitLiveChat() {
	var url = window.location.href;
	var name = $('#firstname').val().trim();
	var custnum = $('#customernum').val().trim();
	var data = { "url": url, "name": name, "custnum": custnum }

	// console.log(JSON.stringify(data));
	$.ajax({
		type: 'POST',
		contentType: 'application/json',
		url: '/resources/webhelp/php/LiveChatHandler.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;
}
// #endregion

// #region Button group
$('.wh_right_tools').prepend('<div class="btn-group">' + 
    '<button type="button" class="btn btn-default btn-sm btn-idev" aria-label="Chat" title="Chat with a live representative" onclick="ShowChatBox()"><i class="bi bi-chat-dots-fill" style="font-size: 2rem; color: #347bb7;"></i></span></button>' + 
    '<button type="button" class="btn btn-default btn-sm btn-idev" aria-label="Feedback" title="Give your feedback" onclick="ShowFeedbackBox()"><i class="bi bi-envelope-fill" style="font-size: 2rem; color: #347bb7;"></i></button>' +
    '<button type="button" class="btn btn-default btn-sm btn-idev" aria-label="Did You Know" title="Did you know" onclick="ShowDykBox()"><i class="bi bi-mortarboard-fill" style="font-size: 2rem; color: #347bb7;"></i></button>' +
    '</div>');
// #endregion

// #region Add DYK list items
$('.DYKList').append('<a href="/InfoDev/Using%20InfoDev%20WebHelp/index.html" target="_blank" class="list-group-item list-group-item-action">You can learn more about WebHelp features <i class="bi bi-box-arrow-up-right"></i></a>');


if (IsChangelog) {
    $('.DYKList').append('<a href="/InfoDev/Using%20InfoDev%20WebHelp/topics/FilteringChangelogs.html#FilteringChangelogs__section_FilterDynamically" target="_blank" class="list-group-item list-group-item-action">You can filter content dynamically <i class="bi bi-box-arrow-up-right"></i></a>');
    $('.DYKList').append('<a href="/InfoDev/Using%20InfoDev%20WebHelp/topics/FilteringChangelogs.html#FilteringChangelogs__section_FilterDynamically" target="_blank"class="list-group-item list-group-item-action">You can filter changelogs by sprint# or PBI# <i class="bi bi-box-arrow-up-right"></i></a>');
    $('.DYKList').append('<a href="/InfoDev/Using%20InfoDev%20WebHelp/topics/FilteringChangelogs.html#FilteringChangelogs__section_FilterByType" target="_blank" class="list-group-item list-group-item-action">You can filter changelogs by release note type <i class="bi bi-box-arrow-up-right"></i></a>');
    $('.DYKList').append('<a href="/InfoDev/Using%20InfoDev%20WebHelp/topics/FilteringChangelogs.html#FilteringChangelogs__section_FoldingContent" target="_blank" class="list-group-item list-group-item-action">You can fold all changelogs dynamically <i class="bi bi-box-arrow-up-right"></i></a>');
}
else{
    $('.DYKList').append('<a href="/InfoDev/Using%20InfoDev%20WebHelp/topics/AccessingRelatedDocumentation.html" target="_blank" class="list-group-item list-group-item-action">You always have quick access to the changelog <i class="bi bi-box-arrow-up-right"></i></a>');

}
// #endregion

// #region Slider box placement
var ToolsPosition = $('.wh_tools').position();
$('#DykBox').css('top', ToolsPosition.top + $('.wh_tools').height() + 10);
$('#FeedbackBox').css('top', ToolsPosition.top + $('.wh_tools').height() + 10);
$('#ChatBox').css('top', ToolsPosition.top + $('.wh_tools').height());
// #endregion

// #region Functions
function ShowChatBox(){
    $('.dykBox').removeClass('show');
    $(".feedbackBox").removeClass("show");
    if ($('.chatBox').hasClass('show')){
        $(".chatBox").removeClass("show");
    }
    else {
        $(".chatBox").addClass("show");
    }
}

function ShowFeedbackBox(){
    $('.dykBox').removeClass('show');
    $(".chatBox").removeClass("show");
    if ($('.feedbackBox').hasClass('show')){
        $(".feedbackBox").removeClass("show");
    }
    else {
        $(".feedbackBox").addClass("show");
    }
}

function ShowDykBox(){
    $(".chatBox").removeClass("show");
    $(".feedbackBox").removeClass("show");
    if ($('.dykBox').hasClass('show')){
        $('.dykBox').removeClass('show');
    }
    else {
        $('.dykBox').addClass('show');
    }
}
// #endregion