//genericAjaxRequest
//Ajax Request class.
//The url is passed as parameter to the ajax request constructor. 
//The call back function can be set separatly. 
//If the callback function is set the send will not be syncronized.
//The send is syncronized if the callback function is null.

function genericAjaxRequest(url) {

/**
*	Valid URL to the resource on the same host that served the initial page
*	URL should be end with "?" if you are planning add cgi arguments.
*	If your URL contains cgi arguments, the last cgi shoud be end with "&".
**/	
	var ajaxRequestURL = url;
	
/**
*	CGI string of parameters, that will be concatenate to the URL for "GET" request.
* 	ajaxRequestCGI clears after each "GET" request, so if you need some 
*	persistent parameters to add to your URL, pass them in the constructor.
**/
	var ajaxRequestCGI = "";

/**
*	Default value: "false". If callbackFunction properties set, set to "true".
**/	
	var isAsynchronous = false;
/**
*	Callback function, used only for asynchronous requests.
*	It will be called on each change of the status of the ajaxRequest object.
**/
	var callbackFunction = null;

/**
*	Defines type of the ajaxRequest "GET"/"POST"
*	Default value: "GET"
**/
	var requestType = "GET";

/**
*	Data that should be send in the request.
*	Used only for "POST" requests and clears after each "POST" request. 
**/
	var requestData = null;

/**
*	Underlying AJAXRequest object.
**/
   	var ajaxRequest = null;

	initAJAXRequest();

/**
*	Initialie AJAXRequest object accordingly to browser. 
**/
	function initAJAXRequest(){
		try {
     		ajaxRequest = new XMLHttpRequest();
   		} catch (trymicrosoft) {
 			try {
       			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (othermicrosoft) {
       			try {
         			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
       			} catch (failed) {
         			ajaxRequest = null;
       			}  
    		}
   		}
	}
	
/**
*	Sends the AJAXRequest.
**/
	function sendAJAXRequest(){
		if(ajaxRequest != null){
			if(ajaxRequest.readyState != 0){
				initAJAXRequest();
			}
			if(ajaxRequest.readyState == 0){
				ajaxRequest.open(requestType, ajaxRequestURL + ajaxRequestCGI, isAsynchronous);
				if (isAsynchronous) {
					ajaxRequest.onreadystatechange = callbackFunctionWrapper;
				}
				ajaxRequest.send(requestData);	
				ajaxRequestCGI = "";		
			} 
   		} 
   	}

	/**
	*	Callback function wrapper.
	*/
	function callbackFunctionWrapper(){
		if (ajaxRequest.readyState == 4) {
    		if (ajaxRequest.status == 200) {
				callbackFunction();
			}
		}
	}

	/**
	*	Sends the AJAX "GET" Request.
	*/
	this.sendAJAXGETRequest = function(){
		requestType = "GET";
		requestData = null;
		sendAJAXRequest();
   	}

/**
*	Sends the AJAX "POST" Request.
**/
	this.sendAJAXPOSTRequest = function(){
		requestType = "POST";
		ajaxRequestCGI = "";
		sendAJAXRequest();
   	}

/**
*	Returns the AJAX response result. 
*	In case of the asyncchronous request this function should be
*	called by the callback function in order to get results.
**/
	this.getResponseText = function(){
		if(!isAsynchronous){
			return ajaxRequest.responseText;
		} else {
   			if (ajaxRequest.readyState == 4) {
       			if (ajaxRequest.status == 200) {
       				return ajaxRequest.responseText;
       			} else {
         			alert(ajaxRequest.status);
				}
       		} else {
       			return null;
       		}
       	}
	}

/**
*	Returns the AJAX response header. 
*	In case of the asyncchronous request this function should be
*	called by the callback function in order to get results.
**/
	this.getResponseHeader = function(headerName){
		if(!isAsynchronous){
			return ajaxRequest.getResponseHeader(headerName);
		} else {
   			if (ajaxRequest.readyState == 4) {
       			if (ajaxRequest.status == 200) {
       				return ajaxRequest.getResponseHeader(headerName);
       			} else {
         			alert(ajaxRequest.status);
				}
       		} else {
       			return null;
       		}
       	}
	}
	
/**
*	Adds key - value pair to the AJAX request cgi.
*	Performs escaping befor to add to the URL.
**/
	this.addRequestCGI = function(key, value){
		ajaxRequestCGI = ajaxRequestCGI + encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&';
	}

/**
*	Adds cgi parameters string to the url. 
*	All the parameters should be escaped, that is a user responsibility.
**/
	this.setRequestCGI = function(cgi){
		ajaxRequestCGI = cgi; 
	}

/**
*	Removes all the cgi parameters.
*	After each request the parameters removes automatically.
**/	
	this.removeRequestCGI = function(){
		ajaxRequestCGI = "";
	}
	
/**
*	Sets AJAX request URL.
**/
	this.setUrl = function(url){
		this.ajaxRequestURL = url;
	}
		
/**
*	Sets a callback function for the asynchronous request.
**/
	this.setCallbackFunction = function(cbfunc) {
		isAsynchronous = true;
		callbackFunction = cbfunc;	
	}
	
/**
*	Sets the request type "GET"/"POST".
**/
	this.setRequestType = function(type){
		if(type == "POST"){
			requestType = "POST";
		} else {
			requestType = "GET";
		}
	}
	
/**
*	Sets the request data for the "POST" request.
**/
	this.setRequestData = function(data){
		requestData = data
	}
	
/**
*	Removesthe request data.
**/
	this.removeRequestData = function(data){
				requestData = data
	}
}

