// As simple as it can get - Ajax

function rating(vars) {
	
	document.getElementById('timer').style.visibility = "visible";
	document.getElementById('timer').src = "images/timer.gif";
	
	//var url='http://bartending.com/rating.php';
	var url = "rating.php";

    var request=(window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');

    if(!request){return};
 request.open("POST", url, true);
  request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.send(vars); 



request.onreadystatechange = function() {
	
    if (request.readyState == 4 && request.status == 200) {
      if (request.responseText) {
	  updateRating(request.responseText);
      }
    }
  };




}


function sendRating(rate_num){

/*
var verify = false;

//alert(document.cookie);

	if(document.cookie.split(";").length>2){
		cookie_key = document.cookie.split(";");
		
		for(var i=0; i<cookie_key.length; i++)
		{
			if(cookie_key[i]){
			cookie_value=cookie_key[i].split("=");
			if(cookie_value[0] == " Bartending_id") var user_id=cookie_value[1];
			if(cookie_value[0] == " Bartending_verify") var verify=cookie_value[1];
			
		}
		}
	}
	
	if(!verify) {
		alert("Please Login to cast a vote");
		return;
	}
*/
var ratings = false;
var user_id = false;
//var radio = document.forms['rating_form'].rating;
var category = document.forms['rating_form'].category.value;
var cat_id = document.forms['rating_form'].cat_id.value;
var user_id = document.forms['rating_form'].user_id.value;

if(!user_id)
{
	//alert('openSearchPopup');
	openSearchPopup('subscribe');
	return;
	
}
/*
	for(i=0; i<radio.length; i++)
	{
		if(radio[i].checked) ratings = radio[i].value;
	}
*/
ratings = document.forms['rating_form'].rating;
//if(ratings && cookie_key) rating('ratings='+ratings+"&category="+category+"&cat_id="+cat_id+"&user_id="+user_id+"&verify="+verify);
	if(ratings) rating('ratings='+ratings+"&category="+category+"&cat_id="+cat_id+"&user_id="+user_id);
	
}
function updateRating(resp)
{


 var values = resp.split(",");

		if(values[0] == 0) 
		{
		document.getElementById('timer').style.visibility = "hidden";
		document.getElementById('rating status').innerHTML = "Not Rated";
		}
		else
		{
		document.getElementById('timer').src = "images/tick.png";	
		document.getElementById('rating status').innerHTML = parseFloat(values[1]).toFixed(2) +" Currently<br>"+values[0]+" Votes";
		 
		//var stars = Math.round(values[1]*21);
		
		//document.getElementById('stars').style.width = stars+"px";
		document.getElementById('rate').disabled = true;
		
		document.getElementById('star_graphic').src = "stars.php?rating="+values[1];
	
		
		}
values = "";


}

function submitComment(){



/*document.forms['add_comment'].submit();
return;

	if(document.cookie.split(";").length>2){
		cookie_key = document.cookie.split(";");
		
		for(var i=0; i<cookie_key.length; i++)
		{
			if(cookie_key[i]){
			cookie_value=cookie_key[i].split("=");
			if(cookie_value[0] == " Bartending_id") document.getElementById('user_id').value=cookie_value[1];
			if(cookie_value[0] == " Bartending_verify") {
				document.getElementById('verify').value=cookie_value[1];
				var verify = cookie_value[1];
			}
			
		}
		}
	}
*/	
var user_id = false;
if(document.forms['rate5'].user_id)
user_id = document.forms['rate5'].user_id.value;


if(!user_id)
{
	openSearchPopup('subscribe');
	return;
	

}


	
	if(document.getElementById('comment').value == '')
	{
	alert("Please enter a comment.");
	return;
	}
	
	else
	
	{
	document.forms['add_comment'].submit();
	}
	
	

}
