document.getElementsByClassName = function(cl){
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes))
			retnode.push(elem[i]);
	}
	return retnode;
}; 

function ajaxFunction(){
	var ajaxRequest;

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				//browsers all not support, rare case
				alert("Your browser broke!");
				return false;
			}
		}
	}
	if(ajaxRequest == null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	return ajaxRequest;
} 

function shoutbox_showData() {
	htmlRequest_shoutbox_showData = ajaxFunction();
	htmlRequest_shoutbox_showData.onreadystatechange = function(){
		if(htmlRequest_shoutbox_showData.readyState == 4){
			document.getElementById("shoutarea").innerHTML = htmlRequest_shoutbox_showData.responseText;
		}
	}
	htmlRequest_shoutbox_showData.open("GET", "shoutbox_outputinfo.php", true);
	htmlRequest_shoutbox_showData.send(null);
}

shoutbox_showData();
setInterval("shoutbox_showData()",1000);

function hide_class(klass)
{
	elts = document.getElementsByClassName(klass);
	for(e = 0; e < elts.length; e += 1)
		elts[e].style.display = "none";
}

function show_class(klass)
{
	elts = document.getElementsByClassName(klass);
	for(e = 0; e < elts.length; e += 1)
		elts[e].style.display = "inline";
}

function captcha_verify(){
	htmlRequest_captcha_verify = ajaxFunction();
	htmlRequest_captcha_verify.onreadystatechange = function(){
		if(htmlRequest_captcha_verify.readyState == 4){
			if( "1" == htmlRequest_captcha_verify.responseText )
			{
				$.fn.colorbox.close();
				shoutbox_saveData();
			}
			else
			{
				Recaptcha.reload();// a challenge can only be attempted once
			}
		}
	}
	
	challenge = Recaptcha.get_challenge();
	response = Recaptcha.get_response();
	params = "challenge="+challenge+"&response="+response;
	htmlRequest_captcha_verify.open("POST", "captcha_verify.php", true);
	htmlRequest_captcha_verify.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	htmlRequest_captcha_verify.setRequestHeader("Content-length", params.length);
	htmlRequest_captcha_verify.setRequestHeader("Connection", "close");
	htmlRequest_captcha_verify.send(params); 
}

function captcha(){
	htmlRequest_captcha = ajaxFunction();
	htmlRequest_captcha.onreadystatechange = function(){
		if(htmlRequest_captcha.readyState == 4){
			if("1" == htmlRequest_captcha.responseText)	// human, proceed
			{
				shoutbox_saveData();
			}
			else					// Testing for humanity
			{
				$.fn.colorbox({
							href:"captcha_colorbox.php", 
							width:"380px", height:"250px", title:"Are you human?", 
							open:true
						},function(){
							// Trick inspired from http://bit.ly/654oJn
							$('#recaptcha_response_field').focus();
						});
			}
		}
	}
	htmlRequest_captcha.open("GET", "human_verify.php", true);
	htmlRequest_captcha.send(null);
} 

function shoutbox_check_args(){
	if(document.shoutbox.shouter.value == "" || document.shoutbox.shouter.value == "NULL" || document.shoutbox.shouter_comment.value == "" || document.shoutbox.shouter_comment.value == "NULL"){
		return false;
	}
	
	return true;
}

function shoutbox_clear(){
	document.shoutbox.shouter_comment.value = ''; // Updates the shoutbox text area to NULL.
	document.shoutbox.shouter_comment.focus(); // Focuses the text area.
}

function shoutbox_saveData(){
	htmlRequest_shoutbox_saveData = ajaxFunction();
	htmlRequest_shoutbox_saveData.onreadystatechange = function(){
		if(htmlRequest_shoutbox_saveData.readyState == 4){// Clear shoutbox
			shoutbox_clear();
		}
	}

	// Send shout
	params = "name="+document.shoutbox.shouter.value+"&message="+document.shoutbox.shouter_comment.value+"&contact="+document.shoutbox.shouter_location.value;
	htmlRequest_shoutbox_saveData.open("POST", "shoutbox_sendshout.php", true);
	htmlRequest_shoutbox_saveData.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	htmlRequest_shoutbox_saveData.setRequestHeader("Content-length", params.length);
	htmlRequest_shoutbox_saveData.setRequestHeader("Connection", "close");
	htmlRequest_shoutbox_saveData.send(params); 
}

function shoutbox_post(){
	// Check input is correct
	if(!shoutbox_check_args())
	{
		alert('You need to fill in name and message!');// TODO: replace this by a nice colorbox
		return;
	}
	
	captcha();// the rest is done in there
}