
			var friends = new Array();
			var sendReq = getXmlHttpRequestObject();
			var receiveReq = getXmlHttpRequestObject();

			var receiveStatusReq = getXmlHttpRequestObject();

			var lastMessage = sender_lastmessage;
			var mTimer;
			//var LoadedLastMessage;
			var oldMessages=5;
			
			
					
			//Function for initializating the page.
			function startChat() {
				//Set the focus to the Message Box.
				//document.getElementById('txt_message').focus();
				//Start Recieving Messages.
				setMessengerStatus(laststatus);
				document.onmousedown=selectmouse;
				document.onmouseup=new Function("isdrag=false");
				GetFriendlist();
				getChatText();
			}		
			//Gets the browser specific XmlHttpRequest Object
			function getXmlHttpRequestObject() {
				if (window.XMLHttpRequest) {
					return new XMLHttpRequest();
				} else if(window.ActiveXObject) {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} else {
					document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.';
				}
			}
			
			//Gets the current messages from the server
			function getChatText() {
				if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
					member_id=sender_id;
					receiveReq.open("GET", 'js/messenger/mysql_getchat.php?chat=1&member_id=' + member_id + '&last=' + lastMessage, true);
					receiveReq.onreadystatechange = handleReceiveChat; 
					receiveReq.send(null);
				}			
			}
			
			function setMessengerStatus(inputStatus){
					laststatus=inputStatus;
					member_id=sender_id;
					sendReq.open("POST", 'js/messenger/mysql_chat_functions.php?chat=1&member_id=' + member_id + '&last=' + lastMessage, true);
					sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					sendReq.onreadystatechange = handleMessengerStatus; 
					var param = 'setstatus=' + inputStatus;
					sendReq.send(param);
										
			}
			function handleMessengerStatus(){
					if (laststatus == 1) {
						document.getElementById("messenger_status_image").src = "images/messenger/status_online.png";
					} else if (laststatus==2){
						document.getElementById("messenger_status_image").src = "images/messenger/status_unsichtbar.png";
					} else if (laststatus==3){
						document.getElementById("messenger_status_image").src = "images/messenger/status_busy.png";
					} else if (laststatus==4){
						document.getElementById("messenger_status_image").src = "images/messenger/status_abwesend.png";
						
					}
			}
			
			//Add a message to the chat server.
			function sendChatText(inputId) {
				windowHasFocus=true;
				if(document.getElementById("text_"+ inputId).value == '') {
					alert("Du hast keine Nachricht eingetragen");
					return;
				}
				//if(tinyMCE.getInstanceById("text_"+ inputId).getContent() == '') {
				//	alert("Du hast keine Nachricht eingetragen");
				//	return;
				//}

				if (sendReq.readyState == 4 || sendReq.readyState == 0) {
					member_id=sender_id;
					sendReq.open("POST", 'js/messenger/mysql_getchat.php?chat=1&member_id=' + member_id + '&last=' + lastMessage, true);
					sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					sendReq.onreadystatechange = handleSendChat; 
					//var param = 'message=' + document.getElementById("text_" + inputId).value;

					var param = 'message=' + escape(document.getElementById("text_"+inputId).value).replace("+", "%2B");
					var empfaenger_id=document.getElementById("empfaenger_" + inputId).value;
					param += '&sender_name=' + sender_name;
					param += '&chat=1';
					param += '&sender_id=' + sender_id;
					param += '&empfaenger_id=' + empfaenger_id;
					sendReq.send(param);
					//document.getElementById("text_" + inputId).value = '';
					

					
					
					// User offline
					if(check_user_status(empfaenger_id)==4){
						document.getElementById("chat_nc_messenger_div_" + empfaenger_id).innerHTML += "<div style='color:#999999'>User ist offline. Nachricht wird zugestellt sobald er online ist.</div>"; 
					}

					//tinyMCE.getInstanceById("text_"+ inputId).setContent("");
					//tinyMCE.execCommand("mceFocus", false, "text_" + inputId);
					document.getElementById("text_" + inputId).value="";
					document.getElementById("text_" + inputId).focus();
					
					//tinyMCE.getInstanceById("text_" + inputId).focus();
					//tinyMCE.activeEditor.focus();
					//inyMCE.execInstanceCommand("text_" + inputId, "mceFocus");
					//mTimer = setTimeout('focusTest(' + inputId + ');',2000); //Refresh our chat in 3,5 seconds
				}						
				
			}
			
			
			function check_user_status(id){
				for(var i=0;i<=friends.length-1;i++){
					if(friends[i][0]==id){
						return friends[i][1];
					}
				}
			}
			
			function focusTest(inputId){
				alert("c");
				tinyMCE.execCommand("mceFocus", false, "text_" + inputId);
				alert("x");
			}
			//When our message has been sent, update our page.
			function handleSendChat() {
				//Clear out the existing timer so we don't have 
				//multiple timer instances running.
				clearInterval(mTimer);
				getChatText();
			}
			//Function for handling the return of chat text
			function handleReceiveChat() {
				var member_id=sender_id;
				if (receiveReq.readyState == 4) {
					var xmldoc = receiveReq.responseXML;
					if (xmldoc != null) {
						var message_nodes = xmldoc.getElementsByTagName("message");
						var n_messages = message_nodes.length
						for (i = 0; i < n_messages; i++) {
							//var user_node = message_nodes[i].getElementsByTagName("user");
							var sender_id_node = message_nodes[i].getElementsByTagName("sender_id");
							var sender_name_node = message_nodes[i].getElementsByTagName("sender_name");
							var empfaenger_id_node = message_nodes[i].getElementsByTagName("empfaenger_id");
							//var empfaenger_name_node = message_nodes[i].getElementsByTagName("empfaenger_name");
							var text_node = message_nodes[i].getElementsByTagName("text");
							var time_node = message_nodes[i].getElementsByTagName("time");
							var date_node = message_nodes[i].getElementsByTagName("date");
							var lastTempMessage=message_nodes[i].getAttribute('id');
							var timestamp_node = message_nodes[i].getElementsByTagName("timestamp");
							
							var sender_idx = sender_id_node[0].firstChild.nodeValue;
							var empfaenger_id = empfaenger_id_node[0].firstChild.nodeValue;
							var auswahl_id;
							var user_name;
							var answer_user_id;
							var chatpartner;
							
							if (empfaenger_id == member_id) {
								auswahl_id = sender_idx;
								user_name = "<font color='#606060'><b>" + sender_name_node[0].firstChild.nodeValue + '</b></font>&nbsp;'
								// ANTWORT-ID-BOX setzen
								answer_user_id = sender_idx;
								chatpartner = sender_name_node[0].firstChild.nodeValue;
							}
							else 
								if (sender_idx == member_id) {
									auswahl_id = empfaenger_id
									user_name = "<font color='#ff0074'><b>" + sender_name_node[0].firstChild.nodeValue + '</b></font>&nbsp;'
									// ANTWORT-ID-BOX setzen
									answer_user_id = empfaenger_id;
									
								}
							
							if (document.getElementById("nc_messenger_div_" + auswahl_id)) {
								document.getElementById("ys_messenger").style.display="block";
								document.getElementById("nc_messenger_div_" + auswahl_id).style.display = 'block';
							}
							else {
								//alert('add' + auswahl_id);
								win.add(auswahl_id);
								win.draw(auswahl_id, chatpartner, 0);
								//getLastChatMessages(auswahl_id);
								//do {
								//} while (LoadedLastMessage == false)  
								//LoadedLastMessage=false;
								
							}
							

							document.getElementById("empfaenger_nc_messenger_div_" + auswahl_id).value = answer_user_id;
														
							var lasttimestamp=parseInt(document.getElementById("timestamp_nc_messenger_div_" + auswahl_id).value);
							var lastuser=parseInt(document.getElementById("lastwriter_nc_messenger_div_" + auswahl_id).value);
							var actualuser=parseInt(sender_idx);
							var actualtimestamp=parseInt(timestamp_node[0].firstChild.nodeValue);

							document.getElementById("timestamp_nc_messenger_div_" + auswahl_id).value=actualtimestamp;
							document.getElementById("lastwriter_nc_messenger_div_" + auswahl_id).value=actualuser;
							
							var d = new Date();
							var curr_date = d.getDate();
							var curr_month = d.getMonth()+1;
							if(curr_month<=9){
								curr_month = "0" + curr_month + "";
							}
							var curr_year = d.getUTCFullYear() + "";
							curr_year=curr_year.substr(2,2);

							var completedate=curr_date + "."  + curr_month + "." + curr_year;
							if(actualtimestamp>lasttimestamp+600 || actualuser!=lastuser ){
								document.getElementById("chat_nc_messenger_div_" + auswahl_id).innerHTML += user_name;
								if (completedate == date_node[0].firstChild.nodeValue) {
									document.getElementById("chat_nc_messenger_div_" + auswahl_id).innerHTML += '<font class="chat_time">(' + time_node[0].firstChild.nodeValue + ')</font><br />';
								} else {
									document.getElementById("chat_nc_messenger_div_" + auswahl_id).innerHTML += '<font class="chat_time">(' + date_node[0].firstChild.nodeValue + ' ' + time_node[0].firstChild.nodeValue + ')</font><br />';								
								}
							}

							document.getElementById("chat_nc_messenger_div_" + auswahl_id).innerHTML += text_node[0].firstChild.nodeValue + '<div style="height:5px;"><!-- --></div>';

						//document.getElementById("chat_nc_messenger_div_" + auswahl_id).scrollTop = document.getElementById("chat_nc_messenger_div_" + auswahl_id).scrollHeight;
							lastMessage =lastTempMessage;
							//alert("empfaenger_" + auswahl_id);
							setTimeout('scrollDownChatWindow(' + auswahl_id + ');', 100);
														
							// Fenster in den Vordergrund
							//layer.setToForeground("nc_messenger_div_" + auswahl_id);
							if(empfaenger_id==member_id){
								newExcitingAlerts();
							}
						}
					}
					mTimer = setTimeout('getChatText();',3500); //Refresh our chat in 3,5 seconds
				}
			}
			//This functions handles when the user presses enter.  Instead of submitting the form, we
			//send a new message to the server and return false.
			function blockSubmit() {
				sendChatText();
				return false;
			}
			//This cleans out the database so we can start a new chat session.
			function resetChat() {
				if (sendReq.readyState == 4 || sendReq.readyState == 0) {
					alert('reset');
					sendReq.open("POST", 'content/boxes/mysql_getchat.php?chat=1&last=' + lastMessage, true);
					sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					sendReq.onreadystatechange = handleResetChat; 
					var param = 'action=reset';
					sendReq.send(param);
					document.getElementById('txt_message').value = '';
				}							
			}
			//This function handles the response after the page has been refreshed.
			function handleResetChat() {
				document.getElementById('div_chat').innerHTML = '';
				getChatText();
			}	

			function getLastChatMessages_Sender(inputId){
				if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
					member_id=sender_id;
					receiveReq.open("GET", 'js/messenger/mysql_getchat.php?chat=1&member_id=' + member_id + '&friend_id=' + inputId + '&oldmessages=0,' + oldMessages, true);
					receiveReq.onreadystatechange = handleReceiveLastChat; 
					receiveReq.send(null);
				}			
			}
			function getLastChatMessages_Sitechange(inputId){
				if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
					member_id=sender_id;
					receiveReq.open("GET", 'js/messenger/mysql_getchat.php?chat=1&member_id=' + member_id + '&friend_id=' + inputId + '&oldmessages=0,' + oldMessages, true);
					receiveReq.onreadystatechange = handleReceiveLastChat; 
					receiveReq.send(null);
				}			
			}
			function getLastChatMessages_Empfaenger(inputId){
				if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
					member_id=sender_id;
					receiveReq.open("GET", 'js/messenger/mysql_getchat.php?chat=1&member_id=' + member_id + '&friend_id=' + inputId + '&oldmessages=1,' + oldMessages, true);
					receiveReq.onreadystatechange = handleReceiveLastChat; 
					receiveReq.send(null);
				}			
			}

			
			function handleReceiveLastChat() {
				var member_id=sender_id;
				if (receiveReq.readyState == 4) {
					var xmldoc = receiveReq.responseXML;
					var oldchat="";
					var lasttimestamp=0;
					var lastuser=0;
					if (xmldoc != null) {
						var message_nodes = xmldoc.getElementsByTagName("message");
						var n_messages = message_nodes.length
						for (i = 0; i < n_messages; i++) {
							//var user_node = message_nodes[i].getElementsByTagName("user");
							var sender_id_node = message_nodes[i].getElementsByTagName("sender_id");
							var sender_name_node = message_nodes[i].getElementsByTagName("sender_name");
							var empfaenger_id_node = message_nodes[i].getElementsByTagName("empfaenger_id");
							//var empfaenger_name_node = message_nodes[i].getElementsByTagName("empfaenger_name");
							var text_node = message_nodes[i].getElementsByTagName("text");
							var time_node = message_nodes[i].getElementsByTagName("time");
							var date_node = message_nodes[i].getElementsByTagName("date");
							var timestamp_node = message_nodes[i].getElementsByTagName("timestamp");

							
							var sender_idx = sender_id_node[0].firstChild.nodeValue;
							var empfaenger_id = empfaenger_id_node[0].firstChild.nodeValue;
							var auswahl_id;
							var user_name;
							var answer_user_id;
							var chatpartner;
							var actualtimestamp=parseInt(timestamp_node[0].firstChild.nodeValue);
							
							
							if (empfaenger_id == member_id) {
								auswahl_id = sender_idx;
								user_name = "<font color='#AAAAAA'><b>" + sender_name_node[0].firstChild.nodeValue + '</b></font>&nbsp;'
								chatpartner = sender_name_node[0].firstChild.nodeValue;
							}
							else {
								if (sender_idx == member_id) {
									auswahl_id = empfaenger_id
									user_name = "<font color='#AAAAAA'><b>" + sender_name_node[0].firstChild.nodeValue + '</b></font>&nbsp;'
								}
							}
							var actualuser=parseInt(sender_idx);
								
							// alten chat davor setzen

							
							var d = new Date();
							var curr_date = d.getDate();
							var curr_month = d.getMonth()+1;
							if(curr_month<=9){
								curr_month = "0" + curr_month + "";
							}
							var curr_year = d.getUTCFullYear() + "";
							curr_year=curr_year.substr(2,2);
						
							var completedate=curr_date + "."  + curr_month + "." + curr_year;
							var datum_string="";
							
							if(actualtimestamp>lasttimestamp+600 || actualuser!=lastuser ){
								if(completedate==date_node[0].firstChild.nodeValue){
									datum_string += '<font class="chat_time">(' + time_node[0].firstChild.nodeValue + ')</font><br />';
								} else {
									datum_string += '<font class="chat_time">(' + date_node[0].firstChild.nodeValue + ' ' + time_node[0].firstChild.nodeValue + ')</font><br />';								
								}
								oldchat += user_name;
								oldchat += datum_string;
							}
							oldchat += text_node[0].firstChild.nodeValue + '<div style="height:5px;"><!-- --></div>';
							
							lasttimestamp=actualtimestamp;
							lastuser=actualuser;
							
						}
					}
					
					if (auswahl_id) {
						document.getElementById("chat_nc_messenger_div_" + auswahl_id).innerHTML = oldchat + document.getElementById("chat_nc_messenger_div_" + auswahl_id).innerHTML;
						setTimeout('scrollDownChatWindow(' + auswahl_id + ');', 100);
					}
					//document.getElementById("chat_nc_messenger_div_" + auswahl_id).innerHTML += '<div style="height:5px;"><!-- --></div>';
					//alert(document.getElementById("chat_nc_messenger_div_" + auswahl_id).scrollHeight);
					//document.getElementById("chat_nc_messenger_div_" + auswahl_id).reca
					//document.getElementById("chat_nc_messenger_div_" + auswahl_id).scrollTop = document.getElementById("chat_nc_messenger_div_" + auswahl_id).scrollHeight;
					//document.getElementById("chat_nc_messenger_div_" + auswahl_id).scrollTop =0;
				}
			}
			
			
			function GetFriendlist(){
				if (receiveStatusReq.readyState == 4 || receiveStatusReq.readyState == 0) {
					receiveStatusReq.open("POST", 'js/messenger/mysql_chat_functions.php?chat=1&member_id=' + member_id, true);
					receiveStatusReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					receiveStatusReq.onreadystatechange = handleGetFriendlist;
					var param = 'updatefriendlist=1';
					receiveStatusReq.send(param);
				}							

			}
			
			function handleGetFriendlist(){
						
				
				if (receiveStatusReq.readyState == 4) {
					
					var xmldoc = receiveStatusReq.responseXML;
					if (xmldoc != null) {
						var friendlist=xmldoc.getElementsByTagName("friendlist")[0].firstChild.nodeValue;
						var friendarray=xmldoc.getElementsByTagName("friends")[0].firstChild.nodeValue;
						var online=xmldoc.getElementsByTagName("online")[0].firstChild.nodeValue;
						var total=xmldoc.getElementsByTagName("total")[0].firstChild.nodeValue;
						document.getElementById("messenger_friends_list_container").innerHTML=friendlist;


						// Online-Freunde eintragen
						document.getElementById("messenger_friends_online").innerHTML="<b>" + online + "</b> von " + total + " Freunden online";
		
						
						//Freund grün einfärben
						for (var i = 0; i <= messageWindows.length - 1; i++) {
							if (document.getElementById("messenger_friendentry_" + messageWindows[i][0])) {
								document.getElementById("messenger_friendentry_" + messageWindows[i][0]).style.color = "#009900";
							}
						}

					}
					
					friends = new Array();
					var friends_1 = new Array();
					friends_1=friendarray.split(";");
					for(var i = 0;i<=friends_1.length - 1;i++){
						//var friends_2=new Array();
						//friends_2=friends_1[i].split(",");
						friends[i]=new Array();
						friends[i] = friends_1[i].split(",");
					}

					
					setTimeout('GetFriendlist();', 5000);
				}
				// GRÜN EINFÄRBEN
				
			}
			
			function scrollDownChatWindow(inputId){
				var x= document.getElementById("chat_nc_messenger_div_" + inputId)
				x.scrollTop=x.scrollHeight;
			}
			
			
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
		
function Set_Cookie(name, value, expires, path, domain, secure){
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());
    
    /*
     if the expires variable is set, make the correct
     expires time, the current script below will set
     it for x number of days, to make it for hours,
     delete * 24, for minutes, delete * 60 * 24
     */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    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" : "");
}

function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


			
