// Flood control, time in seconds:
var floodTime = 2;

floodTime *= 1000;

// 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();
		}
	},
	
	'#shout-name' : function(element){
		element.onfocus = function(){
			onF(element);
		}
		
		element.onkeypress = function(evt){
			return onKP(element, evt);
		}
	}
};

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;
	}
}

// onFocus event handler for the two textboxes
function onF (element) {
	element.value = "";
	defocus(element);
}

// Show the chat when the page loads...
Behaviour.addLoadEvent(setTimeout("loadChat()", 10));

// Clear the onFocus event handler and set the foreground color to black.
function defocus(element) {
	element.onfocus = null;
	element.style.color = "#000000";
}

// Set off the AJAX call to load the chat form into the empty yShout div
function loadChat() {
	if ($("yshout") && shoutFile != "") {
		new ajax ('yshout.php', { 
			postBody: 'reqtype=init&file=' + shoutFile,
			update: $('yshout'),
			onComplete: loadDone
		});
	}
}

// Re-apply Behaviour after the chat loads
function loadDone() {
	setTimeout("Behaviour.apply()", 10);
	setTimeout("setupChat()", 10);
}

var oldNameBackgroundColor, oldTextBackgroundColor;

function setupChat() {
	loadNickname();
	startRefresh();
}

// Send the message
function doSend() {
	saveNickname();

	if (formValidate()) {
		var shoutName = escape($F("shout-name"));
		var toShout = escape($F("shout-text"));

		floodControl();
		
		new ajax ('yshout.php', { 
			postBody: 'reqtype=shout&shout=' + toShout + '&name=' + shoutName + '&file=' + shoutFile,
			update: $('shouts'),
			onComplete: shoutDone
		});
	}
}

// Start refreshing the chat after a message has been sent
function shoutDone() {
	startRefresh();
}

var refreshSet = false;

function startRefresh() {
	if (!refreshSet) {
		setInterval("doRefresh()", 1000);
		refreshSet = true;
	}
}

// Validate the form to ensure that the fields are filled
function formValidate() {
	var shoutName = $F("shout-name");
	var shoutText = $F("shout-text");

	var nameValid = true, textValid = true;
	
	if (shoutName == "Nickname" || shoutName == "")
		nameValid = false;

	if (shoutText == "Shout text" || shoutText == "")
		textValid = false;

	if (!nameValid) {
		changeClass($("shout-name"), "shout-invalid");
		$("shout-name").focus();
		return false;
	} else {
		changeClass($("shout-name"), "shout-valid");
	}

	if (!textValid) {
		changeClass($("shout-text"), "shout-invalid");
		$("shout-text").focus();
		return false;
	} else {
		changeClass($("shout-text"), "shout-valid-shout");
	}

	return true;
}

// Save the nickname to a cookie
function saveNickname() {
	var expireDate = new Date();
	expireDate.setTime(expireDate.getTime() + 365 * 24 * 60 * 60 * 1000);
	
	var nickname = $F("shout-name");
	
	setCookie("yshout_nicknake", nickname, expireDate);
}

// Load the nickname from a cookie if the user has visited the site prior.
function loadNickname() {
	var nickname = getCookie("yshout_nicknake");
	
	if (nickname != null) {
		defocus($("shout-name"));
		$("shout-name").value = nickname;
	}
}

// This gets called each refresh; it reloads the shoutboxes content.
function doRefresh() {
	new ajax ('yshout.php', { 
		postBody: 'reqtype=refresh&file=' + shoutFile,
		update: $('shouts')
	});
}

function floodControl() {
//	$("shout-text").disabled = true;
	$("shout-text").value = "Flood control; please wait " + floodTime / 1000 + " seconds.";
	Form.disable($("shout-form"));
	setTimeout("enableShout()", floodTime);

}

function enableShout() {
	Form.enable($("shout-form"));
		
	$("shout-text").value = "";

	setTimeout('$("shout-text").focus()', 0);
}

function changeClass(element, newClass) {
	element.className = newClass;
}

// ***************************

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}