var timeout_time = 30;
var chat_intervall;
var start_chat_timeout;
var num_return = 0;
var scroll_down = '1';
var lastid = 'startdiv';

//Timeout
function countplus() {
	num_return = num_return + 1;
	
	//debuganzeihe
	if(document.getElementById('debugcount')) {
		document.getElementById('debugcount').innerHTML=num_return;
	}
	
	if(num_return == timeout_time) {
		if(document.getElementById('chat_field')) {
			logout('client_timeout');
		}
	}
}
zahler_interval = window.setInterval("countplus()", 1000);

function timeout_countdown(startstop) {
	if(startstop == '0') {
		num_return = 0;
	}
}
function reload_field() {
	if(document.getElementById("chat_field").scrollTop+480 == document.getElementById("chat_field").scrollHeight || document.getElementById("chat_field").scrollTop+500 == document.getElementById("chat_field").scrollHeight) {
		//anweisung nach unternspringen
		scroll_down = '1';
	} else {
		//nicht springen
		scroll_down = '0';
	}
 	pr_ajax_load_message(scroll_down, lastid);
}
//Chat Initialiesieren
function start_Chat(t) {
	if(t == 1) {
		chat_intervall = window.setInterval("reload_field()", 4000);
	}
	if(t == 0) {
		window.clearInterval(chat_intervall);
	}
	xajax.callback.global.onRequest = function() {timeout_countdown('1')};
	xajax.callback.global.beforeResponseProcessing = function() {timeout_countdown('3')};
}

//logout
function logout(logout_message) {
	pr_ajax_logout(logout_message);
	
	if(logout_message == 'client_timeout') {
		document.getElementById('fullchatid').innerHTML='<div class="error" align="center">Timeout (Server hat l&auml;nger als '+timeout_time+' Sukunden nicht gentwortet<br /><br /><a href="chat.html">zum Login...</a></div>';
	} else {
		document.getElementById('fullchatid').innerHTML='<div align="center"><br /><br /><br /><img src="http://www.planet-roxette.com/pic/ajax-loader.gif" width="31" height="31" alt="load" /></div>';
	}
	start_Chat(0);
	lastid = 'startdiv';
}

//nach untern scrollen
function sroll_down(old) {
	if(old == '1') {
		document.getElementById("chat_field").scrollTop = document.getElementById("chat_field").scrollHeight;
	}
}

//Chat formular löschen
function clear_message(){
 	document.getElementById('message').value='';
}

//letzte ID setzten
function setLastID(id) {
	lastid = id;
}
