/**
/ This controller is responsable to create and to monitor the system's requests.
/ @Package: FireBrick
/ @Author: upa
/ @Last modify: February 22, 2008
/ @Charset: utf-8
/*/

function RequestController() {
	// Constructor
	
		// Creates a new XMLHttpRequest
		this.requestObject = null;
		
		// Code for Mozilla, Netscape and Opera
		if(window.XMLHttpRequest) {
			this.requestObject = new XMLHttpRequest();				
		}
		
		// Code for IE
		else if(window.ActiveXObject) {
			this.requestObject = new ActiveXObject("Msxml2.XMLHTTP");	
		}
		
		else {
			throw("Seu navegador não suporta requisições assíncronas.");	
		}
		
		// requestParameters is the data to be sent with the request
		this.requestParameters;
		
		// responseHandler is the function that will be called when the request successfully ends.
		this.responseHandler;
		
	// End of Constructor
	
	// Methods
		
	/**
	/ This method is a mask for the XMLHttpRequest.open method
	/*/
	this.configure = function(requestParameters,serviceURL,asyn,responseType,responseHandler) {			
		
		// Sets the responseFunction property			
		this.responseHandler = responseHandler || null;
		
		// This reference (it will be used within the onreadystatechange event handler)
		var t = this;
		
		// All the response handlers code are executed within this handler
		this.requestObject.onreadystatechange = function() {
			
			// Checks the readyState property value
			if(t.requestObject.readyState == 4) {					
				
				// Checks the server response status
				if(t.requestObject.status > 199 && t.requestObject.status < 300) {
					
					// Response variable
					var response;
					
					// Checks the response type parameter
					// 1: XMLDocument response
					// 2: Text response
					if(responseType == 1) {							
						response = t.requestObject.responseXML;
					}
					else if(responseType == 2) {
						response = t.requestObject.responseText;
					}
					else {
						throw("Falha no controlador de requisições: 500");
					}
					
					// Calls the response function (passing the service response)
					t.responseHandler(response);
				}
				else {
					
					throw("Falha no controlador de requisições: 501");
				}
			}
			else {
				
				// Loading...
			}
		};

		// Checks if the request will send data
		if(requestParameters) {
			var method = "POST";
			this.requestParameters = requestParameters;	
		}
		else {
			var method = "GET";	
		}
		
		// Opens the request
		this.requestObject.open(method,serviceURL,asyn);
		
		// Checks the requestParameters and sets the content type header (MUST BE DEFINED AFTER THE open METHOD)
		if(method == "POST") {
			
			// Changing the MIME type for the request. MUST be call after XMLHttpRequest.open method
			this.requestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
		}
	};
	
	/**
	/ Sends the request.
	/*/
	this.send = function() {
		
		// The request needs to have a response function reference to be sent
		if(this.responseHandler) {
			
			// Checks if the object has post data
			if(this.requestParameters) {
				this.requestObject.send(this.requestParameters);
			}
			else {
				this.requestObject.send(null);
			}	
		}
		else {
			throw("Falha no controlador de requisições: 503");	
		}
	};
		
	// End of Methods
}
