

var NS = (document.layers) ? true : false;
var IE = (document.all) ? true : false;
var DOM = (document.getElementById) ? true : false;
if (IE) DOM = false;
var MAC = (navigator.platform) && (navigator.platform.toUpperCase().indexOf("MAC") >= 0);
if (NS) MAC = false;

var commandImage;
var sendImage = true;
var checkImgTimer = null;

var checkChatAvailabilityStartTimer = null;

// Call this function only ONCE and it checks for availability every interval
function startCheckChatAvailabilityLoop()
{
	ctcAvailabilityUrlFull = ctcAvailabilityUrl + ctcAvailabilityUrlQS;
	if( checkChatAvailabilityStartTimer == null ){
		checkChatAvailability( ctcAvailabilityUrlFull );
		checkChatAvailabilityStartTimer = setInterval( "checkChatAvailability( '"+ctcAvailabilityUrlFull+"')", ctcAvailabilityInterval );
	}
}

// Call this function only ONCE and it stops checking for chat availability
function stopCheckChatAvailabilityLoop()
{
	if( checkChatAvailabilityStartTimer != null ){
		clearInterval( checkChatAvailabilityStartTimer );
		checkChatAvailabilityStartTimer = null;
	}
}

// Call this function when ever you want to check if chat is available or not
function checkChatAvailability( url )
{

	if ( sendImage )
	{
		sendRequest( url );
		checkImgTimer = setInterval("checkImage()", 1000);
	}
}

function sendRequest( imgUrl )
{

	if (DOM && IE) {
			document.body.removeChild(commandImage);
			commandImage = document.createElement('IMG');
			commandImage.style.visibility = "hidden";
			document.body.appendChild(commandImage);
	}else{
		commandImage= new Image;
	}
	var pageID = Math.round(Math.random()*9999999999);
	imgUrl = imgUrl + "&id=" + pageID;

	commandImage.src = imgUrl;

	sendImage = false;
}

function checkImage()
{
	if (!sendImage)
	{
		if (!commandImage.complete)
		{
			return;
		}
		var w = commandImage.width;
		var h = commandImage.height;
		sendImage = true;
		if( checkImgTimer!=null )
		{
			clearInterval( checkImgTimer );
			checkImgTimer = null;
		}
		checkChatAvailabilityResult(w,h);
	}
}

function checkChatAvailabilityResult( w, h )
{
	if( w=="1" && h=="1" )
	{ // Chat Available
		ctcChatAvailable();
	}
	else
	{ // Chat not available
		ctcChatUnavailable();
	}
}

var ctcChatWindow = null;

function openChatWindow( chatType, width, height, blankURL, aimsChatURL )
{
	if( ctcChatWindow!=null && ctcChatWindow.closed==false ){
		ctcChatWindow.focus();
	}else{
		var chatWindowName = 'ctcChatWindow';
		var chatWindowFeatures = 'left=30,top=10,width='+width+',height='+height;
		ctcChatWindow = window.open( blankURL, chatWindowName, chatWindowFeatures);
		document.forms["CTCCHATFORM"].strChatData.value = ctcChatXML;
		document.forms["CTCCHATFORM"].action = aimsChatURL;
		document.forms["CTCCHATFORM"].target = chatWindowName;
		document.forms["CTCCHATFORM"].method = "POST"
		document.forms["CTCCHATFORM"].submit();
	}
}
