// remember the previous Http object
var xXMLHttpRequest = window.XMLHttpRequest; 

// Provide the XMLHttpRequest class for IE 5-6
if ( typeof XMLHttpRequest == "undefined" ) {
	XMLHttpRequest = function() 
	{
		try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
		throw new Error( "This browser does not support XMLHttpRequest." )
	} ;
}


//XMLHttpRequest.UNSENT = 0; 
//XMLHttpRequest.OPENED = 1; 
//XMLHttpRequest.HEADERS_RECEIVED = 2; 
//XMLHttpRequest.LOADING = 3; 
//XMLHttpRequest.DONE = 4;
	
// Firebug stuff
if ( xXMLHttpRequest.wrapped) 
	XMLHttpRequest.wrapped = xXMLHttpRequest.wrapped ;


function Http(url)
{
	this.parms = new Array() ;
	this.parms_index = 0 ;
	this.execute = http_exec ;
	this.add = http_add ;
	this.get = http_get ;
	this.post = http_post ;
	this.server = url ;
}

function http_add(name,value)
{
	this.parms[this.parms_index] = new Pair(name,value) ;
	this.parms_index++ ;
}

function http_add_form ( form_name )
{
	//document.form_name ;
}

function http_get(post_data)
{
	return this.execute(0,post_data) ;
}

function http_post(post_data)
{
	return this.execute(1,post_data) ;
}

function http_exec(post_flag,post_data)
{
	var url = this.server ;

	var http = new XMLHttpRequest();

	var txt = "" ;
	for ( var i in this.parms ) {
		if ( txt != "" )
			txt += '&' ;
		txt += this.parms[i].name + '=' + this.parms[i].value ;
	}
	
	if ( url[url.length-1] != '?' )
		url += "?" ;
		
	if ( !post_flag ) {
		http.open("GET", url+txt, false, null, null);
		http.send(post_data) ;
	} else {
		http.open("POST", url+txt, false, null, null);
		//http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		http.send(post_data) ;
	}
	
	switch(http.readyState) {
		case 1,2,3:
			throw "Bad ready state: '" + http.readyState ;
			break ;
		case 4:
			if( http.status != 200 )
				throw "Bad status returned: '" + http.status + " " + http.statusText ;
			break;
	}
	
	handle_request_error(http) ;

	return http ;
}

function Pair(name,value)
{
	this.name = name ;
	this.value = value ;
}


function handle_request_error(http)
{
	var xml = http.responseXML ;
	
	if ( xml == null )
		throw http.responseText ;

		
	if ( xml.documentElement.tagName == "parsererror" ) {
		throw "Invalid XML returned" ;
	}

	if ( xml.childNodes.length == 0 )
		throw "Invalid XML returned" ;

	var errors = xml.getElementsByTagName("error") ;
	
	if ( errors.length == 0 )
		return ;
		
	var error_code = errors[0].getAttribute("code") ;
	
	// analyze the case when we are not logged in
	if ( error_code == 13 ) {
		alert("You are not logged in!") ;
		window.location = "/login/" ;
		return ;
	}
    	
	var message = "" ;
	
	if ( config_debug_ ) {
		if ( errors[0].getAttribute("message") != null )
			message = message + errors[0].getAttribute("message") ;
		if ( errors[0].getAttribute("string") != null )
			message = message + " " + errors[0].getAttribute("message") ;
	} else {
		if ( errors[0].getAttribute("string") != null )
			message = errors[0].getAttribute("string") ;
		else
			message = errors[0].getAttribute("message") ;
	}

	//throw message ;
	throw "bbb" ;
}
