﻿var SimpleAjax = function (){
    var transport = SimpleAjax.getTransport();
    if(!transport) return;
    this.transport = transport;
	this.json = null;
    this.aborted = false;
    this.options = {
        method:                 'post'
    ,   contentType:            'application/x-www-form-urlencoded'
    ,   encoding:               'UTF-8'
    ,   timeout: 5
    ,   asyncronios:            true             
    ,   onerror:                window.$void
    ,   onsuccess:              window.$void
    ,   onrequeststatechanged:  window.$void
    };
    
};    

Object.extend(
    SimpleAjax
,   {
        Version:"1.0.0"
    ,   activeRequestCount: 0
    ,   getTransport: function() {
            return Try.Get(
                function() {return new XMLHttpRequest()}
            ,   function() {return new ActiveXObject('Msxml2.XMLHTTP')}
            ,   function() {return new ActiveXObject('Microsoft.XMLHTTP')}
            ) || false;
        }
});

Object.extend( 
    SimpleAjax.prototype
,   {
        checkForTimeout: function(){
            if( this.transport.readyState != 4 ) {
                this.aborted = true;
                this.transport.abort();
            }
        }
    ,   OnReadyStateChangedCallback: function() {
            if(this.transport.readyState == 4){
                if( !this.aborted && this.transport.status >= 200 && this.transport.status < 300 ){
                    this.options.onsuccess(this);
                    this.transport.onreadystatechange = null;
                }else{
                    this.options.onerror(this);
                    this.transport.onreadystatechange = null;
                }
            }else{
                this.options.onrequeststatechanged(this, this.transport.readyState);
            }
            
        }    
    ,   setRequestHeaders: function(){
            var headers = {
                    'X-Requested-With': 'XMLHttpRequest'
                ,   'X-Ajax-Version': SimpleAjax.Version
                ,   'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
                };

            if ( this.options.method == 'post') {
              headers['Content-type'] = this.options.contentType 
                    +
                    (this.options.encoding ? '; charset=' + this.options.encoding : '');

                /* Force "Connection: close" for older Mozilla browsers to work
                * around a bug where XMLHttpRequest sends an incorrect
                * Content-length header. See Mozilla Bugzilla #246651.
                */
                if( this.transport.overrideMimeType 
                    && (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005) {
                    headers['Connection'] = 'close';
                }
            	this.transport.setRequestHeader("Content-length", this.options.postParams? this.options.postParams.length : 0); 
			}
            for (var name in headers){
              this.transport.setRequestHeader(name, headers[name]);
            }
        }
    ,   send: function( url, options ){
           Object.extend( this.options, options );
           
           setTimeout( this.checkForTimeout.bind(this), this.options.timeout * 1000 );
           this.transport.open(this.options.method, url, this.options.asyncronios);
           this.transport.onreadystatechange = this.OnReadyStateChangedCallback.bind(this);
           this.setRequestHeaders();
           this.body = this.options.method == 'post' ? (this.options.postParams || url.split('?')[1] ) : null;
           this.transport.send(this.body);
        }
});
