// start of xml-http-request functions

function createXmlReq(){

	var req;

	try{
		req = new XMLHttpRequest();
	}
	catch(e){
		var ieReq = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
		for(var i=0; i<ieReq.length && !req;i++){
			try{
				req = new ActiveXObject(ieReq[i]);
			}
			catch(e){}
		}
	}

	if(!req){
		alert("Can't create AJAX Request");
	}
	else{
		return req;
	}
}

function doRequest(requestString, url, cb, sync){
	httpRequest = new createXmlReq();
	if(httpRequest){
		try{
			httpRequest.open("POST",url, sync);
                        httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			httpRequest.onreadystatechange= function(){

	if(httpRequest.readyState == 4){

		if(httpRequest.status == 200){

			try{
  
				// handle the response
                                  eval(cb+"("+httpRequest.responseText+")");// this should execute the js-function deined in the response text with the parameters returned
			}
			catch(e){
				alert("Error reading the response!\n"+e.toString());
			}
		}
		else{
			alert("Error receiving the response!\n"+httpRequest.statusText);
		}
	}

}
			httpRequest.send(requestString);                       
                        if(sync === false){
                            eval(cb+"("+httpRequest.responseText+")");
                        }
		}
		catch(e){
			alert("Cannot establish connection to the server!\n"+e.toString());
		}
	}
}

function stateHandler(){
	if(httpRequest.readyState == 4){

		if(httpRequest.status == 200){

		//	try{
				// handle the response httpRequest.responseText
				//eval(cb+"("+"1"+")");// this should execute the js-function deined in the response text with the parameters returned
//                                /alert('boom');
		//	}
		//	catch(e){
		//		alert("Error reading the response!\n"+e.toString());
		//	}
		}
		else{
			alert("Error receiving the response!\n"+httpRequest.statusText);
		}
	}
}


function buildRequest(keys, values){
  params = '';
  for(i=0;i<keys.length;i++){
    params += ((i==0)?'':'&')+keys[i]+'='+encodeURI(values[i]);
  }

  return params+"&n="+Math.random().toString();

}
