
var agentname = "0";
var agentSetCookieName = "";

var salesAgentCodeCookie = "";
var salesAgentNameCookie = "";
var regionCode = "";
var currReg = "";


// Accepts a url and a callback function to run.
function requestCrossDomain(site, callback) {

    // If no url was passed, exit.
    if (!site) {
        //alert('No site was passed.');
        return false;
    }

    // Take the provided url, and add it to a YQL query. Make sure you encode it!
    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=?';

    // Request that YSQL string, and run a callback function.
    // Pass a defined function to prevent cache-busting.
    $.getJSON(yql, cbFunc);

    function cbFunc(data) {
        // If we have something to work with...
        if (data.results[0]) {
            // Strip out all script tags, for security reasons.
            // BE VERY CAREFUL. This helps, but we should do more.
            data = data.results[0].replace(/<body>/gi, '');
            data = data.replace(/<\/body>/gi, '');
            data = data.replace(/<p>/gi, '');
            data = data.replace(/<\/p>/gi, '');
            data = data.replace(/^\s+/, ''); // LTrim
            data = data.replace(/\s+$/, ''); // RTrim
            data = data.split(",");

            //data = data.results[0].replace(/<body[^>]*>[\s\S]*?<\/body>/gi, '');
            //data = data.results[0].replace(/<p[^>]*>[\s\S]*?<\/p>/gi, '');

            // If the user passed a callback, and it
            // is a function, call it, and send through the data var.
            if (typeof callback === 'function') {
                callback(data);
            }
        }
        // Else, Maybe we requested a site that doesn't exist, and nothing returned.
        else throw new Error('Nothing returned from getJSON.');
    }
}

function GetRegionCRM() {

    currReg = geoip_country_code();
    return currReg;
}



function CallCRM() {

    regionCode = GetRegionCRM();

//    var agentCookieName = $.cookies.get('salesAgentName');
//    agentname = agentCookieName;
//    if (agentCookieName == null || agentCookieName == "") {
//        var path = "http://research.xcrmlivepro.com/ChatTranscript/CRMChatAllocation.aspx?ic=2&cc=1&rc=" + regionCode;
//        requestCrossDomain(path, function(data) {
//            salesAgentCodeCookie = data[0].replace("[", "");
//            salesAgentNameCookie = data[1].replace("]", "");
//            salesAgentNameCookie = salesAgentNameCookie.replace("\"", "");
//            salesAgentNameCookie = salesAgentNameCookie.replace("\"", "");

//            salesAgentCodeCookie = salesAgentCodeCookie.replace("\"", "");
//            salesAgentCodeCookie = salesAgentCodeCookie.replace("\"", "");


//            agentname = salesAgentNameCookie;
//            if (salesAgentNameCookie != null && salesAgentNameCookie != '') {
//                $.cookies.set('salesAgentCode', salesAgentCodeCookie, { hoursToLive: 175316 }); //
//                $.cookies.set('salesAgentName', salesAgentNameCookie, { hoursToLive: 175316 });
//            }
//        });
//    }
}

function popitup(url) {

    newwindow = window.open(url, 'name', 'height=475,width=478,resizable=no,status=no,toolbar=no,location=no,menubar=no,top=120,left=140');
    return false;
}



function setButtonURL() {
    //javascript: window.open('http://server.iad.liveperson.net/hc/17505227/?cmd=file&amp;file=visitorWantsToChat&amp;SESSIONVAR!skill=' + agentname + '&amp;site=17505227&amp;imageUrl=http://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/English/General/1a/&amp;referrer=' + escape(document.location) + '', 'chat17505227', 'width=475,height=400,resizable=yes,status=no,toolbar=no,location=no,menubar=no');
//    var URL1 = 'http://server.iad.liveperson.net/hc/17505227/?cmd=file&amp;file=visitorWantsToChat&offlineURL=http://www.papermoz.com//Chat/OfflineSurveyRedirector.html&amp;SESSIONVAR!skill=';

    var URL1 = 'http://server.iad.liveperson.net/hc/17505227/?cmd=file&amp;file=visitorWantsToChat&amp;SESSIONVAR!skill=';
    var URL2 = agentname;
    var URL3 = '&amp;site=17505227&amp;imageUrl=http://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/English/General/1a/&amp;referrer=';
    var URL4 = escape(document.location);
    var URL5 = '\', \'chat17505227';

    var URL = URL1 + URL2 + URL3 + URL4 + URL5;

    popitup(URL);
}

CallCRM();
