var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();
var lastMessage = 0;
var mTimer;
var user;
var clientChat="";
var userClient="";
			//Function for initializating the page.
function changeChat(chatt)
{
	
		document.getElementById('div_chat').innerHTML=""
		getChatTextAdmin(chatt);
		lastChat=chatt;
		setTimeout("getifuserisregistered("+chatt+");",1200);
		
				
}
		

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() 
{
	if (window.XMLHttpRequest) 
	{
		return new XMLHttpRequest();
	} 
	else if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
				
	} 
	else 
	{
		document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.';
	}
}

			
			//Gets the current messages from the server

function getChatText() 
{
			
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
	{
		
		
		receiveReq.open("GET", 'getChat.php?chat='+clientChat+'&last='+lastMessage, true);
		receiveReq.onreadystatechange = handleReceiveChat; 
		receiveReq.send(null);
					
	}			
}

			//Add a message to the chat server.


function sendChatText() 
{
	
	if(document.getElementById('txt_message').value == '' || document.getElementById('txt_message').value =='\n' ) 
		{
			alert("You have not entered a message");
			return;
		}
	else if (sendReq.readyState == 4 || sendReq.readyState == 0) 
	{
		
		sendReq.open("POST", 'getChat.php?chat='+clientChat+'&last=' + lastMessage, true);
		sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		sendReq.onreadystatechange = handleSendChat; 
		var param = 'message=' + document.getElementById('txt_message').value;
		param += '&name='+userClient;
		param += '&chat=1';
		sendReq.send(param);
		document.getElementById('txt_message').value = '';
	 }							
}

			
function handleSendChat() 
{
				//Clear out the existing timer so we don't have 
				//multiple timer instances running.
				
	clearInterval(mTimer);
	getChatText();
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
function deletechat()
 {
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
      {
          alert ("Browser does not support HTTP Request")
          return;
      }
   var url="ajaxstuff.php";
   url=url+"?action=deletechat";
   url=url+"&chatdelete="+lastChat;
   url=url+"&sid="+Math.random();
   xmlHttp.onreadystatechange=handleRfreshButtons;
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
			
}


function viewallmessages()
{
	document.getElementById('div_chat').innerHTML="";
	lastMessage=0;
	getChatTextall();
}
function getChatTextall() 
{
			
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
	{
		receiveReq.open("GET", 'getChat.php?chat='+clientChat+'&last='+lastMessage, true);
		receiveReq.onreadystatechange = handleReceiveChatall; 
		receiveReq.send(null);
					
	}			
}
function handleReceiveChatall() 
{
  if (receiveReq.readyState == 4) 
	{
		
		var chat_div = document.getElementById('div_chat');
		
	    var la=document.getElementById('chat_content'); 
	   
		var xmldoc = receiveReq.responseXML;
		var message_nodes = xmldoc.getElementsByTagName("message"); 
		var n_messages = message_nodes.length
		
		for (i = 0; i < n_messages; i++) 
			{
				
				var user_node = message_nodes[i].getElementsByTagName("user");
				var text_node = message_nodes[i].getElementsByTagName("text");
				var time_node = message_nodes[i].getElementsByTagName("time");
				if(user_node[0].firstChild.nodeValue=='admin')	
				chat_div.innerHTML += '<div class="chat_admin_name">'+user_node[0].firstChild.nodeValue + '</div>';
				else chat_div.innerHTML += '<div class="chat_user_name">Moi</div>';
				if(user_node[0].firstChild.nodeValue=='admin')	
				chat_div.innerHTML += '<div class="chat_admin_texr">' + text_node[0].firstChild.nodeValue + '</div>';
				else
				chat_div.innerHTML += '<div class="chat_user_texr">' + text_node[0].firstChild.nodeValue + '&nbsp;</div>';
				chat_div.innerHTML += '<div class="clear"></div><hr />';
				
				//chat_div.innerHTML += '<font class="chat_time">'+text_node[0].firstChild.nodeValue + '<br />';
				
				la.scrollTop = la.scrollHeight;
				
				lastMessage = (message_nodes[i].getAttribute('id'));
				
			}
		mTimer = setTimeout('getChatText();',2000); //Refresh our chat in 2 seconds
	}
}
//Function for handling the return of chat text
function handleReceiveChat() 
{
  if (receiveReq.readyState == 4) 
	{
		
		var chat_div = document.getElementById('div_chat');
		
	    var la=document.getElementById('chat_content'); 
	   
		var xmldoc = receiveReq.responseXML;
		var message_nodes = xmldoc.getElementsByTagName("message"); 
		var n_messages = message_nodes.length
		 if(n_messages>12) 
		 {
		 chat_div.innerHTML +='<div class="history_chat" onclick="viewallmessages();">Voir les messages pr&eacute;c&eacute;dents</div><hr />';
		 start=n_messages-12;
		 }
		 else start=0;
		for (i = start; i < n_messages; i++) 
			{
				
				var user_node = message_nodes[i].getElementsByTagName("user");
				var text_node = message_nodes[i].getElementsByTagName("text");
				var time_node = message_nodes[i].getElementsByTagName("time");
				if(user_node[0].firstChild.nodeValue=='admin')	
				chat_div.innerHTML += '<div class="chat_admin_name">'+user_node[0].firstChild.nodeValue + '</div>';
				else chat_div.innerHTML += '<div class="chat_user_name">Moi</div>';
				if(user_node[0].firstChild.nodeValue=='admin')	
				chat_div.innerHTML += '<div class="chat_admin_texr">' + text_node[0].firstChild.nodeValue + '</div>';
				else
				chat_div.innerHTML += '<div class="chat_user_texr">' + text_node[0].firstChild.nodeValue + '&nbsp;</div>';
				chat_div.innerHTML += '<div class="clear"></div><hr />';
				
				//chat_div.innerHTML += '<font class="chat_time">'+text_node[0].firstChild.nodeValue + '<br />';
				
				la.scrollTop = la.scrollHeight;
				
				lastMessage = (message_nodes[i].getAttribute('id'));
				
			}
		mTimer = setTimeout('getChatText();',2000); //Refresh our chat in 2 seconds
	}
}



function blockSubmit() 
{
	sendChatText(chat);
	return false;
}
//This cleans out the database so we can start a new chat session.
function resetChat(chat) 
{
	chats=chat;
	if (sendReq.readyState == 4 || sendReq.readyState == 0) 
	{
	sendReq.open("POST", 'getChat.php?chat='+chat+'&last=0' , true);
	sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	sendReq.onreadystatechange = handleResetChat; 
	var param = 'action=reset';
	sendReq.send(param);
	document.getElementById('txt_message').value = '';
	}							
}
//This function handles the response after the page has been refreshed.












function CheckSubmit2(e)
{
	
e=e?e:window.event;
if(e.keyCode==13)
sendChatText();
	 
}


function clear_txt() 
{
	document.getElementById('txt_message').value="";
	//document.chat_form.chat_txt.value="";
}


function setElementVisibility(elementToSet, showItSwitch, keepPlacementSwitch)
{
 if (showItSwitch) {
   elementToSet.style.display = "block";
   elementToSet.style.visibility = "visible";
 }
 else{
   if (keepPlacementSwitch) {
     elementToSet.style.display = "block";
     elementToSet.style.visibility = "hidden";
   }
   else{
     elementToSet.style.display = "none";
   }
 }
}
function deletethechat()
{
	
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
      {
          alert ("Browser does not support HTTP Request")
          return;
      }
  
   var url="ajaxstuff.php";
   url=url+"?action=deletechatclient";
   url=url+"&chat="+clientChat;
   url=url+"&sid="+Math.random();
   xmlHttp.onreadystatechange=deletechatready;
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
   
	
}
function deletechatready()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
         { 
         	
         	alert(xmlHttp.responseText);
         	document.getElementById('div_chat').innerHTML="";
      	 }
}
function closed(box_close)
{
   setElementVisibility(document.getElementById(box_close), false, false); 
   deletethechat();
}

