// Add a trim function to the String object
String.prototype.trim=function(){
     var
         r=/^\s+|\s+$/,
         a=this.split(/\n/g),
         i=a.length;
     while(i-->0)
         a[i]=a[i].replace(r,'');
     return a.join('\n');
}

/*
function replaceNodeText(nodeid, newtext) {
	var textNode = document.createTextNode(newtext);
	var prjnode = document.getElementById(nodeid);
	prjnode.replaceChild(textNode, prjnode.firstChild);
}
*/

/**********************************************************
 * Class: AjaxCall.
 * param: object 
 *	{
 *	request_url: url string
 *	request_method: string "GET" or "POST"
 *	response_callback: function(response)
 *	response_format: string "DOM" or "TEXT" 
 *	}
 *********************************************************/
function AjaxCall(config) {
	this.params = new Object();	
	this.requestURL = config.request_url;
	this.responseCallback = config.response_callback;
	//default request method is set to post
	if (!config.request_method) {
		this.requestMethod = "POST";
	} else {
		this.requestMethod = config.request_method.toUpperCase();
	}
	//default response format is text
	if (!config.response_format) {
		this.responseFormat = "TEXT";
	} else {
		this.responseFormat = config.response_format.toUpperCase();
	}
}
AjaxCall.prototype.setRequestParam = function(name, value) {
	this.params[name] = value;
}
/**
 * Retrieve URLEncoded request parameters
 */
AjaxCall.prototype.getRequestParams = function() {
	var data = "";
	//Loop through the params
	for (var name in this.params) {
		if (data != "") data += "&";
		var val = this.params[name];
		if (val.constructor == Array) { //check if the value is an Array
			for (var i=0; i<val.length; i++) {
				data += (name + "=" + encodeURIComponent(val[i]) );
				if (i < val.length - 1) data += "&";
			}
		} else {
			data += (name + "=" + encodeURIComponent(val) );
		}		
	}
//alert(data);
	return data;
};
AjaxCall.prototype.execute = function() {
	var req;
	var responseCallback = this.responseCallback;
	var responseFormat = this.responseFormat;
    
    // test for supporting XMLHttpRequest
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    // test for supporting IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if (req) {
    		req.onreadystatechange = function() {
    			// only if req shows "complete"
		    if (req.readyState == 4) {
		        // only if "OK"
		        if (req.status == 200) {
		        		var response;
					if (responseFormat == "DOM") {        
		            		response  = req.responseXML.documentElement;
		            	} else if (responseFormat == "TEXT") {
		            		response = req.responseText;
		            	}
		            	responseCallback(response);
		        } else {
		            alert("AjaxCall.execute(): There was a problem:\n" + req.statusText);
		        }
		    }
    		}
    		
    		if (this.requestMethod == "POST") {
	    		req.open("POST", this.requestURL, true);
	    		req.setRequestHeader("Connection", "close");
	    		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    		req.setRequestHeader("Method", "POST "+url+" HTTP/1.1");
	    		req.send(this.getRequestParams());
	    	} else {
	    		req.open("GET", this.requestURL + "?" + this.getRequestParams());
	    		req.send(null);
	    	}
    	}
}
/******************* END Class AjaxCall************************/

