/************************************************************************************************

Usage:
	var connector = new HttpConnector(host, port, method, queryString, functionRefArray, paramsArray);
	connector.connect();

Parameters:
	host : ip
	port : number
	method : get | post
	queryString : include the leading slash
	functionRefArray : array with functions to call
	paramsArray : corresponding params to functionRefArray 

Notes:

	functionRefArray must contain the actual function objects, not the function names (not strings!)

	paramsArray.length must be equal to functionRefArray.length

	if a param has the value ${connectionResponse} then it will
		be replaced with the XmlHttpRequest response text	
	the paramsArray's elements are strings with csv values:
		[ '1,2' , 'param1,"str param 2"' , '${connectionResponse}' , ...]

**************************************************************************************************/

function HttpConnector(host, port, method, queryString, functionRefArray, paramsArray){

	this.host = host;
	this.port = port;
	this.method = method;
	this.queryString = queryString;
	this.paramsArray = paramsArray;
	this.functionRefArray = functionRefArray;
	


	this.connect = function(){
		var functionReferences = this.functionRefArray;
		var parameters = this.paramsArray;		
		var jsHttpObject = this.initJsHttpObject();

		if( jsHttpObject == null){
			alert('jsHttpObject did not initialize');
			return null;
		}


		jsHttpObject.open(this.method, 'http://' + this.host + ':' + this.port + this.queryString, true);

		jsHttpObject.onreadystatechange = function(){
												if (jsHttpObject.readyState == 4){
													if (jsHttpObject.status == 200) {

														for(i=0; i<functionReferences.length; i++){
															if( parameters[i] == '${connectionResponse}')
																parameters[i] = jsHttpObject.responseText;
															eval('functionReferences[i](\''+ parameters[i] +'\');');
														}

														return true;
													}
													else{
														alert("jsHttpObject: Error opening http connection. Please contact your administrator");
														return false;
													}
												}
												else return false;
											};

		jsHttpObject.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
		jsHttpObject.send(null);
		return true;
	}



	this.initJsHttpObject = function (){
		var jsHttpObject = null;
		/*@cc_on	
		@if (@_jscript_version >= 5)
			try {	
				jsHttpObject = new ActiveXObject("Msxml2.XMLHTTP");	
			}catch (e) {	
				try {
					jsHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
				}catch (E) {
					jsHttpObject = null;
				}
			}
		@else
			jsHttpObject = null;	
		@end @*/
	
		if (! jsHttpObject && typeof XMLHttpRequest != 'undefined') {
			try {
				jsHttpObject = new XMLHttpRequest();
			} catch (e) {
			jsHttpObject = null;
			}
		}

		return jsHttpObject;
	}
}
