
/**
* XMLHTTPRequestBuilder
*
* A factory for XMLHTTPRequest objects which is cross browser compatable for FireFox and IE
**/
function XMLHTTPRequestBuilder() {

	if (window.XMLHttpRequest) { 
	// Mozilla
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { 
	// IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}

	return http_request;

}


/**
* XMLHTTPRequestCoordinator
*
* Coorinate the Sending of commands to the server
*
* @param request_url the URL to send the request to
* @param state_change_function the function to call on state change of the XMLHTTPRequest object
* @param get_or_post determine whether to send the request get or post
* @param get_post_contents an object containing all the get or post variables to be sent
**/
function XMLHTTPRequestCoordinator( request_url, state_change_function, get_or_post, get_post_contents ) {

	var get_post_string = get_post_contents.toRequestString();

	//for ( i in get_post_contents ) { 

		//get_post_string += i + "=" + get_post_contents[i] + "&";

	//}

	if ( get_or_post == "GET" ) { 

		request_url += "?" + get_post_string;

	}

	var new_xml_http_request = XMLHTTPRequestBuilder();
	new_xml_http_request.open(get_or_post,request_url,true);

	new_xml_http_request.onreadystatechange = function () { 
		state_change_function(new_xml_http_request);
	};

	if ( get_or_post == "GET" ) {
		new_xml_http_request.send(null);
		return true;
	}
	if ( get_or_post == "POST" ) { 

		new_xml_http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		new_xml_http_request.send( get_post_string );
		return true;
	}		
	
	//failed to send a request	
	return false;
		
}

/**
* AJAXRequestFunctionFactory
*
* Creates a closure containing a target to and a function which will change XML into a DOM object
*
* @param target the target to use for a closure
* @param render_function the function used as the onStateChange of the XMLHTTPRequest function
*
* @returns a function object to set as the onStateChange function of the XMLHTTPRequest
**/
function AJAXRequestFunctionFactory( target, render_function ) { 

	var m_target = target;
	var m_function = render_function;

	function rf ( xobj ) {

		if ( xobj.readyState == 4 ) {
			if ( xobj.status == 200 ) {
				//extract the XML
				var x = xobj.responseXML;
				
				//writeMSG ( "XMLHTTPRequest :: AJAXRequestFunctionFactory :: my target is " + target.id );

				if ( m_target ) { 

					m_function( x, m_target );
					//mdiv = m_function( x );
					//m_target.innerHTML = "";
					//m_target.appendChild( mdiv );

				}
			}
		}
	}

	return rf;

}

/**
* HGRequestObject
*
* Used to build an HGRequestObject
*
* @param request_type the request type
**/
function HGRequestObject( request_type ) { 

	this.request_obj = new Object();
	this.request_obj['request_type'] = request_type;

}

//force the prototype for JS1.1
new HGRequestObject( null );

HGRequestObject.prototype.addRequestElement = function( ename, evalue ) { 
	this.request_obj[ename] = evalue;
	return true;
}

HGRequestObject.prototype.toRequestString = function() { 
	var rstring = "";
	for ( i in this.request_obj ) { 
		rstring += i + "=" + this.request_obj[i] + "&";
	}
	return rstring;
}


		
