﻿// ChatOnline - ver. 2.0
// Work with cookies
// Copyright(c) 2008 WaveAccess, http://wave-access.com

var sLiveHelpCookie = 'LiveHelpCookie';
var iMaxClicks = -1;
var sGuestJid = 'guest_4dd4acb855829f12d7a0c7';
var sJabberServer = '70.38.112.42';
var sGuestPassword = 'password';
var sContactFormURL = '';
var sRootURL = 'http://almworks.com/';
var sWindowParams = 'width=400,height=370,toolbar=0,left=300,top=400';

function clickCounter() {
	if (document.location.href.indexOf("gc.html") > 0)
		return;
	 renderButton(presence, jid);
	 if (iMaxClicks <= 0)
		return; 
	 var iCookie = readCookie(sLiveHelpCookie);
	 if (!iCookie) {
		iCookie = 0;
		setCookie(0);
	 }
	 if (iCookie && iCookie != '-1') {
		iCookie++;
		setCookie(iCookie);
	 }
	 if ((iCookie > iMaxClicks - 1) && presence == 'online') 
		openDIV();
}

function supportOnline() {
 openDIV("");
}
function renderButton(status, jid){
    if (jid != sGuestJid) 
        status='offline';
    var obj = document.getElementById("chatOnlineButtonDiv");
    var chatBtn = document.createElement('IMG');
    try{    
        chatBtn.id = 'chatBtn';    
        chatBtn.src = sRootURL +'GuideChat/images/' + status + '.png';
        if (status == 'online'){
            chatBtn.onclick = Chat;
            chatBtn.style.cssText = "cursor:pointer;";
        }
        else{            
                if (typeof(sContactFormURL) != 'undefined'){
                    if (sContactFormURL.length > 0){                                                
                        chatBtn.style.cssText = "cursor:pointer;";
                        chatBtn.onclick = openContactForm;
                    }
                    else
                        chatBtn.style.cssText = "cursor:default;";                        
                }
                else
                    chatBtn.style.cssText = "cursor:default;";                    
        }       
    obj.appendChild(chatBtn);
    }
    catch(err){}
}
function openContactForm()
{
    window.open(sContactFormURL,'new', sWindowParams);
}

function setSupportJID(){ 
 openDIV();
}
function openDIV() {
 try {
	if (readCookie(sLiveHelpCookie) != '-1') {
 	var chatDIV = document.createElement('DIV');
 	chatDIV.id = 'chatDIV';
 	chatDIV.style.cssText = 'border:1px solid #808080; background:#ffffff; padding:5px 7px 20px 20px; position:absolute; z-index:98854;top:0px;left:0px;width:300px;text-align:center';
 	chatDIV.innerHTML = '<img src="'+ sRootURL +'GuideChat/images/wa_close.gif" onClick="CloseChatDiv()" title="Close" style="cursor:pointer;float:right;margin:0 0 10px 0;clear:both"><div style="FONT:12px Arial, Helvetica, Tahoma, Verdana, sans-serif;text-align:left;padding-right:15px;margin-top:20px;">' + divMessage + '</div><div style="float:none;margin:20px auto 0 auto;width:78px;height:19px"><a onClick="Chat()" style="color:#ffffff;FONT:12px/18px Arial, Helvetica, Tahoma, Verdana, sans-serif;background:url('+ sRootURL +'GuideChat/images/btn_enter_chat.png);width:78px;height:19px;display:block;cursor:pointer">&nbsp;</a></div>';
 	document.body.appendChild(chatDIV);
 	document.getElementById('chatDIV').style.top = (screen.availHeight - document.getElementById('chatDIV').offsetHeight)/2 + "px";
	document.getElementById('chatDIV').style.left = (screen.availWidth - document.getElementById('chatDIV').offsetWidth)/2 + "px";
 	setCookie(-1);
 }
}
 catch (err) { }
}
function CloseChatDiv() {
 try {
	setCookie(-1);
	if (document.getElementById('chatDIV') != null)
	document.getElementById('chatDIV').style.display = "none";
 }
 catch (err) { }
}

function Chat() {
 try {
	 setCookie(-1);
	 CloseChatDiv();
	 window.open('GuideChat/gc.html', 'new', 'width=462,height=450,toolbar=0, left=300,top=400');
 }
 catch (err) { }
}
function readCookie(name) {
 try {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
}
 catch (err) { }
 return null;
}

function setCookie(number) {
 try {
 document.cookie = sLiveHelpCookie + '=' + number + ';path=/';
 }
 catch (err) { }
}

//This code checks if DOM is loaded (IE and FF only, all the rest work on window load).
///////////////////////////////////////////////////////////////////////
startStack = function() { }; // A stack of functions to run onload/domready
registerOnLoad = function(func) {
	var orgOnLoad = startStack;
	startStack = function() {
	orgOnLoad(); func();
	return;
	}
}
var ranOnload = false; // Flag to determine if we've ran the starting stack already.
if (document.addEventListener) { // Mozilla actually has a DOM READY event.
	document.addEventListener("DOMContentLoaded", function() { if (!ranOnload) { ranOnload = true; startStack(); } }, false);
}
else if (document.all && !window.opera) { // This is the IE style which exploits a property of the (standards defined) defer attribute
	document.write("<scr" + "ipt id='DOMReady' defer=true " + "src=//:><\/scr" + "ipt>");
	document.getElementById("DOMReady").onreadystatechange = function() {
if (this.readyState == "complete" && (!ranOnload)) 
	{ ranOnload = true; startStack(); }
}
}
var orgOnLoad = window.onload;
window.onload = function() {
if (typeof (orgOnLoad) == 'function') { orgOnLoad(); }
if (!ranOnload) { ranOnload = true; startStack(); }
}
///////////////////////////////////////////////////////////////////////

//setCookie(-1);
//clickCounter();
registerOnLoad(clickCounter);