﻿function PopChatRequest() {
    //Get session cookie if it exists
    displayed = GetCookie('RacoPopupDisplayed');

    baseUrl = GetBaseUrl();
    $.get(baseUrl + 'ChatCollector.asmx/WebChatAvailable',
    function(data) {
        availalbe = false;
        if (data.childNodes[1]) { //IE
            available = data.childNodes[1].childNodes[0].data;
        } else if (data.childNodes[0]) { //FF/Chrome
            available = data.childNodes[0].childNodes[0].data;
        }

        if (available == 'true' && displayed != 'true') {
            setTimeout(ShowPrompt, 30000);
        }
    });
}
function ShowPrompt() {
    txt = 'Would you like to chat with a RACO Product Specialist?<br /><br /><label for="Name">Name</Label><br /><input type="text" id="Name" /><br /><label for="Company">Company</label><br /><input type="text" id="Company" />';
    $.prompt(txt, {
        buttons: { Ok: true, Cancel: false },
        prefix: 'cleanblue',
        opacity: .4,
        submit: showPopup
    });

    //Log that we displayed a popup
    baseUrl = GetBaseUrl();
    $.get(baseUrl + 'ChatCollector.asmx/PopupOpened', { baseUrl: GetBaseUrl(), pageUrl: location.href },
    function(data) {
        dataSetId = 0;
        if (data.childNodes[1]) { //IE
            dataSetId = data.childNodes[1].childNodes[0].data;
        } else if (data.childNodes[0]) { //FF/Chrome
            dataSetId = data.childNodes[0].childNodes[0].data;
        }
        cookie = ('dataSetId=' + dataSetId);
        document.cookie = cookie;
    });
}
function GetBaseUrl(){
    card = 'http://www.racocard.com/racowebchat/';
    test = 'http://localhost:1674/';
    return card;
}
function showPopup(v, m) {
    name = $('#Name').val();
    company = $('#Company').val();

    if (v == true) {
        pageUrl = location.href;
        url = 'http://www.racocard.com/racowebchat/webchat.aspx?partyName=' + name + '&companyName=' + company + '&referrer=' + pageUrl;
        day = new Date();
        id = day.getTime();
        window.open(url, 'Popup', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=600,left = 240,top = 212', false);
    }
    
    //If we get this far, set the session cookie (lifecycle:  browser memory (will be cleared on browser restart)
    document.cookie = "RacoPopupDisplayed=true";

    //Log the choice
    dataSetId = GetCookie('dataSetId');
    $.get(baseUrl + 'ChatCollector.asmx/LogChoice', { choice: v, partyName: name, companyName: company, dataSetId: dataSetId} );
    return true;
}
function GetCookie(Name) {
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        // if cookie exists
        if (offset != -1) {
            offset += search.length
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset);
            // set index of end of cookie value
            if (end == -1) end = document.cookie.length;
            returnvalue = unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}