// Flood control, time in seconds:
var floodTime = 1;

floodTime *= 1000;

username = "";

var refreshTime = 1500;

var yshout_php=(board_url?board_url+'/':'')+'yshout/yshout.php';

// Set up Behaviour
var myrules = {
	'#shout-form' : function(element){
		element.onsubmit = function(){
			return false;
		}
	},
	
	'#shout-text' : function(element){
		element.onkeypress = function(evt){
			return onKP(element, evt);
		}
		element.onfocus = function() {
			onF(element);
		}
	},
	
	'#shout-button' : function(element){
		element.onclick = function(){
			doSend();
		}
	}
};

var guest;

Behaviour.register(myrules);

// onKeyPress event handler for the two textboxes
function onKP (element, evt) {
	evt = (evt) ? evt : (window.event) ? event : null;

	var charCode;
	if (evt)
	charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :	((evt.which) ? evt.which : 0));

	if (charCode == 13 || charCode == 3) {
		doSend();
		return false;
	}
}
// TYPING NOTIFICATION DISABLED


// disabled by .hc  26/09/06

/*
textempty=true;
function checkText () {
	if ($("shout-text").value && textempty)
	{
		new ajax ('yshout/typers.php?add',{}/*,{postBody:'name='+username}*//*);
		doTypingRefresh();
		textempty=false;
	}
	else if (!$("shout-text").value && !textempty)
	{
		new ajax ('yshout/typers.php',{}/*,{postBody:'name='+username}*//*);
		doTypingRefresh();
		textempty=true;
	}
}
var TypingRefreshSet = false;
var TypingRefreshTime = 600;
function startTypingRefresh() {
	if (!TypingRefreshSet) {
		setTimeout("doTypingRefresh()", TypingRefreshTime);
		TypingRefreshSet = true;
	}
}

function schedTypingRefresh() {
	if(TypingRefreshSet) {
		setTimeout("doTypingRefresh()",TypingRefreshTime);
	}
}

function doTypingRefresh() {
	if($("yshout").style.display == "none") return;
	new ajax ('yshout/_typers.txt', {
		method: 'get',
		update: $('typing'),
		onComplete: schedTypingRefresh
	});
}
 */

// onFocus event handler for the two textboxes
function onF (element) {
	element.value = "";
	defocus(element);
	// setInterval("checkText()",30); // typing notification
}

// Show the chat when the page loads...
//Behaviour.addLoadEvent(function(){setTimeout("loadChat()", 10)}); // obsolete because of DOMFunction

// Clear the onFocus event handler and set the foreground color to black.
function defocus(element) {
	element.onfocus = null;
	element.style.color = null;
}

// Set off the AJAX call to load the chat form into the empty yShout div
function loadChat() {
	if ($("yshout").style.display=="none") return;
	if(get_cookie("yShout_open"))
	{
		$("yshout").innerHTML="Shoutbox loaded in another window. Retrying...";
		setTimeout("loadChat()",Math.random()*1000+1000/*between 1 and 2 seconds*/);
		return;
	}
	set_cookie("yShout_open","true",3/*seconds*/);
	window.onunload = delcookie;
	if ($("yshout") && shoutFile != "") {
		new ajax (yshout_php, { 
			postBody: 'reqtype=init&file=' + shoutFile,
			update: $('yshout'),
			onComplete: loadDone
		});
	}
}

function delcookie() {
	delete_cookie("yShout_open");
}

// Re-apply Behaviour after the chat loads
function loadDone() {
	setTimeout("Behaviour.apply()", 5);
	setTimeout("setupChat()", 5);
}

var oldNameBackgroundColor, oldTextBackgroundColor;

function setupChat() {
	startRefresh();
	// Typing notification
	// startTypingRefresh();
	username = $("forum-name").value;
	if($("forum-name").disabled) guest=false;
	else guest=true;
}

function complex_escape(text)
{
	return escape(textToEntities(text.replace(/&#/g, "&#38;#"))).replace(/\+/g, "%2B");
}

// Send the message
function doSend() {
	if (guest) {
		if($("forum-name").value=='')
		{
			alert("Please enter a username.");
			return;
		}
		username=complex_escape($F("forum-name"));
		set_cookie("username",username,20*365*24*3600);
		var toShout = complex_escape($F("shout-text"));
		floodControl();
		new ajax (yshout_php, { 
			postBody: 'reqtype=shout&shout=' + toShout + '&file=' + shoutFile + '&username=' + username,
			update: $('shouts'),
			onComplete: shoutDone
		});
		return;
	}
	if (formValidate() && $("shout-text").value) {
		var toShout = complex_escape($F("shout-text"));
		floodControl();
		
		new ajax (yshout_php, { 
			postBody: 'reqtype=shout&shout=' + toShout + '&file=' + shoutFile,
			update: $('shouts'),
			onComplete: shoutDone
		});
		// typing notification
		//doTypingRefresh();
	}
}

function autoShout(theText) {
	new ajax (yshout_php, { 
			postBody: 'reqtype=autoshout&shout=' + theText + '&file=' + shoutFile,
			update: $('shouts'),
			onComplete: shoutDone
		});
}

function ajaxGet(args) {
	new ajax (yshout_php+'?'+args, { 
			update: $('shouts'),
			onComplete: shoutDone
		});
}

function goTo(args) {
	document.location=yshout_php+'?'+args;
}

// Start refreshing the chat after a message has been sent
function shoutDone() {
	startRefresh();
}

var refreshSet = false;

function startRefresh() {
	if (!refreshSet) {
		setTimeout("doRefresh()", refreshTime);
		refreshSet = true;
	}
}

function schedRefresh() {
	if (refreshSet) {
		setTimeout("doRefresh()", refreshTime);
	}
}

// Validate the form to ensure that the fields are filled
function formValidate() {
	var shoutText = $F("shout-text");

	var textValid = true;

	if (shoutText == "Shout text" || shoutText == "")
		textValid = false;

	if (!textValid) {
		changeClass($("shout-text"), "shout-invalid");
		$("shout-text").focus();
		return false;
	} else {
		changeClass($("shout-text"), "shout-valid-shout");
	}

	return true;
}

// This gets called each refresh; it reloads the shoutboxes content.
function doRefresh() {
	if($("yshout").style.display == "none") return;
	set_cookie("yShout_open","true",3/*seconds*/);
	new ajax (yshout_php, { 
		postBody: 'reqtype=refresh&file=' + shoutFile,
		update: $('shouts')
	});
	schedRefresh();
}

function floodControl() {
/* note by .hc  25/09/06
Opera can get around this
perhaps disabling the shout-text completely is a good idea?
*/
	$("shout-text").disabled = true;
	$("shout-text").value = "";
	Form.disable($("shout-form"));
	setTimeout("enableShout()", floodTime);

}

function enableShout() {
	Form.enable($("shout-form"));
	$("shout-text").value = "";
	if(!guest) $("forum-name").disabled = true;
	$("shout-text").disabled = false;

	setTimeout('$("shout-text").focus()', 0);
}

function changeClass(element, newClass) {
	element.className = newClass;
}

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;
	}
	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" : "" );
}

// this function gets the cookie, if it exists
function get_cookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function delete_cookie ( cookie_name )
{
	var cookie_date = new Date ( );  // current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

