ajax = {
    // PUBLIC DECLARATIONS
        // Variables
            url: '',
            method: 'post',
            async: false,
            events: {'Uninitialized':null,'Loading':null,'Loaded':null,'Interactive':null,'Complete':null},
            content: {},

            resultHTML: "",
            resultDOC: null,

            error: "",
        // Functions
            init: null,
            get: null,
            post: null,

    // PRIVATE DECLARATIONS
        // Variables
            instance: null,
            state: 'unable',
        // Functions
            // 0 - до того как запрос отправлен (uninitialized)
            // 1 - объект инициализирован (loading)
            // 2 - получен ответ от сервера (loaded)
            // 3 - соединение с сервером активно (interactive)
            // 4 - объект завершил работу (complete)
            onReadyStateChange: null,
            createInstance: null,
            openInstanceURL: null,
            setRequestHeaders: null,
            formatPostData: null,
            sendRequest: null
};
/* -----------------------------------------------------------------------
                             IMPLEMENTATION
----------------------------------------------------------------------- */
// ----------------------------------------
//         PRIVATE DECLARATIONS
// ----------------------------------------
ajax.onReadyStateChange = function()
{
    if( this.instance == null ) return;
    switch( this.instance.readyState )
    {
        case 0:        this.events['Uninitialized']; break;
        case 1:        this.events['Loading']; break;
        case 2:        this.events['Loades']; break;
        case 3:        this.events['Interactive']; break;
        case 4:        this.events['Complete']; break;
    }
};


ajax.createInstance = function()
{
    var instance = null;
    try
    {
        instance = new XMLHttpRequest();
    }
    catch(e)
    {
        try
        {
            instance = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch(e)
        {
            try
            {
                instance = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch(e)
            {
                instance = null;
            }
        }
    }

    this.state = instance != null ? 'free' : 'unable';
    if( this.state == 'unable' )
    {
        this.error = "AJAX.createInstance -> Can't create class instance";
        return false;
    }
    this.error = '';
    return instance;
};


ajax.openInstanceURL = function()
{
    if( this.state != 'busy' )
    {
        this.error = 'AJAX.openInstanceURL -> transport always busy';
        return false;
    }

    try
    {
        this.instance.open( this.method, this.url, this.async );
    }
    catch(e)
    {
        this.error = 'AJAX.openInstanceURL -> error with opening url';
        return false;
    }
    this.error = '';
    return true;
};


ajax.setRequestHeaders = function()
{
    /* Force "Connection: close" for older Mozilla browsers to work
     * around a bug where XMLHttpRequest sends an incorrect
     * Content-length header. See Mozilla Bugzilla #246651. */
    var headers =
    {
        'X-Requested-With': 'XMLHttpRequest',
        'Accept': 'text/javascript, text/html, application/xml, text/xml, */*',
        'Content-type': "application/x-www-form-urlencoded; charset=utf-8",
        'Content-length': this.formatPostData( this.content ).length,
        'Connection': "close"
    };

    var uagent = navigator.userAgent.toLowerCase();
    var is_safari = ( ( uagent.indexOf('safari') != -1 ) || ( navigator.vendor == "Apple Computer, Inc." ) );
    var is_opera  = ( uagent.indexOf('opera') != -1 );
    var is_webtv  = ( uagent.indexOf('webtv') != -1 );
    var is_ie = ( ( uagent.indexOf('msie') != -1 ) && ( !is_opera ) && ( !this.is_safari ) && ( !this.is_webtv ) );
    if( is_ie )
    {
        headers["Pragma"] = "no-cache";
        headers["Cache-Control"] = "must-revalidate, no-cache, no-store";
    }

    try
    {
        for( var name in headers )
        {
            this.instance.setRequestHeader( name, headers[name] );
        }
    }
    catch(e)
    {
        this.error = 'AJAX.setResquestHeaders -> Error with setting request headers';
        return false;
    }

    this.error = '';
    return true;
};


ajax.formatPostData = function()
{
    var data = "";
    for( var i in this.content )
    {
        data += i + "=" + escape( this.content[i] ) + "&";
    }

    data += 'ajax-sended=1&';

    return data;
};


ajax.sendRequest = function()
{
    if( this.state != 'free' )
    {
        this.error = 'AJAX.sendRequest -> transport is not free';
        return false;
    }
    this.state = 'busy';

    var method = this.method.toLowerCase();
    if( ( method != 'post' && method != 'get' ) || this.url.length < 1 )
    {
        this.error = 'AJAX.sendRequest -> unknown method "'+ method +'" or URL is empty';
        return false;
    }

    if( !this.openInstanceURL() ) return false;
    if( !this.setRequestHeaders() ) return false;
    var post = this.formatPostData( this.content );

    try
    {
        if( method == 'get' ) this.instance.send( null );
        if( method == 'post' ) this.instance.send( post );
    }
    catch(e)
    {
        this.error = 'AJAX.sendRequest -> error with transporting data';
        return false;
    }

    this.resultHTML = this.instance.responseText;

    this.state = 'free';
    this.content = {};
    this.error = '';
    return true;
};

// ----------------------------------------
//         PUBLIC DECLARATIONS
// ----------------------------------------
ajax.init = function()
{
    this.instance = this.createInstance();

    if( this.instance == null ) return false;

    this.instance.onreadystatechange = this.onReadyStateChange;
    this.state = 'free';
    return true;
};


ajax.get = function( url )
{
    this.url = url;
    this.method = 'get';
    return this.sendRequest();
};


ajax.post = function( url, content )
{
    if( !content || content == null ) content = null;
    if( content != null ) this.content = content;

    this.url = url;
    this.method = 'post';
    return this.sendRequest();
};


ajax.getResultArray = function()
{

/*    var content = this.resultHTML;

    var result = {};
    var j = 0;

    while( content.match( /(.*?)\=\^\_\^\=/i ) )
    {

        var elem = content.match( /(.*?)\=\^\_\^\=/i );
        result[j] = elem[1]; j++;
        content = content.replace( elem[0], '' );

    }

    result[j] = content;
*/
	var result = {};
	result = this.resultHTML.split('=^_^=');
    return result;

};
