//CHAT
var http_request = false;

function makeRequest(url, room, idusera) {

    http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
            // Przeczytaj o tym wierszu poniżej
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Poddaję się :( Nie mogę stworzyć instancji obiektu XMLHTTP');
        return false;
    }
    http_request.onreadystatechange = function() { alertContents(http_request, room, idusera); };
    http_request.open('GET', url, true);
    http_request.send(null);

}

function sound_on(){
	document.getElementById('sound').value=1;
}
function sound_off(){
	document.getElementById('sound').value=0;
}

function foc(){
	document.getElementById('write').focus();
}

function img_chat(pictsrc){
	return '<a href="'+pictsrc+'" rel="lightbox[chat]" /><img src="'+pictsrc+'" style="width: 250px; height: 150px" /></a>';
}

function alertContents(http_request, room, idusera) {

    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
			
            var z=http_request.responseText;
				
				var f=1;
				var i1='<img class="emotki" src="/forum/emoty/';
				var i2=' alt="" />';
				var e={
					'alt-e':'ę',
					'alt-o':'ó',
					'alt-a':'ą',
					'alt-s':'ś',
					'alt-l':'ł',
					'alt-z':'ż',
					'alt-x':'ź',
					'alt-c':'ć',
					'alt-n':'ń',
					'alt-E':'Ę',
					'alt-O':'Ó',
					'alt-A':'Ą',
					'alt-S':'Ś',
					'alt-L':'Ł',
					'alt-Z':'Ż',
					'alt-X':'Ź',
					'alt-C':'Ć',
					'alt-N':'Ń',
					':D':i1+'92.gif" style="width: 39px; height: 27px"'+i2,
					':-D':i1+'92.gif" style="width: 39px; height: 27px"'+i2,
					'JUPI':i1+'90.gif" style="width: 39px; height: 29px"'+i2,
					'PAPA':i1+'91.gif" style="width: 45px; height: 34px"'+i2,
					'BRAWO':i1+'99.gif" style="width: 52px; height: 26px"'+i2,
					'ŻEGNAM':i1+'105.gif" style="width: 39px; height: 26px"'+i2,
					'ZEGNAM':i1+'105.gif" style="width: 39px; height: 26px"'+i2,
					'NO':i1+'112.gif" style="width: 44px; height: 26px"'+i2,
					'TAK':i1+'113.gif" style="width: 37px; height: 25px"'+i2,
					'SPOKO':i1+'116.gif" style="width: 38px; height: 27px"'+i2,
					'OK':i1+'87.gif" style="width: 42px; height: 26px"'+i2,
					';)':i1+'116.gif" style="width: 38px; height: 27px"'+i2,
					'NIEWIEM':i1+'231.gif" style="width: 47px; height: 26px"'+i2,
					'NIEE':i1+'120.gif" style="width: 31px; height: 26px"'+i2,
					'NIE':i1+'119.gif" style="width: 46px; height: 26px"'+i2,
					'ZŁY':i1+'134.gif" style="width: 58px; height: 26px"'+i2,
					'ZLY':i1+'134.gif" style="width: 58px; height: 26px"'+i2,
					'EJJ':i1+'135.gif" style="width: 54px; height: 33px"'+i2,
					'UWAGA':i1+'140.gif" style="width: 44px; height: 32px"'+i2,
					'HMM':i1+'144.gif" style="width: 28px; height: 34px"'+i2,
					'BLE':i1+'148.gif" style="width: 40px; height: 33px"'+i2,
					'BŁE':i1+'148.gif" style="width: 40px; height: 33px"'+i2,
					'STUK':i1+'152.gif" style="width: 47px; height: 30px"'+i2,
					'WRRR':i1+'155.gif" style="width: 54px; height: 37px"'+i2,
					'UPS':i1+'176.gif" style="width: 44px; height: 25px"'+i2,
					'MNIAM':i1+'177.gif" style="width: 34px; height: 27px"'+i2,
					'THX':i1+'183.gif" style="width: 51px; height: 39px"'+i2,
					'HAHAHA':i1+'201.gif" style="width: 36px; height: 30px"'+i2,
					'STOP':i1+'207.gif" style="width: 51px; height: 47px"'+i2,
					'WOW':i1+'208.gif" style="width: 33px; height: 25px"'+i2,
					'AAA':i1+'212.gif" style="width: 40px; height: 25px"'+i2,
					'PISZE':i1+'217.gif" style="width: 45px; height: 53px"'+i2,
					'UKŁON':i1+'223.gif" style="width: 49px; height: 26px"'+i2,
					'UKLON':i1+'223.gif" style="width: 49px; height: 26px"'+i2,
					'ZIEWA':i1+'226.gif" style="width: 63px; height: 29px"'+i2,
					'CZEKAM':i1+'230.gif" style="width: 47px; height: 30px"'+i2,
					'ZGODA':i1+'236.gif" style="width: 71px; height: 29px"'+i2,
					'ROFTL':i1+'84.gif" style="width: 40px; height: 25px"'+i2,
					':)':i1+'82.gif" style="width: 40px; height: 24px"'+i2,
					':-)':i1+'82.gif" style="width: 40px; height: 24px"'+i2,
					':P':i1+'149.gif" style="width: 41px; height: 26px"'+i2,
					':-P':i1+'149.gif" style="width: 41px; height: 26px"'+i2,
					':p':i1+'149.gif" style="width: 41px; height: 26px"'+i2,
					':-p':i1+'149.gif" style="width: 41px; height: 26px"'+i2,
					':-/':i1+'110.gif" style="width: 39px; height: 25px"'+i2,
					'PLUS':'+',
					'PROCENT':'%',
					'AND':'&',
					'CASH':'#'
				}
				
				for(var i in e){
					f?
					z=z.replace(RegExp(i.replace(/([\\)?*+.^])/g,'\\'+'$1').replace(/>/g,'&gt;'),'g'),e[i])
					:
					z=z.replace(RegExp(e[i],'gi'),i);
				}
				
				//obrazek
				//var wyr=/((http):\/\/{1}([a-z0-9.\-_/]+)(jpg))/g;
				//z=z.replace(wyr, img_chat('$1'));
				
				if (z!='[|]'){
					var z=z.split('[|]');

					//if (document.getElementById('last').value<z[3] && document.getElementById('sound').value==1) document.getElementById('receive').innerHTML='<object type="application/x-shockwave-flash" data="/skiny/standard/receive.swf" width="1" height="1"><param name="movie" value="/skiny/standard/receive.swf" /><param name="menu" value="false" /></object>';
					
					if (z[3]!=0) document.getElementById('last').value=z[3];
					var tr=z[1];
					
					//zamiana adresów email
					var wyr=/([a-z0-9\.\-_]+@[a-z0-9\-\.]+\.[a-z]{2,4})/g;
					tr=tr.replace(wyr, '<a href="mailto:$1">$1</a>');
				
					//link
					var wyr=/((http|https|ftp):\/\/{1}([\w\.\-]+)\.(\w{2,6})([\w\.\-\/]*))/g;
					tr=tr.replace(wyr, '<a href="$1" target="_blank">$1</a>');
					
					//pogrubienie
					//var wyr=/(\[b\])(.*)(\[\/b\])/gi;
					//tr=tr.replace(wyr, '<b>$2</b>');
					//kursywa
					//var wyr=/(\[i\])(.*)(\[\/i\])/gi;
					//tr=tr.replace(wyr, '<i>$2</i>');
					//podkreślenie
					//var wyr=/(\[u\])(.*)(\[\/u\])/gi;
					//tr=tr.replace(wyr, '<u>$2</u>');
					//kolor
					//var wyr=/(\[c\=)([a-z]+)(\])(.*)(\[\/c\])/gi;
					//tr=tr.replace(wyr, '<span style="color: $2">$4</span>');
					//rozmiar
					//var wyr=/(\[s\=)([0-9]+)(\])(.*)(\[\/s\])/gi;
					//tr=tr.replace(wyr, '<span style="font-size: $2pt">$4</span>');
					
					document.getElementById('chat_content').innerHTML+=tr;
					var tr='';
					
					document.getElementById('chat_users_online').innerHTML=z[0];
					document.getElementById('chatrooms').innerHTML=z[2];
					if (document.getElementById('auto').checked) document.getElementById('chat_content').scrollTop=document.getElementById('chat_content').scrollHeight;
				}
        } else {
            alert('Wystąpił problem z zapytaniem.');
        }			
	 	mTimer = setTimeout('refresh_chat('+room+', '+idusera+');',2000);
    }

}

