var refcode = "TOTALGARDENS";
var cookieref = "VIS-CHAT-COOK";
var our_cookie_val;
var image_handle;
var chat_img_handle;

///////////////////////////
// Declare Our Functions //
function changeImageSource() {
	
var d = new Date();
	image_handle.src = 'http://livehelp.taynagroup.co.uk/act.php?cookie=' + our_cookie_val + '&refcode=' + refcode + '&currpage=' + urlencode(document.location) + '&referer=' + urlencode(document.referrer) + '&timeofrequest=' + d.getTime();
	setTimeout('actOnImageWidth()'   , 2000);
	setTimeout('changeImageSource()', 30000);
}
///////////////////////////
function findCookie(key) {
    var s = document.cookie;
    var allCookies = s.split("; ");
    for (i = 0; i < allCookies.length; i++) {
        var keyval = allCookies[i].split("=");
        if (keyval != null && keyval.length == 2 && key == keyval[0])
            return keyval[1];
    }
    return "";
}
///////////////////////////
function checkValueOfFoundCookieOrCreateIfReqd( passed_cookie_val ) {
	if ( passed_cookie_val == '' ) {
		// either we dont have a cookie or it was not successfully retrieved.
		var our_rand_no = Math.round(Math.random()*9999999999);
		var expiry = 120 * 1000 * 60;
		var today = new Date();
		today.setTime( today.getTime() );
		expiry = new Date( today.getTime() + (expiry) );
		setACookie( cookieref, our_rand_no, expiry ); // 120 mins is our session...
		// now we have set cookie, lets try to retrieve it...
		if ( findCookie(cookieref) != '' ) {
			return our_rand_no;
		} else {
			return '';
		}
	}
	//if we got here, cookie is ok
	return passed_cookie_val;
}
//
function setACookie(name, value, expiry) {
    var cookieSet = name + "=" + value + ";path=/" + (expiry == null ? "" : (";expires=" + expiry.toGMTString()));
    document.cookie = cookieSet;
}
//
function actOnImageWidth( ) {
	if ( image_handle.width == 2 || image_handle.width == 3 ) {
		// operator online... show chat image
		chat_img_handle.style.display = '';
		if ( image_handle.width == 3 ) {
			sendChatInvite();
		}
		return;
	}
	// operator unavailable. hide chat image
	chat_img_handle.style.display = 'none';
}
//
function openChatWindow() {
	popup = window.open('http://livehelp.taynagroup.co.uk/chatrequest.php?cookie=' + our_cookie_val,'OnlineHelp','width=484,height=361,scrollbars=no,status=no,toolbar=no,location=no,menubar=no,directories=no,resizable=no');
	popup.focus();
}
//
function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}
//
function sendChatInvite() {
	if ( confirm("Would you like to chat with an operator?") ) {
		openChatWindow();
	}		
}
// End Declare Our Functions //
///////////////////////////////

// create the chat image
document.writeln( "<a href='javascript:openChatWindow()'><img id='chatter_img' src='http://livehelp.taynagroup.co.uk/chat.gif' style='display: none;' border='0' /></a>" );
chat_img_handle = document.getElementById("chatter_img");

// had to functionise as IE threw a wobbly!
// call via onload
function startChat() {

	// create the hidden control image
	image_handle = document.createElement('IMG');
	image_handle.id = 'handle_image';
	image_handle.style.visibility = "hidden";
	document.body.appendChild(image_handle);
	
	// try to get the cookie
	our_cookie_val = findCookie( cookieref );
	
	if ( our_cookie_val == '' ) {
		// try to create
		our_cookie_val = checkValueOfFoundCookieOrCreateIfReqd( our_cookie_val );
		// check creation
		our_cookie_val = findCookie( cookieref );
	}
	
	// set a cookie if not exists
	if ( our_cookie_val != '' ) {
		// start ball rolling
		changeImageSource();
	}
	
}