var constants           = new ConstantControl();


var ScoreControl = Class.create();


ScoreControl.prototype = {
 
	initialize : function(accessCode) {
	
		var errorMessage    = "ScoreControl.prototype.initialize: unable to load xml";
		
		try 
	    {
	        ScoreControl.prototype.AccessCode               = accessCode;
	        ScoreControl.prototype.TestTypeId               = "";
	        ScoreControl.prototype.RC                       = "";
	        ScoreControl.prototype.xmlRequest		        = null;
	        ScoreControl.prototype.xmlItemData		        = null;
	        ScoreControl.prototype.xmlItemTypeOutcomeData   = null;
	        ScoreControl.prototype.xmlTestOutcomeData       = null;
	        ScoreControl.prototype.cacheControl             = Math.uuid(17);
	   	    ScoreControl.prototype.xmlUrl                   = constants.SCORE_SERVICE_URL;
	   	    ScoreControl.prototype.reportPath               = constants.REPORT_PATH;

    	}
    	catch(e) 
        {
            ScoreControl.prototype.error(errorMessage,e);
        }  				
	},
	loadData : function(request){
        var errorMessage    = "ScoreControl.prototype.loadData:";
        try 
	    {
	    
	    
	
            ScoreControl.prototype.xmlRequest 		        = 	removeTextNode(request.responseXML.documentElement);
	        ScoreControl.prototype.xmlItemData		        =	ScoreControl.prototype.xmlRequest.getElementsByTagName("Items");
	         
	         
	        ScoreControl.prototype.TestTypeId               =   ScoreControl.prototype.xmlRequest.getAttribute("testTypeId");
	        ScoreControl.prototype.Locale                   =   ScoreControl.prototype.xmlRequest.getAttribute("locale");
	        ScoreControl.prototype.RC                       =   ScoreControl.prototype.xmlRequest.getAttribute("rc");

	        
	        ScoreControl.prototype.xmlTestOutcomeData       =	ScoreControl.prototype.xmlRequest.getElementsByTagName("TestOutcomes");
	        ScoreControl.prototype.xmlItemTypeOutcomeData   =	ScoreControl.prototype.xmlRequest.getElementsByTagName("ItemTypeOutcomes");
	        ScoreControl.prototype.xmlChartData             =	ScoreControl.prototype.xmlRequest.getElementsByTagName("Charts");
	        
	        ScoreControl.prototype.reportString             =   "";
	        
	        ScoreControl.prototype.loadReportTemplate()
	        ScoreControl.prototype.getTestData();
	        ScoreControl.prototype.getItemTypeData();
	        ScoreControl.prototype.getChartData();
	        ScoreControl.prototype.getTestOutcomeData();
	        
	        ScoreControl.prototype.renderData();
	    }
	    catch(e) 
        {
            ScoreControl.prototype.error(errorMessage,e);
        } 
	},
	
	getTestOutcomeData : function(){
	
		var errorMessage    = "ScoreControl.prototype.getTestOutcomeData";
	    
	    try 
	    {
            var testOutcomes			            = ScoreControl.prototype.xmlTestOutcomeData[0].childNodes;
            
            var testOutcomeArrayDescription         = [];
            var testOutcomeArrayValue               = [];
            var testOutcomeArrayBinding             = [];
            
            for(var i=0;i<testOutcomes.length;i++){
            
                testOutcomeArrayDescription[i]      = testOutcomes[i].getAttribute("Description");
                testOutcomeArrayValue[i]            = testOutcomes[i].getAttribute("Value");  
                testOutcomeArrayBinding[i]          = testOutcomes[i].getAttribute("Binding"); 
                ScoreControl.prototype.reportString = ScoreControl.prototype.reportString.gsub(testOutcomeArrayBinding[i],testOutcomeArrayValue[i]);
            }
          
        }
        catch(e) 
        {
            ScoreControl.prototype.error(errorMessage,e);
        }
	},
	
	getItemTypeData : function(){
	
		var errorMessage    = "ScoreControl.prototype.getTestOutcomeData";
	    
	    try 
	    {
            var itemTypes			            = ScoreControl.prototype.xmlItemTypeOutcomeData[0].childNodes;
           
            
            
            for(var i=0;i<itemTypes.length;i++){
            
                var description             = itemTypes[i].getAttribute("Description");
                var count                   = itemTypes[i].getAttribute("Count");
                var correct                 = itemTypes[i].getAttribute("Correct");
                var attempted               = itemTypes[i].getAttribute("Attempted");
                var performance             = itemTypes[i].getAttribute("Performance");
                var grade                   = itemTypes[i].getAttribute("Grade");
                
                var elCorrect       = "#ITEM_TYPE_" + description.toUpperCase() + "_CORRECT#";
                var elAttempted     = "#ITEM_TYPE_" + description.toUpperCase() + "_ATTEMPTED#";
                var elPerformance   = "#ITEM_TYPE_" + description.toUpperCase() + "_PERFORMANCE#";
                var elGrade         = "#ITEM_TYPE_" + description.toUpperCase() + "_GRADE#";
                
                
                ScoreControl.prototype.reportString = ScoreControl.prototype.reportString.gsub(elCorrect,correct);
                ScoreControl.prototype.reportString = ScoreControl.prototype.reportString.gsub(elAttempted,attempted);
                ScoreControl.prototype.reportString = ScoreControl.prototype.reportString.gsub(elPerformance,performance);
                ScoreControl.prototype.reportString = ScoreControl.prototype.reportString.gsub(elGrade,grade); 
                //ScoreControl.prototype.reportString = ScoreControl.prototype.reportString.gsub(testOutcomeArrayBinding[i],testOutcomeArrayValue[i]);
            }
          
        }
        catch(e) 
        {
            ScoreControl.prototype.error(errorMessage,e);
        }
	},
	
	getTestData : function(){
	
	    ScoreControl.prototype.reportString = ScoreControl.prototype.reportString.gsub('#TIME_TAKEN#',ScoreControl.prototype.xmlRequest.getAttribute("timeRemaining"));
	    ScoreControl.prototype.reportString = ScoreControl.prototype.reportString.gsub('#TEST_NAME#',ScoreControl.prototype.xmlRequest.getAttribute("testDescription"));
	    ScoreControl.prototype.reportString = ScoreControl.prototype.reportString.gsub('#DATE_TAKEN#',ScoreControl.prototype.xmlRequest.getAttribute("startDate"));
	    ScoreControl.prototype.reportString = ScoreControl.prototype.reportString.gsub('#TEST_SESSION_ID#',ScoreControl.prototype.xmlRequest.getAttribute("testSessionId"));
	    
	},
	getChartData : function(){
	    ScoreControl.prototype.reportString = ScoreControl.prototype.reportString.gsub('#CHART_1#',ScoreControl.prototype.xmlChartData[0].childNodes[0].firstChild.nodeValue);
	    ScoreControl.prototype.reportString = ScoreControl.prototype.reportString.gsub('#CHART_2#',ScoreControl.prototype.xmlChartData[0].childNodes[1].firstChild.nodeValue); 
	},
	renderData : function(){
	    $("REPORT_PANEL").update(ScoreControl.prototype.reportString);
	},
	loadScore : function(id,sn){

        var errorMessage    = "ScoreControl.prototype.loadScore";
        try 
        {
        
            if(parseInt(id) > 0)
            {
                url         = ScoreControl.prototype.xmlUrl + "?ts=" + id + "&cc=" + ScoreControl.prototype.cacheControl + "&sn=" + sn;
                var myAjax  = new Ajax.Request(url,{method:'get',asynchronous:false,onSuccess:ScoreControl.prototype.loadData,onFailure:ScoreControl.prototype.error});
            }  
        } 
        catch(e) 
        {
            ScoreControl.prototype.error(errorMessage,e);
        }
    },
    loadReportTemplate: function(){
	
	    var url                 = "";
	    var myAjax              = null;
	    
	    function returnRepeaterData(request)
	    {
            ScoreControl.prototype.reportString=request.responseText.toString();
        }
        
      
        url     = ScoreControl.prototype.reportPath + ScoreControl.prototype.Locale + "/" + ScoreControl.prototype.TestTypeId + "/";

        
        switch(parseInt(ScoreControl.prototype.TestTypeId))
        {
             case 1:
                url = url + "Numerical_Report_V2.htm";
                break;
             case 2:
                url = url + "Verbal_Report_V2.htm";
                break;
             case 3:
                url = url + "Logical_Report_V2.htm";
                break;
        }
        
        url = url + "?cc=" + ScoreControl.prototype.cacheControl;
        
        myAjax  = new Ajax.Request(url,{method:'get',onSuccess:returnRepeaterData,asynchronous:false});
	
	},
	error : function(ue,e){
	    alert(ue + "\n\n" + "-- " + e + " --");
	}
}