/*
	moShBox - mosfetkiller-ShoutBox
	Copyright 2008, 2009 Lukas Palm <kingmassivid@gmail.de>, Paul Wilhelm <paul@mosfetkiller.de>

    This file is part of moShBox.

    moShBox is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    moShBox is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with moShBox.  If not, see <http://www.gnu.org/licenses/>.
*/

var last_request = 0;
var aktiv = setInterval("loadData()", 9000);
setTimeout('loadData();', 100);

var dosProtect = 0;

var oldmsg = 0;

function trim(x) {

 // skip leading and trailing whitespace
 // and return everything in between
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
}

function loadData()
{
	var xmlHttp = false;

	try
	{
		xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
	}

	catch(e)
	{
		try
		{
			xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
		}

		catch(e)
		{
			xmlHttp  = false;
		}
	}

	if (!xmlHttp  && typeof XMLHttpRequest != 'undefined')
	{
		xmlHttp = new XMLHttpRequest();
	}

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		xmlHttp.open('GET', 'moshbox/getmsg.php', true);

		xmlHttp.onreadystatechange = function ()
		{
			if (xmlHttp.readyState == 4)
			{
				dosProtect = dosProtect + 1;

				var msg = xmlHttp.responseText;

				if (msg != oldmsg)
				{
					oldmsg = msg;
					document.getElementById("messagecontainer").innerHTML = xmlHttp.responseText;

					var cdiv = document.getElementById('messagecontainer');
					cdiv.scrollTop = cdiv.scrollHeight;
				}

				if (dosProtect >= 50)
				{
					document.getElementById("messagecontainer").innerHTML = 'Sie waren zu lange inaktiv! Aktualisieren sie die Seite.<br>Diese Maßnahme dient der Entlastung des Servers.';
					clearInterval(active);
				}
			}
		}

		xmlHttp.send(null);
	}
}

function saveData()
{
	var xmlHttp2 = false;

	try
	{
		xmlHttp2  = new ActiveXObject("Msxml2.XMLHTTP");
	}

	catch(e)
	{
		try
		{
			xmlHttp2  = new ActiveXObject("Microsoft.XMLHTTP");
		}

		catch(e)
		{
			xmlHttp2  = false;
		}
	}

	if (!xmlHttp2  && typeof XMLHttpRequest != 'undefined')
	{
		xmlHttp2 = new XMLHttpRequest();
	}

	if (xmlHttp2)
	{
		document.shoutform.txtmessage.value = trim(document.shoutform.txtmessage.value);
		var txtMessage = escape(document.shoutform.txtmessage.value);

		if (txtMessage == '')
		{
			alert("Nachricht zu kurz");
		}
		else
		{
			if (xmlHttp2.readyState == 4 || xmlHttp2.readyState == 0)
			{
				xmlHttp2.open('POST', 'moshbox/putmsg.php');

				xmlHttp2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
				xmlHttp2.send('message='+encodeURIComponent(txtMessage));
			}
			else
			{
				setTimeout('saveData();',1000);
			}

			if (xmlHttp2.readyState == 4 || xmlHttp2.readyState == 0)
			{
				dosProtect = 0;
				setTimeout('loadData();',1000);
			}
			else
			{
				dosProtect = 0;
				setTimeout('loadData();',1000);
			}
		}
	}

	document.shoutform.txtmessage.value = '';
	document.shoutform.txtmessage.focus();
}



function removeMsg(id)
{
	var xmlHttp2 = false;

	try
	{
		xmlHttp2  = new ActiveXObject("Msxml2.XMLHTTP");
	}

	catch(e)
	{
		try
		{
			xmlHttp2  = new ActiveXObject("Microsoft.XMLHTTP");
		}

		catch(e)
		{
			xmlHttp2  = false;
		}
	}

	if (!xmlHttp2  && typeof XMLHttpRequest != 'undefined')
	{
		xmlHttp2 = new XMLHttpRequest();
	}

	if (xmlHttp2)
	{
		if (xmlHttp2.readyState == 4 || xmlHttp2.readyState == 0)
		{
			xmlHttp2.open('POST', 'moshbox/api.php');

			xmlHttp2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			xmlHttp2.send('action=removemsg&id=' + id);
		}
		else
		{
			setTimeout('saveData();',1000);
		}

		if (xmlHttp2.readyState == 4 || xmlHttp2.readyState == 0)
		{
			dosProtect = 0;
			setTimeout('loadData();',1000);
		}
		else
		{
			dosProtect = 0;
			setTimeout('loadData();',1000);
		}
	}
}



function popupSmileyselector()
{
	Smileyselector = window.open('moshbox/api.php?action=listsmilies', 'Smileyselector', 'status=no,scrollbars=yes,resizable=no,width=220,height=400');
	Smileyselector.focus();
}

function closeSmileyselector()
{
	window.Smileyselector.close();
	document.shoutform.txtmessage.focus();
}

function addSmiley(smiley)
{
	document.shoutform.txtmessage.value += ' ' + smiley + ' ';
}
