JSVL 						= JSVL 						|| {};

JSVL.AJAX$CLASSDEF = {

    createXMLRequest: function(){

        return JSVL.TRY.these(
            function() {
                return new XMLHttpRequest()
            },
            function() {
                return new ActiveXObject('Msxml2.XMLHTTP')
            },
            function() {
                return new ActiveXObject('Microsoft.XMLHTTP')
            }
            ) || false;

    },

    createRequest: function(conf){
        var req        = new JSVL.AJAX.REQUEST(conf);
        return req;

    },

    createResponse: function(conf){

        var req = new JSVL.AJAX.RESPONSE(conf);

        return req;

    },

    XMLRequest:null ,
    AJAXRequest:null,


	onCompleted:function(){
		
			    var resp = this.createResponse({
                    Request:this.AJAXRequest
                });
                resp.Body   = this.XMLRequest.responseBody;
                resp.Text   = this.XMLRequest.responseText;
                resp.XML    = this.XMLRequest.responseXML;
                resp.status         = this.XMLRequest.status;
                resp.statusText     = this.XMLRequest.statusText;
                            
                if (resp.status  == 200  ||
                    (resp.status  == 0 && resp.statusText == "OK")) {
                    this.AJAXRequest._onSuccess(resp);
                }
                 
                else
                {
                    this.AJAXRequest.onError(resp);
                }
		
	},

    _onReadyStateChange:function(){
        var state = this.XMLRequest.readyState;
        JSVL.LOGGER.TRACEMSG('JSVL.AJAX =>_onReadyStateChange :'+ state);
        switch(state){
            case 0: // XMLHTTPREQUEST_READY_STATE_UNINITIALIZED
                break;
            case 1: // XMLHTTPREQUEST_READY_STATE_LOADING
                break;
            case 2: // XMLHTTPREQUEST_READY_STATE_LOADED
                break;
            case 3: // XMLHTTPREQUEST_READY_STATE_INTERACTIVE
                break;
            case 4: // XMLHTTPREQUEST_READY_STATE_COMPLETED
            	this.onCompleted();
                break;
            default:
                break;
        }
    },

    _onTimeOut:function(){
        this.AJAXRequest.onTimeOut();
    },
    execute:function( requestConf ){
    	
    	delete this.XMLRequest;
        delete this.AJAXRequest;
        
        this.AJAXRequest = this.createRequest(requestConf);
        var req    =   this.AJAXRequest;
         var me = this;
         var f = function (){
         	
         }
          if (req.responseType == "JSONP" ){
          	
          	 JSVL.DOM.appendJS({
                            onLoadComplete:f,
                            src:req.url
                        });	
          	return;
          }
    	
        var delegateOnReadyStateChange = JSVL.CREATEDELEGATE(this,this._onReadyStateChange);
        var delegateonTimeOut          = JSVL.CREATEDELEGATE(this,this._onTimeOut);
        
        this.XMLRequest  = this.createXMLRequest();
        var xmlReq =  this.XMLRequest;
        
        xmlReq.open(req.method,req.url,req.asynchronous);
        if ( req.timeout && req.timeout > 0 ){
            xmlReq.timeout   = req.timeout;
            xmlReq.ontimeout = delegateonTimeOut;
        }
              
        xmlReq.onreadystatechange =  delegateOnReadyStateChange;
        if ( xmlReq.onerror == null ){
            xmlReq.onerror = this.AJAXRequest.onError;
        }
        xmlReq.send();
        if (req.asynchronous == false ){
        	this.onCompleted();
        }
    }
    
};

JSVL.AJAX  = function(){  
	if ( JSVL.ISFUNCTION(this.setupInstance) ){
		this.setupInstance.apply(this,arguments);
	}
	return this;
}
JSVL.AJAX.prototype 			= new JSVL.OBSERVABLE({CREATINGCLASS:true});
JSVL.AJAX.prototype.constructor = JSVL.AJAX;

JSVL.APPLY(JSVL.AJAX.prototype,JSVL.AJAX$CLASSDEF);


JSVL.AJAX.REQUEST$CLASSDEF = {
       
    responseType:"TEXT" ,
    asynchronous:true   ,
    url:""              ,
    method:"GET"        ,
    timeout:0           ,

    normalizeResponse:function(response) {



    },
    onTimeOut:function(){
         JSVL.LOGGER.TRACEMSG('JSVL.AJAX.Request =>onTimeOut');
    },
    _onSuccess:function(response){

         JSVL.LOGGER.TRACEMSG('JSVL.AJAX.Request =>_onSuccess :'+
            response.status + " " +response.statusText);

        if (this.responseType == "JSON" ){
            var json = response.Text;
            response.OBJ = eval("(" + json + ')');
        }
        else if(this.responseType == "XML2JSON" )
        {
            
            response.OBJ = JSVL.XML.nodeToObject(response.XML.documentElement)

        }
        this.normalizeResponse(response);
         
        this.onSuccess(response);
    },
    onSuccess:function(response){
         JSVL.LOGGER.TRACEMSG('JSVL.AJAX.Request =>onSuccess :'+
            response.status + " " +response.statusText);
    },
    onError:function( response )  {

        JSVL.LOGGER.TRACEMSG('JSVL.Ajax.Request =>onError :'+
            response.status + " " +response.statusText);

    }

};

JSVL.AJAX.REQUEST = function(){  
	if ( JSVL.ISFUNCTION(this.setupInstance) ){
		this.setupInstance.apply(this,arguments);
	}
	return this;
}
JSVL.AJAX.REQUEST.prototype 			  = new JSVL.COREOBJECT({CREATINGCLASS:true});
JSVL.AJAX.REQUEST.prototype.constructor = JSVL.AJAX.REQUEST;

JSVL.APPLY(JSVL.AJAX.REQUEST.prototype,JSVL.AJAX.REQUEST$CLASSDEF);


JSVL.AJAX.RESPONSE$CLASSDEF  = {
            Request:null        ,
            Body:null   ,
            Text:null   ,
            XML:null    ,
            OBJ:null,
            status:null         ,
            statusText:null
};
 
JSVL.AJAX.RESPONSE = function(){  
	if ( JSVL.ISFUNCTION(this.setupInstance) ){
		this.setupInstance.apply(this,arguments);
	}
	return this;
}
JSVL.AJAX.RESPONSE.prototype 			  = new JSVL.COREOBJECT({CREATINGCLASS:true});
JSVL.AJAX.RESPONSE.prototype.constructor = JSVL.AJAX.REQUEST;

JSVL.APPLY(JSVL.AJAX.RESPONSE.prototype,JSVL.AJAX.RESPONSE$CLASSDEF);
 
 
 