function opened(box_open)
{
	
   setElementVisibility(document.getElementById(box_open), true);
  
   startclientchat();
}

function changeStatus(closeBox, OpenBox)
{
		setElementVisibility(document.getElementById(OpenBox), true);
		setElementVisibility(document.getElementById(closeBox), false, false); 
}
function startclientchat()
{
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
      {
          alert ("Browser does not support HTTP Request")
          return;
      }
   var url="ajaxstuff.php";
   url=url+"?action=startclientchat";
   
   
   url=url+"&sid="+Math.random();
   xmlHttp.onreadystatechange=startclientchatresponse;
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
}
function startclientchatresponse()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
             { 
      			
      			var arr=xmlHttp.responseText.split("<&>");
      			document.getElementById('supportonline').innerHTML=arr[0];
      			document.getElementById('supportonlinesec').innerHTML=arr[0];
      			if(arr.length>1)
      			{
      			clientChat=arr[1];
      			userClient=arr[2];
      			}
             }
             
				
}
function onloadchat(user)
{
	xmlHttp=GetXmlHttpObject();
	 if (xmlHttp==null)
      {
          alert ("Browser does not support HTTP Request")
          return;
      }
   var url="ajaxstuff.php";
   url=url+"?action=onload";
   url=url+"&user="+user;
   url=url+"&sid="+Math.random();
   xmlHttp.onreadystatechange=onloadchatresponse;
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
}
function onloadchatresponse()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
          { 
          	
          	var la=xmlHttp.responseText.split('<&>');
          	clientChat=la[0];
          	
          	userClient=la[1];
      		getChatText();	
         }
}

			