var GetChaturl="/inc/boxes/chat/getChatData.php";
var SChaturl="/inc/boxes/chat/sendChatData.php";
var UOurl="/inc/boxes/chat/uo.php";
var lastID=0;
var hist=new Array();
var hn=0;
var uo_old='';
var halerts=0;
var httpRChat=getHTTPObject();
var httpRUO=getHTTPObject();
var httpSChat=getHTTPObject();

function highlight(){
	if(d.forms['chatForm'].elements['chathighlight'].checked) halerts=0;
	else halerts=1
	}

function receiveChatText(){
	if(httpRChat.readyState==4 || httpRChat.readyState==0){
  	httpRChat.open('GET',GetChaturl+'?lastID='+lastID+'&rand='+Math.floor(Math.random()*1000000),true);
    httpRChat.onreadystatechange=handlehHttpRChat; 
  	httpRChat.send(null)
	}
}

function receiveUO(){
	if (httpRUO.readyState==4 || httpRUO.readyState==0){
  	httpRUO.open('GET',UOurl,true);
    httpRUO.onreadystatechange=handlehHttpUO; 
  	httpRUO.send(null)
	}
}

function handlehHttpUO(){
  if(httpRUO.readyState==4)
  	{
  	result=httpRUO.responseText.split('ζ');
    if(result.length>0 && (insertUO=d.getElementById('users_online'))&& uo_old!=httpRUO.responseText)
    	{
			clearChild(insertUO);
			divUO=ce('div');
			for(i=0;i<(result.length-1);i=i+3)
				{
				if (i==(result.length-4)) 
					ac(ac(divUO,insertNewUO(result[i],result[i+1])),ctn('.'));
				else 
					ac(ac(ac(divUO,insertNewUO(result[i],result[i+1])),ctn(',')),ce('br'))
				}
			insertUO=d.getElementById("users_online");
			ac(insertUO,divUO);
			uo_old=httpRUO.responseText
    	}
    setTimeout('receiveUO();',4999)
  	}
	}

function handlehHttpRChat(){
  if (httpRChat.readyState==4)
  	{
    results=httpRChat.responseText.split('ζ');
    if (results.length>1)
    	{
    	insertO=d.getElementById("outputList");
    	oDiv=ce('div');
    	dd=d.forms['chatForm'];
    	var hcontent='';
	    for(i=1;i<(results.length-1);i=i+7)
	    	{
	    	ac(oDiv,insertNewContent(results[i],results[i+1],results[i+2],results[i+3],results[i+4],results[i+5]));
	    	//if (results[i+1]=="sūkā") alert (escape(dd.elements['name'].value)+':::'+escape(results[i+1]));
	    	esc1=escape(results[i+1]);
	    	esc2=escape(dd.elements['name'].value);
	    	if (esc1.match(new RegExp('\\b'+esc2+'\\b','i')))
	    	if (results[i+1].substring(0,4)=='/me ') hcontent=hcontent+'"'+results[i]+' '+results[i+1].substring(3,results[i+1].length)+'"\n';
	    	else hcontent=hcontent+'"'+results[i]+': '+results[i+1]+'"\n'
	    	}
	    insertO.insertBefore(oDiv, insertO.lastChild);
	    var t1=insertO.scrollHeight*4; //IE?
			var t2=insertO.offsetHeight;
			insertO.scrollTop=(t1>t2)?t1:t2;
			prevID=lastID;
			lastID=results[results.length-8];
			if (prevID && hcontent && (!halerts))
				{
				if (confirm('Ieskaties HC.LV čatā:\n'+hcontent+'\n Vai atslēgt šos paziņojumus?'))
					{
					d.forms['chatForm'].elements['chathighlight'].checked=false;
					highlight()
					}
				}
    }
    setTimeout('receiveChatText();',3000)
  }
}


