/*Starts created by Prem S.G on 02 jun 2009*/

function NewXMLHttpRequest()
{
	var xmlReq = false;
	// Create XMLHttpRequest object in non-Microsoft browsers
	if (window.XMLHttpRequest)
	{
		xmlReq = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		
		try
		{
			// Try to create XMLHttpRequest in later versions
			// of Internet Explorer
			xmlReq = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e1)
		{
			// Failed to create required ActiveXObject
			try
			{
				// Try version supported by older versions
				// of Internet Explorer
				xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e2)
			{
				// Unable to create an XMLHttpRequest by any means
				xmlReq = false;
			}
		}
	}

return xmlReq;
}

/*
* Returns a function that waits for the specified XMLHttpRequest
* to complete, then passes it XML response to the given handler function.
* req - The XMLHttpRequest whose state is changing
* responseXmlHandler - Function to pass the XML response to
*/
function GetReadyStateHandler(req, responseXmlHandler)
{
	// Return an anonymous function that listens to the XMLHttpRequest instance
	return function ()
	{
		// If the request's status is "complete"
		if (req.readyState == 4)
		{
			// Check that we received a successful response from the server
			if (req.status == 200)
			{
				// Pass the XML payload of the response to the handler function.
				responseXmlHandler(req.responseText ? req.responseText : req.responseXML);
			}
			else
			{
				// An HTTP problem has occurred
				alert("HTTP error "+req.status+": "+req.statusText);
			}
		}
	}
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function changeRating(id,vote)
{
	url = document.getElementById('site_root').value+"php/bookRating.php?bid=" + id + "&vote=" + vote;
	//alert(url);
	var req = NewXMLHttpRequest();
	req.onreadystatechange = GetReadyStateHandler(req, displayRating);
	req.open("GET", url);
	req.send(null);
}

function displayRating(_var)
{
	
	_var = _var.split('::');
	_var[0] = trim(_var[0]);
	var divName = "rating_"+_var[0];
	if(_var[2] == 'alert')
	{
		alert('You already rated this book');
		
		document.getElementById('RatingDiv'+_var[0]).innerHTML ='';
	}else{
	document.getElementById(divName).innerHTML =_var[1];
	document.getElementById('RatingDiv'+_var[0]).innerHTML ='Successfully Rated.';
	}
}

function hide()
{
	document.getElementById("alertmessage").innerHTML = "";
}

