﻿// Live Chat Scripts.  Image and URL swapping based on availability image provided by InstantService.
// Update cache and database with callback to WebService.
// P. Rutins 2007

function loadLiveChatService(obj) {
    if (typeof(LiveChatService) == 'undefined') {
        var s = document.createElement('script');
        var h = document.getElementsByTagName('head').item(0);
        s.type = 'text/javascript';
        //if (document.implementation.hasFeature("HTMLEvents", "2.0")) { // Mozilla
        //    s.addEventListener("load", function(){ alert('Event Listener Fired'); alert(typeof(LiveChatService)); }, false);
        //} else {
        s.onreadystatechange = function() { // IE/Safari
            //alert(this.readyState);
            if (this.readyState === 'loaded') {
                obj.callbackService(); //alert(typeof(LiveChatService));
            }};
        s.onload = function() { 
            obj.callbackService(); // alert(typeof(LiveChatService)); 
            };
        s.src = '/services/LiveChatService.asmx/js';
        h.appendChild(s);
    } else {
        obj.callbackService();
    }
}

/*******************************************/

/* LiveChatDepartment object - contains all the info necessary to send availability back to the server, if necessary. */
var LiveChatDepartment = function(ctrlID, AI, DI, Department, Path, imageOn, imageOff, available) {
    this.controlID = ctrlID;
    this.ai = (AI || '5657');
    this.di = (DI || '10642');
    this.department = (Department || 'default');
    this.path = (Path || 'admin.instantservice.com/resources/smartbutton/5657/10642');
    this.available = (available || false);
    this.initialize();
};

/* use an in-page callback. Not currently used, but works. */
function ReceivePartialPageData(rValue) 
{ var ret = rValue.split('|');
    if (ret.length > 1) {
        var c = $(ret[1]);
        if (c) { c.innerHTML = ret[2]; }  
    } else { ProcessError(ret[0]); }};
function ReceiveServiceData(rValue)
{
    //alert(rValue);
};
function ProcessError(errText)
{   //alert(errText);
    // do nothing with it. 
};
LiveChatDepartment.prototype.callbackPage = function() {
    // this callback uses the prototype library's Ajax.Request instead of all the MS bloat libraries.    
    // it does require a runat="server" form on the page.  This function is not typically used, use the callbackService instead.
    callMeBack((this.available?'available':'unavailable'), this.controlID, ReceiveServiceData, ProcessError);
};

/* use a callback to the LiveChatService.  Once required a scriptmanager on the page, no longer.
   <asp:ScriptManager runat="server"><Services><asp:ServiceReference path="../services/LiveChatService.asmx"></asp:ServiceReference></Services></asp:ScriptManager> 
   We can get away without using a ScriptManager, but if we want to use this we still need the basic callback library from MS. (http://www.orvis.com/LiveChatService/js).  This is only required if we need to get object definitions from the web service.  In this case, the call is simple enough that we can build the JSON object by hand to pass it to the service.
*/
LiveChatDepartment.prototype.callbackService = function() {
    // this callback calls the LiveChatService to notify the application of the availability status of the chat department.
    // LiveChatService.SetAvailability(ControlID, Department, AI, DI, Available, SaveToDB, linkImageOn, linkImageOff, onSuccess, onError);
    // LiveChatService.SetAvailability(this.controlID, this.department, this.ai, this.di, this.available, false, '', '');
    callSOAPService('/services/LiveChatService.asmx', 'http://www.orvis.com/LiveChatService', 'SetAvailability', { ControlID: this.controlID, Department: this.department, AI: this.ai, DI: this.di, Available: this.available, SaveToDB: false, LinkImageOn: '', LinkImageOff: '' }, ReceiveServiceData, ProcessError);
    // We don't really care about the returned value, since we've already done the display changes needed.
    // If we do, we can pass in an onSuccess function and an onError function.
};

/* department detects its availability by looking for available.gif */
LiveChatDepartment.prototype.detectAvailability = function() {
    var i = new Image();
    i.chatDept = this;
    i.onload = function(type, target, width, height) { 
        // show the Available button
        this.chatDept.show();
        this.chatDept.available = true;
        this.chatDept.callbackService();
        //loadLiveChatService(this.chatDept);
        } ; 
    i.onerror = function(type, target, width, height) { 
        this.chatDept.hide();
        this.chatDept.available = false;
        this.chatDept.callbackService();
        //loadLiveChatService(this.chatDept);
        } ;
    i.src = 'https://' + this.path + '/available.gif';
};

