/* AJAX functions */

var requests = 0;
var request = new Array();

function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		ro = new XMLHttpRequest();
	}
	if(!ro || !document.getElementById)
		alert("Your browser does not support the scripting on this page.");
	return ro;
}

function submitForm(form, to) {
	var params = "";
	var len = form.length;
	for(var e = 0; e < len; e++) {
		params += escape(form.elements[e].name) + "=" + escape(form.elements[e].value) + ((e == len-1) ? "" : "&");
	} sndReq(to, params);
	return false;
}

function sndReq(url, param) {
	var http = createRequestObject();
	if(!http) {setStatus("Failed to send request."); return;}
	http.open('POST', url, true);
	http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	http.setRequestHeader("Content-length", param.length);
	http.setRequestHeader("Connection", "close");
	http.onreadystatechange = handleResponses;
	request[requests] = http;
	http.send(param);
	requests++;
}
/*
handleResponses() should beable to dynamically deal with any AJAX response
it recieves as long as the RESPONSE follows these guidelines:

the first line of the response (everything before a \n) should be like:

location1=*;location2=some data until newline;
           ^ completes assignment
					 
locations being the ID attribute in which the (data) should be placed
* = everything after the first line;
otherwise, everything until the next semicolon (;) or no more data exists

for instance, the first line of a request to comment.php will be:

comments_for_post_1=*;num_comments_for_post_1=7;\n

*/
function handleResponses() {
	for (var i = 0; i < request.length; i++) {
		http = request[i];
		if(http.readyState == 4) {
			parseResponse(http.responseText);
			request.splice(i,1); i--;	requests--;
		}
	}
	if(request.length != 0) {
		set('status', 'loading...');
		document.getElementById('status').style.visibility = "visible";
	} else { 
		set('status', '');
		document.getElementById('status').style.visibility = "hidden";
	}
}

function parseResponse(data) {
	//alert(data);
	//document.getElementById('data').innerHTML += data+"\n";
	try {
		var endofinfo = data.indexOf("\n");
		if (endofinfo < 0) endofinfo = data.length-1;
		var infoline = data.substring(0,endofinfo);
		var fulldata = data.slice(endofinfo);
		var loc = "";
		var val = "";
		for(var i = 0; i < infoline.length; i++) {
			var curchar = infoline.charAt(i);
			if(curchar == '=') {
				while(curchar != ';' && curchar != '\n' && i < infoline.length) {
					curchar = infoline.charAt(++i);
					if(curchar != ';' && curchar != '\n') val += curchar;
				} if(val == '*') val = fulldata;
				set(loc, val);
				//i++;
				loc = "";
				val = "";
			} else {
				loc += curchar;
			}
		}
	} catch(err) {
		alert("AJAX response not validated:\n\nerror: " + err.toString());
	}
}

function set(where, towhat) {
	//setStatus(where+"="+towhat);
	if(where == "alert") {
		alert(towhat);
		return;
	} if(document.getElementById(where))
		document.getElementById(where).innerHTML = towhat;
	else
		alert("'"+where+"' not found in document");
}
