function sleep(millisecond)
{
    var now = new Date();
    var exitTime = now.getTime() + millisecond;

    while(true)
    {
        now = new Date();
        if(now.getTime() > exitTime) return;
    }
}

function ajax ()
{
	this.method         = "POST";
	this.lastError      = "";
	this.lastErrorCode  = 0;
	this.xmlhttp        = null;
	this.vars           = Array();
	this.response       = null;
	this.responseXML    = null;
	this.responseStatus = new Array(2);
	this.busy           = false;

	this.constructor   = function () {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.lastError     = "Faild to create XMLHTTP";
				this.lastErrorCode = 100;
			}
		}
		if (this.xmlhttp.overrideMimeType) this.xmlhttp.overrideMimeType("text/xml"); //Firefox bug
	};

	this.set = function (name, value){
		this.vars[name] = value;
	};

	this.runResponse = function() {
		eval(this.response);
	}

	this.query = function (urlstring, asynchronous, content)
	{
		if (this.lastErrorCode > 0 || this.busy) {
			return false;
		} else {
			if (this.xmlhttp) {
				this.busy = true;
				try {
					window.cursorwait.display(true);
				} catch (e1) {}
							
				var _this = this;

				/*if (!content) {
					this.method = "GET";
				}*/
				
				this.xmlhttp.open(this.method, urlstring, asynchronous);
				
				this.xmlhttp.onreadystatechange = function() {
					switch (_this.xmlhttp.readyState) {
						case 1:
							_this.busy = true;
							/*try {
								cursorwait.display(true);
							} catch (e1) {}*/
							if (_this.onLoading)
								_this.onLoading();
							break;
							
						case 2:
							if (_this.onLoaded)
								_this.onLoaded();
							break;
							
						case 3:
							if (_this.onInteractive)
								_this.onInteractive();
							break;
							
						case 4:
							_this.response          = _this.xmlhttp.responseText;
							_this.responseXML       = _this.xmlhttp.responseXML;
							_this.responseStatus[0] = _this.xmlhttp.status;
							_this.responseStatus[1] = _this.xmlhttp.statusText;

							/*var doc = (new DOMParser()).parseFromString(req.responseText, “text/xml”);)*/ /*FIREFOX LOX*/

							if (_this.responseStatus[0] == "200") {
								if (_this.onCompletion)
									_this.onCompletion();
							} else {
								if (_this.onError)
									_this.onError();
							}
							/*try {
								cursorwait.display(false);
							} catch (e1) {}*/
							_this.busy = false;
							//self.URLString = "";
							break;
					}
					try {
						window.cursorwait.display(false);
					} catch (e1) {}
					this.busy = false;
				};

				this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
				if (content != undefined) {
					//this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
					this.xmlhttp.send(content);
				} else {
					this.xmlhttp.send('asd=asd');
				}
				//if method GET, then this parameter may be null
			}
			return true;
		}
	};

	this.constructor();
}
//myobject.prototype.newContainedValue = someValue;

function xmlhttp (startCount)
{
	this.startCount = startCount;
	this.maxCount   = 10;
	this.objects    = [];//new Array();
	this.response   = "";

	var _this = this;

	if (this.startCount > this.maxCount)
		this.startCount = this.maxCount;

	this.constructor = function ()
	{
		for (var i = 0; (i < this.startCount && i < this.maxCount); i++) {
			this.objects.push({"ajax" : new ajax(), "onCompletion" : null});
			//this.objects[i] = new ajax();
		}
	};

	/*this.objects.push = function (row)
	{
		this.objects[this.objects.length] = row;
		return this.objects.length;
	}*/

	this.cloneFunction = function ( _func )
	{
		// Find first the whole function string:
		var functionString = _func.toString();
		// Extract the function body:
		var bodyString     = functionString.substring( functionString.indexOf("{")+1, functionString.lastIndexOf("}") );
		// Extract the function argument:
		var functionArg = functionString.substring( functionString.indexOf("(")+1, functionString.indexOf(")") );
		// Build an array containing all function arguments:
		var argArray = functionArg.split(",");
		// Start building the cloning string:
		var retString = "new Function(";
		// Add paramters to cloning string:
		for ( var i = 0; i < argArray.length; i++ )
		{
			retString += "'" + argArray[i] + "',";
		}
		// Add function body to cloning string:
		retString += "'" + bodyString + "')";
		// The function body contains unwanted newlines, remove them:
		retString = retString.replace(/\n/g," ");
		return retString;
	}

/*var list = [
{fname: "Dave", lname: "Smith"},
{fname: "Mary", lname: "Brown"}];
[color=blue]
> //add object ("Sue","Wood") to arrays
> // How?[/color]

list.push({fname: "Sue", lname: "Brown"});*/

	this.query = function (urlstring, asynchronous, content)
	{
		for (var i = 0; i < this.maxCount; i++)
		{
			/*document.getElementById("debug").innerHTML = document.getElementById("debug").innerHTML
			 + this.objects[i] + " : " + this.objects.length + ";<br>";*/
			if (this.objects[i]) {
				if (this.objects[i].ajax.busy) {
					continue;
				} else {
					this.objects[i].onCompletion      = this.onCompletion;
					this.onCompletion                 = null;
					this.objects[i].ajax.onCompletion = function () {
						_this.response    = this.response;
						_this.responseXML = this.responseXML;
						_this.objects[i].onCompletion();
						//_this.objects[i].ajax.busy = false;
					};
					this.objects[i].ajax.query(urlstring, asynchronous, content);
					return true;
				}
			} else {
				//it runs when fail free object
				var len = this.objects.length;
				if (len < this.maxCount) {
					//this.objects[len] = {"ajax" : new ajax()};
					try {
						this.objects.push({"ajax" : new ajax()});
						return this.query(urlstring, asynchronous, content);
					} catch (er) {alert(PLS_WAIT);}
				}
				return false;
			}
		}
	};

	this.constructor();
}