  function checkRated(){
	
	    		var ri = $('.rateit');
//	    		var ri = $(this);
	    	    //var status = true;
	    	    var ratetype = ri.rateit('type');
	    	    var ratetypeid = ri.rateit('typeid');     
	    	    var rateflag = ri.rateit('flag');
	    	    //if the use pressed reset, it will get value: 0 (to be compatible with the HTML range control), we could check if e.type == 'reset', and then set the value to  null .
	    	    var ratevalue = ri.rateit('value');
	    	   // alert("hi "+ratevalue);	    	    
	    	    var userid = $(".rateit").attr("data-rateit-id");
                    var userid = ri.rateit('id');
                   // alert(userid);
//	    	    if(uid != 0){
//	    	    	userid = uid;
//	    	    }
//	    	    else{
//	    	    	var userid = ri.rateit('id');
//	    	    }
//	    	    alert(userid);
$.blockUI({ message: '' }); 
	    	    $.ajax({
	    	        url: MAIN_URL+'index/ratecheck', //your server side script
	    	        async : false,
	    	        data: { id: userid, value: ratevalue, type: ratetype, typeid : ratetypeid }, //our data
	    	        type: 'POST',
	    	        success: function (data) { 
	    	            //alert(data);
	    	           // $('#response').append('<li>' + data + '</li>');
	    	            $('.rateit').rateit('flag', data);
	    	           // $('#starring .rateit').css('cursor','default');	             
	    	        }
	    	   });	
                   $("#joinMessage").hide();
                    $("#jmessage").show();
                  // alert(ri.rateit('flag'));
	    	   if(ri.rateit('flag') == 1 && ri.rateit('id') >= 0){
	    	    	var ratetype1;
	    	    	if(ratetype == 'B'){
	    	    		ratetype1 = "page";
	    	    	}
	    	    	else if(ratetype == 'C'){
	    	    		ratetype1 = "group";
	    	    	}
	    	    	$('#jmessage').html('You have already rated the '+ ratetype1);
    	            $('#jmessage').addClass('error_div');
    	            $('#jmessage').removeClass('success_div');
//	    			alert('You have already rated the '+ ratetype1);
 setTimeout(function() {
        $("#jmessage").hide;
    	},10000);
	    			$.colorbox.close();
	    		}
                       // alert(ri.rateit('flag'));
                               if(ri.rateit('flag') == 2){
                            
                            $('#jmessage').html("You can't rate your own group");
    	            $('#jmessage').addClass('error_div');
    	            $('#jmessage').removeClass('success_div');
                     $(window).scrollTop(0);
	    	            //alert("Your rating has been saved.");
	    	            $.colorbox.close();
                        }
                        else if(ri.rateit('flag') == 3){
                            
                            $('#jmessage').html("You can't rate your own page");
    	            $('#jmessage').addClass('error_div');
    	            $('#jmessage').removeClass('success_div');
                     $(window).scrollTop(0);
	    	            //alert("Your rating has been saved.");
	    	            $.colorbox.close();
                        }
                        else if(ri.rateit('flag') == 4){
                             $('#jmessage').addClass('error_div');
                             $('#jmessage').html("You have already rated");
    	           
    	            $('#jmessage').removeClass('success_div');
                     $(window).scrollTop(0);
                        }
                        else if(ri.rateit('flag') == 5){
                             $('#jmessage').html("You are not a member of this group");
    	            $('#jmessage').addClass('error_div');
    	            $('#jmessage').removeClass('success_div');
                     $(window).scrollTop(0);
                        }
	    	   else //if(ri.rateit('flag') != 1 && ri.rateit('id') > 0)
	    	   {
	    		   saveRate();
	    	   }
	    	   
}
  
function saveRate(){
	
	var ri = $('.rateit');
//	var ri = $(this);
	    	   // var status = true;
	    	    var ratetype = ri.rateit('type');
	    	    var ratetypeid = ri.rateit('typeid');     
	    	    var rateflag = ri.rateit('flag');
	    	    //if the use pressed reset, it will get value: 0 (to be compatible with the HTML range control), we could check if e.type == 'reset', and then set the value to  null .
	    	    var ratevalue = ri.rateit('value');
	    	   // alert("hi "+ratevalue);
	    	   // var userid = $(".rateit").attr("data-rateit-id");
	    	    var userid = ri.rateit('id'); 
	    //	    ri.rateit('readonly', true);
	    	    $.blockUI({ message: '' }); 
	    	    $.ajax({
	    	        url: MAIN_URL+'index/rateit', //your server side script
	    	        async : false,
	    	        data: { id: userid, value: ratevalue, type: ratetype, typeid : ratetypeid }, //our data
	    	        type: 'POST',
	    	        success: function (data) { 
	    	           // alert(data);
	    	           // $('#response').append('<li>' + data + '</li>');
	    	           // $('.rateit').rateit('value', data);
	    	           // $('#starring .rateit').css('cursor','default');
	    	            $('#jmessage').html("Your rating has been saved.");
	    	            $('#jmessage').addClass('success_div');
	    	            $('#jmessage').removeClass('error_div');
	    	            //alert("Your rating has been saved.");
	    	            $.colorbox.close();
//                             setTimeout(function() {
//        $("#jmessage").hide;
//    	},10000);
	    	        }
	    	    });
}

