// JavaScript Document - AJAX LIBRARY
<!--


// Cookie functions: Set, Get, Delete have been declared in jscript_common file


var http_sub_request = false;
var xhrTimeout;
//var last_mod_header; 
//var etag_header;

function ajaxTimeout(channel_id,last_mod_header,etag_header,cookie_domain){
   http_sub_request.abort();
   // resubscribe with last timestamp from cookie
   subscribe_channel(channel_id,last_mod_header,etag_header,cookie_domain);
   
}

function get_ajax() {
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
          return new XMLHttpRequest();
          if (http_sub_request.overrideMimeType) {
              http_sub_request.overrideMimeType('text/xml');
          }
    } else if (typeof window.XMLHttpRequest == "undefined") {  //  IE 5.x-6.x 

		  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
		  catch(e) {}
		  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
		  catch(e) {}
		  try { return new ActiveXObject("Msxml2.XMLHTTP"); }
		  catch(e) {}
		  try { return new ActiveXObject("Microsoft.XMLHTTP"); }
		  catch(e) {}
		  throw new Error("This browser does not support XMLHttpRequest.");
 
	}
}


function release_channel() {
	// to avoid problems with connections not being closed properly in IE6
	http_sub_request.abort();
}


function subscribe_channel(channel_id,last_mod_header,etag_header,cookie_domain) {
// set etag_header to 'no_etag' to try to get etag from cookie

 
  http_sub_request = get_ajax();


  if (!http_sub_request) {
	         
            // display quite warning on top of the page
			//alert('Uaktualnij swoją przegladarkę. Obecna wersja Twojej przeglądarki nie wspiera opcji chat.');
            return false;
  } else {
	  
	    subscribe_url='/subscribe?id='+channel_id;
		
		no_cache_url=subscribe_url+'&ac=' + String((new Date()).getTime()).replace(/\D/gi,''); // to avoid caching
        http_sub_request.open('GET', no_cache_url, true);

		nowdate = new Date();
        http_sub_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
		http_sub_request.setRequestHeader("Cache-Control", "no-cache");
        http_sub_request.setRequestHeader("Pragma","no-cache");
	   

		if (etag_header!='no_etag') {
			 http_sub_request.setRequestHeader('If-None-Match', etag_header);
			 Set_Cookie('chat_etag'+channel_id, etag_header, 1, '/', cookie_domain, '' );
		} else { //get etag from cookie
			 cookie_etag_header= Get_Cookie('chat_etag'+channel_id);
			 if (cookie_etag_header!=null) { 
			     etag_header=cookie_etag_header;
			     http_sub_request.setRequestHeader('If-None-Match', cookie_etag_header);
				 Set_Cookie('chat_etag'+channel_id, cookie_etag_header, 1, '/', cookie_domain, '' );
			 }
		}
		 
        
		if (last_mod_header!=false) {
		     http_sub_request.setRequestHeader('If-Modified-Since', last_mod_header);
			 Set_Cookie('chat_last_mod'+channel_id, last_mod_header, 1, '/', cookie_domain, '' );
		}   
		else {
		    cookie_last_mod_header= Get_Cookie('chat_last_mod'+channel_id);
			if (cookie_last_mod_header!=null) { // resubscribe with proper header stored in cookie
				 last_mod_header=cookie_last_mod_header;
				 http_sub_request.setRequestHeader('If-Modified-Since', cookie_last_mod_header);
				 Set_Cookie('chat_last_mod'+channel_id, cookie_last_mod_header, 1, '/', cookie_domain, '' );
			   
			 
			}
		}
		
        http_sub_request.onreadystatechange = function() { 
		   response_subscribe_function(http_sub_request,channel_id,cookie_domain); 
        };		
		
		http_sub_request.send(null);
        xhrTimeout=setTimeout("ajaxTimeout('"+channel_id+"','"+last_mod_header+"','"+etag_header+"','"+cookie_domain+"');",55000); // set timeout to 55 sec
		
  }

}



 

function response_subscribe_function(http_sub_request,channel_id,cookie_domain) {

        if (http_sub_request.readyState == 4) {

            if (http_sub_request.status == 200) {
				clearTimeout(xhrTimeout); 

				
                last_mod_header=http_sub_request.getResponseHeader('Last-Modified');
				etag_header=http_sub_request.getResponseHeader('Etag');

				
				// DECODE XML response
				var xmldoc = http_sub_request.responseXML;
			    var root_node = xmldoc.getElementsByTagName('root').item(0);
				
        		var msg_corrupted=false;    
                
				// msg
                var msg_node=root_node.firstChild;
			    if (msg_node.childNodes[0] == undefined) {	msg_corrupted=true;	}
				else { msg_text = decodeURIComponent(unescape(msg_node.childNodes[0].nodeValue)); };  
				  
				 // from  - login
				from_node = msg_node.nextSibling;
				if (from_node.childNodes[0] == undefined) { msg_corrupted=true; }
				else { from_text = decodeURIComponent(unescape(from_node.childNodes[0].nodeValue)); }

				 // from_id  - login
				from_id_node = from_node.nextSibling;
				if (from_id_node.childNodes[0] == undefined) { msg_corrupted=true; }
				else { from_id_text = decodeURIComponent(unescape(from_id_node.childNodes[0].nodeValue)); }
				
				// user_key - private key
				user_key_node = from_id_node.nextSibling;
				if (user_key_node.childNodes[0] == undefined) { msg_corrupted=true; }
				else { user_key_text = decodeURIComponent(unescape(user_key_node.childNodes[0].nodeValue)); } 
				
				// timestamp
				timestamp_node = user_key_node.nextSibling;
				if (timestamp_node.childNodes[0] == undefined) { msg_corrupted=true; }
				else { timestamp_text = decodeURIComponent(unescape(timestamp_node.childNodes[0].nodeValue));	} 
				
				// msg_type
				msg_type_node = timestamp_node.nextSibling;
				if (msg_type_node.childNodes[0] == undefined) { msg_corrupted=true; }
				else { msg_type_text = decodeURIComponent(unescape(msg_type_node.childNodes[0].nodeValue)); } 				
			
	            if (msg_corrupted==false) {
				  
                  fxb_receive_msg(msg_text,from_text,from_id_text, user_key_text,timestamp_text,msg_type_text);
			    }
				
				subscribe_channel(channel_id,last_mod_header,etag_header,cookie_domain); // resubscribe

			} else if (http_sub_request.status == 404) {
				
				//invalid channel
            } else {
               // error
            }
        }
		
}

 

