var RatingControl = Class.create();


RatingControl.prototype = {
 
	initialize : function() {
	
		var errorMessage    = "RatingControl.prototype.initialize";

		try 
	    {

            RatingControl.prototype.xmlUrl             = "/xmlweb/Rating/get.aspx";
            RatingControl.prototype.postUrl            = "/xmlweb/Rating/put.aspx";
            RatingControl.prototype.uniqueID           = 0;
            RatingControl.prototype.ratingSubmted      = false;
             
        }
    	catch(e) 
        {
            RatingControl.prototype.error(errorMessage,e);
        }  				
	},
	loadData : function(request){
	    
	    var errorMessage    = "RatingControl.prototype.load";
	    
	    try 
	    {
		   
		   if((document.getElementById("RATING_COUNT") != null) && (document.getElementById("RATING_STARS") != null) )
		   {  
            
                var  ratingStar    = parseInt(request.responseXML.documentElement.getElementsByTagName('rating')[0].getAttribute('ratingStar'));
                var  ratingCount   = parseInt(request.responseXML.documentElement.getElementsByTagName('rating')[0].getAttribute('ratingCount'));
            
                $("RATING_COUNT").update("(" + ratingCount + ")");
                
                switch(ratingStar)		
		         {
		            case 1:
		                    $("RATING_STARS").addClassName("onestar");
		                break;
		            case 2:
		                    $("RATING_STARS").addClassName("twostar");
		                break;
		            case 3:
		                    $("RATING_STARS").addClassName("threestar");
		                break;
		            case 4:
		                    $("RATING_STARS").addClassName("fourstar");
		                break;
		            case 5:
		                    $("RATING_STARS").addClassName("fivestar");
		                break;
		         }  
		     }
        }
        catch(e) 
        {
            RatingControl.prototype.error(errorMessage,e);
        } 
	},
	put : function(value){
	    
	    var errorMessage    = "RatingControl.prototype.put";
	    
	    try 
	    {
	        function ajResponse()
	        {
	            RatingControl.prototype.ratingSubmted  = true;
	            RatingControl.prototype.get(RatingControl.prototype.uniqueID);
	        }
	        
	         function ajaxError()
	        {
	        }
	        
	        if(RatingControl.prototype.ratingSubmted  == false)
	        {
	        
                var url = RatingControl.prototype.postUrl + "?unid=" + RatingControl.prototype.uniqueID + "&s=" + value;
                var myAjax  = new Ajax.Request(url,{method:'get',onSuccess:ajResponse,onFailure:ajaxError,asynchronous:false});
            
            }
            else
            {
                alert("Rating has already been submitted.");
            }
           
        }
        catch(e) 
        {
            RatingControl.prototype.error(errorMessage,e);
        } 
	},
	get : function(uniqueID){
	
	    var errorMessage    = "RatingControl.prototype.get";
	    
        	    
        //Home			    = 1
        //Tests			    = 2
        //Psychometrics		= 3
        //How We Can Help	= 4
        //Asessment Centres	= 5	
        //Econmic  Impact	= 6
        //Consultation		= 7
        //CQI		        = 8

	    try 
	    {   
	        switch(uniqueID)
	        {
	            case "HOME_PAGE":
	                RatingControl.prototype.uniqueID = 9;
	                break;
                case "ASSESSMENTCENTRES_PAGE":
                    RatingControl.prototype.uniqueID = 5;
                    break;
                case "ECONOMICIMPACT_PAGE":
                    RatingControl.prototype.uniqueID = 6;
                    break;
                case "CONSULTATION_PAGE":
                    RatingControl.prototype.uniqueID = 7;
                    break;
                case "PSYCHOMETRICS_PAGE":
                    RatingControl.prototype.uniqueID = 3;
                    break;
                case "HOWWECANHELP_PAGE":
                    RatingControl.prototype.uniqueID = 4;
                    break;
                case "CQI_PAGE":
                    RatingControl.prototype.uniqueID = 4;
                    break;
                case "RESULT_PAGE":
                    RatingControl.prototype.uniqueID = 9;
                    break;
            }

	        function ajaxError()
	        {
	        }
	        
	        
	         url = RatingControl.prototype.xmlUrl + "?unid=" + RatingControl.prototype.uniqueID;

             var myAjax  = new Ajax.Request(url,{method:'get',onSuccess:RatingControl.prototype.loadData,onFailure:ajaxError,asynchronous:false});
            
            
	    } 
	    catch(e) 
	    {
	        RatingControl.prototype.error(errorMessage,e);
	    } 
	
	},
	error : function(ue,e){
	    alert(ue + "\n\n" + "-- " + e + " --");
	}
}