// JavaScript Document

var check = false;
 
function getXhr()
{
	var xhr = null;
	if(window.XMLHttpRequest)
		xhr = new XMLHttpRequest();
	else if(window.ActiveXObject)
	{
		try {
		xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else
	{
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		xhr = false;
	}
	return xhr;
}
 
var xhrRefresh = getXhr();
var xhrPost = getXhr();
 
function chatTimer()
{
	refresh_chat();
	setTimeout("chatTimer();",100000);
}

function goTo(debut)
{
	xhrRefresh.onreadystatechange = function(){
		if(xhrRefresh.readyState == 4 && xhrRefresh.status == 200)
		{
			document.getElementById('commentaire').innerHTML = xhrRefresh.responseText;
		}
	}
	
	$('#goto_comm').val(debut);
	
	xhrRefresh.open("POST","modules/Download/comm.php?action=update&from="+debut+"&fichier=" + fichier,true);
	xhrRefresh.send(null);
}

$(document).ready(function(){
	//global vars
	var inputUser = $("#nick");
	var inputMessage = $("#message");
	var inputDlid = $("#dl_id");
	var loading = $("#loading");
	var messageList = $(".Affiche_fichier > ul");
	
	//functions
	function updateShoutbox(){
		//just for the fade effect
		messageList.hide();
		loading.fadeIn();
		//send the post to shoutbox.php
		$.ajax({
			type: "POST", url: "modules/Download/comm.php", data: "action=update&fichier=" + fichier,
			complete: function(data){
				loading.fadeOut();
				messageList.html(data.responseText);
				messageList.fadeIn(2000);
				refresh_chat();
			}
		});
	}


	//check if all fields are filled
	function checkForm(){
		if(inputUser.attr("value") && inputMessage.attr("value") && inputDlid.attr("value"))
			return true;
		else
			return false;
	}
	
	//Load for the first time the shoutbox data
	updateShoutbox();
	
	//on submit event
	$("#form_comm").submit(function(){
		if(checkForm()){
			var nick = inputUser.attr("value");
			var message = inputMessage.attr("value");
			var dlid = inputDlid.attr("value");
			//we deactivate submit button while sending
			$("#send_comm").attr({ disabled:true, value:"Soumettre le commentaire" });
			$("#send_comm").blur();
			//send the post to shoutbox.php
			
			$.ajax({
				type: "POST", url: "modules/Download/comm.php", data: "action=insert&nick=" + nick + "&message=" + message + "&dlid=" + dlid,
				complete: function(data){
					messageList.html(data.responseText);
					updateShoutbox();
					//reactivate the send button
					$("#send_comm").attr({ disabled:false, value:"Commentaire soumis" });
					$("#message").attr({ disabled:false, value:"Composez votre message ..." });
				}
			 });
			
		}
		else alert("Remplissez s'il vous plaît tous les champs !");
		//we prevent the refresh of the page after submitting the form
		return false;
	});
	
});


function refresh_chat()
{
	xhrRefresh.onreadystatechange = function(){
		if(xhrRefresh.readyState == 4 && xhrRefresh.status == 200){
			document.getElementById('commentaire').innerHTML = xhrRefresh.responseText;
		}
	}
	
	debut = $('#goto_comm').val();
	
	xhrRefresh.open("POST","modules/Download/comm.php?action=update&from="+debut+"&fichier=" + fichier,true);
	xhrRefresh.send(null);
}
 
if(!check)
{
	check = true;
	chatTimer();
}