function insertNewContent(liName,liText,liUrl,liLoc,liReg,liTime)
	{
	oLi=ce('p');
	oSp=ce('span');
	oSptxt=ce('span');
	dd=d.forms['chatForm'];
	
	esc1=escape(liText);
	esc2=escape(dd.elements['name'].value);
	if(esc1.match(new RegExp('\\b'+esc2+'\\b','i'))) stils(oSptxt,'color:#f00;');
	if (liText.substring(0,4)=='/me ')
		{
		stils(oSptxt,'color:#39c;');
		liText=liText.substring(3,liText.length)
		}
	else ac(oSp,ctn(': '));

	add_links(oSptxt,liText,12);

  //alert 
 	//if(d.forms['chatForm'].elements['reg'].value) 
 	if (liReg!=0) liLoc=liLoc.substr(liLoc.indexOf(' '));

	oSpan=ac(sa(sa(sa(ce('a'),'class','name'),'title','No: '+liLoc),'href',liUrl),ctn(liName));
	if (d.forms['chatForm'].elements['big_chat'].value==true) ac(oLi,ctn('['+liTime+'] '));
	if (liReg!=0)
		{
		PImg=ce('img');
		sa(PImg,'height','12');
		if (liUrl.substring(7,16)=='www.hc.lv')
			if (liName=='agony_')
			sa(PImg,'src','/inc/images/agony_.gif');
			else sa(PImg,'src','/inc/images/ru.gif');
		if (liUrl.substring(7,16)=='web.hc.lv')
			sa(PImg,'src','/inc/images/webu.gif');
//		else
//			{
//			sa(PImg,'src','/inc/images/ru_s.gif');
//			sa(oSpan,'target','_blank')
//			}
		ac(ac(oLi,sa(sa(PImg,'width','12'),'alt','Reģistrēts lietotājs')),ctn(' '))
		}
  return(ac(ac(oLi,oSpan),ac(oSp,oSptxt)))
	}


function insertNewUO(UOid,UOname,last){
	return(ac(ac(ac(ce('span'),sa(sa(sa(sa(ce('img'),'src','/inc/images/online15x15.gif'),'width','12'),'height','12'),'alt','Online')),ctn(' ')),ac(sa(sa(ce('a'),'href',('/konts/lietotaji/'+UOid+'/')),'title',('Info par '+UOname)),ctn(UOname))))
}

function sendComment() {
	dd=d.forms['chatForm'];
	cChatText=dd.elements['chatbarText'].value;
	if(cChatText.match(new RegExp('\\bdirst\\b','i'))
	||cChatText.match(new RegExp('\\bpimpis\\b','i'))
	||cChatText.match(new RegExp('\\bpizdjec\\b','i'))
	||cChatText.match(new RegExp('\\bpizģec\\b','i'))
	||cChatText.match(new RegExp('\\bdirsa\\b','i'))
	||cChatText.match(new RegExp('\\bdirsaa\\b','i'))
	||cChatText.match(new RegExp('\\bpeza\\b','i'))
	||cChatText.match(new RegExp('\\bpezha\\b','i'))
	||cChatText.match(new RegExp('\\bpeža\\b','i'))
	||cChatText.match(new RegExp('\\bpizda\\b','i'))
	||cChatText.match(new RegExp('\\bpiizda\\b','i'))
	||cChatText.match(new RegExp('\\bpīzdac\\b','i'))
	||cChatText.match(new RegExp('\\bpohuj\\b','i'))
	||cChatText.match(new RegExp('\\bnahuj\\b','i')))
		{
		alert ('Vai, kas tā runā?');
		location.href='http://lleo.aha.ru/na/lv/'
		}
	if (cChatText!='' && (httpSChat.readyState == 4 || httpSChat.readyState == 0))
		{
		hist.push(cChatText);
		if(hist.length>10) hist.shift();
		cName=dd.elements['name'].value;
		cReg=dd.elements['reg'].value;
		cUrl=dd.elements['chat_url'].value;
		cLoc=dd.elements['chat_loc'].value;
		param='n='+ encodeURIComponent(cName)+'&c='+ encodeURIComponent(cChatText)+'&u='+encodeURIComponent(cUrl)+'&l='+encodeURIComponent(cLoc)+'&r='+cReg;
		httpSChat.open("POST",SChaturl,true);
		httpSChat.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
    httpSChat.setRequestHeader("Connection","close");
  	httpSChat.onreadystatechange=handlehHttpSChat;
  	httpSChat.send(param);
  	dd.elements['chatbarText'].value=''
		}
}

function handlehHttpSChat(){
  if(httpSChat.readyState==4) receiveChatText()
	}

function checkName(){
	currentName=d.forms['chatForm'].elements['name'];
	if(currentName.value=='')	currentName.value='Viesis_'+Math.floor(Math.random()*10000)
	}