$(document).ready(function() {
		//alert("hi ");
		//$('#starring .rateit').bind(' reset', function (e) {
	        var ri = $('#starring .rateit');

	        //if the use pressed reset, it will get value: 0 (to be compatible with the HTML range control), we could check if e.type == 'reset', and then set the value to  null .
	        var ratevalue = RATEVALUE;
	//alert(RATEVALUE);
	        var userid = RATE_USERID; // if the product id was in some hidden field: ri.closest('li').find('input[name="productid"]').val()
	        var ratetype = RATETYPE;
	        var ratetypeid = RATETYPE_ID; 
	        var rateflag = RATEFLAG;
	        $('#starring .rateit').css('cursor','pointer');
	        //maybe we want to disable voting?
	       // if(rateflag == 1){
	       if((rateflag == 1 || rateflag == 2) && userid > 0 ){
	        	$('#starring .rateit').rateit('readonly', true);
                        $('#starring .rateit').css('cursor', 'default');
	        }
	        
	        $('#starring .rateit').rateit('value', ratevalue);
	        $('#starring .rateit').rateit('type', ratetype);
	        $('#starring .rateit').rateit('id', userid);
	        $('#starring .rateit').rateit('typeid', ratetypeid);
	        $('#starring .rateit').rateit('flag', rateflag);
	        
	    //});
	});

	$('#starring .rateit').live("click",function (e) {
	//	alert("aaa");
	 var ri = $(this);
	var ratevalue = $(this).rateit('value');
	var userid = $(this).rateit('id'); // if the product id was in some hidden field: ri.closest('li').find('input[name="productid"]').val()
	var ratetype = $(this).rateit('type');
        var ratetypeid = ri.rateit('typeid'); 
	var ratetype1;
        var status = true;
	var rateflag = $(this).rateit('flag');
       // alert(userid);
	if(ratetype == 'B'){
		ratetype1 = "page";
	}
	else if(ratetype == 'C'){
		ratetype1 = "group";
	}
	if(rateflag == 1 && userid <= 0){
	//	alert('You need to login to rate the '+ ratetype1);
		
	}
	else if(rateflag == 1 && userid >= 0){
		$('#jmessage').html('You have already rated the '+ ratetype1);
        $('#jmessage').addClass('error_div');
        $('#jmessage').removeClass('success_div');
        return;
//		alert('You have already rated the '+ ratetype1);
	}
        else if(rateflag == 2 && userid >= 0){
            $('#jmessage').html("You can't rate your own "+ ratetype1);
            $('#jmessage').addClass('error_div');
            $('#jmessage').removeClass('success_div'); 
            return;
        }
         else if(rateflag == 3 && userid >= 0){
            $('#jmessage').html('You should be a member to rate this '+ ratetype1);
            $('#jmessage').addClass('error_div');
            $('#jmessage').removeClass('success_div'); 
            return;
        }
         if(rateflag == 1 && userid <= 0){ //alert("2");
		    	status = checkLogin1(ratetype,ratetypeid,'rate');	    	
		     }
                    // alert(status);
                    // status = checkLogin1(ratetype,ratetypeid,'rate');
		 if(status != false){
			// alert("here");
		    	checkRated();	    
		 }
		 
//         setTimeout(function() {
//        $("#jmessage").hide;
//    	},10000);
	//alert(ratetype);
	//alert(rateflag);
	}); 
	$('#starring .rateit').bind('rated', function (e) {
	     var ri = $(this);
	     var status = true;
	     var ratetype = ri.rateit('type');
	     var ratetypeid = ri.rateit('typeid');     
	     var rateflag = ri.rateit('flag');
	     //if the use pressed reset, it will get value: 0 (to be compatible with the HTML range control), we could check if e.type == 'reset', and then set the value to  null .
	     var ratevalue = ri.rateit('value');
	    // alert("hi "+ratevalue);
	     var userid = ri.rateit('id'); // if the product id was in some hidden field: ri.closest('li').find('input[name="productid"]').val()
	     if(rateflag == 1 && userid <= 0){ //alert("2");
		    	status = checkLogin1(ratetype,ratetypeid,'rate');	    	
		     }	 
		 if(status != false){
			// alert("here");
		    	checkRated();	    
		 }
		 
	 });