function chat_send(channel_id,message_field,from,from_id,user_key,event) {
	var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

	 message=message_field.value;
	 
	if (theCode == 13) { // enter detected

	  var reNewLines=/[\n\r]/g;
      message=message.replace(reNewLines, "");
      if  (message.length>1500) { alert('Zbyt długa wiadomość - pojedyńcza wiadomość nie może być dłuższa niż 3000 znaków.');}
	  else {
      	if ((message.length>0)&&(message!="\r\n")) {
		  from=fxb_ucfirst(from);
		  send_channel(channel_id,message,from,from_id,user_key);
	  	}
	  	message=message_field.value;
	 	message_field.value=''; 
	  
	  }
	  return false;
	} else return true;
}





// send a message to the channel
function send_channel(channel_id,message,from,from_id,user_key) {

   http_request =  get_ajax(); 

   if (!http_request) {
       alert('Uaktualnij swoją przegladarkę. Obecna wersja Twojej przeglądarki nie wspiera opcji chat.');
        return false;
    } else {

       url='/publish?id='+channel_id;

		url=url+'&ac=' + String((new Date()).getTime()).replace(/\D/gi,'');
        http_request.open('POST', url, true);
        http_request.onreadystatechange = function() { 
            // put your request functions here
		   response_send_function(http_request,channel_id); 
        };
		send_date=new Date();
		sent_time=send_date.getTime();
		postvar='<?xml version="1.0" encoding="ISO-8859-2" ?><root><msg>'+escape(encodeURIComponent(message))+'</msg><from>'+escape(encodeURIComponent(from))+'</from><fromid>'+escape(encodeURIComponent(from_id))+'</fromid><userkey>'+escape(encodeURIComponent(user_key))+'</userkey><timestamp>'+escape(encodeURIComponent(sent_time))+'</timestamp><msgtype>1</msgtype></root>';
 
        http_request.setRequestHeader('Content-Type', 'application/xml'); 
		http_request.setRequestHeader("Cache-Control", "no-cache");
		
		http_request.setRequestHeader("Pragma","no-cache");
        http_request.send(postvar);
		
		// remove html tags
  	    message=message.fxb_htmlentities();
	    from=from.fxb_htmlentities();
		
		user_id=channel_id.substring(6);
        box_id=global_login_id+'_'+user_id;
		
		
		text_div_id='fxb_chat_txt_'+box_id;
        //document.getElementById(text_div_id).scrollTop+=30;
	 
		x_lines=Math.round(document.getElementById(text_div_id).innerHTML.length/45);
		document.getElementById(text_div_id).scrollTop=x_lines*30;
		 // display and log a message in the chat history
		fxb_display_message(box_id,'Ja',message,sent_time,1);
		 
    }  
}





function response_send_function(http_request,channel_id) {
var n_online;
        if (http_request.readyState == 4) {

            if (http_request.status == 202) {

				send_date=new Date();
		        sent_time=send_date.getTime();
				user_id=channel_id.substring(6);
                box_id=global_login_id+'_'+user_id;
				
				if (document.getElementById('fxb_single_friend_id_'+user_id)!= undefined) {
					
					obj=document.getElementById('fxb_single_friend_id_'+user_id);
					obj.parentNode.removeChild(obj);
					n_online=document.getElementsByName('fxb_n_online_friends').length;
					document.getElementById('fxb_online_friends_lnk').innerHTML='Czat (<strong>'+n_online+'</strong>)';
					if (n_online==0) document.getElementById('fxb_friends_list_content').innerHTML='<div class="fxb_empty_online_list" id="fxb_empty_online_list">Brak znajomych online.</div>';
				}
				
				fxb_display_message(box_id,'0','Użytkownik nie jest już dostepny online.',sent_time,2);
				
			} else if (http_request.status == 201) {

				// accepted and there are some subscribers 		

            } else {
               // general error
			    send_date=new Date();
		        sent_time=send_date.getTime();
				user_id=channel_id.substring(6);
                box_id=global_login_id+'_'+user_id;
				fxb_display_message(box_id,'0','Wystąpił błąd. Wiadomość nie została dostarczona.',sent_time,2);
			   
            }
        }
		
}











function fxb_onload(channel_id,domain,my_login_id,my_login,my_private_user_key) {
// put here all chat onload functions

 global_login_id=my_login_id; // my login id
 global_login=my_login;
 global_login_key=my_private_user_key; // my private identification key to confirm that msg comes from me
 global_domain=domain;
 fxb_prel_img(); // preload images for ie6
 
 // update my online status again in 1 min and 57 sec
 fxb_update_online_timeout=setTimeout('fxb_update_online_status();',117000);

 subscribe_channel(channel_id,false,'no_etag',domain); // subscribe to chat channel
 restore_chat_boxes();	 // load opened chat boxes
 
 
}

 


function fxb_update_online_status() {

 
	http_status_request = get_ajax();
	if (!http_status_request) { return false;} // ajax error
	else {
	     var no_cache_url;
	 	 no_cache_url='/ajax/chat_online_status.php'+'?&ac=' + String((new Date()).getTime()).replace(/\D/gi,''); // avoid caching
         http_status_request.open('GET', no_cache_url, true);
		 http_status_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
		 http_status_request.setRequestHeader("Cache-Control", "no-cache");
         http_status_request.setRequestHeader("Pragma","no-cache");

		   
		 http_status_request.onreadystatechange = function() { 
		    if (http_status_request.readyState == 4) {
		        if (http_status_request.status == 200) {
					 
		               clearTimeout(xhrTimeout_online_status); 
					   // update my online status again in 1 min and 57 sec
 		               fxb_update_online_timeout=setTimeout('fxb_update_online_status();',117000); 
					}  
				}
		 }
 
         http_status_request.send(null);
		 xhrTimeout_online_status=setTimeout('fxb_online_status_timeout();',10000); // set timeout to 10 sec
		
		}
 
}



// in case server is busy - so we wont lock other requests
function fxb_online_status_timeout() {
   http_status_request.abort();
   // update my online status again in 1 min and 57 sec
   fxb_update_online_timeout=setTimeout('fxb_update_online_status();',117000);
}

 



 
 









