
/**
 * Biblioteka do obslugi requestow HTTP od JS
 *
 * @author Mariusz Chwalba <koder at mediasystems dot pl>
 * @copyright 2008 Media Systems
 * @package lib
 * @subpackage js
 */


function HTTPRequest( url )
{
        this.oninit       = null;
        this.onconnecting = null;
        this.onheaders    = null;
        this.onloading    = null;
        this.onload       = null;
        this.data         = null;

        this.url = url;
        this.method = 'POST';
        this.async = true;
        
        this.data = {};
        
        if (window.XMLHttpRequest)
        {
                this.request = new XMLHttpRequest();
        }
        else if(window.ActiveXObject)
        {
                this.request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else
        {
                alert( 'AJAX not supported' );
        }
        
        this.onreadystatechange = function( wrapper )
        {
                var state   = wrapper.request.readyState;
                
                switch( state )
                {
                        case 0:
                                if ( wrapper.oninit ) return wrapper.oninit();
                                break;
                        case 1:
                                if ( wrapper.onconnecting ) return wrapper.onconnecting();
                                break;
                        case 2:
                                if ( wrapper.onheaders ) return wrapper.onheaders();
                                break;
                                
                        case 3:
                                if ( wrapper.onloading ) return wrapper.onloading();
                                break;
                                
                        case 4:
                                if ( wrapper.onload ) return wrapper.onload();
                                break;
                                
                }
        }
        
        this.getEncodedData = function()
        {
                if ( this.data == null ) return null;
                
                var out='';
                for( key in this.data )
                {
                        out += ( escape( key ) + '=' + ( this.data[key].toString().replace(/&/,'%26') ) + '&' );
                }
                
                return out;
        }
        
        this.send = function()
        {
                this.request.open( this.method, this.url, this.async );
                var iam = this;
                this.request.onreadystatechange = function() { iam.onreadystatechange( iam ); };
                
                if ( this.method=='POST' )
                {
                        this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                }
                
                this.request.send( this.getEncodedData() );
                
        }
        
        this.response = function()
        {
                var text = this.request.responseText;
                if ( text == '' ) return '';
                var obj = eval( '(' + text + ')' );
                return obj;
        }
}

