var req = null;
//var host = 'http://autonews.ru';

var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;

var onReadyStateLoading;
var onReadyStateInteractive;
var onReadyStateLoaded;
var onReadyStateComplete;

function setReadyState (on_loading, on_loaded, on_interactive, on_complete) {
    onReadyStateLoading = on_loading ? on_loading : null;
    onReadyStateLoaded = on_loaded ? on_loaded : null;
    onReadyStateInteractive = on_interactive ? on_interactive : null;
    onReadyStateComplete = on_complete ? on_complete : null;
}

function sendRequest (func, url, params, method, contentType ) {
    if (!method) {
        method = 'GET';
    }
    if (!contentType && method=="POST"){
        contentType='application/x-www-form-urlencoded';
    }
    req = initXMLHTTPRequest();
    if (req) {
        req.onreadystatechange = func ? func : onLoadState;
        req.open(method, url, true);
        req.setRequestHeader("Accept-Language", "ru, en");
        req.setRequestHeader("Accept-Charset", "windows-1251");
        if(contentType)
                req.setRequestHeader('Content-Type', contentType);
        req.send(params);
        return req;
    }
    return false;
}

function initXMLHTTPRequest () {
    var xReq = null;
    if (window.XMLHttpRequest){
        xReq = new XMLHttpRequest();
    } else if (window.ActiveXObject){
        xReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xReq;
}

function onLoadState() {
    var ready = req.readyState;
    var data = null;
    if( ready == READY_STATE_LOADING ) {
        if(onReadyStateLoading)
            onReadyStateLoading(req);
    } else if ( ready == READY_STATE_LOADED ) {
        if(onReadyStateLoaded)
            onReadyStateLoaded(req);
    } else if ( ready == READY_STATE_INTERACTIVE ) {
        if(onReadyStateInteractive)
            onReadyStateInteractive(req);
    } else if ( ready == READY_STATE_COMPLETE ) {
        if(onReadyStateComplete)
            onReadyStateComplete(req);
    }
}