var  global_user_channel_id;
var  global_user_name;
var  global_user_img;
var  global_user_info;
var  global_user_history='';
var  global_history_scroll=0;


function fxb_prel_img() {
// IE6 bug - IE6 doesn't load images created via DOM
 fxb_image1 = new Image();
 fxb_image1.src = "/img/broadcast.gif";
 fxb_image2 = new Image();
 fxb_image2.src = "/img/fxb_minimize.gif";
 fxb_image3 = new Image();
 fxb_image3.src = "/img/fxb_close.gif";
 fxb_image4 = new Image();
 fxb_image4.src = "/img/fxb_grad2.gif";
 fxb_image5 = new Image();
 fxb_image5.src = "/img/fxb_grad3.gif";

}

/* User interface */
// start chat with that user
function fxb_chat_open(channel_id,user_name,user_img,user_info) {
// channel_id=channelKEY(5)_userID
 
   user_id=channel_id.substring(6);
   box_id=global_login_id+'_'+user_id;
   
   if (fxb_load_chat_history(box_id)==false) {
	   // set empty chat history cookie
	   global_user_history='';
       global_history_scroll=0;
	   fxb_set_chat_history_cookie(box_id,channel_id,user_name,user_img,user_info);
   }
	
   global_user_channel_id=channel_id;
   global_user_name=user_name;
   
   if (user_img=='') {global_user_img=false;}
   else {global_user_img=user_img;}
   
   global_user_info=user_info;
   
   create_chat_box(box_id,'0',1);		
  
	

}


// save chat history box cookie
//cookie format: channel_id|user_name|user_img|user_info|chat_history
function fxb_set_chat_history_cookie(box_id,channel_id,user_name,user_img,user_info) {

  cookie_name='fxb_box_'+box_id;
  cookie_value=encodeURIComponent(channel_id)+'|'+encodeURIComponent(user_name)+'|'+encodeURIComponent(user_img)+'|'+encodeURIComponent(user_info)+'|';
  Set_Cookie( cookie_name, cookie_value, '', '/', global_domain, '' );
  
}

 


 



function fxb_receive_msg(message,from,from_id, user_key,timestamp,msg_type) {
	 
	 message=message.fxb_htmlentities();
	 from=from.fxb_htmlentities();
	 
	msg_user=from;
	msg_user_key=user_key;
    confirm_sender_status=false;

object_id='fxb_usr_key_'+msg_user;
 
// confirm identity of sender via object_id 
if (document.getElementById(object_id) != null) {
    if  (document.getElementById(object_id).value==msg_user_key) { 
	  //confirm_sender_status=true;
	  fxb_receive_msg_confirmed(message,from,from_id, user_key,timestamp,msg_type);
	}
	else {} //confirm_sender_status=false;
}
else { // confirm identity of sender via ajax
   
	http_confirm_request = get_ajax();
	if (!http_confirm_request) { confirm_sender_status=false;} // ajax error
	else {
	     var no_cache_url;
	 	 no_cache_url='/ajax/chat_user_confirm.php'+'?&ac=' + String((new Date()).getTime()).replace(/\D/gi,''); // avoid caching
         http_confirm_request.open('POST', no_cache_url, true);
		 http_confirm_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
		 http_confirm_request.setRequestHeader("Cache-Control", "no-cache");
         http_confirm_request.setRequestHeader("Pragma","no-cache");

		   
		 http_confirm_request.onreadystatechange = function() { 
		    if (http_confirm_request.readyState == 4) {
		        if (http_confirm_request.status == 200) {
					 
		            clearTimeout(xhrTimeout_confirm); 
					// process xml response
					var xmldoc2 = http_confirm_request.responseXML;
					
					// global_user_channel_id,global_user_name,global_user_img,global_user_info
				    var confirmed_node = xmldoc2.getElementsByTagName('confirmed').item(0);
					var login_node = xmldoc2.getElementsByTagName('login').item(0); 
			        var login_id_node = xmldoc2.getElementsByTagName('login_id').item(0);
					var channel_id_node = xmldoc2.getElementsByTagName('channel_id').item(0);			
					var user_info_1_node = xmldoc2.getElementsByTagName('user_info_1').item(0);
					var user_info_2_node = xmldoc2.getElementsByTagName('user_info_2').item(0);
					var user_img_node = xmldoc2.getElementsByTagName('user_img').item(0);
					
					  
                    if (confirmed_node.firstChild.data=='true') { // identity confirmed - display the message
						
						// create id fields so confirm via ajax isn't needed anymore
					
						hidden_login_id='fxb_usr_id_'+login_id_node.firstChild.data;
						
						new_hidden = document.createElement('input');
                        new_hidden.id='fxb_usr_key_'+msg_user; 
                        new_hidden.setAttribute('type', 'hidden');
                        new_hidden.setAttribute('name', hidden_login_id);
                        new_hidden.setAttribute('value', msg_user_key);


                        document.body.appendChild(new_hidden);	
						
						global_user_channel_id=channel_id_node.firstChild.data;
						global_user_name=login_node.firstChild.data;
						global_user_img=user_img_node.firstChild.data;
						global_user_info=user_info_1_node.firstChild.data+'<br />'+user_info_2_node.firstChild.data;
						 					
						fxb_receive_msg_confirmed(message,from,from_id, user_key,timestamp,msg_type);
						
					} else {} // confirm_sender_status=false;
					
					
				}
				else {} // confirm_sender_status=false;
			}
			else {} // confirm_sender_status=false;
		    
		 }
	 
		 postvar='login='+msg_user+'&user_key='+msg_user_key;
 
         http_confirm_request.send(postvar);
		 xhrTimeout_confirm=setTimeout('fxb_confirm_sender_timeout();',10000); // set timeout to 10 sec
		
		}
		 
	}
  
}

// in case server is busy - so we wont lock other requests
function fxb_confirm_sender_timeout() {
   http_confirm_request.abort();
}

 







