﻿ function ajaxFunction(){
	var ajaxRequest;

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				//browsers all not support, rare case
				alert("Your browser broke!");
				return false;
			}
		}

	}
	return ajaxRequest;
}

function showData() {
	htmlRequest = ajaxFunction();
	if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
		alert ("Browser does not support HTTP Request");
		return;
	} 

	htmlRequest.onreadystatechange = function(){
		if(htmlRequest.readyState == 4){
			document.getElementById("shoutarea").innerHTML = htmlRequest.responseText;
		}
	}
	htmlRequest.open("GET", "outputinfo.php", true);
	htmlRequest.send(null);
}

showData();
setInterval("showData()",2000); // 2 seconds

function saveData(){
	htmlRequest = ajaxFunction();
	if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
		alert ("Browser does not support HTTP Request");
		return;
	} 

	if(document.shoutbox.name.value == "" || document.shoutbox.name.value == "NULL" || document.shoutbox.message.value == "" || document.shoutbox.message.value == "NULL"){
		alert('Ban Phai Login Hoac La Message Qua Ngan!');
		return;
	}
	htmlRequest.open('POST', 'sendshout.php');
	htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	htmlRequest.send('name='+document.shoutbox.name.value+'&message='+document.shoutbox.message.value+'&mid='+document.shoutbox.mid.value); 

	document.shoutbox.message.value = ''; // Updates the shout boxâ€™s text area to NULL.
	document.shoutbox.message.focus(); // Focuses the text area.

} 