function zeruj(){
	document.getElementById('last').value=0;
}

// usuwanie bialych spacji
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
// usuwanie bialych spacji
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

function enter(e, url, nick, tresc, room, admin, idusera){

var f=1;				
var ee={
	'ę':'alt-e',
	'ó':'alt-o',
	'ą':'alt-a',
	'ś':'alt-s',
	'ł':'alt-l',
	'ż':'alt-z',
	'ź':'alt-x',
	'ć':'alt-c',
	'ń':'alt-n',
	'Ę':'alt-E',
	'Ó':'alt-O',
	'Ą':'alt-A',
	'Ś':'alt-S',
	'Ł':'alt-L',
	'Ż':'alt-Z',
	'Ź':'alt-X',
	'Ć':'alt-C',
	'Ń':'alt-N',
	'+':'PLUS',
	'&':'AND',
	'#':'CASH',
	'%':'PROCENT'
}
for(var i in ee){
	f?
	tresc=tresc.replace(RegExp(i.replace(/([\\)?*+.^])/g,'\\'+'$1').replace(/>/g,'&gt;'),'g'),ee[i])
	:
	tresc=tresc.replace(RegExp(ee[i],'gi'),i);
	
	f?
	nick=nick.replace(RegExp(i.replace(/([\\)?*+.^])/g,'\\'+'$1').replace(/>/g,'&gt;'),'g'),ee[i])
	:
	nick=nick.replace(RegExp(ee[i],'gi'),i);
}
	var tresc=tresc.replace(/(<([^>]+)>)/gi, '');
	var tresc=tresc.replace(/(<|>)/gi, '');
	var tresc=LTrim(RTrim(tresc));
	var klawisz=e.keyCode ? e.keyCode : e.charCode;
	var last_message=document.getElementById('last').value;
	if (klawisz==13 && tresc!='' && tresc.length>0) {
		document.getElementById('write').value='';
		clearInterval(mTimer);
		//pogrubienie
		if (document.getElementById('ichat_b').checked) tresc='<b>'+tresc+'</b>';
		//kursywa
		if (document.getElementById('ichat_i').checked) tresc='<i>'+tresc+'</i>';
		//podkreślenie
		if (document.getElementById('ichat_u').checked) tresc='<u>'+tresc+'</u>';
		//kolor
		if (document.getElementById('f_color').value!=0) tresc='<span style="color: '+document.getElementById('f_color').value+'">'+tresc+'</span>';
		if (document.getElementById('f_size').value!=8) tresc='<span style="font-size: '+document.getElementById('f_size').value+'pt">'+tresc+'</span>';
		var url=url+'?write='+tresc+'&nick='+nick+'&last_message='+last_message+'&room='+room+'&admin='+admin+'&userid='+idusera;
		makeRequest(url, room, idusera);
	}
}