function fxb_receive_msg_confirmed(message,from,from_id, user_key,timestamp,msg_type) {

     
   		box_id=global_login_id+'_'+from_id;
		 
		//box exists
		if (document.getElementById('chat_box_'+box_id) != null) {
			
			// if minimized  - blink
			if (document.getElementById('chat_box_'+box_id).style.display=='none') {
		   	     document.getElementById('chat_button_'+box_id).style.backgroundImage='url(img/fxb_grad3.gif)';
				 fxb_state_minmax_box(box_id,'2');
			} 
			 
			// display the message
			 
			fxb_display_message(box_id,from,message,timestamp,1);
			
		} else { // create box
		         var n;
				 var i;
				 var create_minimized;

				 // check if any box is already maximized
				 create_minimized=false;
				 n=document.getElementsByName('fxb_id_box').length;
				 
				 for (i=0;i<n;i++) {	 

			          div_id=document.getElementsByName('fxb_id_box')[i].id;	 
		    		  extracted_box_id=div_id.substring(7);
					  check_box_id='chat_box_'+extracted_box_id;
					  if (document.getElementById(check_box_id).style.display=='block') create_minimized=true; 
					  
		         }
		         
				 // load chat history & user info from cookie or ajax
		         if (fxb_load_chat_history(box_id)==false) {
 
					 //no cookie - global_user_info has already been set up by confirm request
                      global_user_history='';
                      global_history_scroll=0;
					  // save in the cookie
			          fxb_set_chat_history_cookie(box_id,global_user_channel_id,global_user_name,global_user_img,global_user_info);
			
					 
					 
				 } else {
						// global_user_info   loaded from a cookie
				 }
				 
				 // if any box is maximized - create minimized box 
			     if (create_minimized==true) { 
				         create_chat_box(box_id,2,1);  
				 }
				 else { // create normal box
				
				         create_chat_box(box_id,0,1);  
				 }
				 
				 // display the message
				 
				 fxb_display_message(box_id,from,message,timestamp,1);	
					
		}
		

}









var timestamp_timeout = new Array();

// display message and log history
function fxb_display_message(box_id,user,msg,timestamp,msg_type) {
// msg_type:	 1- normal message, 2- info/error msg
	
var chat_history;

// add message to the chat box
text_div_id='fxb_chat_txt_'+box_id;
if (msg_type==1)	{
	div_message='<div class="fxb_msg"><strong>'+user+': </strong>'+fxb_replace_ico(msg)+'</div>';
	document.getElementById(text_div_id).innerHTML=document.getElementById(text_div_id).innerHTML+div_message;
	
	// display last message time
	var current_time;
	var time=new Date();
	var month=time.getMonth();
	month+=1;
	var day=time.getDay()
	var hour=time.getHours();
	var minutes=time.getMinutes();
	var seconds=time.getSeconds();
	month=month+"";
	hour=hour+"";
	minutes=minutes+"";
	seconds=seconds+"";
	 
	if (month.length==1) month="0"+month;
	if (hour.length==1) hour="0"+hour;
	if (minutes.length==1) minutes="0"+minutes;
	if (seconds.length==1) seconds="0"+seconds;
	 
	
	current_time=day+'/'+month+'/'+time.getFullYear()+' '+hour+ ':'+minutes+':'+seconds;
	
    if (timestamp_timeout[box_id]!=undefined) clearTimeout(timestamp_timeout[box_id]); 
    timestamp_timeout[box_id]=setTimeout("fxb_last_message('"+box_id+"','"+current_time+"');",120000); 	
	
} else {
	div_message='<div class="fxb_msg2">'+msg+'</div>';
	document.getElementById(text_div_id).innerHTML=document.getElementById(text_div_id).innerHTML+div_message;
}

//auto scroll down

 document.getElementById(text_div_id).scrollTop+=30;
 x_lines=Math.round(msg.length/45)*30;
 document.getElementById(text_div_id).scrollTop+=x_lines;
	 
 // log chat history
  cookie_name='fxb_box_'+box_id;
  cookie_box_info=Get_Cookie(cookie_name); 
  
  if (cookie_box_info!=null) { 
  
  
		 	cookie_box_info=cookie_box_info.split('|'); 
				
			
				
			compressed_line=encodeURIComponent(user)+':'+encodeURIComponent(msg)+':'+timestamp+':'+msg_type+':';
			new_history_size=cookie_box_info[4].length+compressed_line.length;
			
			chat_history=decodeURIComponent(cookie_box_info[4]);
						
	        if (new_history_size >2000) {
			   // cut off old history in the way that it removes only full lines
			   		   
			    length_to_remove= new_history_size - 2000;
			   
			   	decompressed_data=chat_history.split(':');
			    var i;
			    var n;

 
			    n=decompressed_data.length -1;
				
				 	  
			    skip_line=true;
				chat_history='';
				
			    for (i=0;i<n;i=i+4) {	
				
					
				
				    if (skip_line==false) {
					  chat_history=chat_history+decompressed_data[i]+':'+decompressed_data[i+1]+':'+decompressed_data[i+2]+':'+decompressed_data[i+3]+':';
					  
					}
					else {


						line_length=decompressed_data[i].length;
						line_length+=decompressed_data[i+1].length; 
						line_length+=decompressed_data[i+2].length; 
						line_length+=decompressed_data[i+3].length;
						length_to_remove=length_to_remove-line_length;						
				 		
						if (length_to_remove>0) {}
						else skip_line=false;
					}
					
			}
			   

			}  
				 
		chat_history=chat_history+encodeURIComponent(user)+':'+encodeURIComponent(msg)+':'+timestamp+':'+msg_type+':';
				
			 
	 
			chat_history=encodeURIComponent(chat_history);
			
			new_cookie_value=cookie_box_info[0]+'|'+cookie_box_info[1]+'|'+cookie_box_info[2]+'|'+cookie_box_info[3]+'|'+chat_history;
			
			Set_Cookie( cookie_name, new_cookie_value, '', '/', global_domain, '' );
  }
  
  
			
}




function fxb_menu_over(object) {
	 document.getElementById(object.id).getElementsByTagName('a')[0].style.textDecoration ='underline';
}

function fxb_menu_out(object) {
	document.getElementById(object.id).getElementsByTagName('a')[0].style.textDecoration ='none';
	document.getElementsByName('fxb_menu_lnk')[0].style.textDecoration ='none';
}


