		var isFirefox;
		var isIE;
		var timeout;
		var pingtimeout;
		var textFocus   = true;
		var starsadded  = false;
		var md4hash     = 0;
		var timedout    = 0;
		var pingsent	= 0;
		var requestsent = 0;
		var postsent	= 0;
		var chatsent	= 0;
		var pingRate	= 1000 * 30;	 // 30 seconds
		var refreshRate = 1000 * 1;		 // two seconds
		var rnd			= Math.random(); // random seed
		var nume;
		var user;
		var targett;
            var q;
            var scro=1;


function KeyCheck(e)
{
var KeyID = (window.event) ? event.keyCode : e.keyCode;
if(KeyID==13){
procin();}
}


function settarget(tg)
{
targett=tg;
fetchBufferText();
}
		
		function getAjax() {
          var xmlhttp;
          
          if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
            try {
              xmlhttp = new XMLHttpRequest();
            } catch (e) {
              xmlhttp = false;
            }
		  }
          return xmlhttp;
        }
		function sniff() {
		
			var ff = "Netscape";
			var ie = "Microsoft Internet Explorer";
			
			isFirefox = ( navigator.appName == ff );
			isIE = (navigator.appName == ie ); 
		}
		function scrollChatPane()
		{
			pane = document.getElementById( "mar" );
			
			while( pane.scrollTop < pane.scrollHeight - pane.offsetHeight )
			{
				pane.scrollTop = pane.scrollTop + 10;
			}
		}
		function showLoadScreen()
		{
			var loading = "<div style=\"text-align:center;\"><h4>Loading...</h4></div>";
			
			chat = document.getElementById( "chatbuffer" );
			user = document.getElementById( "userlist" );
			
			chat.innerHTML = loading;
			user.innerHTML = loading;
		
		}
		function resetPing()
		{
			//pingtimeout = window.setTimeout( "pingServer()", pingRate );
		}
		function setTimers()
		{
			timeout = window.setTimeout( "fetch()", refreshRate );
			//interval=setInterval('fetchBufferText()',2000,);
		}
		function fetch()
		{
			window.clearTimeout( timeout );
			fetchBufferText();
			if(t++ % update_period) { 
			//  fetchUserList();
			}    
		}
	function fetchBufferText()
		{     clearTimeout(q);
			user = nume;
			url = 'mesaje.php?id=' + user + '&targ=' + targett + '&rand=' + rnd;
			req = getAjax();
			
			req.onreadystatechange = function(){
			
				if( req.readyState == 4 && req.status == 200 ) {
				
					//obj.disabled=true;
					obj = document.getElementById( "mar" );
					scrt=obj.scrollTop;
					obj.value = req.responseText;
					obj.scrollTop=scrt;
                              if(scro==1)
                                {scrollChatPane();scro=0;}
				}
			}
			
			req.open( 'GET', url , true );
			req.send( null );

			url2 = 'lista.php?rand=' + rnd;
			req2 = getAjax();
			
			req2.onreadystatechange = function(){
			
				if( req2.readyState == 4 && req2.status == 200 ) {
				
					//obj.disabled=true;
					obj2 = document.getElementById( "lista" );
					obj2.innerHTML = req2.responseText;
				    //scrollChatPane();
				}
			}
			
			req2.open( 'GET', url2 , true );
			req2.send( null );
			

q=setTimeout("fetchBufferText()",3000);
			//updateStatus();
			//changeTitle(); // this doesn't work
		}

function pingServer()
		{
url = 'active.php?id='+nume+'&targ= '+'&rand='+rnd+'&mesaj=ping';
req = getAjax();
req.onreadystatechange = function(){
			
            if( req.readyState == 4 && req.status == 200 ) {
				
				}
			}
			
req.open( 'GET', url , false );
req.send( null );
w=setTimeout("pingServer()",30000);
        }

		function captureReturn( event )
		{
			if( isIE ) {
			
				event = window.event;
			}
			//
			// Supress event bubbling
			//
			if( event.keyCode == 13 ) {
				//
				// fetch the feed
				//
				procin();
					
				if( isIE ) {
				
					event.returnValue = false;
					
				} else {
				
					event.preventDefault();
				
				}
			}
			
		}


function startChat(nm)
		{   nume=nm;
		    targett=nm;
			sniff();
			pingServer();
			//showLoadScreen();
			setTimers();
			resetPing();
			//if( isFirefox ) {
				mytext = document.getElementById( "txar" );
			//}
			mytext.focus();
			document.onkeyup = KeyCheck;       
			//mytext.onkeypress = captureReturn;
			
		}


function procin()
{
mesaj=document.getElementById( "txar" ).value;
url = 'active.php?id='+nume+'&targ='+targett+'&rand='+rnd+'&mesaj='+mesaj;
req = getAjax();
req.onreadystatechange = function(){
			
            if( req.readyState == 4 && req.status == 200 ) {
				
					obj = document.getElementById( "txar" );
					obj.value = '';
					scro=1;
                              fetchBufferText();
				}
			}
			
req.open( 'GET', url , true );
req.send( null );
}