// PlayItOnTheWeb Ajax Functions
// Copyright 2007-2008 PlayItOnTheWeb.com

function ajaxEncode(array){
	var r=new String();
	for (var i=0;i<array.length;i++){
		var command=array[i];
		if (r.length>0) r+='|';
		for (var j=0;j<command.length;j++){
			if (j>0) r+='_';
			r+=command[j];
		}
	}
	window.status=theStatus;
	return r;
}
function ajaxEnqueue(command,p1){
	commArray[commArray.length]=new Array(ajaxIdx++,command,p1);
	if (ClientDebug) theDoc.f_ajax.ajaxqueue.value=ajaxEncode(commArray);
	window.status=theStatus;
}
function ajaxDequeue(commandIdx,result){
	var newArray=new Array();
	var command;
	commandIdx=new String(commandIdx);
	for (var i=0;i<commArray.length;i++){
		if (commArray[i][0]!=commandIdx) newArray[newArray.length]=commArray[i];
		else command=commArray[i][1];
	}
	commArray=newArray;
	if (ClientDebug) theDoc.f_ajax.ajaxqueue.value=ajaxEncode(commArray);
	window.status=theStatus;
	return command;
}
function ajaxProcess(){
	if (req.readyState==4){
		if (ClientDebug) theDoc.f_ajax.ajax1.value="{"+req.status+"}{"+req.responseText+"}";
		if (req.status==200){
			if (ClientDebug) theDoc.f_ajax.ajax1.value="["+req.responseText+"]";
			chat=new Array();
			var resultArray=req.responseText.split('|');
			for (var i=0;i<resultArray.length;i++){
				var result=resultArray[i].split('_');
				if (result[0]=='S'){
					chat[chat.length]=new Array(result[1],result[2],result[3],result[4]);
					needsUpdate++;
				} else if (result[0]=='X'){
					PlayersInRoom='';
					for (var j=1;j<result.length;j++){
						if (result[j].substring(0,5)=='Guest'){
							PlayersInRoom+=result[j]+'<br />';
						} else {
							PlayersInRoom+='<a href="/profile/'+result[j]+'">'+result[j]+'</a><br />';
						}
					}
					theDoc.getElementById("thisroom").innerHTML = PlayersInRoom;
				} else {
					var command=ajaxDequeue(result[0]);
					if (command=='rate'){
						if (result[1]=='1'){
							theDoc.getElementById("ratewrap").innerHTML = "<b>Success!</b> Thanks for rating this game!";
							theDoc.getElementById("ratecash").innerHTML = theDoc.getElementById("icotick").innerHTML;
						} else {
							theDoc.getElementById("ratewrap").innerHTML = result[1];
						}
					} else if (command=='email'){
						if (result[1]=='1'){
							theDoc.getElementById("email2").innerHTML = "<b>Success!</b> E-mail has been sent.<br /><center><a href=\"javascript:void(0);\" onclick=\"ajaxEmailReset();\">Send another</a></center>";
							theDoc.getElementById("emailcash").innerHTML = theDoc.getElementById("icotick").innerHTML;
						} else {
							theDoc.getElementById("email2").innerHTML = result[1] + "<br /><center><a href=\"javascript:void(0);\" onclick=\"ajaxEmailReset();\">Try again</a></center>";
						}
					} else if (command=='flag'){
						if (result[1]=='1'){
							theDoc.getElementById("flag2").innerHTML = "<b>Success!</b> Report has been sent for this game.";
						} else {
							theDoc.getElementById("flag2").innerHTML = result[1] + "<br /><center><a href=\"javascript:void(0);\" onclick=\"ajaxFlagReset();\">Try again</a></center>";
						}
					} else if (command=='fav'){
						if (result[1]=='1'){
							theDoc.getElementById("mark2").innerHTML = "<b>Success!</b> Your favorites list has been updated.";
						} else {
							theDoc.getElementById("mark2").innerHTML = result[1] + "<br /><center><a href=\"javascript:void(0);\" onclick=\"ajaxMarkReset();\">Try again</a></center>";
						}
					} else {
						//needsRefresh=true;
						//poptip(result[1],'white',400);
						//forcetip();
						//if (document.all) location.href='/index.asp';
						//else window.content.location.href='/index.asp';
					}
				}
			}
			ajaxLastRun=new Date();
			ajaxRunning=false;
			if (theDoc.getElementById("ajaxproc")) theDoc.getElementById("ajaxproc").style.visibility="hidden";
			if (ClientDebug) theDoc.f_ajax.ajaxstatus.value="[ajaxRunning:"+ajaxRunning+", checkingAjax:false]";
			theStatus='Done';
		}
	}
	window.status=theStatus;
}
function ajaxRun(){
	ajaxRunning=true;
	if (theDoc.getElementById("ajaxproc")) theDoc.getElementById("ajaxproc").style.visibility="visible";
	if (ClientDebug) theDoc.f_ajax.ajaxstatus.value="[ajaxRunning:"+ajaxRunning+", checkingAjax:false]";
	if (commArray.length>0) theStatus='Synchronizing with server...'; else theWindow.status='Done';
	if (commAlways.length>0) commArray[commArray.length]=new Array(ajaxIdx++,commAlways,'');
	if (document.all) req=new ActiveXObject("Microsoft.XMLHTTP");
	else req=new XMLHttpRequest();
	req.onreadystatechange=ajaxProcess;
	req.open('POST','http://'+location.host+'/ajax.asp',true);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	req.send('c='+escape(ajaxEncode(commArray)));
	window.status=theStatus;
}
function init(){
	commArray=new Array();
	ajaxIdx=0;
	needsRefresh=false;

	theDoc=document; theWindow=window;
	stopAjax=false;
	needsUpdate=1;

	ajaxRunning=false;
	ajaxFirstRun=new Date();
	ajaxLastRun=new Date();
	commAlways='R_'+gamename;
	theStatus='Done';
	window.status=theStatus;
	//alert(typeof(theDoc.getElementById("anchorchat")));
	showJoins=true;
}
function run(){
	if (needsUpdate>0){
		ajaxUpdate();
	} else {
		var d=new Date();
		var delay=5000;
		if (theDoc.getElementById("anchorchat")){
			if (theDoc.getElementById("anchorchat").className!="selected") delay=60000;
		}
		if (!ajaxRunning && (commArray.length>0 || (d-ajaxLastRun>delay))) ajaxRun();
		else if (ClientDebug) theDoc.f_ajax.ajaxstatus.value="[ajaxRunning:"+ajaxRunning+", checkingAjax:true]";
	}
	if (!stopAjax) window.setTimeout('run()',100);
	window.status=theStatus;
}
function ajaxUpdate(){
	needsUpdate=0;

	var s='<table cellpadding="0" cellspacing="0"><tr><td></td><td style="font:9px Arial;color:#0f0;">Joining chat...  connected.  Welcome to PIOTW Chat.</td></tr>';
	for (var i=0;i<chat.length;i++){
		if (showJoins || chat[i][3].substr(0,5)!=ChatEventCode+''){
			var user=chat[i][1];
			if (user=='lucas' || user=='allen') user='<span style="color:#0f0;">'+user+'</span>';
			if (chat[i][2]=='Lobby') var room='the '+chat[i][2];
			else if (chat[i][2]=='private') var room='a private game';
			else var room='<a style="font:9px Arial;" href="http://www.playitontheweb.com/games/'+flatten_name(chat[i][2])+'-game.htm">'+chat[i][2]+'</a>';
			if (chat[i][3]==ChatEventCode+'JOIN') var msg=user+' has joined '+room+'.';
			else if (chat[i][3]==ChatEventCode+'LEAVE') var msg=user+' has left '+room+'.';
			else var msg=user+' says, "'+filter_badwords(chat[i][3])+'"';
			s+='<tr valign="top"><td style="font:9px Arial;width:80px;" nowrap="yes">'+chat[i][0]+'</td>';
			s+='<td style="font:9px Arial;width:300px;">'+msg+'</td>';
			if (chat[i][3].substr(0,5)!=ChatEventCode){
				if (room!='the Lobby') room='playing '+room; else room='in '+room;
				s+='<td style="font:9px Arial;width:180px;padding-left:10px;">'+room+'</td></tr>';
			}
		}
	}
	s+='</table>';
	var chatdiv=theDoc.getElementById('chatdiv');
	chatdiv.innerHTML=s;
	chatdiv.scrollTop=chatdiv.scrollHeight;
	window.status=theStatus;
}
function flatten_name(s){
	var r='';
	for (var i=0;i<s.length;i++){
		if (s.substr(i,1)==' ') r+='-';
		else if (s.substr(i,1)=='#') r+='|';
		else r+=s.substr(i,1);
	}
	return r;
}
function filter_badwords(s){
	var badWords=["fuck","cunt","cock","pussy","faggot","shit","damn","bitch","twat","cum","slut"];
	var r=s;
	var regexp1;
	for (var i=0;i<badWords.length;i++){
		regexp1 = new RegExp(badWords[i]);
		var r=r.replace(regexp1,repeatstr("*",badWords[i].length));
	}
	return r;
}
function repeatstr(str,cnt) {
	return (new Array(cnt + 1)).join(str);
}
function chat_keyup(e){
	var keynum;
	if(window.event) keynum = e.keyCode;	// IE
	else if(e.which) keynum = e.which;		// Netscape/Firefox/Opera
	if (keynum==13) chat_send();
}
function chat_send(){
	if (document.f_chat.msg.value.length>0){
		ajaxEnqueue('say',gamename+'_'+document.f_chat.msg.value);
		document.f_chat.msg.value='';
	}
}
var ClientDebug,commArray,commAlways,ajaxIdx,needsRefresh;
var theDoc,theStatus,needsUpdate;
var req,ajaxRunning,ajaxFirstRun,ajaxLastRun,ajaxInterval,stopAjax;
var PlayersInRoom,OtherRooms;
var showJoins;
