/*
	NAME			: shoutbox.js
	DESCRIPTION		: ajax interface for shoutbox
	OSDATE-VERSION	: osDate 2.1.x (LGPL)
	LICENSE			: GPL
	AUTHOR			: Darren Gates, Vijay Nair for original script
	AUTHOR			: Ralf Strehle (ralf dot strehle at yahoo dot de) (osDate forum user name: exotic)
	COPYRIGHT		: 2009, by author
	SUPPORT			: by author
	
	This program 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 2 of the License, or
	(at your option) any later version.
	
	This program 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 this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

var shoutbox_timeout;

var http_sb = createRequestObject();

function getSBMsg(all)
{
	http_sb.open('get', doc_root + 'shoutbox.ajax.php?a=ping&all=' + all, true);
	http_sb.onreadystatechange = handleResponse_sb;
	http_sb.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_sb.send(null);
	if (all == 'Y') {
		clearTimeout(shoutbox_timeout);
	}
	shoutbox_timeout = setTimeout('getSBMsg("'+all+'")', (shoutbox_refresh_interval * 1000));
}

function sendSBMsg()
{
	var msg1 = document.getElementById('shoutbox_text').value;
	
	if (msg1.length > shout_text_max) {
		alert(sb_error);
		return false;
	}
	if (msg1.length <= 0) {
		alert(sb_msg_blank);
		return false;
	}
	var msg = encodeURIComponent(msg1.replace(/&/g, "|amp|"));
	document.getElementById('shoutbox_text').value = '';
	http.open('get', doc_root + 'shoutbox.ajax.php?a=sendSBMsg&msg=' + msg, false);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
	handleResponse();
}

function handleResponse_sb()
{
	if (http_sb.readyState == 4) {
		var response = http_sb.responseText;
		if (response != 'undefined' && response != '') {
			var update = new Array();
			var up2 = new Array();
			if (response.indexOf('|||') != -1) {
				update = response.split('|||');
				for (var i = 1; i < update.length; i++) {
					up2 = update[i].split("|:|");
					if (up2[0] != 'undefined' && up2[0] != '' && document.getElementById(up2[0])) {
						document.getElementById(up2[0]).innerHTML = up2[1];
					}
				}
			}
		}
	}
}

function CountMax()
{
	var ch_left = 0;
	var v = document.getElementById('shoutbox_text').value;
	
	ch_left = shout_text_max - v.length;
	
	if (ch_left < 0) {
		alert(sb_error);
		document.getElementById('shoutbox_text').value = v.substring(0, shout_text_max);
		v = document.getElementById('shoutbox_text').value;
		ch_left = shout_text_max - v.length;
	}
	
	document.getElementById('shoutbox_counter').value = ch_left;
}

function SmilieInsert(Smilie)
{
	document.getElementById('shoutbox_text').value += Smilie + ' ';
	document.getElementById('shoutbox_text').focus();
}