// online friends list
function fxb_friends_box(object) {
	if (document.getElementById('fxb_friends_online_box_cont').style.display=='block') {
		document.getElementById('fxb_friends_online_box_cont').style.display='none'	;
		document.getElementById('fxb_friends_online_button').style.backgroundImage='none';
        fxb_state_friends_box('0');
	}
	else { 
	   fxb_refresh_online_friends();
	   document.getElementById('fxb_friends_online_box_cont').style.display='block';
	   document.getElementById('fxb_friends_online_button').style.backgroundImage='url(img/fxb_grad2.gif)';
	   fxb_state_friends_box('1');
 
	}
 
}



function chat_box_close(box_id) {

    obj=document.getElementById('chat_box_'+box_id); 
	obj.parentNode.removeChild(obj);
	
	obj=document.getElementById('chat_button_'+box_id); 
	obj.parentNode.removeChild(obj);
	fxb_state_close_box(box_id);

	
	// move other boxes to the left
    n=document.getElementsByName('fxb_id_box').length;
		  
    for (i=0;i<n;i++) {	 
		     div_id=document.getElementsByName('fxb_id_box')[i].id;	 
		     extracted_box_id=div_id.substring(7);		  
			  
             new_pos=185+(((n-1)-i)*175);
             document.getElementById('chat_box_'+extracted_box_id).style.marginRight=new_pos+'px';
	 
		 } 
	if (n==0) document.getElementById('fxb_chat_buttons').style.borderRightWidth='0px';
}



function chat_box_min(box_id) {
if (document.getElementById('chat_box_'+box_id).style.display!='none') {
    document.getElementById('chat_box_'+box_id).style.display='none';
	document.getElementById('chat_button_'+box_id).style.backgroundImage='none';
	fxb_state_minmax_box(box_id,1);
}
}

function chat_box_min_all() {
// minimize all boxes
var i;
var n;
  n=document.getElementsByName('fxb_id_box').length;
   
	     for (i=0;i<n;i++) {	 
		     div_id=document.getElementsByName('fxb_id_box')[i].id;	 
		     extracted_box_id=div_id.substring(7);

			 chat_box_min(extracted_box_id);
		 }
}

function chat_box_max(box_id) {
	
	  //minimize other windows
      chat_box_min_all();
     // max requested box
	 document.getElementById('chat_box_'+box_id).style.display='block';
	 document.getElementById('chat_button_'+box_id).style.backgroundImage='url(img/fxb_grad2.gif)';
	 setTimeout("fxb_ie6_scroll_down('"+box_id+"')",0);
	 document.getElementById('fxb_form_message_'+box_id).focus();
	 fxb_state_minmax_box(box_id,0);
	
}

function chat_box_minmax(box_id) {
	if (document.getElementById('chat_box_'+box_id).style.display=='block') {
      chat_box_min(box_id);
	}
	else { // maximize
	  chat_box_max(box_id);
	}
 
}

 
// BOX_box_id = (my_login_id)_(other_login_id)

function create_chat_box(box_id,state,set_new_cookie) {
// state: 0-normal(maximized,don't blink), 1-minimized(don't blink), 2-minimized(new message - blink)  
// send_id = pid = user channel id
// global_user_channel_id = where to send the messages ,global_user_info- info about the user displayed at the top of the box
// set_new_cookie: 0 -no(used when restoring boxes while refreshing page), 1-set state cookie 

 

if  (document.getElementById('chat_button_'+box_id)==null) {

// minimize other boxes
if (state==0) chat_box_min_all();


document.getElementById('fxb_chat_buttons').style.borderRightWidth='1px';  

// create button
document.getElementById('fxb_chat_buttons').innerHTML='<div id="chat_button_'+box_id+'"  onmouseover="javascript:fxb_menu_over(this)" onmouseout="javascript:fxb_menu_out(this)" class="fxb_chat_button_c" style="background-image:url(/img/fxb_grad2.gif);"><div class="fxb_chat_button_n" onclick="javascript:chat_box_minmax(\''+box_id+'\')"><div style="float: left; padding:2px 6px 0px 0px;"><img src="/img/broadcast.gif"  height="9" width="8"></div><div style="float: left;"><a href="javascript:void(0)" id="fxb_id_'+box_id+'"  name="fxb_id_box"  class="fxb_menu_lnk">'+global_user_name+'</a></div><div class="clear-both2"></div></div><div style="float:right;padding:5px 10px 5px 0px;"><a href="javascript:chat_box_close(\''+box_id+'\')" title="Zamknij"><img src="/img/fxb_close.gif" width="14" height="14" border="0"  /></a></div><div class="clear-both2"></div></div>'+document.getElementById('fxb_chat_buttons').innerHTML;

// new message - blink
if (state==1)  document.getElementById('chat_button_'+box_id).style.backgroundImage='none';
else if (state==2)  document.getElementById('chat_button_'+box_id).style.backgroundImage='url(/img/fxb_grad3.gif)';

// create chat box
 new_div = document.createElement('div');
 new_div.id='chat_box_'+box_id;
 new_div.className ='fxb_chat_box_c';
 new_div.style.display='block';
 n_boxes=document.getElementsByName('fxb_id_box').length;
 pos_margin_right=185+(n_boxes-1)*175;
 new_div.style.marginRight=pos_margin_right+'px';

//minimized window - don't show chat box
if (state!=0) {
	 new_div.style.display='none';
}

var user_img;
if ((global_user_img==false) || (global_user_img=='false')) {
user_img= '<img src="img/default/no-img-profil.gif" height="21" width="28"  class="fxb_img_border">';
} else {
user_img= '<img src="storage/users/'+global_user_img+'" border="0" height="23" width="30">';
}
 
 
 new_div.innerHTML='<div class="fxb_friends_header">' +
'<div class="fxb_chat_box_h">Czat z '+global_user_name+'</div>' +
'<div style="float:right;padding-left:3px;"><a href="javascript:chat_box_close(\''+box_id+'\')" title="Zamknij"><img src="/img/fxb_close.gif" width="14" height="14" border="0"  /></a></div>' +
'<div style="float:right;"><a href="javascript:chat_box_min(\''+box_id+'\')" title="Minimalizuj"><img src="/img/fxb_minimize.gif" width="14" height="14" border="0"  /></a></div>' +
'<div class="clear-both2"></div></div>' +

'<div class="fxb_chat_box_i">' +
'<div class="fxb_chat_box_i2">' +
'<div class="fxb_chat_box_i3">' +
'<a href="profil/'+global_user_name+'" title="Zobacz profil">'+user_img+'</a>'+
'</div>' +
'<div class="fxb_chat_box_i4">'+global_user_info+'</div>' +
'<div class="clear-both2"></div></div>' +

'<div id="fxb_chat_txt_'+box_id+'" class="fxb_chat_box_txt">'+global_user_history+

'</div>'+
 
'<div id="chat_send" class="fxb_chat_box_send">' +
'<form action="chat.php" method="post" enctype="application/x-www-form-urlencoded" id="fxb_form_chat_'+box_id+'" name="form_chat">' +
'<textarea name="message" cols="19" rows="2" id="fxb_form_message_'+box_id+'" wrap="virtual"  onkeydown="javascript:return chat_send(\''+global_user_channel_id+'\',this,\''+global_login+'\',\''+global_login_id+'\',\''+global_login_key+'\',event)"  class="fxb_chat_textarea"></textarea></form></div>' +
'</div>';
 
 
 

 
document.body.appendChild(new_div);	
     
setTimeout("fxb_ie6_scroll_down('"+box_id+"')",0); // running function from timeout otherwise IE6 won't scoll down
 
if (set_new_cookie==1) {
  if (state==0) fxb_state_open_box(box_id,'0');
  else if (state==2) fxb_state_open_box(box_id,'2');
  else fxb_state_open_box(box_id,'1');
}

if (state==0) document.getElementById('fxb_form_message_'+box_id).focus();
 
} 
else { chat_box_max(box_id);}
 
}





