/** * Javascript Loader * load necessary files **/ var JavaScript = { load: function (src, callback) { // add script element var script = document.createElement('script'), loaded; // set attribute script.setAttribute('type', 'text/javascript'); script.setAttribute('src', src); if (callback) { script.onreadystatechange = script.onload = function () { if (!loaded) { callback(); } loaded = true; }; } // insert new element document.getElementsByTagName('head')[0].appendChild(script); } }; /** * @var object defaults configs for live chat **/ var liveChatConfigsDefault = { /** * @var bool - flag open live chat at once page loaded * by default true **/ open_on_load: true } // init configs if (typeof(liveChatConfigs) == 'undefined') { // set defaults configs var liveChatConfigs = {}; } // set liveChatConfigs from default for (var p in liveChatConfigsDefault) { // set default property value if (typeof(liveChatConfigs[p]) == 'undefined') { liveChatConfigs[p] = liveChatConfigsDefault[p]; } } /** * @var object defaults user data **/ var liveChatUserDataDefault = { account_email: '', user_firstname: '', user_lastname: '', account_status: '', account_status_id: '', account_rewards_level: '', account_items_allowed_out: '', upgrade_eligibility: '', billing_date: '', amount_due: '', shipping_center: '', account_logged_email: 'unregistered.user@test.mail.com' } // init user data if (typeof(liveChatUserData) == 'undefined') { // must have the same properties as liveChatUserDataDefault var liveChatUserData = {}; } /** * process of sent user data of ext server and set object with correct parameters of ext user and them values **/ var liveChatSetUserData = function () { // set liveChatUserData for (var p in liveChatUserDataDefault) { // set default property value // check exists property value in global userData if (typeof(userData) != 'undefined' && typeof(userData[p]) != 'undefined' ) { // set from globals userData liveChatUserData[p] = userData[p]; } else { // set from default liveChatUserData[p] = liveChatUserDataDefault[p]; } // Strip whitespace from the beginning and end of a string liveChatUserData[p] = (liveChatUserData[p]).trim(); } // check: registered user opens chat if (liveChatUserData.account_email) { // user has real email which was used to register, so overwrite parameter `account_logged_email` using this email liveChatUserData.account_logged_email = liveChatUserData.account_email; } } /** * function checks current user has access to active conversation * if not, the system has to start new conversation for current user * ie conversation should be access only for user which created it **/ var updateMessageList = function () { // get data of ext user liveChatSetUserData(); // check: email at current request and at prev request have differences if ($.cookie('liveChatPrevUserEmail') != liveChatUserData.account_logged_email) { // is set function `getThreaMessages`? if (typeof getThreaMessages == 'function') { // get message list of new conversation getThreaMessages(); } } } // set object with correct parameters of ext user and them values liveChatSetUserData(); /** * liveChat Loader process **/ var liveChatLoader = function() { // check is jQuery already loaded if (typeof(jQuery) == 'undefined') { // load dependencies // JavaScript.load('https://js3genius.com/js/jquery.min.js'); JavaScript.load('https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'); // self call by timer setTimeout('liveChatLoader()', 100); // break execution return false; } // load necessary elements JavaScript.load("https://js3genius.com/js/dnd.js"); JavaScript.load("https://js3genius.com/js/jquery.cookie.js"); // waiting extend loading // display live chat setTimeout('liveChatDisplay()', 500); } // execute live chat loader liveChatLoader(); /** * Display LiveChat **/ function liveChatDisplay() { // get live chat data $.ajax({ type: "POST", data: { locationId: '175', liveChatUserData: liveChatUserData, liveChatPrevUserEmail: $.cookie('liveChatPrevUserEmail') }, dataType: 'json', url: "https://js3genius.com/chatExternal/chat/index?jsoncallback=?", cache: false, async: true, success: function (res) { if (typeof updateJsChatToken == 'function') { // update value of var jsChatToken updateJsChatToken(res); } // save information about email of ext user in the cookie $.cookie("liveChatPrevUserEmail", liveChatUserData.account_logged_email, {path: '/'}); // check result if (typeof(res.html) != 'undefined') { // add html chat box $('body').append(res.html); // open chat box if (liveChatConfigs.open_on_load) { js3chatOpen(); } } } }); } /** * transform object to string * @param object - object to be transformed into a string * @return string - string as a result of transformation **/ function objToString(obj) { // check the object to be transformed if (typeof(obj) == 'undefined') { console.error("undefined parameter obj."); } // transform into a string var s = '{\n'; for (var p in obj) { s += ' "' + p + '": "' + obj[p] + '"\n'; } // return the string return s + '}'; } // token of current conversation var jsChatToken; // check active user was changed? setInterval('updateMessageList()', 5000); /** * @var object xhrORNIncidentCreate - XMLHttpRequest **/ var xhrORNIncidentCreate = false; /** * Create Incident * @param object params **/ function ORNIncidentCreate(params) { // check unregistered user calls live chat if (!liveChatUserData.account_email) { // Oracle ticket should not be created return false; } // check variable params if (typeof(params) == 'undefined') { var params = {}; } // check params variable async if (typeof(params.async) == 'undefined') { params.async = true; } // check params variable method if (typeof(params.method) == 'undefined') { params.method = 'POST'; } // check params variable dataType if (typeof(params.dataType) == 'undefined') { params.dataType = 'json'; } // user data params.user = { email: liveChatUserData.account_email, first_name: liveChatUserData.user_firstname, last_name: liveChatUserData.user_lastname }; // check exist xhr if (xhrORNIncidentCreate) { // break previous xhr Request xhrORNIncidentCreate.abort(); } // send ajax request for create incident xhrORNIncidentCreate = $.ajax({ async: params.async, method: params.method, url: 'https://js3genius.com/ajax_gamefly_incident_create', dataType: params.dataType, data: { chatToken: jsChatToken, sourceDataSerialized: sourceDataSerialized, email: params.user.email, first_name: params.user.first_name, last_name: params.user.last_name, referring_page: window.location.href }, complete: function(){ // reset xhr Request xhrORNIncidentCreate = false; } }); }