var xmlHttp	= createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
	var xmlHttp;
	
	try
	{
		xmlHttp	= new XMLHttpRequest();
	}
	catch(e)
	{
		xmlHttp	= false;
	}
	
	if(!xmlHttp)
	{
		alert("error creating XMLHttpRequest object");
	}
	else
	{
		return xmlHttp;
	}
}

function loadChatMessage()
{
		xmlHttp.open("GET","includes/chat/chat.inc.php?action=loadChatMessage",true);
		xmlHttp.onreadystatechange = handleServerResponse;
		xmlHttp.send(null);
}

function postChatMessage()
{
		uname	= encodeURIComponent(document.getElementById("chatName").value);
		email   = encodeURIComponent(document.getElementById("chatEmail").value);
                message = encodeURIComponent(document.getElementById("chatMessage").value);
                
                if(uname == "" || uname == encodeURIComponent("name"))
                {
                  checkName = prompt("enter your nick name!");
                  if(checkName === null) return 0;
                  if(checkName == "") uname = "anonim";
                  document.getElementById("chatName").value = checkName;
                }
                else if(email == "" || email == encodeURIComponent("your email") || isEmailInvalid(email))
                {
                  checkEmail = prompt("enter your email!");
                  if(checkEmail === null) return 0;
                  if(checkEmail == "") uname = "no email";
                  document.getElementById("chatEmail").value = checkEmail;
                }
                else
                {
                  if(message != "")
                  {
                    document.getElementById("chatMessage").value = "";
		    xmlHttp.open("GET","includes/chat/chat.inc.php?action=postChatMessage&name="+uname+"&email="+email+"&message="+message,true);
		    xmlHttp.send(null);
		  }
		}
}

function handleServerResponse()
{
	if(xmlHttp.readyState == 4)
	{
		if(xmlHttp.status == 200)
		{
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			message	= xmlDocumentElement.firstChild.data;
			
			document.getElementById("divChatMessage").innerHTML	= message;
			
			setTimeout('loadChatMessage()',800);
		}
		else
		{
			alert(xmlhttp.statusText);
		}	
	}
}

function enterPressed(arg) 
{ 
	if (arg == 13) postChatMessage(); 
}

function isEmailInvalid(e)
{
  return 0;
}

setTimeout('loadChatMessage()',800);
