// url="http://getfiles.ru?id=123&..."
// callback=function(result,status){...} - function, that calls after server answer
// postParameters = {param1:'blabla', param2:123, param3:['abc',210], ...}
var JsonByAjax = function(url,callback,postParameters)
{
    // that's for avoid carbage collection
    var currentInstanceNum_ = JsonByAjax.instanceCounter_;
    JsonByAjax.instanceCounter_++;
    if (JsonByAjax.instanceCounter_>1000000) JsonByAjax.instanceCounter_=0;
    JsonByAjax.instanceCollection_[currentInstanceNum_]=this;
    
    var httpRequest = (function() {
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
return xmlhttp;

    })();
    

	if(!httpRequest) alert('can\'t initialized ajax-object');
    if(!url) alert('not found url-parameter');
	if (httpRequest.overrideMimeType) httpRequest.overrideMimeType('text/xml');
	
	var getSendContentFromPostParameters = function()
	{
	    sendContent = '';
        for (var id in postParameters) {
            var item = postParameters[id];
            var prefix = escape(id)+'=';
            if (item instanceof Array) {
                for (var i in item) {
                    if (sendContent.length) sendContent+='&';
                    sendContent+=prefix+escape(item[i]);
                }
            } else {
                if (sendContent.length) sendContent+='&';
                sendContent+=prefix+escape(item);
            }
        }
        return sendContent;
	};
	
	//resolve caching problems request results
	var now = "timestamp=" + new Date().getTime();
	url += (url.indexOf("?")+1) ? "&" : "?";
	url += now;
    var sendContent = null;
	if(postParameters) {
	    sendContent = getSendContentFromPostParameters();
	    httpRequest.open("POST", url, true);
		httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpRequest.setRequestHeader("Content-length", postParameters.length);
		httpRequest.setRequestHeader("Connection", "close");
	} else {
        httpRequest.open("GET", url, true);
	}

	httpRequest.onreadystatechange = function () {//Call a function when the state changes.
		if (httpRequest.readyState == 4) {//Ready State will be 4 when the document is loaded.
            var result = null;
			if(
			    httpRequest.status == 200
			    && httpRequest.responseText
			) {
				result = httpRequest.responseText;
				// translate JSON, avoid IE errors
				result = result.replace(/[\n\r]/g,"");
				result = eval('('+result+')');
			}
			callback(result, httpRequest.status);
			delete JsonByAjax.instanceCollection_[currentInstanceNum_];
		}
	}

	httpRequest.send(sendContent);
};

JsonByAjax.instanceCounter_=0;
JsonByAjax.instanceCollection_=new Object();