// JavaScript Document

var step = 1000;

var maxWait = 8000;

var minWait = 0;

var currentWait  = minWait;

var requestQueue = '';

function sendRequest3(){

	ajax.displayProgress = false;

	ajax.handleResponse = handleResponse3;

	ajax.request.push(query);

	ajax.sendRequest();

}

function handleResponse3() {

	if(ajax.requestObject.readyState == 4){

		ajax.iBusy = false;

		var response = ajax.requestObject.responseText;

		var update = new Array();

		if(response.indexOf('|' != -1)) {

			update = response.split('|');
			
			callFunction = update[0] + "(\"" + response + "\")";

			setTimeout(callFunction , 1);

		}

	}

}

function sendText(newText){

	if(document.getElementById('writeBox').value.length == 0 && newText == 13){

		return false;

	} else {
	
		var message = document.getElementById('writeBox').value;

		query = "../chat/index.php?action=sendText&message=" + message;
		
		document.getElementById('writeBox').value = '';

		remaining = 255;

		document.getElementById('remaining').style.color = '#BBBBBB';

		document.getElementById('remaining').innerHTML = "("+remaining+")";

		sendRequest3(query);
	
	}
	
}

function receivePollMessages(response){

	if(typeof response != undefined && response.length > 0 && response.indexOf('|' != -1)){

		var update = new Array();
	
		update = response.split('|');	
	
		if(update[1] == "update" || update[1] == "replace"){
	
			currentWait = 2000;

			step = 1000;

			var targetDiv = update[2];

			if(update[1] == "update"){
				
				document.getElementById(targetDiv).innerHTML = document.getElementById(targetDiv).innerHTML + update[3];

			}

			if(update[1] == "replace"){

				document.getElementById(targetDiv).innerHTML = update[3];

			}
		
			scrollDiv.activeScroll();
	
		}

	}
		
}

function startPollMessages(){

	var messagePolling = setInterval("pollMessages()" , 2000);

}

function pollMessages(){

	if(ajax.iBusy == false){
	
		query = '../chat/index.php?action=pollMessages';
	
		sendRequest3(query);	

	}

}

var remaining = 255;

function checkInput(newText){

	remaining = 255 - document.getElementById('writeBox').value.length;
	
	if(remaining < 1){
	
		document.getElementById('remaining').style.color = '#FF0000';
		
		document.getElementById('remaining').innerHTML = "(<strong style='text-decoration:blink;'>"+remaining+"</strong>)";

	} else {

		document.getElementById('remaining').innerHTML = "("+remaining+")";

	}	

	if(newText != 13 && document.getElementById('writeBox').value.length > 254){

		return false;

	} else {
		
		if(newText == 13){
			
			sendText(newText);

			return false;
			
		} else {

			return true;

		}

	}
	
}

function updateDiv(response){

	currentWait  = minWait;

	step = 500;
			
	var update = new Array();

	update = response.split('|');	
	
	var targetDiv = update[1];

	document.getElementById(update[1]).innerHTML = document.getElementById(update[1]).innerHTML + update[2];	

	scrollDiv.activeScroll();

}

function startPollingChats(){

	chatPolling = setInterval("pollChats()" , 5000);

}

function pollChats(){

	if(ajax.iBusy == false){
		
		query = '../chat/index.php?action=pollChats';

		sendRequest3(query);	

	}	
	
}

function chat_exists(response){
	
	var update = new Array();

	update = response.split("|");

	var answer = update[1];

	if(answer == "yes"){

		document.getElementById("chatRequestDiv").style.display = 'block';

	}

	if(answer == "no") {

		document.getElementById("chatRequestDiv").style.display = 'none';

	}

}

function openChat(chatID){

	query = '../chat/index.php?action=openChat&chatID=' + chatID;

	window.location = (query);

}

var chatWindow = null;

function acceptChat(){

	if(chatWindow != null){
	
		chatWindow.close();

	}

	var urlStr = "../chat/index.php?action=acceptChat";
	
	var optionsStr = "scrollbars," + "resizable,height=" + 425 + ",width=" + 400;

	chatWindow = window.open(urlStr, 'chatWindow' , optionsStr);;
	
	if(chatWindow != null){
		
		document.getElementById("chatRequestDiv").style.display = 'none';

		chatWindow.focus();
		
	} else {
	
		alert("Your browser is blocking the chat window. Please allow popups from this website then click on OK to proceed.");
	
	}

}

function ignoreChat(done){

	if(typeof done != undefined){

		return false;

	}

	if(ajax.iBusy){

		setTimeout("ignoreChat()" , 3000);

	} else {

		document.getElementById("chatRequestDiv").style.display = 'none';
	
		sendRequest3("../index.php?action=ignoreChat");

	}

}