/* Recuperation des messsages depuis un ecriture */
var refreshFromAdd = 0 ;
var blockedChat = 0 ;
function get_chat_messages()
{
	var xhr = get_xmlhttp() ;
	if(xhr==null) {
		return false ;
	}
	
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			document.getElementById('chat_messages').innerHTML = xhr.responseText ;
			if(refreshFromAdd == 0) {
				setTimeout("get_chat_messages()", 10000) ;
			} else {
				refreshFromAdd = 0 ;
			}
			document.getElementById('chat_messages').scrollTop = document.getElementById('chat_messages').scrollHeight;
		}
	}
	
	/* Build get url */
	scriptAjax = '/scripts/ajax/get_chat_message.php' ;
	xhr.open("GET", scriptAjax, true) ;
	xhr.send(null) ;

}

function add_chat_messages()
{
	if(blockedChat == 0) {
	if(document.getElementById('chat_message').value != '') {
	var xhr = get_xmlhttp() ;
	if(xhr==null) {
		return false ;
	}
	
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			document.getElementById('chat_message').value = '' ;
			refreshFromAdd = 1 ;
			document.getElementById('chat_send').disabled = true ;	
			setTimeout("enable_chat_send()", 10000) ;
			blockedChat = 1 ;
			get_chat_messages() ;
		}
	}
	var params = 'message='+encodeURI(document.getElementById('chat_message').value) ;
	
	/* Build get url */
	scriptAjax = '/scripts/ajax/add_chat_message.php' ;
	xhr.open("POST", scriptAjax, true) ;
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.setRequestHeader("Connection", "close")	
	xhr.send(params) ;
	}
	}
}

function enable_chat_send()
{
	document.getElementById('chat_send').disabled = false ;
	blockedChat = 0 ;	
}

function login_user_chat()
{
	var loggedPseudo = document.getElementById('chat_pseudo').value ;
	if(document.getElementById('read_chart').checked == true ) { 
	if(loggedPseudo.length > 2) {
		document.getElementById('too_short').style.display = 'none' ;
		var xhr = get_xmlhttp() ;
		if(xhr==null) {
			return false ;
		}
		
		document.getElementById('read_chart').checked = false ;
			
		xhr.onreadystatechange=function()
		{
			if(xhr.readyState==4 && xhr.status==200) {
				if(xhr.responseText == 'redirect') { 
					window.location = "/index.html" ;	
				} else {
					document.getElementById('chat_message_form').style.display = 'block' ;
					document.getElementById('chat_login_form').style.display = 'none' ;
				}
			}
		}
	
		/* Build get url */
		/* todo : Send this value in POST */
		scriptAjax = '/scripts/ajax/login_user_chat.php?pseudo='+loggedPseudo ;
		xhr.open("GET", scriptAjax, true) ;
		xhr.send(null) ;
	} else {
		document.getElementById('too_short').style.display = 'inline' ;
	}
	} else {
		alert("Merci de lire et d'accepter la charte d'utilisation") ;
	}
}

function logout_user_chat()
{
	var xhr = get_xmlhttp() ;
	if(xhr==null) {
		return false ;
	}

	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			document.getElementById('chat_message_form').style.display = 'none' ;
			document.getElementById('chat_login_form').style.display = 'block' ;
		}
	}
	
	/* Build get url */
	scriptAjax = '/scripts/ajax/logout_user_chat.php' ;
	xhr.open("GET", scriptAjax, true) ;
	xhr.send(null) ;
}

function check_chat_return_msg(e)
{
	var characterCode ;

	if(e && e.which){
		e = e ;
		characterCode = e.which ;
	} else {
		e = event ;
		characterCode = e.keyCode ;
	}

	if(characterCode == 13){ 
		add_chat_messages();
		return false ;
	} else {
		return true ;
	}
}

function check_chat_return_login(e)
{
	var characterCode ;

	if(e && e.which){
		e = e ;
		characterCode = e.which ;
	} else {
		e = event ;
		characterCode = e.keyCode ;
	}

	if(characterCode == 13){ 
		login_user_chat();
		return false ;
	} else {
		return true ;
	}
}


