var ajaxOk=true;
var sendRequest = "";

function AddSmileChat(sm)
{
	document.frm_input.message.value+=sm + ' ';
	document.frm_input.message.focus();
}
function RefreshMain()
{
	ajaxReq('chat/refresh_main_mini.php','maintab');
	setTimeout('RefreshMain()',4000);
}
function RefreshRooms()
{
	ajaxReq('chat/refresh_rooms.php','roomstab');
	ajaxReq('chat/refresh_private_rooms.php','proomstab');
	setTimeout('RefreshRooms()',30000);
}
function RefreshUsers()
{
	ajaxReq('chat/refresh_users.php','userstab');
	setTimeout('RefreshUsers()',30000);
}
function makeConn() 
{ 
	var http_request; 
	if (window.XMLHttpRequest) 
	{ 
		try { http_request = new XMLHttpRequest(); } 
		catch (e) { http_request = false; }
	} 
	else 
	{ 
		if (window.ActiveXObject) 
		{ 
			try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } 
			catch (e) { http_request = false; }
		} 
		else 
			http_request = false;
	}
	return http_request;
}

function SendMsg()
{
	msg=document.frm_input.message.value;
	sendto=document.frm_input.sendto.value;
	priv=document.frm_input.priv.checked;
	if (priv==true) priv=1; else priv=0;
	if (sendto.length==0)
	{
		priv=0;
		document.frm_input.priv.checked=false;
	}
	target='maintab';
	
	sendRequest = makeConn(); 
	if (msg.length > 0 && (sendRequest.readyState == 4 || sendRequest.readyState == 0)) 
	{ 
		msg = encodeURIComponent(msg);
		var dataSend = "message="+msg+"&sendto="+sendto+"&priv="+priv;
		sendRequest.open("POST", "chat/msg_send.php", true); 
		sendRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); 
		sendRequest.send(dataSend);
		sendRequest.onreadystatechange = function(){ RefreshMain(); }; 
	}
	document.frm_input.message.value='';
	document.frm_input.message.focus();
}

function ajaxReq(url,target)
{
	var xmlHttp;
	try
  {
  	xmlHttp=new XMLHttpRequest();
  }
	catch (e)
  {
  	try
    {
    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  	catch (e)
    {
    	try
      {
      	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    	catch (e)
      {
      	ajaxOk=false;
      	return false;
      }
    }
  }
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			if (xmlHttp.responseText=='ERR')
				document.location='/';
			document.getElementById(target).innerHTML=xmlHttp.responseText;
		}
	}
	//xmlHttp.Charset = "windows-1250";
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return true;
}