LiveChatDepartment.prototype.show = function() {
    var a = $(this.controlID + '_InstantServiceAvailable');
    a.style.display = 'inline';
    var u = $(this.controlID + '_InstantServiceUnavailable');
    u.style.display = 'none'; 
    var i = $(this.controlID + '_chat_available');
    i.value = 'true';       
};

LiveChatDepartment.prototype.hide = function() {
    var a = $(this.controlID + '_InstantServiceAvailable');
    a.style.display = 'none';
    var u = $(this.controlID + '_InstantServiceUnavailable');
    u.style.display = 'inline';        
    var i = $(this.controlID + '_chat_available');
    i.value = 'false';       
};

LiveChatDepartment.prototype.initialize = function() {
    Event.observe($(this.controlID + '_LiveChat'), "chat:detect", this.detectAvailability.bindAsEventListener(this));
    window.setTimeout('$("' + this.controlID + '_LiveChat").fire("chat:detect");', 1000);
};

/*******************************************/


// For smartbuttons that cannot use the .NET control. Load in content to the smartbutton div
function showSmartButton(useContent) 
{  if (useContent) {
    useContent.style.display = 'inline';
    return true;
  }}

// For smartbuttons that cannot use the .NET control. Detect using the smartbutton image -- based on code provided by InstantService 5/2/07
// https://admin.instantservice.com/help/setup/advancedsmartbutton_help.html
function InstantServiceSmartButton1(ai, di, department)
{
    // requires divs named '_InstantServiceAvailable' instead of 'InstantServiceAvailable'.
    new LiveChatDepartment('', ai, di, department, 'admin.instantservice.com/resources/smartbutton', '', '', false);
};

function InstantServiceSmartButton(ai, di) 
{   var a = document.getElementById('InstantServiceAvailable');
    a.style.display = 'none';
    var u = document.getElementById('InstantServiceUnavailable');
    u.style.display = 'none';
    var i = new Image(); 
    i.onload = function() { showSmartButton(a); } ; 
    i.onerror = function() { showSmartButton(u); } ;
    //alert('http://' + path + '/available.gif');
    i.src = 'http://admin.instantservice.com/resources/smartbutton/' + ai + '/' + di + '/available.gif?' + Math.floor(Math.random()*10001); }


var ChatWindow = null;
function ShowChatWindow(height, width, url) 
{ 
    ChatWindow = window.open(url + escape(document.location), 'custclient', 'scrollbars=0,left=1, top=1,width=' + width + ',height=' + height);
	FocusChat(); 
}  

function FocusChat() 
{ 
    if (ChatWindow != null) { 
        ChatWindow.focus(); 
    } 
}  

function ShowWindow(height, width, url) 
{ 
    var nw = window.open(url, 'OrvisHelpWindow_', 'toolbar=no,location=0,directories=no,status=no,menubar=0,scrollbars=yes,resizable=1,copyhistory=0,left=1,top=1,width=' + width + ',height=' + height);     if (nw) { nw.focus(); } 
} ;

/* -- use the following HTML code in a subject page for a smartbutton that doesn't use the control.
<!-- <dd>For even faster response, try our Live Chat.</dd> -->
<script language="JAVASCRIPT" type="text/javascript" src="/ClientSideScripts/Prototype.js"></script>
<script language="JAVASCRIPT" type="text/javascript" src="/ClientSideScripts/LiveChat.js"></script>
<div id="InstantServiceAvailable" style="display:none;">
	<b>For even faster response, try our Live Chat!</b>
	<div class="dd">Our Orvis Customer Service Team can provide immediate online assistance for questions relating to placing orders, Orvis products, and website navigation, just to name a few.  <b><a onclick="ShowChatWindow(320, 500, '/store/instant_service_chat.aspx?ai=5657&amp;di=10642'); return false;" href="">Orvis Live Chat</a></b> is available everyday, 8 a.m. through 11 p.m. ET.
	</div>
</div>
<div id="InstantServiceUnavailable" style="display:none;">
	<b>For even faster response, try our Live Chat!</b>
	<div class="dd">Our Orvis Customer Service Team can provide immediate online assistance for questions relating to placing orders, Orvis products, and website navigation, just to name a few.  <b><a onclick="ShowWindow(550, 750, '/intro_newwindow.asp?subject=1308'); return false;" href="">Orvis Live Chat</a></b> is available everyday, 8 a.m. through 11 p.m. ET.</div>				
</div>
<script language="javascript" type="text/javascript">
	InstantServiceSmartButton('5657','10642');
</script>
*/

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();