function requestData(sending, handler)
{
	IE = (window.ActiveXObject)? true : false;
	if( IE )
	{
		http = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else
	{
		http = new XMLHttpRequest();
	}
	
	if( http )
	{
		http.onreadystatechange = function(){getData(handler);};
		http.open("POST", "shoutbox_ajax.php?", true);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
		http.setRequestHeader("Content-length", sending.length);
		http.setRequestHeader("Connection", "close");
		http.send(sending);
	}
}

function getData(handler)
{
	if( http.readyState == 4 && http.status == 200 )
	{
		eval(handler + '(http);');
	}
}

function postShout()
{
	var shout_form = document.getElementById("shout_form");
	if( !shout_form.message.value.match(/\S/) )
	{
		alert("Your shout cannot be blank!");
		return false;
	}
	if( !shout_form.color.value.match(/\S/) )
	{
		shout_form.color.value = "0";
		setCookie("color", "0");
	}
	if( !shout_form.face.value.match(/\S/) )
	{
		shout_form.face.value = "0";
		setCookie("face", "0");
	}
	var color = encodeURIComponent(shout_form.color.value);
	var face = encodeURIComponent(shout_form.face.value);
	var message = encodeURIComponent(shout_form.message.value);
	var mode = encodeURIComponent(shout_form.mode.value);
	
	requestData("mode="+mode+"&color="+color+"&face="+face+"&message="+message, "confirmShout");
	
	shout_form.message.value = '';
	
	return false;
}

function confirmShout(http)
{
	if( http.responseText == "1" )
	{
		requestData("mode=obtain", "getShouts");
	}
	else if( http.responseText == "-1" )
	{
		alert("Shout failed! Please login first.");
	}
	else
	{
		alert("Shout failed! Please contact an administrator.");
	}
}

function setCookie(value, set)
{
	document.cookie = value+"="+set+"; expires=13/02/3000 00:00:00;";
}

function getCookie(name)
{
	var findit = document.cookie.match(name+'=(.*?)(;|$)');
	if( findit )
	{
		return(unescape(findit[1]));
	}
	else
	{
		return null;
	}
}

function updateStyle(cookie_load)
{
	var shout_form = document.getElementById("shout_form");
	if( cookie_load == null )
	{
		var color = shout_form.color.options[shout_form.color.selectedIndex].value;
		var face = shout_form.face.options[shout_form.face.selectedIndex].value;
		shout_form.message.style.color = ( color == 0 ) ? "" : color;
		shout_form.message.style.fontFamily = ( face == 0 ) ? "" : face;
		setCookie("color", color);
		setCookie("face", face);
	}
	else
	{
		if( getCookie("color") && getCookie("face") )
		{
			shout_form.message.style.color = ( getCookie("color") == 0 ) ? "" : getCookie("color");
			shout_form.message.style.fontFamily = ( getCookie("face") == 0 ) ? "" : getCookie("face");
			shout_form.color.value = getCookie("color");
			shout_form.face.value = getCookie("face");
		}
	}
}

updateStyle(true);

function insertUBBC(obj)
{
	document.getElementById("shout_form").message.value += "[" + obj.value.toLowerCase() + "]";
	obj.value += "*";
	obj.onclick = function(){outsertUBBC(this);};
	document.getElementById("shout_form").message.focus();
}

function outsertUBBC(obj)
{
	document.getElementById("shout_form").message.value += "[/" + obj.value.split("*")[0].toLowerCase() + "]";
	obj.value = obj.value.split("*")[0];
	obj.onclick = function(){insertUBBC(this);};
	document.getElementById("shout_form").message.focus();
}

function getShouts(http)
{
	var data = http.responseXML.documentElement;
	var shout = data.getElementsByTagName('shout');
	var shoutbox = document.getElementById("shoutbox");
	html = '<table width="100%" cellpadding="4" cellspacing="0">';
	for( s = 0; s < shout.length; s++ )
	{
		var user = shout[s].getElementsByTagName('user_id')[0].firstChild.data.split("|");
		var color = ( shout[s].getElementsByTagName('color')[0].firstChild.data == 0 ) ? '' : 'color: '+shout[s].getElementsByTagName('color')[0].firstChild.data+';';
		var face = ( shout[s].getElementsByTagName('face')[0].firstChild.data == 0 ) ? '' : 'font-family: '+shout[s].getElementsByTagName('face')[0].firstChild.data+';';
		var message = shout[s].getElementsByTagName('message')[0].firstChild.data;		
		message = message.replace(/\[b\](.*?)\[\/b\]/gim, "<strong>$1</strong>");
		message = message.replace(/\[i\](.*?)\[\/i\]/gim, "<em>$1</em>");
		message = message.replace(/\[u\](.*?)\[\/u\]/gim, "<span style=\"text-decoration: underline;\">$1</span>");		
		var time = shout[s].getElementsByTagName('time')[0].firstChild.data;
		
		html += '<tr><td width="90" nowrap="nowrap" align="left"><span class="gensmall"><span class="'+user[1]+'" onmouseover="obj=this;getit = setTimeout(\'showInfo('+user[0]+')\', 500);" onmouseout="clearTimeout(getit);"><a href="./profile.php?mode=viewprofile&u='+user[0]+'">'+user[2]+'</a></span>:</span></td><td nowrap="nowrap" align="left"><span style="font-size: 10px;' + color + face + '">' + message + '</span></td></tr>';
	}
	html += '</table>';
	shoutbox.innerHTML = html;
	setTimeout('requestData("mode=obtain", "getShouts")', 10000);
}

requestData("mode=obtain", "getShouts");
