/*********************************
Detective Del Spooner: Oh hell no.
*********************************/




var humanName = "Human", aiName = "Bytten";

function sendText() {
	var message = document.getElementById("message");
	
	var chatLog = document.getElementById("chat-log").getElementsByTagName("table")[0];
	
	// create rows in the chatlog table
	var rowCount = chatLog.rows.length;
	var row = chatLog.insertRow(rowCount);
	row.className = "human-message";
	var cell1 = row.insertCell(0);
	cell1.innerHTML = humanName.replace(/</, "&lt;");
	var cell2 = row.insertCell(1);
	cell2.innerHTML = ": "+message.value.replace(/</, "&lt;");
	row = chatLog.insertRow(rowCount+1);
	row.className = "ai-message";
	cell1 = row.insertCell(0);
	cell1.innerHTML = aiName;
	cell2 = row.insertCell(1);
	cell2.innerHTML = "is typing... <a id=\"msg"+rowCount+"\"></a>";

	// scroll to it
	scrollChatTo("msg"+rowCount);

	// send AJAXY request
	var request = new XMLHttpRequest();
	request.open("POST", "/bytten/bot.php", true);
	request.setRequestHeader("Content-type",
		"application/x-www-form-urlencoded");
	request.onreadystatechange = function() {
		if (request.readyState == 4 && request.status == 200) {
			try {
				processResponse(request.responseXML, rowCount+1);
			} catch (e) {
				error();
				alert(request.responseText);
			}
		}
	}
	var vars = "message="+escape(message.value);
	request.send(vars);
	
	message.value = "";
}

function error() {
	// Get the info from the XML response
	var human = response.getElementsByTagName("human")[0];
	var humanMessage = human.childNodes[0].nodeValue;
	humanName = human.attributes.getNamedItem("name").value;
	var ai = response.getElementsByTagName("response")[0];
	var aiMessage = ai.childNodes[0].nodeValue;
	aiName = ai.attributes.getNamedItem("name").value;
	var chatLog = document.getElementById("chat-log").getElementsByTagName("table")[0];
	
	// create rows in the chatlog table
 	var rowCount = chatLog.rows.length;
	var row = chatLog.rows[rowNo];
	row.deleteCell(1);
	var cell2 = row.insertCell(1);
	cell2.innerHTML = " was unable to respond due to an error.<a id=\"msg"+rowCount+"\"></a>";
	
	// scroll to it
	scrollChatTo("msg"+rowCount);
}

function processResponse(response, rowNo) {
	// Get the info from the XML response
	var human = response.getElementsByTagName("human")[0];
	var humanMessage = human.childNodes[0].nodeValue;
	humanName = human.attributes.getNamedItem("name").value;
	var ai = response.getElementsByTagName("response")[0];
	var aiMessage = ai.childNodes[0].nodeValue;
	aiName = ai.attributes.getNamedItem("name").value;
	var chatLog = document.getElementById("chat-log").getElementsByTagName("table")[0];
	
	// create rows in the chatlog table
 	var rowCount = chatLog.rows.length;
	var row = chatLog.rows[rowNo];
	row.deleteCell(1);
	var cell2 = row.insertCell(1);
	cell2.innerHTML = ": "+aiMessage+"<a id=\"msg"+rowCount+"\"></a>";
	
	// scroll to it
	scrollChatTo("msg"+rowCount);
}

function docLoaded() {
	scrollChatToBottom();
	document.getElementById("message").focus();
}

function scrollChatToBottom() {
	scrollChatTo("chat-bottom");
}

function scrollChatTo(id) {
	var top = document.getElementById(id).parentNode.parentNode.offsetTop;
	document.getElementById("chat-log").scrollTop = top;
}