function fxb_ie6_scroll_down(box_id) {
	 
document.getElementById('fxb_chat_txt_'+box_id).scrollTop+=global_history_scroll;	
 
	}


function restore_chat_boxes() {
// fxb_state cookie: 1st value- 0 - contact list box|1-open 0-closed| box_id_1|1-open,2-minimized|box_id_2|1-open,2-minimized  etc...	
// example 0|1|122_123|2|122_124|1
 
	var i;
	var n;
	var z;

	
	cookie_name='fxb_state_'+global_login_id;
	cookie_value=Get_Cookie(cookie_name); // check which boxes are open

    if (cookie_value!=null) {
		 cookie_value=cookie_value.split('|');
 
         if (cookie_value[1]=='1') {
			 document.getElementById('fxb_friends_online_box_cont').style.display='block';
	         document.getElementById('fxb_friends_online_button').style.backgroundImage='url(img/fxb_grad2.gif)';
		 } 
		 
		 
	     n=cookie_value.length;
 
		 for (i=2;i<n;i=i+2) {
			 z=i+1;
             box_id=cookie_value[i]; 
			 state=cookie_value[z]; 
			  
			  
			 
			 // load box info and chat history from a cookie
			 // cookie format: user_channel_id|user_name|user_img|user_info|chat_history
 
		     if (fxb_load_chat_history(box_id)==true) {
				  
	   	           create_chat_box(box_id,state,'0');
		     } else {  // no cookie - create box with info from ajax query 
				        //get global variables from ajax query
		    		var user_id;
					var box_id_part;
					box_id_part = box_id.split('_');
					user_id=box_id_part[1];
					
			        global_user_history='';
                    global_history_scroll=0;
					
   		    	    fxb_get_ajax_user_info(user_id,state);
			 }
		 }
		 
	}
	
	 
 
}





// call this function before creating new window
function fxb_load_chat_history(box_id) {
 
  var	cookie_box_info;


   
  
  cookie_name='fxb_box_'+box_id;
  cookie_box_info=Get_Cookie(cookie_name); 
  
  if (cookie_box_info!=null) { 
  
            global_user_channel_id='';
            global_user_name='';
            global_user_img='';
            global_user_info='';
            global_user_history='';
            global_history_scroll=0;
  
		 	cookie_box_info=cookie_box_info.split('|');
				
         	global_user_channel_id=decodeURIComponent(cookie_box_info[0]);
            global_user_name=decodeURIComponent(cookie_box_info[1]);
			global_user_img=decodeURIComponent(cookie_box_info[2]);
			global_user_info=decodeURIComponent(cookie_box_info[3]);
				
			var compressed_user_history;
			compressed_user_history=	decodeURIComponent(cookie_box_info[4])
				
			//encodeURIComponent(user)+':'+encodeURIComponent(msg)+':'+timestamp+':'+msg_type+':';
			var compressed_data;
			compressed_data=compressed_user_history.split(':');
			var i;
			var n;
			var z;
			var k;
			var msg;
			n=compressed_data.length;
			
			for (i=0;i<n;i=i+4) {	
				
				z=i+1;
				k=i+3;
				// put together in html format
				msg=decodeURIComponent(compressed_data[z])
				
				if (compressed_data[k]==1) {
				     global_user_history=global_user_history+'<div class="fxb_msg"><strong>'+decodeURIComponent(compressed_data[i])+': </strong>'+fxb_replace_ico(msg)+'</div>' ; 
				} else if (compressed_data[k]==2)  {
					 global_user_history=global_user_history+'<div class="fxb_msg2">'+msg+'</div>' ; 
				}
				//scroll_layer_id='fxb_chat_txt_'+box_id;
				global_history_scroll+=30;
				x_lines=Math.round(msg.length/45)*30;
                global_history_scroll+=x_lines;
				 
			
			}
			
		 
				
	        return true;		 	 
  } 
  else return false;
}




