Built motion from commit 5fc88b3.|0.0.60
[motion.git] / server / components / xchatty / xchatty.js
1 /**
2  *
3  * xChatty: xCally Shuttle WebChat Client
4  * Powered by Xenialab
5  *
6  * @author          Xenialab
7  * @link            http://www.xcally.com
8  * @since           xChatty v 2.0
9  *
10  */
11 var xChatty = {
12     config: {
13         websiteId: {{websiteId}},
14         signedUp: {{signedUp}},
15         windowFocused: {{windowFocused}},
16         isNewPage: {{isNewPage}},
17         processingSignUp: {{processingSignUp}},
18         processingEnquiry: {{processingEnquiry}},
19         processingSend: {{processingSend}},
20         visitor: {
21             fullname: '',
22             avatar: ''
23         },
24         timerReadMessages: {{timerReadMessages}},
25         timerCheckStatus: {{timerCheckStatus}},
26         timerSendMessages: '',
27         timerSignupResponse: '',
28         isOnline: {{isOnline}},
29         decayHistory: {{decayHistory}},
30         defaultTimeout: {{defaultTimeout}},
31         isInitialStatusCheck: {{isInitialStatusCheck}},
32         muteNotificationSoundOnce: {{muteNotificationSoundOnce}},
33         assetBase: '{{& assetBase}}',
34         rootPath: '{{& rootPath}}',
35         hasSessionSaf: {{hasSessionSaf}},
36         restoreOpenState: {{restoreOpenState}},
37         animateHover: {{animateHover}},
38         hideEmail: {{hideEmail}},
39         hideOffline: {{hideOffline}},
40         headerShape: '{{headerShape}}',
41         downloadTranscript: {{downloadTranscript}}
42     },
43     Lang: {
44         "HeaderCurrentlyChatting": "Live Chat",
45         "HeaderOnline": '{{HeaderOnline}}',
46         "HeaderOffline": "{{HeaderOffline}}",
47         "OperatorOfflineMessage": "It appears as if all operators are currently offline.",
48         "OfflineMessage": "{{& OfflineMessage}}",
49         "OnlineMessage": '{{& OnlineMessage}}',
50         "UsernamePlaceholder": '{{UsernamePlaceholder}}',
51         "EmailPlaceholder": '{{EmailPlaceholder}}',
52         "EnquiryMessagePlaceholder": "{{EnquiryMessagePlaceholder}}",
53         "EnquiryButtonText": "{{EnquiryButtonText}}",
54         "EnquirySubmitSuccess": "Your message was sucessfully submitted - we will contact you as soon as possible.",
55         "StartChatButtonText": '{{StartChatButtonText}}',
56         "OperatorComposing": "{fullname} is typing...",
57         "FirstMessageText": "",
58         "ExitChatButtonText": "Exit Chat",
59         "ExitChatQuestionText": "Are you sure?",
60         "ExitChatButtonConfirmText": "Yes",
61         "ExitChatButtonCancelText": "No",
62         "MessagePlaceholderText": "Your message...",
63         "MessageSendButtonText": "Send",
64         "ValidationEmailRequired": "Please enter your email address",
65         "ValidationEmailInvalid": "Please enter a valid email address",
66         "ValidationEmailMaxLength": "The email address may not exceed 85 characters",
67         "ValidationUsernameRequired": "Please enter your name",
68         "ValidationUsernameMaxLength": "The name may not exceed 45 characters",
69         "ValidationEnquiryRequired": "Please enter your enquiry",
70         "ValidationEnquiryMaxLength": "The enquiry may not exceed 1500 characters",
71         "ValidationMessageRequired": "Please enter your message",
72         "ValidationMessageMaxLength": "The message may not exceed 750 characters",
73         "DownloadTranscript": "Download Transcript"
74     },
75     init: function() {
76         jQuery(".xc_chat_container").css('margin-bottom', '-50px');
77         if (!xChatty.config.hideOffline)
78             jQuery(".xc_chat_container").show();
79         jQuery(".xc_chat_container").animate({
80             marginBottom: 0
81         }, {
82             duration: 500,
83             queue: true,
84             complete: function() {
85                 if (xChatty.config.restoreOpenState) {
86                     xChatty.config.isNewPage = true;
87                     jQuery(".xc_chat_head").trigger("click");
88                 }
89             }
90         });
91         xChatty.initAnimateHover();
92         jQuery(window).focus(function() {
93             xChatty.config.windowFocused = true;
94         });
95         jQuery(window).blur(function() {
96             xChatty.config.windowFocused = false;
97         });
98         jQuery(document).on("click", ".xc_chat_head", xChatty.headerClick);
99         // jQuery(document).on("click", ".xc_btn_enquiry", xChatty.enquiryClick);
100         jQuery(document).on("submit", ".xc_form_enquiry", xChatty.enquirySubmit);
101         // jQuery(document).on("click", ".xc_btn_signup", xChatty.signUpClick);
102         jQuery(document).on("submit", ".xc_form_signup", xChatty.signUpSubmit);
103         jQuery(document).on("click", ".xc_btn_send", xChatty.sendClick);
104         jQuery(document).on("submit", ".xc_form_reply", xChatty.onSendSubmit);
105         jQuery(document).on("click", ".xc_btn_exit_chat", xChatty.btnExitChatClick);
106         jQuery(document).on("click", ".xc_btn_exit_chat_confirm", xChatty.btnExitChatConfirmClick);
107         jQuery(document).on("click", ".xc_btn_exit_chat_cancel", xChatty.btnExitChatCancelClick);
108         jQuery(document).on("click", ".xc_btn_notifications_close", xChatty.btnNotificationsCloseClick);
109         jQuery(document).on("keyup", ".xc_chat_container textarea, .xc_chat_container input", xChatty.messageKeyUp);
110         jQuery(document).on("keydown", ".xc_input_message", xChatty.messageKeyDown);
111         jQuery('[placeholder]').each(function() {
112             jQuery(this).css('color', '#ccc').val(jQuery(this).attr('placeholder'));
113         }).bind('focus', function() {
114             jQuery(this).css('color', 'inherit');
115             if (jQuery(this).val() === jQuery(this).attr('placeholder')) {
116                 jQuery(this).val('');
117             }
118         }).bind('blur', function() {
119             if (jQuery(this).val() === '') {
120                 jQuery(this).val(jQuery(this).attr('placeholder')).css('color', '#ccc');
121             }
122         });
123         xChatty.readMessages();
124     },
125     initAnimateHover: function() {
126         if (xChatty.config.animateHover !== true)
127             return;
128         jQuery(".xc_chat_container").animate({
129             opacity: 0.7
130         }, {
131             duration: 0,
132             queue: false
133         });
134         jQuery(".xc_chat_container").hover(function() {
135             jQuery(this).animate({
136                 opacity: 1
137             }, {
138                 duration: 200,
139                 queue: false
140             });
141         }, function() {
142             if (!jQuery(this).hasClass("xc_box_open"))
143                 jQuery(this).animate({
144                     opacity: 0.7
145                 }, {
146                     duration: 200,
147                     queue: false
148                 });
149         });
150         jQuery(".xc_chat_container").bind("chatOpen", function(e, isOpen) {
151             if (isOpen) {
152                 jQuery(this).animate({
153                     opacity: 1
154                 }, {
155                     duration: 200,
156                     queue: false
157                 });
158             } else {
159                 jQuery(this).animate({
160                     opacity: 0.7
161                 }, {
162                     duration: 200,
163                     queue: false
164                 });
165             }
166         });
167     },
168     // enquiryClick: function(e) {
169     //     e.preventDefault();
170     //     jQuery(".xc_form_enquiry").submit();
171     // },
172     btnExitChatClick: function(e) {
173         e.preventDefault();
174         jQuery(".xc_exit_chat_confirmation_wrapper").show();
175         jQuery(this).hide();
176     },
177     btnExitChatConfirmClick: function(e) {
178         e.preventDefault();
179         clearTimeout(xChatty.config.timerReadMessages);
180         clearTimeout(xChatty.config.timerCheckStatus);
181         var formData = jQuery(this).serializeArray();
182         formData.push({
183             name: 'data[sign_out]',
184             value: true
185         });
186         jQuery.ajax({
187             url: xChatty.config.rootPath + "signout",
188             data: formData,
189             dataType: "jsonp",
190             jsonpCallback: xChatty.getJsonpCallbackName(),
191             success: function(result) {
192                 if (result.success === true) {
193                     xChatty.resetDefaultState();
194                 } else {}
195             },
196             complete: function() {
197                 xChatty.checkStatus(true);
198             }
199         });
200     },
201     btnExitChatCancelClick: function(e) {
202         e.preventDefault();
203         jQuery(".xc_exit_chat_confirmation_wrapper").hide();
204         jQuery(".xc_btn_exit_chat").show();
205     },
206     removeOldMessages: function () {
207       jQuery(".xc_conversation_container").empty();
208       jQuery(".xc_conversation_container").html('<p class="xc_message_intro" data-id="-10">' + xChatty.Lang.FirstMessageText + '</p>');
209     },
210     resetDefaultState: function() {
211         xChatty.config.signedUp = false;
212         xChatty.config.visitor = {
213             fullname: "",
214             avatar: ""
215         };
216         jQuery(".xc_conversation_container").empty();
217         jQuery(".xc_conversation_container").html('<p class="xc_message_intro" data-id="-10">' + xChatty.Lang.FirstMessageText + '</p>');
218         jQuery(".xc_exit_chat_confirmation_wrapper").hide();
219         jQuery(".xc_btn_exit_chat").show();
220         jQuery(".xc_chat_head").trigger("click");
221     },
222     enquirySubmit: function(e) {
223         e.preventDefault();
224         if (xChatty.config.processingEnquiry)
225             return false;
226         var validationErrors = {};
227         var signupForm = jQuery(".xc_form_enquiry");
228         jQuery('.xc_form_enquiry :input').each(function () {
229           var el = jQuery(this);
230           if (el.val() === el.attr('placeholder')) {
231             el.val('');
232           }
233           if ((jQuery.trim(el.val()) === '' || el.val() === el.attr('placeholder')) && el.attr('required') === 'required' ) {
234             validationErrors[el.attr('id')] = ['Please enter the field ' + el.attr('data-label')];
235           }
236         });
237         // if (jQuery.trim(jQuery(".xc_input_enquiry_fullname").val()) === '' || jQuery(".xc_input_enquiry_fullname").val() === jQuery(".xc_input_enquiry_fullname").attr("placeholder")) {
238         //     validationErrors["fullname"] = [xChatty.Lang.ValidationUsernameRequired];
239         // }
240         // if (jQuery.trim(jQuery(".xc_input_enquiry_email").val()) === '' || jQuery(".xc_input_enquiry_email").val() === jQuery(".xc_input_enquiry_email").attr("placeholder")) {
241         //     validationErrors["email"] = [xChatty.Lang.ValidationEmailRequired];
242         // }
243         // if (jQuery.trim(jQuery(".xc_input_enquiry_message").val()) === '' || jQuery(".xc_input_enquiry_message").val() === jQuery(".xc_input_enquiry_message").attr("placeholder")) {
244         //     validationErrors["message"] = [xChatty.Lang.ValidationEnquiryRequired];
245         // }
246         // if (validationErrors.hasOwnProperty("fullname") || validationErrors.hasOwnProperty("email") || validationErrors.hasOwnProperty("message")) {
247         //     xChatty.renderNotifications(validationErrors, true);
248         //     return;
249         // }
250         if (Object.keys(validationErrors).length > 0) {
251           xChatty.renderNotifications(validationErrors,true);
252           return;
253         }
254         xChatty.config.processingEnquiry = true;
255         var enquiryForm = jQuery(this);
256         enquiryForm.addClass("xc_enquiry_sending");
257         var formData = jQuery(this).serializeArray();
258         formData.push({
259             name: 'data[Enquiry][websiteId]',
260             value: xChatty.config.websiteId
261         });
262         formData.push({
263             name: 'data[Enquiry][current_page]',
264             value: encodeURIComponent(encodeURIComponent(document.URL))
265         });
266         xChatty.handleAjaxSubmitCall(formData, enquiryForm);
267         return false;
268     },
269     handleAjaxSubmitCall: function(formData, enquiryForm) {
270       jQuery.ajax({
271           url: xChatty.config.rootPath + "submit",
272           data: formData,
273           dataType: "jsonp",
274           jsonpCallback: xChatty.getJsonpCallbackName(),
275           success: function(result) {
276               if (result.success === true) {
277                   jQuery(".xc_notifications_wrapper").slideUp();
278                   xChatty.clearNotifications();
279                   jQuery(".xc_form_enquiry")[0].reset();
280                   var notifications = {
281                       success: [xChatty.Lang.EnquirySubmitSuccess]
282                   };
283                   xChatty.renderNotifications(notifications, false);
284               } else {
285                   xChatty.renderNotifications(result.errors, true);
286               }
287               xChatty.config.processingEnquiry = false;
288               enquiryForm.removeClass("xc_enquiry_sending");
289               jQuery(".xc_chat_container input, .xc_chat_container textarea").trigger("blur");
290           },
291           error: function(err) {
292             var error = {
293               'message': 'Service temporarily unavailable. We back as soon as possible!'
294             };
295             xChatty.renderErrors(error, true);
296             xChatty.config.timerSendMessages = setTimeout(function() {
297               xChatty.handleAjaxSubmitCall(formData, enquiryForm);
298             }, 5000);
299           }
300       });
301     },
302     onSendSubmit: function() {
303         var message = jQuery.trim(jQuery(".xc_input_message").val());
304         message = jQuery('<p>' + message + '</p>').text();
305         jQuery(".xc_input_message").val(message);
306         if (message === "" || jQuery(".xc_input_message").val() === jQuery(".xc_input_message").attr("placeholder"))
307             return false;
308         if (xChatty.config.processingSend)
309             return false;
310         var messageForm = jQuery(this);
311         messageForm.addClass("xc_message_sending");
312         xChatty.config.processingSend = true;
313         var formData = jQuery(".xc_form_reply").serializeArray();
314         formData.push({
315             name: 'data[Message][current_page]',
316             value: encodeURIComponent(encodeURIComponent(document.URL))
317         });
318         formData.push({
319             name: 'data[last_id]',
320             value: xChatty.getMaxId()
321         });
322         formData.push({
323             name: 'data[is_new_page]',
324             value: xChatty.config.isNewPage
325         });
326         xChatty.config.isNewPage = false;
327         jQuery(".xc_input_message").val("");
328
329         var tmpMessage = {};
330         tmpMessage.id = xChatty.getMaxId() + 999;
331         tmpMessage.fullname = xChatty.config.visitor.fullname;
332         tmpMessage.body = message;
333         tmpMessage.createdAt = new Date();
334         xChatty.addMessageToChat(tmpMessage, {
335             confirmed: false
336         });
337         xChatty.scrollBottom(false);
338         xChatty.handleAjaxSendCall(formData, tmpMessage, messageForm);
339         return false;
340     },
341     handleAjaxSendCall: function(formData, tmpMessage, messageForm) {
342       jQuery.ajax({
343           url: xChatty.config.rootPath + "send",
344           data: formData,
345           dataType: "jsonp",
346           jsonpCallback: xChatty.getJsonpCallbackName(),
347           success: function(result) {
348               if (result.success === true) {
349                   jQuery(".xc_notifications_wrapper").slideUp();
350                   jQuery("[data-id=" + tmpMessage.id + "]").remove();
351                   // xChatty.addMessageToChat(result.data);
352                   xChatty.addMessageToChat(result.messages[0]);
353                   for (var k = 0; k < result.messages.length; k++) {
354                       xChatty.addMessageToChat(result.messages[k]);
355                   }
356                   if (result.messages.length > 0)
357                       xChatty.scrollBottom(true);
358                   xChatty.config.processingSend = false;
359                   messageForm.removeClass("xc_message_sending");
360               } else {
361                   jQuery("[data-id=" + tmpMessage.id + "]").css('border', '1px dashed #f00');
362               }
363           },
364           error: function(err) {
365             var error = {
366               'message': 'Service temporarily unavailable. We back as soon as possible!'
367             };
368             xChatty.renderErrors(error, true);
369             xChatty.config.timerSendMessages = setTimeout(function() {
370               xChatty.handleAjaxSendCall(formData, tmpMessage, messageForm);
371             }, 5000);
372           }
373       });
374     },
375     headerClick: function(e) {
376         e.preventDefault();
377         // if (!xChatty.config.signedUp && (e.originalEvent !== undefined) && !jQuery(".xc_chat_toggle_container").is(":visible") && xChatty.config.hasSession !== true) {
378         //     jQuery('<form method="post" action="' + xChatty.config.rootPath + 'discussions/session_redir"><input type="hidden" name="data[referer]" value="' + window.location + '" /></form>').submit();
379         // }
380         jQuery(".xc_notification_badge_number").fadeOut();
381         jQuery(".xc_notification_badge_number").html('');
382         jQuery(".xc_chat_container").removeClass('xc_has_unread_messages');
383         if (!xChatty.config.signedUp) {
384             if (xChatty.config.isOnline) {
385                 jQuery(".xc_signup_wrapper").show();
386                 jQuery(".xc_conversation").hide();
387                 jQuery(".xc_enquiry_wrapper").hide();
388             } else {
389                 jQuery(".xc_enquiry_wrapper").show();
390                 jQuery(".xc_signup_wrapper").hide();
391                 jQuery(".xc_conversation").hide();
392             }
393         } else {
394             jQuery(".xc_enquiry_wrapper").hide();
395             jQuery(".xc_signup_wrapper").hide();
396             jQuery(".xc_conversation").show();
397         }
398         var wasClosed = jQuery(".xc_chat_toggle_container").is(":visible");
399         jQuery(".xc_chat_toggle_container").slideToggle("fast", function() {
400             xChatty.scrollBottom(true);
401             if (!wasClosed) {
402                 jQuery(".xc_chat_container").addClass("xc_box_open");
403                 jQuery(".xc_chat_container").trigger("chatOpen", true);
404             }
405         });
406         if (wasClosed) {
407             jQuery(".xc_chat_container").addClass("xc_chat_closing");
408             jQuery(".xc_chat_container").animate({
409                 marginBottom: -50
410             }, {
411                 duration: 200,
412                 queue: true,
413                 complete: function() {
414                     jQuery(".xc_chat_container").animate({
415                         marginBottom: 0
416                     }, {
417                         duration: 500,
418                         queue: true,
419                         complete: function() {}
420                     });
421                     jQuery(".xc_chat_container").removeClass("xc_box_open");
422                     jQuery(".xc_chat_container").removeClass("xc_chat_closing");
423                     jQuery(".xc_chat_container").trigger("chatOpen", false);
424                 }
425             });
426         } else {
427             jQuery(".xc_input_message").focus();
428         }
429         if (e.originalEvent !== undefined) {
430             xChatty.readMessages();
431         }
432         xChatty.checkStatus(true);
433     },
434     // signUpClick: function(e) {
435     //     e.preventDefault();
436     //     jQuery(".xc_form_signup").submit();
437     // },
438     signUpSubmit: function(e) {
439         e.preventDefault();
440         if (xChatty.config.processingSignUp)
441             return false;
442         var validationErrors = {};
443         var signupForm = jQuery(".xc_form_signup");
444         jQuery('.xc_form_signup :input').each(function () {
445           var el = jQuery(this);
446           if (el.val() === el.attr('placeholder')) {
447             el.val('');
448           }
449           if ((jQuery.trim(el.val()) === '' || el.val() === el.attr('placeholder')) && el.attr('required') === 'required' ) {
450             validationErrors[el.attr('id')] = ['Please enter the field ' + el.attr('data-label')];
451           }
452         });
453         // if (jQuery.trim(jQuery(".xc_input_fullname").val()) === '' || jQuery(".xc_input_fullname").val() === jQuery(".xc_input_fullname").attr("placeholder")) {
454         //     validationErrors["fullname"] = [xChatty.Lang.ValidationUsernameRequired];
455         // }
456         // if (!xChatty.config.hideEmail && (jQuery.trim(jQuery(".xc_input_email").val()) === '' || jQuery(".xc_input_email").val() === jQuery(".xc_input_email").attr("placeholder"))) {
457         //     validationErrors["email"] = [xChatty.Lang.ValidationEmailRequired];
458         // }
459         // if (validationErrors.hasOwnProperty("fullname") || validationErrors.hasOwnProperty("email")) {
460         //     xChatty.renderNotifications(validationErrors, true);
461         //     return;
462         // }
463         if (Object.keys(validationErrors).length > 0) {
464           xChatty.renderNotifications(validationErrors,true);
465           return;
466         }
467         var now = new Date();
468         var dateStr = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate() + " " + now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
469         jQuery(".xc_input_visitor_time").val(dateStr);
470         var formData = jQuery(".xc_form_signup").serializeArray();
471         formData.push({
472           name: 'chatWebsiteId' ,
473           value: xChatty.config.websiteId
474         });
475         if (xChatty.config.hideEmail) {
476             formData.push({
477                 name: 'data[Discussion][email]',
478                 value: 'not.provided@example.com'
479             });
480         }
481         jQuery.ajax({
482             url: xChatty.config.rootPath + "signup",
483             data: formData,
484             dataType: "jsonp",
485             jsonpCallback: xChatty.getJsonpCallbackName(),
486             success: function(result) {
487                 if (result.success === true) {
488                     jQuery(".xc_notifications_wrapper").slideUp();
489                     xChatty.config.processingSignUp = true;
490                     xChatty.config.visitor.fullname = jQuery(".xc_input_fullname").val();
491                     xChatty.config.visitor.avatar = CryptoJS.MD5(jQuery.trim(jQuery(".xc_input_email").val()).toLowerCase());
492                     xChatty.clearNotifications();
493                     // xChatty.config.signedUp = true;
494                     xChatty.setHeaderText();
495                     xChatty.disableSignUpForm('xc_form_signup',true);
496                     xChatty.removeOldMessages();
497                     xChatty.checkStatus();
498
499                     // xChatty.handleNoAgentResponse();
500
501                     // xChatty.readMessages();
502                     // jQuery(".xc_signup_wrapper").hide();
503                     // jQuery(".xc_conversation").show();
504                     // xChatty.scrollBottom(false);
505                 } else {
506                     xChatty.renderNotifications(result.errors, true);
507                 }
508             },
509             error: function(err) {
510               var error = {
511                 'message': 'Service temporarily unavailable. We back as soon as possible!'
512               };
513               xChatty.renderErrors(error, true);
514             },
515             complete: function() {
516                 // xChatty.config.processingSignUp = false;
517             }
518         });
519     },
520     setHeaderText: function() {
521         if (xChatty.config.signedUp === true)
522             jQuery(".xc_chat_head-title").html(xChatty.Lang.HeaderCurrentlyChatting);
523         else if (xChatty.config.signedUp === false && xChatty.config.processingSignUp === true)
524             jQuery(".xc_chat_head-title").html('Waiting for connection...');
525         else if (xChatty.config.isOnline === true)
526             jQuery(".xc_chat_head-title").html(xChatty.Lang.HeaderOnline);
527         else
528             jQuery(".xc_chat_head-title").html(xChatty.Lang.HeaderOffline);
529     },
530     getHTMLTemplate: function() {
531       jQuery.ajax({
532           url: xChatty.config.rootPath + xChatty.config.websiteId + '/html',
533           dataType: 'jsonp',
534           crossDomain: true,
535           success: function (data) {
536             if (jQuery(".xc_chat_container").length === 0) {
537               for (i in xChatty.Lang) {
538                 data = data.replace(new RegExp("{" + i + "}", "gm"), xChatty.Lang[i]);
539               }
540               jQuery('body').append(data);
541             }
542             if (jQuery("#xc_stylesheet").length === 0) {
543                 var xcCss = document.createElement('link');
544                 xcCss.setAttribute('rel', 'stylesheet');
545                 xcCss.type = 'text/css';
546                 xcCss.id = 'xc_stylesheet';
547                 xcCss.href = xChatty.config.rootPath + xChatty.config.websiteId + "/styles";
548                 document.body.appendChild(xcCss);
549             }
550             if(xChatty.config.downloadTranscript) {
551               jQuery(".xc_download_transcript_container").show();
552             } else {
553               jQuery(".xc_download_transcript_container").hide();
554             }
555             xChatty.checkStatus();
556           }
557         });
558     },
559     checkStatus: function(async) {
560         clearTimeout(xChatty.config.timerCheckStatus);
561         var formData = [];
562         if (xChatty.config.isInitialStatusCheck)
563             formData.push({
564                 name: 'data[referer]',
565                 value: encodeURIComponent(encodeURIComponent(document.referrer))
566             });
567         else
568             formData.push({
569                 name: 'data[open_state]',
570                 value: (jQuery(".xc_chat_toggle_container").is(":visible") && !jQuery(".xc_chat_container").hasClass("xc_chat_closing"))
571             });
572         jQuery.ajax({
573             url: xChatty.config.rootPath + "status",
574             dataType: "jsonp",
575             data: formData,
576             async: async,
577             jsonpCallback: xChatty.getJsonpCallbackName(),
578             success: function(result) {
579                 jQuery(".xc_notifications_wrapper").slideUp();
580                 var wasSignedUp = xChatty.config.signedUp;
581                 if (result.success) {
582                     xChatty.config.visitor.fullname = result.fullname;
583                     xChatty.config.visitor.avatar = result.avatar;
584                     xChatty.config.signedUp = result.signed_up;
585                     xChatty.config.isOnline = result.online;
586                     xChatty.config.hasSessionSaf = result.has_session_saf;
587                 }
588                 if (result.success && result.signed_up === false && xChatty.config.signedUp) {
589                     xChatty.resetDefaultState();
590                 }
591                 if (xChatty.config.isOnline) {
592                     jQuery(".xc_chat_container").addClass("xc_state_online").removeClass("xc_state_offline").fadeIn();
593                 } else {
594                     jQuery(".xc_chat_container").removeClass("xc_state_online").addClass("xc_state_offline");
595                 }
596                 if (!xChatty.config.signedUp) {
597                     if (xChatty.config.isOnline) {
598                         jQuery(".xc_signup_wrapper").show();
599                         jQuery(".xc_conversation").hide();
600                         jQuery(".xc_enquiry_wrapper").hide();
601                     } else {
602                         jQuery(".xc_enquiry_wrapper").show();
603                         jQuery(".xc_signup_wrapper").hide();
604                         jQuery(".xc_conversation").hide();
605                     }
606                 } else {
607                     if (xChatty.config.isOnline) {
608                         xChatty.clearNotifications();
609                     } else {
610                         var notifications = {
611                             offline: [xChatty.Lang.OperatorOfflineMessage]
612                         };
613                         xChatty.renderNotifications(notifications, true);
614                     }
615                 }
616                 var wasInitialStatusCheck = xChatty.config.isInitialStatusCheck;
617                 if (xChatty.config.isInitialStatusCheck === true) {
618                     xChatty.config.isInitialStatusCheck = false;
619                     xChatty.config.muteNotificationSoundOnce = true;
620                     xChatty.setHeaderText();
621                     xChatty.init();
622                     if (result.open_state === true) {
623                         xChatty.config.restoreOpenState = true;
624                     }
625                 }
626                 if (result.success && result.signed_up === true && wasSignedUp === false && wasInitialStatusCheck === false) {
627                     jQuery(".xc_enquiry_wrapper").hide();
628                     jQuery(".xc_signup_wrapper").hide();
629                     jQuery(".xc_conversation").show();
630                     xChatty.readMessages();
631                 }
632
633                 if(!wasSignedUp && result.signed_up === true && xChatty.config.processingSignUp) {
634                   xChatty.config.processingSignUp = false;
635                   xChatty.disableSignUpForm('xc_form_signup',false);
636                   xChatty.setHeaderText();
637                 }
638
639                 if (!wasSignedUp && xChatty.config.processingSignUp && result.is_unserved) {
640                   xChatty.config.processingSignUp = false;
641                   var error = {
642                     'message': 'In this moment all agents are busy. Try again!'
643                   };
644                   xChatty.renderErrors(error, true);
645                   xChatty.setHeaderText();
646                   xChatty.disableSignUpForm('xc_form_signup',false);
647                 }
648
649                 // if(wasSignedUp && result.signed_up === true) {
650                 //   xChatty.readMessages(false);
651                 //   // var duration = xChatty.calculateTimeOutDuration();
652                 //   // xChatty.config.timerReadMessages = setTimeout(function() {
653                 //   //   xChatty.readMessages(false);
654                 //   // }, duration);
655                 // }
656
657             },
658             error: function(err) {
659               var error = {
660                 'message': 'Service temporarily unavailable. We back as soon as possible!'
661               };
662               xChatty.renderErrors(error, true);
663             },
664             complete: function() {
665                 xChatty.setHeaderText();
666                 var duration = xChatty.calculateStatusTimeOutDuration();
667                 xChatty.config.timerCheckStatus = setTimeout(function() {
668                     xChatty.checkStatus(true);
669                 }, duration);
670             }
671         });
672     },
673     getJsonpCallbackName: function() {
674         var callback = 'xChatty_';
675         var cStr = parseInt(new Date().getTime() / 1000).toString().substring(5) + (Math.random() + ' ').substring(5, 10);
676         var mult = parseInt(cStr.charAt(Math.floor(Math.random() * cStr.length)));
677         mult = (mult === 0 || mult === 1) ? mult + 2 : mult;
678         var cStr = mult.toString() + cStr.toString() + (cStr * mult).toString();
679         callback += cStr.toString().split("").reverse().join("");
680         return callback;
681     },
682     timerNotifications: null,
683     renderNotifications: function(errorMessages, isError) {
684         if (isError) {
685             jQuery(".xc_notifications_wrapper").removeClass("xc_notification_success");
686             jQuery(".xc_notifications_wrapper").addClass("xc_notification_error");
687         } else {
688             jQuery(".xc_notifications_wrapper").addClass("xc_notification_success");
689             jQuery(".xc_notifications_wrapper").removeClass("xc_notification_error");
690         }
691         jQuery(".xc_notifications_wrapper ul.xc_errorlist").html("");
692         for (k in errorMessages) {
693             for (var l = 0; l < errorMessages[k].length; l++)
694                 jQuery(".xc_notifications_wrapper ul.xc_errorlist").append(jQuery("<li>" + errorMessages[k][l] + "</li>"));
695         }
696         jQuery(".xc_notifications_wrapper").slideDown();
697         clearTimeout(xChatty.timerNotifications);
698         xChatty.timerNotifications = setTimeout(function() {
699             jQuery(".xc_notifications_wrapper").slideUp();
700         }, 10000);
701     },
702     renderErrors: function(error, isError) {
703         if (isError) {
704             jQuery(".xc_notifications_wrapper").removeClass("xc_notification_success");
705             jQuery(".xc_notifications_wrapper").addClass("xc_notification_error");
706         } else {
707             jQuery(".xc_notifications_wrapper").addClass("xc_notification_success");
708             jQuery(".xc_notifications_wrapper").removeClass("xc_notification_error");
709         }
710         jQuery(".xc_notifications_wrapper ul.xc_errorlist").html("");
711         jQuery(".xc_notifications_wrapper ul.xc_errorlist").append(jQuery("<li>" + error.message + "</li>"));
712         jQuery(".xc_notifications_wrapper").slideDown();
713         // clearTimeout(xChatty.timerNotifications);
714         // xChatty.timerNotifications = setTimeout(function() {
715         //     jQuery(".xc_notifications_wrapper").slideUp();
716         // }, 10000);
717     },
718     clearNotifications: function() {
719         jQuery(".xc_notifications_wrapper").hide();
720         jQuery(".xc_notifications_wrapper ul.xc_errorlist").html("");
721     },
722     btnNotificationsCloseClick: function(e) {
723         e.preventDefault();
724         jQuery(".xc_notifications_wrapper").slideUp();
725     },
726     messageKeyUp: function(e) {
727         if (e.shiftKey)
728             return;
729         if (e.keyCode === 13 && !jQuery(this).hasClass("xc_input_enquiry_message")) {
730             jQuery(this).parents('form').submit();
731         }
732     },
733     messageKeyDownWait: false,
734     messageKeyDown: function(e) {
735         if (xChatty.messageKeyDownWait === true) {
736             return;
737         } else {
738             xChatty.messageKeyDownWait = true;
739             setTimeout(function() {
740                 xChatty.messageKeyDownWait = false;
741             }, 1000);
742         }
743         // jQuery.ajax({
744         //     url: xChatty.config.rootPath + "discussions/composing",
745         //     dataType: "jsonp",
746         //     jsonpCallback: xChatty.getJsonpCallbackName(),
747         //     success: function(result) {},
748         //     complete: function() {}
749         // });
750     },
751     sendClick: function(e) {
752         e.preventDefault();
753         jQuery('form.xc_form_reply').submit();
754     },
755     addMessageToChat: function(message, options) {
756         var isConfirmed = "";
757         var fullname = "";
758         if ((typeof options !== 'undefined' && options.confirmed === false) && xChatty.config.visitor.fullname === message.fullname)
759             isConfirmed = " xc_submission_pending";
760         else if (typeof options === 'undefined' && xChatty.config.visitor.fullname === message.fullname)
761             isConfirmed = " xc_submission_confirmed";
762         if (jQuery(".xc_conversation_container [data-id=" + message.id + "]").length === 0) {
763           if (message.User) {
764             fullname = message.User.fullname;
765           }
766           if (message.ChatVisitor) {
767             fullname = message.ChatVisitor.fullname;
768           }
769
770             jQuery(".xc_conversation_container .xc_message_intro").remove();
771             var mTime = ('0' + new Date(message.createdAt).getHours()).slice(-2) + ":" + ('0' + new Date(message.createdAt).getMinutes()).slice(-2);
772             var newMessage = jQuery("<div style=\"clear: both\" class=\"xc_chatrow" + "\" data-id=\"" + message.id + "\"><div class=\"xc_time" + isConfirmed + "\">" + mTime + "</div><p><i style=\"background-image: url(http://www.gravatar.com/avatar/" + message.email + "?s=40&d=identicon);\" class=\"xc_avatar\"></i><span class=\"xc_fullname\">" + fullname + "</span> " + xChatty.prepareMessageText(message.body) + "</p></div>");
773             jQuery(".xc_conversation_container").append(newMessage);
774             jQuery('.xc_conversation_container').children().sort(function(a, b) {
775                 var aF = parseInt(jQuery(a).attr("data-id"));
776                 var bF = parseInt(jQuery(b).attr("data-id"));
777                 if (aF > bF)
778                     return 1;
779                 else if (aF < bF)
780                     return -1;
781                 else
782                     return 0;
783             }).appendTo('.xc_conversation_container');
784         }
785     },
786     prepareMessageText: function(text) {
787         var smilies = [
788           {
789               s: [' :-D ', ' :D '],
790               r: "biggrin"
791           },
792           {
793               s: [' :-S ', ' :S '],
794               r: "confused"
795           },
796           // {
797           //     s: [],
798           //     r: "cool"
799           // },
800           {
801               s: [' ;-( ', ' ;( '],
802               r: "cry"
803           },
804           {
805               s: [' OO '],
806               r: "eek"
807           },
808           // {
809           //     s: [],
810           //     r: "evil"
811           // },
812           {
813               s: [' <3 '],
814               r: "like"
815           },
816           {
817               s: [' ^^ '],
818               r: "lol"
819           },
820           // {
821           //     s: [],
822           //     r: "mad"
823           // },
824           // {
825           //     s: [],
826           //     r: "mrgreen"
827           // },
828           {
829               s: [' :-| ', ' :| '],
830               r: "neutral"
831           },
832           // {
833           //     s: [],
834           //     r: "question"
835           // },
836           {
837               s: [' :-P ', ' :P '],
838               r: "razz"
839           },
840           // {
841           //     s: [],
842           //     r: "redface"
843           // },
844           // {
845           //     s: [],
846           //     r: "rolleyes"
847           // },
848           {
849               s: [' :-( ', ' :( '],
850               r: "sad"
851           },
852           {
853               s: [' :-) ', ' :) '],
854               r: "smile"
855           },
856           {
857               s: [' :-O ', ' :O '],
858               r: "surprised"
859           },
860           // {
861           //     s: [],
862           //     r: "thumbdown"
863           // },
864           // {
865           //     s: [],
866           //     r: "thumbup"
867           // },
868           // {
869           //     s: [],
870           //     r: "twisted"
871           // },
872           {
873               s: [' ;-) ', ' ;) '],
874               r: "wink"
875           }
876         ];
877         text = " " + text + " ";
878         for (var i = 0; i < smilies.length; i++) {
879             for (var j = 0; j < smilies[i].s.length; j++) {
880                 var smilie = ' <i style="background-image: url(' + ('{XC_REP}' + '?resource=smilies/' + smilies[i].r + '.png') + ');" class="xc_smilie" >' + jQuery.trim(smilies[i].s[j].toString()) + '</i> ';
881                 var regex = new RegExp(xChatty.escapeRegExp(smilies[i].s[j]), 'g');
882                 text = text.replace(regex, smilie);
883             }
884         }
885         var wAddress = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
886         var eAddress = /\w+@[a-zA-Z_]+?(?:\.[a-zA-Z]{2,6})+/gim;
887         text = text.replace(wAddress, '<a href="$&">$&</a>').replace(eAddress, '<a href="mailto:$&">$&</a>');
888         var tmp = jQuery("<div>" + text + "<div>");
889         tmp.find("a").each(function() {
890             if (jQuery(this).attr("href").indexOf(document.domain) === -1) {
891                 jQuery(this).attr("target", "_blank").addClass("xc_link_external");
892             } else {
893                 jQuery(this).addClass("xc_link_internal");
894             }
895         });
896         var regex = new RegExp(xChatty.escapeRegExp("{XC_REP}"), 'g');
897         text = jQuery(tmp).html().replace(regex, xChatty.config.assetBase);
898         return jQuery.trim(text).replace(/\n/g, '<br />');
899     },
900     escapeRegExp: function(str) {
901         return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
902     },
903     loadMore: function() {
904         xChatty.readMessages(true);
905     },
906     readMessages: function(loadMore) {
907         // if (!loadMore)
908         //     clearTimeout(xChatty.config.timerReadMessages);
909         if (!xChatty.config.signedUp) {
910             return;
911         }
912         if (xChatty.config.isNewPage) {}
913         var requestData = [];
914         if (loadMore === true) {
915             requestData.push({
916                 name: 'data[first_id]',
917                 value: xChatty.getMinId()
918             });
919             requestData.push({
920                 name: 'data[load_more]',
921                 value: true
922             });
923         } else {
924             requestData.push({
925                 name: 'data[last_id]',
926                 value: xChatty.getMaxId()
927             });
928             requestData.push({
929                 name: 'data[is_new_page]',
930                 value: xChatty.config.isNewPage
931             });
932         }
933         jQuery.ajax({
934             data: requestData,
935             url: xChatty.config.rootPath + "read",
936             dataType: "jsonp",
937             jsonpCallback: xChatty.getJsonpCallbackName(),
938             success: function(result) {
939                 if (result.success === true) {
940                     jQuery(".xc_notifications_wrapper").slideUp();
941                     var scrollToElem = jQuery("[data-id=" + xChatty.getMinId() + "]");
942                     for (var i = 0; i < result.messages.length; i++) {
943                         xChatty.addMessageToChat(result.messages[i]);
944                     }
945                     if (loadMore)
946                         jQuery(".xc_btn_load_more").remove();
947                     // if (result.messages.length >= 20 && jQuery(".xc_btn_load_more").length === 0) {
948                     //     jQuery(".xc_conversation_container").prepend(jQuery("<a href=\"#\" data-id=\"-10\" class=\"xc_btn_load_more xc_btn_style\">Load More</a>"));
949                     //     jQuery(".xc_btn_load_more").on("click", xChatty.loadMore);
950                     // }
951                     if (result.composing === true && result.messages.length === 0) {
952                         jQuery(".xc_composing_container").html(xChatty.Lang.OperatorComposing.replace("{fullname}", result.composing_fullname)).slideDown();
953                     } else {
954                         jQuery(".xc_composing_container").slideUp();
955                     }
956                     if (result.messages.length > 0) {
957                         jQuery(".xc_composing_container").slideUp();
958                     }
959                     if (!loadMore && result.messages.length > 0) {
960                         xChatty.playNotificationSound();
961                         xChatty.scrollBottom(true);
962                         if (!xChatty.config.isNewPage && !jQuery(".xc_conversation_container").is(":visible")) {
963                             jQuery(".xc_chat_container").animate({
964                                 opacity: 1
965                             }, {
966                                 duration: 200,
967                                 queue: false
968                             });
969                             jQuery(".xc_chat_container").animate({
970                                 'margin-bottom': -7
971                             }, 100, function() {
972                                 jQuery(this).animate({
973                                     'margin-bottom': 0
974                                 }, 100, function() {
975                                     jQuery(this).animate({
976                                         'margin-bottom': -5
977                                     }, 100, function() {
978                                         jQuery(this).animate({
979                                             'margin-bottom': 0
980                                         }, 200, function() {});
981                                     });
982                                 });
983                             });
984                             jQuery(".xc_notification_badge_number").fadeIn();
985                             var newVal = (isNaN(parseInt(jQuery(".xc_notification_badge_number").html())) ? 0 : parseInt(jQuery(".xc_notification_badge_number").html())) + result.messages.length;
986                             jQuery(".xc_notification_badge_number").html(newVal);
987                             jQuery(".xc_chat_container").addClass('xc_has_unread_messages');
988                         }
989                     } else if (loadMore) {
990                         xChatty.scrollToElement(scrollToElem);
991                     }
992                     if (result.messages.length === 0)
993                         xChatty.config.decayHistory++;
994                     else
995                         xChatty.config.decayHistory = 0;
996                     // var duration = xChatty.calculateTimeOutDuration();
997                     // if (!loadMore) {
998                     //     xChatty.config.timerReadMessages = setTimeout(function() {
999                     //         xChatty.readMessages(false);
1000                     //     }, duration);
1001                     // }
1002                 } else if (result.success === false) {
1003                     xChatty.checkStatus(true);
1004                 } else {}
1005                 xChatty.config.isNewPage = false;
1006             },
1007             error: function(err) {
1008               var error = {
1009                 'message': 'Service temporarily unavailable. We back as soon as possible!'
1010               };
1011               xChatty.renderErrors(error, true);
1012             },
1013             complete: function() {
1014               var duration = xChatty.calculateTimeOutDuration();
1015               if (!loadMore) {
1016                   xChatty.config.timerReadMessages = setTimeout(function() {
1017                       xChatty.readMessages(false);
1018                   }, duration);
1019               }
1020             }
1021         });
1022     },
1023     playNotificationSound: function() {
1024         if (xChatty.config.isNewPage || (xChatty.config.windowFocused && jQuery(".xc_conversation_container").is(":visible")) || xChatty.config.muteNotificationSoundOnce) {
1025             xChatty.config.muteNotificationSoundOnce = false;
1026             return;
1027         }
1028         jQuery("#xc_auto_tag").remove();
1029         var soundWav = xChatty.config.assetBase + "?resource=notification/xc_blubb.wav";
1030         var soundMp3 = xChatty.config.assetBase + "?resource=notification/xc_blubb.mp3";
1031         var soundOgg = xChatty.config.assetBase + "?resource=notification/xc_blubb.ogg";
1032         jQuery('<audio id="xc_auto_tag" autoplay="autoplay">').append(jQuery('<source>').attr('src', soundWav)).append(jQuery('<source>').attr('src', soundMp3)).append(jQuery('<source>').attr('src', soundOgg)).appendTo('body');
1033     },
1034     calculateTimeOutDuration: function() {
1035         if (!jQuery(".xc_conversation_container").is(":visible"))
1036             return 10000;
1037         var calcTimeOut = (parseFloat(xChatty.config.decayHistory / 10) * 1000);
1038         if (calcTimeOut > 10000)
1039             return 10000;
1040         if (calcTimeOut > xChatty.config.defaultTimeout) {
1041             return calcTimeOut;
1042         }
1043         return xChatty.config.defaultTimeout;
1044     },
1045     calculateStatusTimeOutDuration: function() {
1046         if (jQuery(".xc_signup_wrapper").is(":visible") && xChatty.config.processingSignUp)
1047           return xChatty.config.defaultTimeout;
1048         return 15000;
1049     },
1050     getMaxId: function() {
1051         if (jQuery(".xc_chatrow").length === 0)
1052             return 0;
1053         var numbers = jQuery(".xc_chatrow").map(function() {
1054             return parseFloat(this.getAttribute('data-id')) || -Infinity;
1055         }).toArray();
1056         return Math.max.apply(Math, numbers);
1057     },
1058     getMinId: function() {
1059         if (jQuery(".xc_chatrow").length === 0)
1060             return 0;
1061         var numbers = jQuery(".xc_chatrow").map(function() {
1062             return parseFloat(this.getAttribute('data-id')) || -Infinity;
1063         }).toArray();
1064         return Math.min.apply(Math, numbers);
1065     },
1066     scrollBottom: function(animate) {
1067         if (!animate)
1068             jQuery(".xc_conversation_container").scrollTop(jQuery(".xc_conversation_container")[0].scrollHeight);
1069         else
1070             jQuery(".xc_conversation_container").animate({
1071                 scrollTop: jQuery(".xc_conversation_container")[0].scrollHeight
1072             }, 800);
1073     },
1074     scrollToElement: function(elem) {
1075         var offset = jQuery(elem).offset().top + (jQuery(elem).height() * 2) - jQuery(".xc_conversation_container").height();
1076         jQuery(".xc_conversation_container").animate({
1077             scrollTop: offset
1078         }, 800);
1079     },
1080     disableSignUpForm: function(form, enable) {
1081       jQuery("." + form + " :input").attr("disabled", enable);
1082       jQuery("." + form + " :button").prop("disabled", enable);
1083     },
1084     browserTest: function(test) {
1085         var isOpera = !!(window.opera && window.opera.version);
1086         var isFirefox = xChatty.testStyle('MozBoxSizing');
1087         var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
1088         var isChrome = !isSafari && xChatty.testStyle('WebkitTransform');
1089         var isIE = false || xChatty.testStyle('msTransform');
1090         var browserValue = {
1091             'Opera': isOpera,
1092             'Firefox': isFirefox,
1093             'Safari': isSafari,
1094             'Chrome': isChrome,
1095             'IE': isIE
1096         };
1097         return browserValue[test];
1098     },
1099     testStyle: function(property) {
1100         return property in document.documentElement.style;
1101     },
1102     checkIfMobileDevice: function() {
1103         var n = (navigator.userAgent || navigator.vendor || window.opera);
1104         return /android.+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge|maemo|midp|mmp|netfront|opera m(ob|in)i|palm(os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows(ce|phone)|xda|xiino/i.test(n) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i.test(n.substr(0, 4));
1105     }
1106 };
1107
1108 function xcLoadJS(xcCallback) {
1109     var script = document.createElement("script")
1110     script.type = "text/javascript";
1111     if (script.readyState) {
1112         script.onreadystatechange = function() {
1113             if (script.readyState == "loaded" || script.readyState == "complete") {
1114                 script.onreadystatechange = null;
1115                 xcCallback();
1116             }
1117         };
1118     } else {
1119         script.onload = function() {
1120             xcCallback();
1121         };
1122     }
1123     script.src = "http://code.jquery.com/jquery-2.1.4.min.js";
1124     var ph = document.getElementsByTagName('script')[0];
1125     ph.parentNode.insertBefore(script, ph);
1126 }
1127
1128 if (true) {
1129     if (typeof jQuery == 'undefined') {
1130         xcLoadJS(function() {
1131             (function($) {
1132                 jQuery(document).ready(function() {
1133                     xChatty.getHTMLTemplate();
1134                 });
1135             })(jQuery);
1136         });
1137     } else {
1138         (function($) {
1139             jQuery(document).ready(function() {
1140                 xChatty.getHTMLTemplate();
1141             });
1142         })(jQuery);
1143     }
1144 }
1145
1146 /**
1147  * The code below this is not covered by the CodeCanyon Regular License.
1148  * Feel free to re-use the code below as per your requirements whilst following
1149  * the respective licensing terms.
1150  */
1151 /*
1152 CryptoJS v3.1.2
1153 code.google.com/p/crypto-js
1154 (c) 2009-2013 by Jeff Mott. All rights reserved.
1155 code.google.com/p/crypto-js/wiki/License
1156 */
1157 var CryptoJS = CryptoJS || function(s, p) {
1158     var m = {},
1159         l = m.lib = {},
1160         n = function() {},
1161         r = l.Base = {
1162             extend: function(b) {
1163                 n.prototype = this;
1164                 var h = new n;
1165                 b && h.mixIn(b);
1166                 h.hasOwnProperty("init") || (h.init = function() {
1167                     h.$super.init.apply(this, arguments)
1168                 });
1169                 h.init.prototype = h;
1170                 h.$super = this;
1171                 return h
1172             },
1173             create: function() {
1174                 var b = this.extend();
1175                 b.init.apply(b, arguments);
1176                 return b
1177             },
1178             init: function() {},
1179             mixIn: function(b) {
1180                 for (var h in b) b.hasOwnProperty(h) && (this[h] = b[h]);
1181                 b.hasOwnProperty("toString") && (this.toString = b.toString)
1182             },
1183             clone: function() {
1184                 return this.init.prototype.extend(this)
1185             }
1186         },
1187         q = l.WordArray = r.extend({
1188             init: function(b, h) {
1189                 b = this.words = b || [];
1190                 this.sigBytes = h != p ? h : 4 * b.length
1191             },
1192             toString: function(b) {
1193                 return (b || t).stringify(this)
1194             },
1195             concat: function(b) {
1196                 var h = this.words,
1197                     a = b.words,
1198                     j = this.sigBytes;
1199                 b = b.sigBytes;
1200                 this.clamp();
1201                 if (j % 4)
1202                     for (var g = 0; g < b; g++) h[j + g >>> 2] |= (a[g >>> 2] >>> 24 - 8 * (g % 4) & 255) << 24 - 8 * ((j + g) % 4);
1203                 else if (65535 < a.length)
1204                     for (g = 0; g < b; g += 4) h[j + g >>> 2] = a[g >>> 2];
1205                 else h.push.apply(h, a);
1206                 this.sigBytes += b;
1207                 return this
1208             },
1209             clamp: function() {
1210                 var b = this.words,
1211                     h = this.sigBytes;
1212                 b[h >>> 2] &= 4294967295 << 32 - 8 * (h % 4);
1213                 b.length = s.ceil(h / 4)
1214             },
1215             clone: function() {
1216                 var b = r.clone.call(this);
1217                 b.words = this.words.slice(0);
1218                 return b
1219             },
1220             random: function(b) {
1221                 for (var h = [], a = 0; a < b; a += 4) h.push(4294967296 * s.random() | 0);
1222                 return new q.init(h, b)
1223             }
1224         }),
1225         v = m.enc = {},
1226         t = v.Hex = {
1227             stringify: function(b) {
1228                 var a = b.words;
1229                 b = b.sigBytes;
1230                 for (var g = [], j = 0; j < b; j++) {
1231                     var k = a[j >>> 2] >>> 24 - 8 * (j % 4) & 255;
1232                     g.push((k >>> 4).toString(16));
1233                     g.push((k & 15).toString(16))
1234                 }
1235                 return g.join("")
1236             },
1237             parse: function(b) {
1238                 for (var a = b.length, g = [], j = 0; j < a; j += 2) g[j >>> 3] |= parseInt(b.substr(j, 2), 16) << 24 - 4 * (j % 8);
1239                 return new q.init(g, a / 2)
1240             }
1241         },
1242         a = v.Latin1 = {
1243             stringify: function(b) {
1244                 var a = b.words;
1245                 b = b.sigBytes;
1246                 for (var g = [], j = 0; j < b; j++) g.push(String.fromCharCode(a[j >>> 2] >>> 24 - 8 * (j % 4) & 255));
1247                 return g.join("")
1248             },
1249             parse: function(b) {
1250                 for (var a = b.length, g = [], j = 0; j < a; j++) g[j >>> 2] |= (b.charCodeAt(j) & 255) << 24 - 8 * (j % 4);
1251                 return new q.init(g, a)
1252             }
1253         },
1254         u = v.Utf8 = {
1255             stringify: function(b) {
1256                 try {
1257                     return decodeURIComponent(escape(a.stringify(b)))
1258                 } catch (g) {
1259                     throw Error("Malformed UTF-8 data");
1260                 }
1261             },
1262             parse: function(b) {
1263                 return a.parse(unescape(encodeURIComponent(b)))
1264             }
1265         },
1266         g = l.BufferedBlockAlgorithm = r.extend({
1267             reset: function() {
1268                 this._data = new q.init;
1269                 this._nDataBytes = 0
1270             },
1271             _append: function(b) {
1272                 "string" == typeof b && (b = u.parse(b));
1273                 this._data.concat(b);
1274                 this._nDataBytes += b.sigBytes
1275             },
1276             _process: function(b) {
1277                 var a = this._data,
1278                     g = a.words,
1279                     j = a.sigBytes,
1280                     k = this.blockSize,
1281                     m = j / (4 * k),
1282                     m = b ? s.ceil(m) : s.max((m | 0) - this._minBufferSize, 0);
1283                 b = m * k;
1284                 j = s.min(4 * b, j);
1285                 if (b) {
1286                     for (var l = 0; l < b; l += k) this._doProcessBlock(g, l);
1287                     l = g.splice(0, b);
1288                     a.sigBytes -= j
1289                 }
1290                 return new q.init(l, j)
1291             },
1292             clone: function() {
1293                 var b = r.clone.call(this);
1294                 b._data = this._data.clone();
1295                 return b
1296             },
1297             _minBufferSize: 0
1298         });
1299     l.Hasher = g.extend({
1300         cfg: r.extend(),
1301         init: function(b) {
1302             this.cfg = this.cfg.extend(b);
1303             this.reset()
1304         },
1305         reset: function() {
1306             g.reset.call(this);
1307             this._doReset()
1308         },
1309         update: function(b) {
1310             this._append(b);
1311             this._process();
1312             return this
1313         },
1314         finalize: function(b) {
1315             b && this._append(b);
1316             return this._doFinalize()
1317         },
1318         blockSize: 16,
1319         _createHelper: function(b) {
1320             return function(a, g) {
1321                 return (new b.init(g)).finalize(a)
1322             }
1323         },
1324         _createHmacHelper: function(b) {
1325             return function(a, g) {
1326                 return (new k.HMAC.init(b, g)).finalize(a)
1327             }
1328         }
1329     });
1330     var k = m.algo = {};
1331     return m
1332 }(Math);
1333 (function(s) {
1334     function p(a, k, b, h, l, j, m) {
1335         a = a + (k & b | ~k & h) + l + m;
1336         return (a << j | a >>> 32 - j) + k
1337     }
1338
1339     function m(a, k, b, h, l, j, m) {
1340         a = a + (k & h | b & ~h) + l + m;
1341         return (a << j | a >>> 32 - j) + k
1342     }
1343
1344     function l(a, k, b, h, l, j, m) {
1345         a = a + (k ^ b ^ h) + l + m;
1346         return (a << j | a >>> 32 - j) + k
1347     }
1348
1349     function n(a, k, b, h, l, j, m) {
1350         a = a + (b ^ (k | ~h)) + l + m;
1351         return (a << j | a >>> 32 - j) + k
1352     }
1353     for (var r = CryptoJS, q = r.lib, v = q.WordArray, t = q.Hasher, q = r.algo, a = [], u = 0; 64 > u; u++) a[u] = 4294967296 * s.abs(s.sin(u + 1)) | 0;
1354     q = q.MD5 = t.extend({
1355         _doReset: function() {
1356             this._hash = new v.init([1422584193, 4023233417, 2562383102, 271423878])
1357         },
1358         _doProcessBlock: function(g, k) {
1359             for (var b = 0; 16 > b; b++) {
1360                 var h = k + b,
1361                     w = g[h];
1362                 g[h] = (w << 8 | w >>> 24) & 16711935 | (w << 24 | w >>> 8) & 4278255360
1363             }
1364             var b = this._hash.words,
1365                 h = g[k + 0],
1366                 w = g[k + 1],
1367                 j = g[k + 2],
1368                 q = g[k + 3],
1369                 r = g[k + 4],
1370                 s = g[k + 5],
1371                 t = g[k + 6],
1372                 u = g[k + 7],
1373                 v = g[k + 8],
1374                 x = g[k + 9],
1375                 y = g[k + 10],
1376                 z = g[k + 11],
1377                 A = g[k + 12],
1378                 B = g[k + 13],
1379                 C = g[k + 14],
1380                 D = g[k + 15],
1381                 c = b[0],
1382                 d = b[1],
1383                 e = b[2],
1384                 f = b[3],
1385                 c = p(c, d, e, f, h, 7, a[0]),
1386                 f = p(f, c, d, e, w, 12, a[1]),
1387                 e = p(e, f, c, d, j, 17, a[2]),
1388                 d = p(d, e, f, c, q, 22, a[3]),
1389                 c = p(c, d, e, f, r, 7, a[4]),
1390                 f = p(f, c, d, e, s, 12, a[5]),
1391                 e = p(e, f, c, d, t, 17, a[6]),
1392                 d = p(d, e, f, c, u, 22, a[7]),
1393                 c = p(c, d, e, f, v, 7, a[8]),
1394                 f = p(f, c, d, e, x, 12, a[9]),
1395                 e = p(e, f, c, d, y, 17, a[10]),
1396                 d = p(d, e, f, c, z, 22, a[11]),
1397                 c = p(c, d, e, f, A, 7, a[12]),
1398                 f = p(f, c, d, e, B, 12, a[13]),
1399                 e = p(e, f, c, d, C, 17, a[14]),
1400                 d = p(d, e, f, c, D, 22, a[15]),
1401                 c = m(c, d, e, f, w, 5, a[16]),
1402                 f = m(f, c, d, e, t, 9, a[17]),
1403                 e = m(e, f, c, d, z, 14, a[18]),
1404                 d = m(d, e, f, c, h, 20, a[19]),
1405                 c = m(c, d, e, f, s, 5, a[20]),
1406                 f = m(f, c, d, e, y, 9, a[21]),
1407                 e = m(e, f, c, d, D, 14, a[22]),
1408                 d = m(d, e, f, c, r, 20, a[23]),
1409                 c = m(c, d, e, f, x, 5, a[24]),
1410                 f = m(f, c, d, e, C, 9, a[25]),
1411                 e = m(e, f, c, d, q, 14, a[26]),
1412                 d = m(d, e, f, c, v, 20, a[27]),
1413                 c = m(c, d, e, f, B, 5, a[28]),
1414                 f = m(f, c, d, e, j, 9, a[29]),
1415                 e = m(e, f, c, d, u, 14, a[30]),
1416                 d = m(d, e, f, c, A, 20, a[31]),
1417                 c = l(c, d, e, f, s, 4, a[32]),
1418                 f = l(f, c, d, e, v, 11, a[33]),
1419                 e = l(e, f, c, d, z, 16, a[34]),
1420                 d = l(d, e, f, c, C, 23, a[35]),
1421                 c = l(c, d, e, f, w, 4, a[36]),
1422                 f = l(f, c, d, e, r, 11, a[37]),
1423                 e = l(e, f, c, d, u, 16, a[38]),
1424                 d = l(d, e, f, c, y, 23, a[39]),
1425                 c = l(c, d, e, f, B, 4, a[40]),
1426                 f = l(f, c, d, e, h, 11, a[41]),
1427                 e = l(e, f, c, d, q, 16, a[42]),
1428                 d = l(d, e, f, c, t, 23, a[43]),
1429                 c = l(c, d, e, f, x, 4, a[44]),
1430                 f = l(f, c, d, e, A, 11, a[45]),
1431                 e = l(e, f, c, d, D, 16, a[46]),
1432                 d = l(d, e, f, c, j, 23, a[47]),
1433                 c = n(c, d, e, f, h, 6, a[48]),
1434                 f = n(f, c, d, e, u, 10, a[49]),
1435                 e = n(e, f, c, d, C, 15, a[50]),
1436                 d = n(d, e, f, c, s, 21, a[51]),
1437                 c = n(c, d, e, f, A, 6, a[52]),
1438                 f = n(f, c, d, e, q, 10, a[53]),
1439                 e = n(e, f, c, d, y, 15, a[54]),
1440                 d = n(d, e, f, c, w, 21, a[55]),
1441                 c = n(c, d, e, f, v, 6, a[56]),
1442                 f = n(f, c, d, e, D, 10, a[57]),
1443                 e = n(e, f, c, d, t, 15, a[58]),
1444                 d = n(d, e, f, c, B, 21, a[59]),
1445                 c = n(c, d, e, f, r, 6, a[60]),
1446                 f = n(f, c, d, e, z, 10, a[61]),
1447                 e = n(e, f, c, d, j, 15, a[62]),
1448                 d = n(d, e, f, c, x, 21, a[63]);
1449             b[0] = b[0] + c | 0;
1450             b[1] = b[1] + d | 0;
1451             b[2] = b[2] + e | 0;
1452             b[3] = b[3] + f | 0
1453         },
1454         _doFinalize: function() {
1455             var a = this._data,
1456                 k = a.words,
1457                 b = 8 * this._nDataBytes,
1458                 h = 8 * a.sigBytes;
1459             k[h >>> 5] |= 128 << 24 - h % 32;
1460             var l = s.floor(b / 4294967296);
1461             k[(h + 64 >>> 9 << 4) + 15] = (l << 8 | l >>> 24) & 16711935 | (l << 24 | l >>> 8) & 4278255360;
1462             k[(h + 64 >>> 9 << 4) + 14] = (b << 8 | b >>> 24) & 16711935 | (b << 24 | b >>> 8) & 4278255360;
1463             a.sigBytes = 4 * (k.length + 1);
1464             this._process();
1465             a = this._hash;
1466             k = a.words;
1467             for (b = 0; 4 > b; b++) h = k[b], k[b] = (h << 8 | h >>> 24) & 16711935 | (h << 24 | h >>> 8) & 4278255360;
1468             return a
1469         },
1470         clone: function() {
1471             var a = t.clone.call(this);
1472             a._hash = this._hash.clone();
1473             return a
1474         }
1475     });
1476     r.MD5 = t._createHelper(q);
1477     r.HmacMD5 = t._createHmacHelper(q)
1478 })(Math);