if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
	try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
	throw new Error( "This browser does not support XMLHttpRequest." )
};

var key = false;
var selecteddiv = '';
var curid = '';

String.prototype.trim = function() {
	var x=this;
	x=x.replace( /^\s*/, "" );
	x=x.replace( /\s*$/, "" );
	return x;
}


function tabLoadContent(id) {

	if ( curid != '' ) {
		document.getElementById('tab_' + curid).className = 'tab';
	}
	
	key = 'shoutbox_' + id;
	
	loadContent();
	curid = id;


	document.getElementById('tab_' + id).className = 'tab-selected';
}

var insert_url = shoutbox_url + 'insert.php';
var load_url = shoutbox_url + 'fetch.php';

function ajax(url, data) {
	var request = new XMLHttpRequest();
	request.open("POST", url, true);

	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	request.onreadystatechange = function(){
		if(request.readyState == 4){
			// Get the data from the server's response

			if ( request.responseText != '' ) {
				// update the shoutbox thread

				//var current_contents = document.getElementById('shoutbox_contents').innerHTML;

				document.getElementById('boxcontent').innerHTML = request.responseText;
			}
			else {
				// display the javascript error
				alert( 'There was an error submitting the shout... please re-load the page and try again.' );
			}
		}
	}

	request.send(data);
}

function loadContent() {
	ajax( load_url, 'key=' + key );
}

function submitComment() {

	var comment = document.theForm.comment.value; 
       var pid = document.theForm.pid.value;
	
	if ( comment.trim() == '' ) {
		alert( 'Please give us a comment to post!' );
		return false;
	}

	if ( comment != '' ) {
		postdata='key=' + key + '&content=' + document.theForm.comment.value;
	}

       if ( pid != '' ) {
              postdata = postdata + '&pid=' + pid;
       }

	ajax( insert_url, postdata );

	return false;
}

function init(){
	var stretchers = document.getElementsByClassName('box');
	var toggles = document.getElementsByClassName('tab');
	var myAccordion = new fx.Accordion(
		toggles, stretchers, {opacity: false, height: true, duration: 600}
	);
	//hash functions
	var found = false;
	toggles.each(function(h3, i){
		var div = Element.find(h3, 'nextSibling');
			if (window.location.href.indexOf(h3.title) > 0) {
				myAccordion.showThisHideOpen(div);
				found = true;
			}
		});
		if (!found) myAccordion.showThisHideOpen(stretchers[0]);
}
