function Question(id, target, content, funcOnResult)
{
	this.id = id;
	this.target = target;
	this.content = content;
	this.funcOnResult = funcOnResult;
	this.request = null;
}

Question.current = null;
Question.queue = new Array();

Question.onResponse = function()
{
	if( Question.current != null ) {
		if( Question.current.checkAnswer() ) {
			Question.current = null;
			
			if( Question.queue.length > 0 ) {
				var next = Question.queue[ 0 ];
				var rest = new Array();
				
				for( var j = 1; j < Question.queue.length; j ++ )
					rest[ j ] = Question.queue[ j ];
				
				Question.current = null;
				Question.queue = rest;
				Question.send( next );
			}
		}
	}
}

Question.send = function(q)
{
	if( (Question.current == null) || (Question.queue.length <= 0) ) {
		Question.current = q;
		Question.current.send();
		Question.queue.length = 0;
	} else {
		Question.queue[ Question.queue.length ] = q;
	}
}

Question.prototype.ask = function()
{
	if (window.XMLHttpRequest)
		this.request = new XMLHttpRequest();
	else if (window.ActiveXObject)
	{
		try {
			this.request = new ActiveXObject("Msxml2.XMLHTTP")
		} catch (e) {
			try{
				this.request = new ActiveXObject("Microsoft.XMLHTTP")
			} catch (e) {
				return false;
			}
		}
	} else {
		return false;
	}
	
	this.request.onreadystatechange = function() { Question.onResponse(); };
	Question.send( this );
	
	return true;
}

Question.prototype.send = function()
{
	if( this.content == null ) {
		this.request.open( "GET", this.target );
		this.request.send( null );
	} else {
		this.request.open( "POST", this.target );
		this.request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
		this.request.send( this.content );
	}
}

Question.prototype.checkAnswer = function()
{
    if( (this.request.readyState == 4) &&
		((this.request.status == 200) || (window.location.href.indexOf("http")==-1)) )
    {
        var sr = this.request.responseText;

        try {
			var structuredAnswer = sr.parseJSON(null);
			this.funcOnResult( this.id, true, structuredAnswer );
        } catch( exception ) {
			this.funcOnResult( this.id, false, null );
        }

        this.request = null;
        return true;
    }
    
    return false;
}