function fxb_get_ajax_user_info(user_id,state) {
	
 
  
  http_info_request = get_ajax();
  if (!http_info_request) { return false; } // ajax error
  else {
	 var no_cache_url;
	 no_cache_url='/ajax/chat_user_info.php'+'?&ac=' + String((new Date()).getTime()).replace(/\D/gi,''); // avoid caching
	 http_info_request.open('POST', no_cache_url, true);
	 http_info_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	 http_info_request.setRequestHeader("Cache-Control", "no-cache");
	 http_info_request.setRequestHeader("Pragma","no-cache");
		 
	 http_info_request.onreadystatechange = function() { 
			 if (http_info_request.readyState == 4) {
		         if (http_info_request.status == 200) {
		            clearTimeout(xhrTimeout_user_info); 
					// process xml response
					var xmldoc2 = http_info_request.responseXML;
								 
				    // return xml data only if you are friend or his channel_id is public otherwise set all fields to null
				   
					var login_node = xmldoc2.getElementsByTagName('login').item(0); 
			        var login_id_node = xmldoc2.getElementsByTagName('login_id').item(0);
					var channel_id_node = xmldoc2.getElementsByTagName('channel_id').item(0);			
					var user_info_node = xmldoc2.getElementsByTagName('user_info').item(0);
					var user_img_node = xmldoc2.getElementsByTagName('user_img').item(0);
			 
				    box_id=global_login_id+'_'+user_id;		
					global_user_channel_id=channel_id_node.firstChild.data;
					global_user_name=login_node.firstChild.data;
					global_user_img=user_img_node.firstChild.data;
					global_user_info=user_info_node.firstChild.data;
					
					
					 // save in the cookie
			        fxb_set_chat_history_cookie(box_id,global_user_channel_id,global_user_name,global_user_img,global_user_info);
					create_chat_box(box_id,state,'0');
			  
						
                    return true;
					
				}
				else return false;
			}
			else return false;
		    // return fxb_confirm_response(http_info_request);
		 }
		 postvar='login_id='+user_id;
         http_info_request.send(postvar);
		 xhrTimeout_user_info=setTimeout('fxb_user_info_timeout();',10000); // set timeout to 10 sec
		
  }	 
 

}



function fxb_user_info_timeout() {
	http_info_request.abort();
}



// functions responsible for state of fixed bar- while reloading pages

function fxb_state_minmax_box(box_id,box_state) {
// box_state: 1 -minimize, 0-maximize,  2-minimized blink
var i;
var n;
var z;

    cookie_name='fxb_state_'+global_login_id;
	cookie_value=Get_Cookie(cookie_name);
	if (cookie_value==null) { new_cookie_value='0|0';}
    else {
		 cookie_value=cookie_value.split('|');
		 new_cookie_value=cookie_value[0]+'|'+cookie_value[1];
		 n=cookie_value.length;
 
		 for (i=2;i<n;i=i+2) {
			 z=i+1;
             if (box_id==cookie_value[i]) {
			     new_cookie_value=new_cookie_value+'|'+cookie_value[i]+'|'+box_state;
			 } else {
				 new_cookie_value=new_cookie_value+'|'+cookie_value[i]+'|'+cookie_value[z];
			 }
		 }
	}
	Set_Cookie( cookie_name, new_cookie_value, '', '/', global_domain, '' );
    
}

 
 
 
 
 

function fxb_state_open_box(box_id,box_state) {
// add new box to state cookie - to keep open boxes while reloading pages
// box_state: 1 -minimized, 0-normal

cookie_name='fxb_state_'+ global_login_id;

cookie_value=Get_Cookie(cookie_name);  
if (cookie_value==null)  cookie_value='0|0';
 
cookie_value=cookie_value+'|'+box_id+'|'+box_state;

Set_Cookie( cookie_name, cookie_value, '', '/', global_domain, '' );
 
}


function fxb_state_close_box(box_id) {
// remove box from state cookie	
var i;
var n;
var z;

    cookie_name='fxb_state_'+global_login_id;
	cookie_value=Get_Cookie(cookie_name);
	if (cookie_value==null) { new_cookie_value='0|0';}
    else {
		 cookie_value=cookie_value.split('|');
		 new_cookie_value=cookie_value[0]+'|'+cookie_value[1];
		 n=cookie_value.length;
 
		 for (i=2;i<n;i=i+2) {
			 if (box_id!=cookie_value[i]) {
				 z=i+1;
				 new_cookie_value=new_cookie_value+'|'+cookie_value[i]+'|'+cookie_value[z];
			 }
		 }
		
	}
	Set_Cookie( cookie_name, new_cookie_value, '', '/', global_domain, '' );
 
 // remove cookie with history if no chat history
 cookie_name='fxb_box_'+box_id;
 cookie_box_info=Get_Cookie(cookie_name); 
  
  if (cookie_box_info!=null) { 
		 	cookie_box_info=cookie_box_info.split('|'); 
			if (cookie_box_info[4].length<1) {Delete_Cookie(cookie_name,'/',global_domain);}		 
  }
 
}



function fxb_state_friends_box(state) {
//state: 1-open 0-closed

  cookie_name='fxb_state_'+global_login_id;
  cookie_value=Get_Cookie(cookie_name);
	if (cookie_value==null) { 
	     new_cookie_value='0|'+state;   
	}
    else {
		  new_cookie_value='0|'+state+cookie_value.substring(3);	
	}
  Set_Cookie( cookie_name, new_cookie_value, '', '/', global_domain, '' );	
	
}






