window.onunload = function() {
	//window.location.href="http://localhost/ratage2/about.php";
}

var ShoutboxUsername = "";

function shoutboxInit() {

	var getUN = readCookie("ShoutboxUsername");
	var theC = readCookie("lastEnt");

	//alert(getUN && theC=="shoutbox");

	if(theC=="shoutbox") {
		if(getUN) {
			//alert("theC: " + theC);
			loadIntoSection("miniChat.php?un=" + getUN, "entContent", "Interactive Shoutbox");
			findNewMessages();
			ShoutboxUsername = getUN;
		} else {
			loadIntoSection("miniChat.php", "entContent", "Interactive Shoutbox");
		}
	}

	if(theC=="minesweeper") {
			loadIntoSection("minesweeper.php", "entContent", "Interactive Shoutbox");
			initt("minesweeperDiv");
	}

	if(!theC) {
		loadIntoSection("miniEntMenu.php", "entContent", "Entertainment Menu");
	}
}

function startShouting() {
	iun = document.getElementById("inputUsername");
	if(!iun) {
		alert("Shoutbox Error: Cant find element 'inputUsername' to get username");
	}

	if(iun.value && iun.value != "") {
		if(iun.value.length < 3) {
			alert("Your username must be at least 3 characters long");
			return;
		} else {
			ShoutboxUsername = iun.value;
			createCookie("ShoutboxUsername",ShoutboxUsername,0);
			loginWithName(ShoutboxUsername);
		}
	} else {
		alert("Please enter a username");
		return;
	}
}

function loginWithName(inName) {
	//alert("Found Username: " + inName);
	smf = document.getElementById("sendMessageForm");
	und = document.getElementById("usernameDiv");
	uns = document.getElementById("usernameSpan");

	smf.style.visibility="visible";
	und.style.visibility="visible";
	uns.innerHTML = inName;

	mwc = document.getElementById("messageWindowContainer");
	mwc.innerHTML = "<div id='messagesDiv'></div>";
	findNewMessages();
}






var sbHTTP = getHTTPObject();
var efups = 0;
function findNewMessages() {
	md = document.getElementById("messagesDiv");
	if(!md) {
		efups++;
		if(efups > 15) {
			return;
			//alert("too many errors: stopping shoutbox service!");		
		} else {
			//Trying again!
			setTimeout("findNewMessages();",2000);
		}
		return;
	}

	efups = 0;
	//alert("Chat thing workin!");

	url="getShoutboxMessages.php";
		sbHTTP.open("GET", url, true);
		sbHTTP.onreadystatechange = gotReply;
		sbHTTP.send(null);

	//md.innerHTML = "blah blah blah... and blah!";
}

var lastrt = ""
function gotReply() {
	if(sbHTTP.readyState==4) {
		rt = sbHTTP.responseText;
		//alert("Respone text: " + rt);
		md = document.getElementById("messagesDiv");


		if(rt.indexOf("<TITLE>404 Not Found</TITLE>") != -1) {
			md.innerHTML = "<b>Error conecting to server!</b>";
			return;
		}

		if(rt != lastrt) {
			md.innerHTML = rt;
			lastrt = rt;
		}
		//alert(rt);
		
		setTimeout("findNewMessages();",5000);
	}
}





var smHTTP = getHTTPObject();
function sendMessage() {
	messBox = document.getElementById("ShoutNewMessage");
	newMess = messBox.value;
	//alert('sending message');
		
	if(!ShoutboxUsername) {
		alert("Error: Could not find username. Please login agian.");
		sbLogout();
		return;
	}

	if(newMess) {
		var nmurl = "setShoutboxMessage.php?inusername=" + ShoutboxUsername + "&inmessage=" + newMess;
		//alert('sending message url:\n' + nmurl);
		smHTTP.open("GET", nmurl, true);
		smHTTP.onreadystatechange = sendMessageResult;
		smHTTP.send(null);
	}
}

function sendMessageResult() {
 if(smHTTP.readyState==4) {
	var smrt = smHTTP.responseText;
	//alert("Send Message Response: " + smrt);
	
	if(smrt.indexOf("good") == -1) {
		alert("Shoutbox Server Returned Error:\n " + smrt);
		if(smrt.indexOf("logout") != -1) {
			sbLogout();
		}
	} else {
		messBox = document.getElementById("ShoutNewMessage");
		messBox.value = "";
	}
 }
}



function sbLogout() {
	eraseCookie("ShoutboxUsername");
	loadIntoSection("miniChat.php", "entContent", "Interactive Shoutbox");
}