var intCommentTimeout = null;
var intListTimeout = null;

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
function launchChat() {
	var rand = parseInt(Math.random()*999999999999999);

	return window.open ("/custom/chat/chat-popup.php?rand=" + rand, "chat", "resizable=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,width=530,height=300");		
}

function getcomment(pass) {
	if (pass == 1) {
		getInfo('getcomment', 'chat','pass=1&chat_id='+conChatID,conLoc+'getcomment.php','aftergetcomment();');
	} else {
		getInfo('getcomment', 'chat','chat_id='+conChatID,conLoc+'getcomment.php','aftergetcomment();');
	}	
}

var prevCommentCount = 0;
function aftergetcomment() {
	var rowCount = document.getElementById("rowCount");
	if (rowCount != null)
	{
		if (prevCommentCount != rowCount.value)
		{
			if (window.focus) 
				window.focus();
		}
			
		prevCommentCount = rowCount.value;
	}
	else
		prevCommentCount = 0;

	//if (blnStatus) {
		intCommentTimeout = setTimeout('getcomment()',4000);
	//}
}

function checkEnterKey(e) {
	var key;
	
	if (window.event) {
		key = window.event.keyCode; //IE
	} else {
		key = e.which; //FIREFOX
	}

	if (key == 13) {
		addcomment();
		return false;
	}
}

function addcomment() {
	var strName = document.form.name.value;
	var strCompany = document.form.company.value;
	var strComment = document.form.comment.value;
	
	strComment = trim(strComment);
	if (strComment.length > 0) {
		_addcomment(conChatID, strName, strCompany, strComment);
		document.form.comment.value = '';	//UNIQUE - THIS DOESN'T WORK
	}
}

function _addcomment(chat_id, name, company, comment) {
	var strTemp = 'chat_id='+chat_id;
	strTemp += '&name='+escape(name);
	strTemp += '&company='+escape(company);
	strTemp += '&comment='+escape(comment);

	getInfo('addcomment','nowhere',strTemp,conLoc+'addcomment.php');
}

function cleanup(element) {
	switch(element) {
		case 'chat':
			DIVtoBottom();
			break;
	}
}

function DIVtoBottom() {
	var objDiv = document.getElementById("box");
	objDiv.scrollTop = objDiv.scrollHeight;
}


function debug_stop() {
	blnStatus = false;
}

function debug_start() {
	blnStatus = true;
}

//-------------ADMIN----------------------
function getlist() {
	getInfo('getlist', 'chatlist','',conLoc+'getlist.php','aftergetlist();');

	intListTimeout = setTimeout('getlist()',3000);
}

var prevListCount = 0;
function aftergetlist() {
	var rowCount = document.getElementById("rowCount");
	var divRowCount = document.getElementById("divRowCount");
	if (rowCount != null)
	{
		if (divRowCount != null)
			divRowCount.innerHTML = "<b>Total number of users: " + eval(rowCount.value) + "</b>";

		//if there are more rows now then there were before
		if (prevListCount < rowCount.value)
		{
			if (window.focus) 
			{
				alert("a new chat has been started");
				this.window.focus();			
			}
		}
		else
		{
			//rows are the same or less - do nothing
		}

		prevListCount = rowCount.value;
	}
	else
		prevListCount = 0;
}


function popwin(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=250,height=400,left = 860,top = 400');");

//width=530,height=300
}

function joinchat(talkto,id) {
//	popwin(conLoc+'admin-chat.php?talkto='+escape(talkto)+'&name='+document.form.name.value+'&chatid='+id);

	var url = conLoc+'admin-chat.php?talkto='+escape(talkto)+'&name='+document.form.name.value+'&chatid='+id;
	var wdow = window.open(url,id, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=250,height=400,left = 860,top = 400');
	if (window.focus) {wdow.focus();}
}