//////// For Ajax-y stuff ////////////
function t_loadurl(dest, displayObject, imagePath) {
	try {
		// Detect support for object (rather than browser sniffing)
		// Moz supports XMLHttpRequest. IE uses ActiveX.
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
		
	} catch (e) {
		// browser doesn't support ajax
		// ....
	}
	
	//Set global "objID" var for triggered to insert content.
	objID = displayObject;
	
	// the xmlhttp object triggers an event everytime the status changes
	// triggered() function handles the events
	xmlhttp.onreadystatechange = triggered;
	
	// set loading graphic
        if (imagePath.length > 0)
	  document.getElementById(objID).innerHTML = "<div class=\"loadingGraphic\"><img src=\"" + imagePath + "\" alt=\"Submitting...\" /><\/div>";
	
	// open takes in the HTTP method and url.
	xmlhttp.open("GET", dest);
	
	// send the request. if this is a POST request we would have
	// sent post variables: send("name=aleem&gender=male)
	// Moz is fine with just send(); but
	// IE expects a value here, hence we do send(null);
	xmlhttp.send(null);
}
function triggered() {
	
	// Other readyState codes:
	// 0=Uninitialised 1=Loading 2=Loaded 3=Interactive

	// Successful Response
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		// xmlhttp.responseText object contains the response.
		var r = xmlhttp.responseText;
		// strip all text after AJAX_DEBUG_BELOW, put in object
		var html  = r.split('AJAX_DEBUG_BELOW')[0];
		var debug = r.split('AJAX_DEBUG_BELOW')[1];
		
		//Need a robust solution here that breaks apart alternating sections of script and non script code 
		// i think a pair of recursive functions would be the best thing here
		
		var processed_html = t_script_processing(html);
		
		// document.getElementById(objID).innerHTML = processed_html + debug;

		document.getElementById(objID).innerHTML = processed_html;
	}
	
	// Problem Response
	//if ((xmlhttp.readyState == 4) && (xmlhttp.status != 200)) {
	// xmlhttp.responseText object contains the response.
	//	var r = '<p><em>This information is temporarily unavailable online<\/em>.<\/p>';
	//	document.getElementById(objID).innerHTML = r;
	//}
}

function t_script_processing(s){

	var start1 = '<scr';
	var start2 = 'ipt type=\"text/javascript\">';

	var end1   = '<\/scr';
	var end2   = 'ipt>';
	
	var start  = start1 + start2;
	var end    = end1   + end2;

	var s_array      = s.split(start);
	
	if (s_array.length == 1){
		return s;
	}
	
	var before_script    = s_array[0];
	var script_plus_rest = s_array[1];
	var spr_array        = script_plus_rest.split(end);
	var script_code      = spr_array[0];
	
	var rest_code = '';
	for (i=1;i<spr_array.length;i=i+1){
		rest_code        += spr_array[i];
	}
		
	t_startndw();
	eval(script_code);
	t_stopndw();
	var script_out  = global_ndw;
	
	return before_script + script_out + t_script_processing(rest_code)

}

var global_ndw;
var global_olddw;

function t_startndw(){
	global_olddw = document.write;
	global_ndw = '';
	document.write = t_nodocwrite;
}
function t_nodocwrite(s){
	global_ndw += s;
}
function t_stopndw(){
	document.write = global_olddw;
}

function t_submit_form(name){
	document.getElementById(name).submit();
}
////////// End Ajax-y Stuff /////////////////
