﻿// JavaScript Document
// JQuery Function: Nguyen Nhat Tan
// Website: http://www.hssvbentre.net
var i=0;
$(document).ready(function(){
	$("#del_chat").click(function(){
		if(confirm('Clear all chat content?')){
			$.ajax({
				type: "POST", url: "shoutbox.php", data: "action=clear",
				complete: function(data){
					updateShoutbox();
				}
			 });
			inputMessage.attr({value:""});		
		}
		//we prevent the refresh of the page after submitting the form
		return false;
	});
	var TimerId = 0;
	// Chat box
	var inputUser = $("#nick");
	var total = $("#total");
	var inputMessage = $("#chat_message");
	var loading = $("#loading");
	var messageList = $(".content > ul");
		//functions
	function updateShoutbox(){
		$.ajax({
			type: "POST", url: "shoutbox.php", data: "action=update",
			complete: function(data){
				loading.fadeOut();
				messageList.html(data.responseText);
			}
		});
	}
	updateShoutbox();
	//check if all fields are filled
	function checkForm(){
		if(inputMessage.attr("value"))
			return true;
		else
			return false;
	}

	//on submit event
	
	$("#inputform").submit(function(){
		if(checkForm()){
			var nick = inputUser.attr("value");
			var chat_message = inputMessage.attr("value");
			$.ajax({
				type: "POST", url: "shoutbox.php", data: "action=insert&message=" + chat_message,
				complete: function(data){
					updateShoutbox();
				}
			 });
			inputMessage.attr({value:""});
			
		}
		else alert("Chưa nhập nội dung!");
		//we prevent the refresh of the page after submitting the form
		return false;
	});
	
		
	//Load for the first time the shoutbox data
	setInterval(updateShoutbox,12000);
 });