// connect via ajax and confirm identity of sender
function fxb_refresh_online_friends() {
 
      
	http_friends_request = get_ajax();
	if (!http_friends_request) { return false;} // ajax error
	else {
	     var no_cache_url;
	 	 no_cache_url='/ajax/chat_friends_online.php'+'?&ac=' + String((new Date()).getTime()).replace(/\D/gi,''); // avoid caching
         http_friends_request.open('POST', no_cache_url, true);
		 http_friends_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
		 http_friends_request.setRequestHeader("Cache-Control", "no-cache");
         http_friends_request.setRequestHeader("Pragma","no-cache");
		  
		 http_friends_request.onreadystatechange = function() { 
		      if (http_friends_request.readyState == 4) {     
		        if (http_friends_request.status == 200) {  
		             clearTimeout(xhrTimeout_friends);  
					// process xml response
					var xmldoc2 = http_friends_request.responseXML;
					
				    var n_online_node = xmldoc2.getElementsByTagName('n_online').item(0);
					
					var login_node = xmldoc2.getElementsByTagName('login').item(0); 
			        var login_id_node = xmldoc2.getElementsByTagName('login_id').item(0);
					var channel_id_node = xmldoc2.getElementsByTagName('channel_id').item(0);			
					var user_info_1_node = xmldoc2.getElementsByTagName('user_info_1').item(0);
					var user_info_2_node = xmldoc2.getElementsByTagName('user_info_2').item(0);
					var user_img_node = xmldoc2.getElementsByTagName('user_img').item(0);
					var user_rate_node = xmldoc2.getElementsByTagName('user_rate').item(0); 

					n_online=n_online_node.firstChild.data;
					 
                    if (n_online=='0') { 
					   document.getElementById('fxb_friends_list_content').innerHTML='<div class="fxb_empty_online_list">Brak znajomych online.</div>';
					   document.getElementById('fxb_online_friends_lnk').innerHTML='Czat (<strong>'+n_online+'</strong>)';
					}
					else {	
						// refresh list
					    document.getElementById('fxb_online_friends_lnk').innerHTML='Czat (<strong>'+n_online+'</strong>)';
						
						var list_html;
						var i;
						list_html='';
					    for (i=0;i<n_online;i++) {						 
		 
					
							login_node = xmldoc2.getElementsByTagName('login').item(i).firstChild.data; 
							login_id_node = xmldoc2.getElementsByTagName('login_id').item(i).firstChild.data;
							channel_id_node = xmldoc2.getElementsByTagName('channel_id').item(i).firstChild.data;			
							user_info_1_node = xmldoc2.getElementsByTagName('user_info_1').item(i).firstChild.data;
							user_info_2_node = xmldoc2.getElementsByTagName('user_info_2').item(i).firstChild.data;
							user_img_node = xmldoc2.getElementsByTagName('user_img').item(i).firstChild.data;
							user_rate_node = xmldoc2.getElementsByTagName('user_rate').item(i).firstChild.data; 
							
							list_html+='<div class="fxb_friends_single" id="fxb_single_friend_id_'+login_id_node+'"><div class="fxb_friends_single_img"><a onclick="javascript:fxb_chat_open(\''+channel_id_node+'\',\''+login_node+'\',\''+user_img_node+'\',\''+user_info_1_node+'<br />'+user_info_2_node+'\')"  href="javascript:void(0)" title="Rozpocznij rozmowę"><img src="storage/users/'+user_img_node+'" border="0" height="23" width="30"></a></div><div class="fxb_friends_single_u"><a onclick="javascript:fxb_chat_open(\''+channel_id_node+'\',\''+login_node+'\',\''+user_img_node+'\',\''+user_info_1_node+'<br />'+user_info_2_node+'\')" name="fxb_n_online_friends"  href="javascript:void(0)" class="fxb_list_lnk" title="Rozpocznij rozmowę">'+login_node+' ('+user_rate_node+')</a></div><div class="clear-both2"></div></div>';
							 
						
				 
					    }
					 		
							document.getElementById('fxb_friends_list_content').innerHTML=list_html;
							
						 
						
					}  
					
					
				}
	        }			
		 }
		 
		
		 postvar='';
         http_friends_request.send(postvar);
		 xhrTimeout_friends=setTimeout('fxb_refresh_friends_timeout();',5000); // set timeout to 5 sec
		
		}
		
 
}

 
 
 
function fxb_last_message(box_id,timestamp) {
    obj=document.getElementById('fxb_last_time_'+box_id);
	if (obj!=undefined)	obj.parentNode.removeChild(obj);
	
  	document.getElementById('fxb_chat_txt_'+box_id).innerHTML+='<div class="fxb_msg2" id="fxb_last_time_'+box_id+'">Ostatnia wiadomość: '+timestamp+'</div>';
	 document.getElementById('fxb_chat_txt_'+box_id).scrollTop+=30;
} 
 
 
 
 
 
 

// in case server is busy - so we wont lock other requests
function fxb_refresh_friends_timeout() {
   http_friends_request.abort();
}
 
 
 
function fxb_ucfirst(str) {
    var firstLetter = str.substr(0, 1);
    return firstLetter.toUpperCase() + str.substr(1);
}
 
 
String.prototype.fxb_htmlentities = function () {
  return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
};
 

function fxb_replace_ico(str) {
 str=str.replace(':(','<img src="/img/forum/sad.gif" width="15" height="15" style="vertical-align:text-bottom;" />');
 str=str.replace(':-(','<img src="/img/forum/sad.gif" width="15" height="15" style="vertical-align:text-bottom;" />');
 str=str.replace(';(','<img src="/img/forum/cry.gif" width="15" height="15" style="vertical-align:text-bottom;" />');
 str=str.replace(';-(','<img src="/img/forum/cry.gif" width="15" height="15" style="vertical-align:text-bottom;" />');
 str=str.replace(':D','<img src="/img/forum/happy.gif" width="15" height="15" style="vertical-align:text-bottom;" />');
 str=str.replace(':-D','<img src="/img/forum/happy.gif" width="15" height="15" style="vertical-align:text-bottom;" />');
 str=str.replace(';)','<img src="/img/forum/wink.gif" width="15" height="15" style="vertical-align:text-bottom;" />');
 str=str.replace(';-)','<img src="/img/forum/wink.gif" width="15" height="15" style="vertical-align:text-bottom;" />'); 
 str=str.replace(':)','<img src="/img/forum/smile.gif" width="15" height="15" style="vertical-align:text-bottom;" />');
 str=str.replace(':-)','<img src="/img/forum/smile.gif" width="15" height="15" style="vertical-align:text-bottom;" />');
 
 return str;
 
}
 
 
function fxb_show_desc(n,show)  {
	
if (show==1) {	
	new_desc = document.createElement('div');
    new_desc.id='fxb_desc_'+n; 
	new_desc.className ='fxb_desc_layer';
    new_desc.style.display='block';
     
  if (n==1) {
    new_desc.style.marginLeft='182px';					
    new_desc.innerHTML='Aktualności';    
  }  
  else  if (n==2) {
    new_desc.style.marginLeft='210px';					
    new_desc.innerHTML='Znajomi';    
  } 
  else  if (n==3) {
    new_desc.style.marginLeft='240px';					
    new_desc.innerHTML='Moje Zdjęcia';    
  } 
  else  if (n==4) {
    new_desc.style.marginLeft='265px';					
    new_desc.innerHTML='Moje Kalendarium';    
  } 
  else  if (n==5) {
    new_desc.style.marginLeft='290px';					
    new_desc.innerHTML='Mój Blog';    
  }   
  
  
    document.body.appendChild(new_desc);	
} else {
	 
	obj=document.getElementById('fxb_desc_'+n);
	if (obj!=undefined) obj.parentNode.removeChild(obj);
}

}
 
//-->