// IF AGENTS ARE AVAILABLE:
function agents_available() {
	try {
		document.getElementById('smartbutton').style.display='block';
		document.getElementById('smartbutton').innerHTML = '<a href="" onClick="s.pageName=\'pfc:other:Chat:Home\';s.events=\'\';s.t();window.open(\'https://admin.instantservice.com/links/5526/21088\', \'custclient\', \'width=600,height=160\'); return false;"><img SRC="https://a248.e.akamai.net/7/248/497/0001/www.proflowers.com/siteimages/InstatServiceChatButton.gif" border="0" width="95" height="23"></a>';
		return true;
	} catch (ex) {
		return false;
	}
}

// IF AGENTS ARE NOT AVAILABLE:
function agents_not_available() {
    document.getElementById('smartbutton').style.display='none';
    return true;
}

// THIS FUNCTION WILL FIND THE DIV TAG AND WRITE THE NEEDED JS INTO IT
enableLiveChat = function() {
    // get the advanceSmartButtonScript div tag which is included in UCHeader control.
	var scriptDiv = document.getElementById("advanceSmartButtonScript");
	if (scriptDiv != null)
	{
	    if (scriptDiv.style.display =='block')
	    {
            scriptDiv.innerHTML = '<img src="https://admin.instantservice.com/resources/smartbutton/5526/21088/available.gif?'+Math.floor(Math.random()*10001)+'" style="width:0;height:0;visibility:hidden;position:absolute;" onLoad="agents_available()" onError="agents_not_available()">';
	    }
	}
}

// following code attaches the enableLiveChat function to window onload event without 
// loosing any previous function assignments
var old_window_onload = (window.onload) ? window.onload :  function(){};
window.onload = function(){old_window_onload();enableLiveChat();}