  function checkRated1(rateField){
      //alert("here");exit;
	
	    		var ri = $('#'+rateField);

	    	    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 = $('#'+rateField).attr("data-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 , flag : rateflag}, //our data
	    	        type: 'POST',
	    	        success: function (data) { 
	    	            //alert(data);	    	           
	    	        	$('#'+rateField).rateit('flag', data);
	    	           // $('#starring .rateit').css('cursor','default');	             
	    	        }
	    	   });
                   //alert(ri.rateit('flag'));
                   //alert(ri.rateit('id'));exit;
                  //  alert(ri.rateit('flag'));
	    	   if(ri.rateit('flag') == 1 && ri.rateit('id') >= 0){
	    	    	var ratetype1;
	    	    	if(ratetype == 'B'){
	    	    		ratetype1 = "business";
	    	    	}
	    	    	else if(ratetype == 'C'){
	    	    		ratetype1 = "community";
	    	    	}
	    	    $('#jmessage').html('You have already rated the '+ ratetype1);
    	            $('#jmessage').addClass('error_div');
    	            $('#jmessage').removeClass('success_div');
    	            $(window).scrollTop(0);
//	    			alert('You have already rated the '+ ratetype1);
	    			$.colorbox.close();
	    		}
                       else 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').html("You have already rated");
    	            $('#jmessage').addClass('error_div');
    	            $('#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)
	    	   {
	    		   saveRate1(rateField);
	    	   }
	    	   
}
  
function saveRate1(rateField){
	
	var ri = $('#'+rateField);
	    	    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 = $('#'+rateField).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 , flag : rateflag}, //our data
	    	        type: 'POST',
	    	        success: function (data) { 
	    	           // alert(data);
	    	          //$('.rateit').rateit('value', data);
	    	        	//$('#'+rateField).rateit('value', data);
	    	           // $('#starring .rateit').css('cursor','default');
	    	            $('#jmessage').html("Your rating has been saved.");
	    	            $('#jmessage').addClass('success_div');
	    	            $('#jmessage').removeClass('error_div');
	    	            $(window).scrollTop(0);
	    	            //alert("Your rating has been saved.");
	    	            $.colorbox.close();
	    	        }
	    	    });
}

$(document).ready(function() {
   $('#starring .rateit').css('cursor','pointer'); 
	});

	$('#starring .rateit').live("click",function (e) {
            //alert("rate live");
          var status = true;
	 var ri = $(this);
         var rateField = $(this).attr("id");
	var ratevalue = $(this).rateit('value');
        var ratetypeid = ri.rateit('typeid');
	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 ratetype1;
	var rateflag = $(this).rateit('flag');
        
       // alert(rateflag);
	if(ratetype == 'B'){
		ratetype1 = "page";
	}
	else if(ratetype == 'C'){
		ratetype1 = "business";
	}
	if(rateflag == 1 && userid <= 0){
	//	alert('You need to login to rate the '+ ratetype1);
        status = checkLogin2(ratetype,ratetypeid,'rate',rateField);	
		
	}
//	else if(rateflag == 1 && userid >= 0){
//		$('#jmessage').html('You have already rated the '+ ratetype1);
//        $('#jmessage').addClass('error_div');
//        $('#jmessage').removeClass('success_div');
//        $(window).scrollTop(0);
////		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'); 
            $(window).scrollTop(0);
            return;
        }
        if(status != false){
			// alert("here");
		    	checkRated1(rateField);	    
		 }
	//alert(ratetype);
	//alert(rateflag);
	}); 

//	 $('#starring .rateit').bind('rated', function (e) {
//	     var ri = $(this);
//	     var rateField = $(this).attr("id");
//	     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 = checkLogin2(ratetype,ratetypeid,'rate',rateField);	    	
//		     }	 
//		 if(status != false){
//			// alert("here");
//		    	checkRated1(rateField);	    
//		 }
//		 
//	 });