function refresh_chat(room, idusera){
	var url='/includes/funkcje/send_chat.php?last_message='+document.getElementById('last').value+'&room='+room+'&userid='+idusera;
	makeRequest(url, room, idusera);
}

function init_chat(room, idusera){
	document.getElementById('noJS').style.display='none';
	document.getElementById('iChat').style.display='block';
	document.getElementById('write').focus();
	var url='/includes/funkcje/send_chat.php?last_message='+document.getElementById('last').value+'&room='+room+'&userid='+idusera;
	makeRequest(url, room, idusera);
}

function emotki(x){
	var d=document.getElementById(x);
	if (d.style.display=='none') d.style.display='';
	else d.style.display='none';
}
function insert_emot(x){
	document.getElementById('write').value+=x;
	document.getElementById('write').focus();
	emotki('emotki');
}
//CHAT

//funkcja wywołująca popupy
function popup(tresc, w, h, l, t) {
noweokno=window.open('', '', 'menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=yes, status=no, width='+w+', height='+h+', left='+l+', top='+t+'')

noweokno.document.open();
noweokno.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-equiv="Content-type" content="text/html; charset=iso-8859-2" /><link rel="Stylesheet" href="skiny/standard/stylesheet.css" type="text/css" /><title>Panel administracyjny</title></head><body><div class="git_metryka opracowania_linijka" style="margin: 5px">');
noweokno.document.write(tresc);
noweokno.document.write('</div></body></html>');
noweokno.document.close();
noweokno.focus();
}

//funkcje do buttonów
function over(id){
	document.getElementById('_'+id+'_1_1').className='lg_over';
	document.getElementById('_'+id+'_1_2').className='sg_over';
	document.getElementById('_'+id+'_1_3').className='pg_over';
	
	document.getElementById('_'+id+'_2_1').className='ld_over';
	document.getElementById('_'+id+'_2_2').className='sd_over';
	document.getElementById('_'+id+'_2_3').className='pd_over';
}

//funkcje do buttonów
function out(id){
	document.getElementById('_'+id+'_1_1').className='lg';
	document.getElementById('_'+id+'_1_2').className='sg';
	document.getElementById('_'+id+'_1_3').className='pg';
	
	document.getElementById('_'+id+'_2_1').className='ld';
	document.getElementById('_'+id+'_2_2').className='sd';
	document.getElementById('_'+id+'_2_3').className='pd';
}

function set_cookie( name, value, expires, path, domain, secure ) {
var today = new Date();
today.setTime( today.getTime() );
if ( expires )
{
expires = expires * 1000;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}