var messageId = 0;
var whoAmI = 1;
var startTimeoutInterval = 1000 * 5; //frequency to check for the chat starting // 5 seconds
var maxWaitForAgent = 1000 * 60 * 10; //interval to wait before displaying the "loadingLongTime message //10 minutes
var waitForAgentInterval = 1000 * 5; //check for an agent every 5 seconds
var inactivityInterval = 1000 * 60 * 10; //prompt for inactivity after 10 minutes
var resetTitleInterval = 1000 * 10;
var lastMessageId = null;
/* setTimeout timers */
var startTimeout;
var waitingTimeout;
var getMessageTimeout;
var waitForAgentTimeout;
var resetTitleTimeout;
var inactivityTimeout;
function resetTitle () {
clearTimeout(resetTitleTimeout);
$(document).attr("title", "Chat");
}
$(document).focus(function() {
resetTitle();
});
$(window).unload(function(e) {
if ( chatSessionInitalized ) {
sendEndChat();
reset();
} else {
return true;
}
});
$(document).ready(function(event) {
$('
Your chat has been inactive for more than 10 minutes.
Do you wish to end your chat session?
').dialog({
autoOpen: false,
width: 340,
height: 240,
modal: true,
buttons: {
"Yes": function() {
sendEndChat();
},
"No": function() {
clearTimeout(inactivityTimeout);
inactivityTimeout = setTimeout(function() {
inactivityPrompt();
}, inactivityInterval);
$(this).dialog("close");
}
},
open: function () {
clearTimeout(inactivityTimeout);
inactivityTimeout = setTimeout(function() {
sendEndChat();
}, 60 * 1000);
$(window).focus();
},
closeOnEscape: false,
draggable: false,
resizable: false,
title: 'Chat Session Inactive' });
$('Please wait a moment and a Customer Service agent will assist you.
').dialog({
autoOpen: false,
width: 300,
height: 180,
modal: true,
buttons: { "Cancel": function() {
reset();
}
},
open: function () {
$(window).focus();
},
closeOnEscape: false,
draggable: false,
resizable: false,
title: 'Starting Chat Session...' });
$('It is taking longer than expected to get an agent to handle your request. Would you like to continue waiting?
').dialog({
autoOpen: false,
width: 300,
height: 180,
modal: true,
buttons: {
"Yes": function() {
waitingTimeout = setTimeout(function() {
timeoutStartChat();
}, maxWaitForAgent);
if ( chatSessionInitalized == true ) {
waitForAgentTimeout = setTimeout(function() {
checkForAgent();
}, waitForAgentInterval);
} else {
startTimeout = setTimeout(function() {
startChat();
}, startTimeoutInterval);
}
$("#loading").dialog('open');
$(this).dialog("close");
},
"No": function() {
reset();
}
},
open: function () {
clearTimeout(waitingTimeout);
clearTimeout(startTimeout);
clearTimeout(waitForAgentTimeout);
$(window).focus();
},
closeOnEscape: false,
draggable: false,
resizable: false,
title: 'Starting Chat Session...' });
$('Are you sure you wish to end your chat session?
').dialog({
autoOpen: false,
width: 320,
height: 240,
modal: true,
buttons: {
"Yes": function() {
sendEndChat();
},
"No": function() {
$(this).dialog("close");
}
},
open: function () {
$(window).focus();
},
closeOnEscape: false,
draggable: false,
resizable: false,
title: 'End Chat Session' });
$('Your chat session has ended.
Would you like a copy of the transcript of this chat emailed to you?
').dialog({
autoOpen: false,
width: 320,
height: 280,
modal: true,
buttons: {
"Yes": function() {
sendTranscript();
$(this).dialog("close");
},
"No": function() {
$(this).dialog("close");
}
},
open: function () {
$(window).focus();
},
close: function() {
$("#chatEnded").dialog("open");
},
closeOnEscape: false,
draggable: false,
resizable: false,
title: 'Chat Session has Ended.' });
$('Thank you for using our Live Chat system.
').dialog({
autoOpen: false,
width: 320,
height: 240,
modal: true,
buttons: {
"Close": function() {
$(this).dialog("close");
}
},
open: function () {
$(window).focus();
},
close: function () {
window.close();
},
closeOnEscape: false,
draggable: false,
resizable: false,
title: 'Chat Session has Ended.' });
$('Your chat session has unexpectedly ended. We apologize for any inconvenience.
').dialog({
autoOpen: false,
width: 320,
height: 240,
modal: true,
buttons: {
"Close": function() {
$(this).dialog("close");
}
},
open: function () {
$(window).focus();
},
close: function () {
window.close();
},
closeOnEscape: false,
draggable: false,
resizable: false,
title: 'Chat Session has Ended.' });
$('').dialog({
autoOpen: false,
width: 320,
height: 240,
modal: true,
buttons: {
"Close": function() {
$(this).dialog("close");
$("#name").focus();
}
},
open: function () {
$(window).focus();
},
closeOnEscape: false,
draggable: false,
resizable: false,
title: 'Error.' });
$('a.ui-dialog-titlebar-close').remove();
$("textarea").keyup(function (e) {
clearTimeout(inactivityTimeout);
inactivityTimeout = setTimeout(function() {
inactivityPrompt();
}, inactivityInterval);
if ( ( 13 == e.keyCode) || (13 == e.which ) ) {
$("#send").focus();
$("#send").click();
}
});
$("#name").keyup(function (e) {
if ( ( 13 == e.keyCode) || (13 == e.which ) ) {
$("#emailAddress").focus();
}
});
$("#emailAddress").keyup(function (e) {
if ( ( 13 == e.keyCode) || (13 == e.which ) ) {
login();
}
});
$(window).focus();
});
function reset() {
clearTimeout(startTimeout);
clearTimeout(waitingTimeout);
clearTimeout(waitForAgentTimeout);
clearTimeout(getMessageTimeout);
clearTimeout(inactivityTimeout);
if ( chatSessionInitalized == true ) {
sendEndChat();
}
loginStarted = false;
messageId = 0;
lastMessageId = null;
$("div.messageWindow").empty();
if ( !arguments[0] ) {
$("#login").css("display","block");
}
$("#chat").hide();
$("div.dialog").dialog( "isOpen" ).dialog("close");
$(window).focus();
}
function login() {
if ( loginStarted == true ) return;
if ( $("#name").val() == "" || $("#emailAddress").val() == "" ) {
$("#customError").html("A valid name and email address are required.").dialog("open");
return;
}
var x = $("#emailAddress").val();
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length){
$("#customError").html("A valid email address is required.").dialog("open");
return;
}
loginStarted = true;
$("#footer").css("display","none");
$("#login").css("display","none");
$('#loading').dialog("open");
startChat();
clearTimeout(startTimeout);
startTimeout = setTimeout(function() {
startChat();
}, startTimeoutInterval);
clearTimeout(waitingTimeout);
waitingTimeout = setTimeout(function() {
timeoutStartChat();
}, maxWaitForAgent);
}
function timeoutStartChat () {
clearTimeout(waitingTimeout);
$('#loadingLongTime').dialog("open");
}
function startChat() {
if ( loginStarted == false ) {
reset();
return;
}
$.post("server.php",
{
method: "startChat",
name: $("#name").val(),
emailAddress: $("#emailAddress").val(),
ckV4: ckV4,
ckV6: ckV6
},
function(data, status) {
if($("status",data).text() == "15") {
reset();
$("#customError").html($("error",data).text()).dialog("open");
return;
}
if($("status",data).text() == "1") {
chatSessionInitalized = true;
clearTimeout(startTimeout);
clearTimeout(waitForAgentTimeout);
waitForAgentTimeout = setTimeout(function() {
checkForAgent();
}, waitForAgentInterval);
} else if ($("status",data).text() == "99" || $("status",data).text() == "98") {
reset();
$("#chatEndedUnexpectedly").dialog("open");
return;
}
},
"xml");
clearTimeout(startTimeout);
startTimeout = setTimeout(function() {
startChat();
}, startTimeoutInterval);
}
function checkForAgent() {
if ( loginStarted == false ) {
reset();
return;
}
clearTimeout(waitForAgentTimeout);
if ( chatSessionInitalized == false ) {
clearTimeout(startTimeout);
startTimeout = setTimeout(function() {
startChat();
}, startTimeoutInterval);
return;
}
$.post("server.php",
{
method: "checkForAgent"
},
function(data, status) {
if($("status",data).text() == "1") {
clearTimeout(startTimeout);
clearTimeout(waitingTimeout);
clearTimeout(waitForAgentTimeout);
var name = $("agent",data).text();
otherName = name;
$("div#who").html(name);
$('div.messageWindow').scrollTop($('div.messageWindow').scrollTop() + 100);
$("div.dialog").dialog("close");
$("#chat").show();
resizeChat();
$(window).focus();
$("textarea").focus();
$(document).attr("title", name + " has entered the chat...");
clearTimeout(resetTitleTimeout);
resetTitleTimeout = setTimeout(function() {
resetTitle();
},resetTitleInterval);
clearTimeout(getMessageTimeout);
getMessage();
} else if ($("status",data).text() == "99" || $("status",data).text() == "98") {
reset();
$("#chatEndedUnexpectedly").dialog("open");
return;
}
},
"xml");
waitForAgentTimeout = setTimeout(function() {
checkForAgent();
}, waitForAgentInterval);
}
function sendTranscript () {
$.post("server.php",
{
method: "sendTranscript"
}
);
}
function sendEndChat() {
clearTimeout(startTimeout);
clearTimeout(waitingTimeout);
clearTimeout(waitForAgentTimeout);
clearTimeout(getMessageTimeout);
clearTimeout(inactivityTimeout);
$("div.dialog").dialog("close");
if ( chatSessionInitalized ) {
$.post("server.php",
{
method: "endChat"
}
);
}
if ( messageId > 0 ) {
$("#chatTranscript").dialog("open");
} else {
$("#chatEnded").dialog("open");
}
chatSessionInitalized = false;
}
function sendMessage() {
if ( loginStarted == false || chatSessionInitalized == false ) {
reset();
return;
}
if ( !checkCharacterCount() ) {
$("#messageTooLong").dialog("open");
return;
}
$(":input").prop("disabled",true);
messageId += 1;
var currentDate = new Date();
var message = $("textarea").val();
$(":input").prop("disabled",false);
$("textarea").val("").focus();
updateCharacterCount();
if ( message == "" || message == "\n" ) return;
/* Get any Pending messages before drawing this one */
clearTimeout(getMessageTimeout);
getMessage();
drawMessageLine(currentDate,message,whoAmI,messageId);
clearTimeout(inactivityTimeout);
inactivityTimeout = setTimeout(function() {
inactivityPrompt();
}, inactivityInterval);
$.post("server.php",
{
messageId: messageId,
method: "send",
message: message
},
function(data, status) {
if ($("status",data).text() == "99" || $("status",data).text() == "98") {
reset();
$("#chatEndedUnexpectedly").dialog("open");
return;
} else if ($("status",data).text() != "1") {
$("div#message-" + $("messageId",data).text()).addClass("failed");
$("div#message-" + $("messageId",data).text() + " div.sending").html("Failed To Send").css("background-image","none");
return;
}
$("div#message-" + $("messageId",data).text() + " div.sending").remove();
},
"xml");
}
function getMessage() {
if ( loginStarted == false || chatSessionInitalized == false ) {
reset();
return;
}
$.post("server.php",
{
method: "get",
typing: typingStatus,
lastMessageId: lastMessageId
},
function(data, status) {
if($("typingStatus",data).text() == "true") {
showTyping();
} else {
hideTyping();
}
if($("status",data).text() != "2") $(window).focus();
if ($("status",data).text() == "99" || $("status",data).text() == "98") {
reset();
$("#chatEndedUnexpectedly").dialog("open");
clearTimeout(getMessageTimeout);
return;
}
if ($("status",data).text() == "10") {
clearTimeout(getMessageTimeout);
if ( messageId > 0 ) {
$("#chatTranscript").dialog("open");
} else {
$("#chatEnded").dialog("open");
}
return;
}
if($("status",data).text() != "1") return;
$('#loading').dialog("close");
if ( $("#chat").css("display") != "block" ) {
$("#chat").show();
$("textarea").focus()
}
$("message",data).each(function(id) {
var message = $("message",data).get(id);
var text = $("text",message).text();
var name = $("name",message).text();
var who = $("who",message).text();
var timestamp = $("timestamp",message).text();
lastMessageId = $("messageId",message).text();
var currentDate = gmtTimeStampToDate(timestamp);
drawMessageLine(currentDate,text,who);
clearTimeout(inactivityTimeout);
inactivityTimeout = setTimeout(function() {
inactivityPrompt();
}, inactivityInterval);
newMesageAlert();
});
},
"xml");
clearTimeout(getMessageTimeout);
getMessageTimeout = setTimeout(function() {
getMessage();
}, getMessageTimeoutInterval);
}
function inactivityPrompt() {
$("div.dialog").dialog("close");
$("#inactivity").dialog("open");
}