// ========== LIVE CHAT
// This code creates the Live Chat slideout box that appears after the user clicks the 
// Chat sidebar button.

$('body').append('<div id="ChatBox" class="chatBox">' +
'<div class="confirmEnv">' +
    '<div id="ChatBoxTitleBar">' +
        '<div style="display:inline-block"><a href="#" id="closeChat"><i class="closeLCPane"></i></a></div>' +
    '</div>' +
    '<div 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>' +
    '<div>' +
        '<input class="chatTextbox" type="text" name="firstname" value="Name (required)" id="firstname" autocomplete="off">' +
    '</div>' +
    '<div id="provideName"><span class="credReminder">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="credReminder">Please provide your customer number.</span></div>' +

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

var enableDisable = function(){
    var currentTime = new Date().getHours();
    var currentDay = new Date().getDay();
    if (currentDay >= 1 && currentDay <= 5 && currentTime >= 8 && 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();

    // if(window.location.href.indexOf("Expere") === -1) {
// 	$('#chat').css('display', 'none');
// }

// Show chat slidecontainer if Expere
// if(window.location.href.indexOf("Expere") > -1) {
// 	$('.slidecontainer').css('display', 'block');
// }
    
// 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');
            $('#provideName').css('display','block');
            $('#provideCustNum').css('display','none');
            switchval = null;
            break;

        case 5: //customer num is missing
            $('#ChatBox').css('height','320px');
            $('#provideCustNum').css('display','block');
            $('#provideName').css('display','none');
            switchval = null;
            break;
            
        case 8: // name AND cust num are missing
            $('#ChatBox').css('height','350px');
            $('#provideName').css('display','block');
            $('#provideCustNum').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-includes/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;
}
