pcwa.core.scope('pcwa.ext.ajax');

pcwa.ext.ajax.RestRequester= function RestRequester(config){
	
	var xhr = Ext.lib.Ajax.getConnectionObject().conn;
	var inUse = false;
	
	this.setRequestHeader = function setRequestHeader(n, v){
		if ( typeof n == 'string' && typeof v == 'string' ){
			config.headers[n] = v;
		}
	}
	
	this.setRequestHeaders = function setRequestHeaders(obj){
		config.headers = {};
		for ( var e in obj){
			config.headers[e] = obj[e];
		}
	}
	
	this.getRequestHeader = function getRequestHeader(key){
		if (config.headers[key]){
			return config.headers[key];
		} else {
			return "";
		}
	}
	
	this.getRequestHeaders = function getRequestHeaders(){
		var tmp = new Array();
		var cnt = 0;
		
		for (var e in config.headers){
			tmp[cnt] = e;
			cnt++;
		}
		
		return tmp;
	}
	
	this.setMethod = function setMethod(m){
		if ( m.length > 0 ){
			if ( m.toUpperCase() == 'PUT' || m.toUpperCase() == 'DELETE' ){
				config.method = 'POST';
				config.headers['x-request-method'] = m.toUpperCase();
			} else {
				config.method = m.toUpperCase();
				if ( config.headers['x-request-method'] ){
					delete config.headers['x-request-method'];
				}
			}
		}
	}

	this.getMethod = function getMethod(){
		if ( config.headers.x_request_method ){
			return config.headers.x_request_method;
		} else {
			return config.method;
		}
	}
	
	this.setURL = function setURL(url){
		config.url = url;
	}

	this.getURL = function getURL(){
		return config.url;
	}
	
	this.setSuccess = function setSuccess(cb){
		config.success = cb;
	}

	this.getSuccess = function getSuccess(){
		return config.success;
	}
	
	this.setFailure = function setFailure(cb){
		config.failure = cb;
	}

	this.getfailure = function getfailure(){
		return config.failure;
	}
	
	this.setScope = function setScope(scope){
		config.scope = scope;
	}

	this.getScope = function getScope(){
		return config.scope;
	}	
	
	this.sendRequest = function sendRequest(){
		
		if( inUse ){ return;} else { inUse = true;}
		
		if (config.method == null || config.url == null){
			return false;
		}
		xhr.onreadystatechange = processResponse;
		xhr.open(config.method, config.url, true);
		
		if (config.headers){
			for (var e in config.headers){
				xhr.setRequestHeader(e,config.headers[e]);
			}			 
		}
		
		xhr.send(config.entity);
		return true;
		
	}
	
	function processResponse(){
		if (xhr.readyState == 4) {
			if ( xhr.status >= 400 && xhr.status != 1223){
				if (config.callback != null){
					config.callback.call(config.scope, false, xhr, config.callbackParams);
					inUse = false;
					return;
				}
				if (config.failure != null){
					config.failure.call(config.scope, xhr, config.callbackParams);					
					inUse = false;
				}
			} else {
				if (config.callback != null){
					config.callback.call(config.scope, true, xhr, config.callbackParams);
					inUse = false;
					return;
				}
				if (config.success != null){
					config.success.call(config.scope, xhr, config.callbackParams);
					inUse = false;
				}
			}			
		}
	}

	if ( config == null ){ var config = new Object();}	
	if ( !config.headers ) { config.headers = {};}	
	if ( !config.method ){ config.method = null;} else { this.setMethod(config.method);}	
	if ( !config.url ){ config.url = null;}
	if ( !config.success ){ config.success = null;}
	if ( !config.failure ){ config.failure = null;}
	if ( !config.callback ){ config.callback = null;}	
	if ( !config.callbackParams ){ config.callbackParams = {};}	
	if ( !config.scope ){ config.scope = window;}
	if ( !config.entity ){ config.entity = null;}
};


