/**
 *  ChatMain.js : 
 *	Contains all of the front-end code for the chat client.
 *	By Tristan Leonard, programmed originally for use on www.theblowmedown.com
 *	This code, or portions of it, are free to use in any project as long as some
 *	mention of the source is made. My explicit consent is not necessary, but if 
 *	you like you may let me know that my code is being utilized and, hopefully, 
 *	improved upon! My e-mail address is trisco2001@gmail.com.
 */

var xmlHttp;
var lastMessage;
var userName;
var emailAddress;


// The screen will attempt to be updated every X seconds.
setInterval(UpdateLayout, 1400);

// Invokes an update of the layout.
function UpdateLayout()
{		
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				alert("Your browser sucks!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange = CoversationLoaded;	
	xmlHttp.open("GET", "chat/getRecentConvo.php", true);
	xmlHttp.setRequestHeader("MessageCount", 10);
	xmlHttp.send(null);
}

// The conversation was loaded and is ready to populate
// only when the request's ready state is equal to 4.
function CoversationLoaded()
{
	if(xmlHttp == undefined)
	{
		UpdateLayout();
	}
	if (xmlHttp.readyState == 4)
	{
		var lblMessage = document.getElementById("conversation");
		lblMessage.innerHTML = xmlHttp.responseText;

/*		
		var typingMessage = "Someone is typing a message.";
		if (xmlHttp.responseText.indexOf("#SUM1ISTALKING#") != -1)
		{
			var lblTypeStatus = document.getElementById("typestatus");
			if (lblTypeStatus.innerHTML != typingMessage)
				lblTypeStatus.innerHTML = typingMessage;
		}
		else
		{
			var lblTypeStatus = document.getElementById("typestatus");
			
			lblTypeStatus.innerHTML = "";
		}
*/	
	}
}

function KeyPress_TextBox(e)
{
	var characterCode;

	// No idea what this is for; something about 
	// IE vs Mozilla differences.
	if(e && e.which){
		e = e;
		characterCode = e.which;
	}
	else{
		e = event;
		characterCode = e.keyCode;
	}
	
	// If the character passed is 13 (Carriage Return) 
	// or 10 (Enter), click the send button.
	if (characterCode == 13 || characterCode == 10)
	{
		Click_SendButton();
		return;
	}
		
	xmlHttp = new XMLHttpRequest();
	xmlHttp.onreadystatechange = function(){
		// Do Nothing
	};	
	xmlHttp.open("GET", "chat/logtype.php", true);
	xmlHttp.send(null);
}

function Click_SendButton()
{
	try
	{
		// This chat only works for Safari, Firefox, Opera 
		// and only the latest version of Internet Explorer. 
		// Go Microsoft. 
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// You could try to declare xmlHttp as a new ActiveX 
		// version of the control here, but meh.
		alert("This browser is certainly not an iPhone.");
	}
	
	// When the message gets posted, call postMessage. 
	// In Javascript, function pointers are super easy!
	xmlHttp.onreadystatechange = MessagePosted;	
	
	// We're going to open the postMessage.php file and run it.
	// POST isn't quite relevant until I figure out how to set 
	// POST variables from here. 
	xmlHttp.open("GET", "chat/postMessage.php", true);
	
	// Set Request Header will add an additional variable to the 
	// SERVER variable, and will take the format of 
	// $_SERVER['HTTP_NEWMESSAGE'] here.
	xmlHttp.setRequestHeader("NewMessage", "X" + lastMessage);
	xmlHttp.setRequestHeader("UserName", userName);
	xmlHttp.setRequestHeader("EmailAddress", emailAddress);
	
	// Sends off the request! Wheee!
	xmlHttp.send(null);
}

/// This method is invoked twice for step 1, then once for 2, 3, and 4 each.
function MessagePosted()
{
	// The request has changed its state, but it may not be finished.
	// Regardless, we can change the text box's value to empty string. 
	var txtMessage = document.getElementById("txtMessage");
	var txtEmail = document.getElementById("txtEmail");
	var txtName = document.getElementById("txtName");
	
	// Wait a minute... we set the lastMessage variable once we've posted?
	// There are two invokes of this method where the ready 
	// state is 1. One for the open, and one more after. The second one must
	// occur after the setRequestHeader, otherwise all messages would be "" 
	// because of what happened on the lines below this comment. 


	if (txtMessage.value != "[Message]")
		lastMessage = txtMessage.value;
	else
		lastMessage = "";


	if (txtEmail.value != "[E-Mail]")
		emailAddress = txtEmail.value;
	else
		emailAddress = "";
	if (txtName.value != "[Name]")
		userName = txtName.value;
	else
		userName = "";	

	txtMessage.value = "";
}

function Focus_NameBox()
{
	var txtName = document.getElementById("txtName");
	if (txtName.value == "[Name]")
		txtName.value = "";
	
	var txtEmail = document.getElementById("txtEmail");
	if (txtEmail.value == "")
		txtEmail.value = "[E-Mail]";
	
	var txtMessage = document.getElementById("txtMessage");
	if (txtMessage.value == "")
		txtMessage.value = "[Message]";
}

function Focus_EmailBox()
{
	var txtEmail = document.getElementById("txtEmail");
	if (txtEmail.value == "[E-Mail]")
		txtEmail.value = "";
	
	var txtName = document.getElementById("txtName");
	if (txtName.value == "")
		txtName.value = "[Name]";
	
	var txtMessage = document.getElementById("txtMessage");
	if (txtMessage.value == "")
		txtMessage.value = "[Message]";
}

function Focus_MessageBox()
{
	var txtName = document.getElementById("txtName");
	if (txtName.value == "")
		txtName.value = "[Name]";
		
	var txtEmail = document.getElementById("txtEmail");
	if (txtEmail.value == "")
		txtEmail.value = "[E-Mail]";
	
	var txtMessage = document.getElementById("txtMessage");
	if (txtMessage.value == "[Message]")
		txtMessage.value = "";
}

