var Post = new Object();
Post.SendForm = function(form, updateDiv)
{
	var query = Post.buildQuery(form);
	Ajax.Request(form.method, form.action, query, Post.OnResponse, updateDiv);

	jQuery('#busyAnimation').css('display', 'block');	//start the busy animation 
	jQuery('body').css('cursor', 'progress');	//start the busy cursor indicator 
}




//customised to send a post ajax request.  data must be in the form "key1=value1&key2=value3..." 
Post.Send = function(queryString, url, updateDiv)
{
	Ajax.Request('post', url, queryString, Post.OnResponse, updateDiv);

	jQuery('#busyAnimation').css('display', 'block');	//start the busy animation 
	jQuery('body').css('cursor', 'progress');	//start the busy cursor indicator 
}



////////////  supporting functions  //////////////////////////////////////////////////


Post.OnResponse = function(responseText, updateDiv)
{
	if (updateDiv != null) {
		document.getElementById(updateDiv).innerHTML = responseText;	      
	}
	else {
		DisplayResponses(responseText);
	}
	
	jQuery('#busyAnimation').css('display', 'none');	//stop the busy animation 
	jQuery('body').css('cursor', 'default');	//stop the busy cursor indicator 
}



Post.buildQuery = function(form)
{
	var query = "";
	for(var i=0; i<form.elements.length; i++)
	{
		var key = form.elements[i].name;
		var value = Post.getElementValue(form.elements[i]);
		if(key && value)
		{
			query += key +"="+ value +"&";
		}
	}
	return query;
}

Post.getElementValue = function(formElement)
{
	if(formElement.length != null) var type = formElement[0].type;
	if((typeof(type) == 'undefined') || (type == 0)) var type = formElement.type;

	switch(type)
	{
		case 'undefined': return;

		case 'radio':
			for(var x=0; x < formElement.length; x++) 
				if(formElement[x].checked == true)
			return formElement[x].value;

		case 'select-multiple':
			var myArray = new Array();
			for(var x=0; x < formElement.length; x++) 
				if(formElement[x].selected == true)
					myArray[myArray.length] = formElement[x].value;
			return myArray;

		case 'checkbox': return formElement.checked;
	
		default: return formElement.value;
	}
}


var Ajax = new Object();
Ajax.isUpdating = true;

Ajax.Request = function(method, url, query, callback, updateDiv)
{
	this.isUpdating = true;
	this.callbackMethod = callback;
	this.divToUpdate = updateDiv;
	this.request = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP"); 
	this.request.onreadystatechange = function() { Ajax.checkReadyState(); };
	
	if(method.toLowerCase() == 'get') url = url+"?"+query;
	this.request.open(method, url, true);
	this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	this.request.send(query);
}
	
Ajax.checkReadyState = function(_id)
{
	switch(this.request.readyState)
	{
		case 1: break;
		case 2: break;
		case 3: break;
		case 4:
			this.isUpdating = false;
			this.callbackMethod(this.request.responseText, this.divToUpdate);
	}
}



function DisplayResponses(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
//	qs = qs.myDecode();
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = pair[0].myDecode();
		
		var value = pair[1].myDecode();
		
		this.params[name] = value;
	
		if (name == 'javascript') {
			eval(value);
		}
		else {
			//update any id=="name" with the new html/value
			if (obj = document.getElementById(name)) {
				jQuery('#'+obj.id).fadeOut(0);
				
				//update the element with the specified id (id="name")
				obj.innerHTML = value;
				obj.value = value;

				//also update any elements with the same name (name="name") for multiple element support
				if (document.getElementsByName(name).length) {
					document.getElementsByName(name)[0].innerHTML = value;
					document.getElementsByName(name)[0].value = value;
				}
				
				fadeIn(obj.id);
			}
/* .html() method doesn't work properly in IE

			if (jQuery('#' + name).length) {
				jQuery('#' + name).fadeOut(0).html(value).attr('value', value).fadeIn("slow");
			}	
*/			//update any class=="name" with the new html/value
			if (jQuery('.' + name).length) {
				jQuery('.' + name).fadeOut(0).html(value).attr('value', value).fadeIn("slow");
			}	
				
				
		}
	}
}



function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}
