var xmlHttp = createXmlHttpRequestObject();

//holds the remote server address and parameters
var getlist = "/place/rate";

function createXmlHttpRequestObject()
{
		//variable to hold the object
		var xmlHttp;
		
		//this will work for non IE6(and below)
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch(e)
		{
			//assume IE6 or older
			var versions = new Array('MSXML2.XMLHTTP.6.0',
									 'MSXML2.XMLHTTP.5.0',
									 'MSXML2.XMLHTTP.4.0',
									 'MSXML2.XMLHTTP.1.0',
									 'MSXML2.XMLHTTP',
									 'Microsoft.XMLHTTP');
			for(var i=0; i<versions.length && !xmlHttp; i++)
			{
				try
				{
					//try to create the xmlHttpRequest object
					xmlHttp = new ActiveXObject(versions[i]);
				}
				catch(e) {} //ignore the error
			}
		}
		
		if(!xmlHttp)
		{
			alert("Error creating the XMLHttpRequest Object.");
		}
		else
		{
			return xmlHttp;
		}
}


function rate(place_id, rating)
{
	var serverParams = "place_id="+place_id+"&rating="+rating;
	//var serverParams = "rating="+rating;

	//only continue if xmlHttp isn't void
	if(!xmlHttp) return;
	
	//don't try to make server requests if the XMLHtppRequest object is busy
	if (xmlHttp.readyState != 0 && xmlHttp.readyState != 4)
	{
			alert("Can't connect to the server, please try later.");
	}
	else
	{
		//try to connect
		try
		{
			//get the two values enteres by the user
			xmlHttp.open("GET", getlist+'?'+serverParams, true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		catch(e)
		{
			alert("Cannot connect to the server:\n" + e.toString());
		}
	}

}


//function called when the state of the HTTP request changes
function handleRequestStateChange()
{
	if(xmlHttp.readyState == 4)
	{
		//contine only if HTTP status is 200
		if(xmlHttp.status == 200)
		{
			try
			{
				//do something with the response
				handleServerResponse();
			}
			catch(e)
			{
				//display error
				alert("Error reading the response: " + e.toString())
			}
		}
		else
		{
			//display status message
			alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
		}
	}
}



//function called to handle the response
function handleServerResponse()
{
	//read the message from the server
	var response = xmlHttp.responseText;

	//error check
	
	//obtain a reference to the div on the page

    myDiv = document.getElementById("rating");    
	myDiv.innerHTML = response;

}
