﻿function displayClassesDropMenu(){
    // Check to see if the "CategorySelector" element exists.
    if(document.getElementById("CategorySelector")){
        // Get the "CategorySelector" element.
        var categorySelector = document.getElementById("CategorySelector");
        // Send a request to get selected calendar month.
	    // Create XmlHttpObject
	    var xmlHttp = createHttpRequestObj();
	    // Check to see if the "xmlHttp" object was created
	    if (xmlHttp == null){
		    // The "xmlHttp" object was NOT created
		    alert ("Browser does not support HTTP Request");
		    return;
	    }
	    // Build URL and append the relevenat querystring variables
	    var url="xml/classesXML.xml";
	    url=url+"?sid="+Math.random();
	    xmlHttp.onreadystatechange=function() {
		    if(xmlHttp.readyState==4){
			    // Get the XML Document object.
			    var xmlDoc = xmlHttp.responseXML;
			    // Get all of the categories.
			    var categories = xmlDoc.getElementsByTagName("category");
			    // Clear the contents of the "CategorySelector" element.
                categorySelector.innerHTML = "";
			    // Check to see if there area any elements in the "categories" array.
			    if(categories.length > 0){
			        var categoryOpts = new Array();
			        categoryOpts[0] = ["", "- Select a category -"];
			        // Loop through each element in the "categories" array.
			        for(i=0; i<categories.length; i++){
			            // Get all of the details for the current catagory.
			            var catClass = categories[i].getAttribute("class");
			            var catTitle = getNodeValue(categories[i].getElementsByTagName("title")[0]);
			            categoryOpts[i+1] = [i, catTitle];
			            //var catImgSrc = categories[i].getElementsByTagName("image")[0].getAttribute("src");
			            //var catImgCaption = getNodeValue(categories[i].getElementsByTagName("image")[0]);
			            //var catPopStart = categories[i].getElementsByTagName("population")[0].getAttribute("start");
			            //var catPopEnd = categories[i].getElementsByTagName("population")[0].getAttribute("end");
			            //var catJudgeTime_hours = categories[i].getElementsByTagName("judgingTime")[0].getAttribute("hours");
			            //var catJudgeTime_mins = categories[i].getElementsByTagName("judgingTime")[0].getAttribute("mins");
			        }
			        // End of For Loop.
			        // Create a new select menu and give it an instance name of "classSelect".
                    var classSelect = new SelectMenu(categoryOpts, "category");
                    // Add the XML Document object to the select menu as a property.
                    classSelect.classesXML = xmlDoc;
                    // Assign the "showClassInfo()" function to the onchange event handler.
                    classSelect.onchange = showClassInfo;
                    // Append text to the "categorySelector" element.
                    categorySelector.appendChild(document.createTextNode("Select a category: "));
                    // Append the "classSelect" object to the "categorySelector" element.
                    categorySelector.appendChild(classSelect);
			    }
			    else{
			        // Indicate that there are no categories to be displayed.
                    categorySelector.innerHTML = "No categories found";
			    }
			    // End of IF Else statement.
		    }
		    else{
		        // Indicate that the category select menu is being loaded.
    			categorySelector.innerHTML = "Loading categories..."
		    }
		    // End of IF Else statement
	    }
	    // End of "xmlHttp.onreadystatechange" function
	    // Open a GET request to the appropriate URL in asynchronus mode.
	    xmlHttp.open("GET", url, true);
	    // Send the request.
	    xmlHttp.send(null);
	}
	// End of IF statement.
}
// End of function "displayClassesDropMenu()".

function showClassInfo(){
     // Check to see if the "ClassInfo" element exists.
    if(document.getElementById("ClassInfo")){
        // Get the "ClassInfo" element.
        var classInfoDiv = document.getElementById("ClassInfo");
        // Clear the "classInfoDiv".
        classInfoDiv.innerHTML = "";
        var xml = this.classesXML;
        var selectedNodeNo = this.value;
        if(!isNaN(selectedNodeNo) && selectedNodeNo !== ""){
            selectedNodeNo = selectedNodeNo * 1;
            // Get all of the categories.
		    var categories = xml.getElementsByTagName("category");
            var catClass = categories[selectedNodeNo].getAttribute("class");
            var catTitle = getNodeValue(categories[selectedNodeNo].getElementsByTagName("title")[0]);
            var catImgSrc = categories[selectedNodeNo].getElementsByTagName("image")[0].getAttribute("src");
            var catImgCaption = getNodeValue(categories[selectedNodeNo].getElementsByTagName("image")[0]);
            var catPopStart = categories[selectedNodeNo].getElementsByTagName("population")[0].getAttribute("from");
            var catPopEnd = categories[selectedNodeNo].getElementsByTagName("population")[0].getAttribute("to");
            var population;
            if(catPopStart == "" || catPopStart == "0") population = "Up to "+catPopEnd;
            else if(catPopEnd == "" || catPopEnd == "0") population = catPopStart+" and over";
            else population = catPopStart+" to "+catPopEnd;
            var catJudgeTime_hours = categories[selectedNodeNo].getElementsByTagName("judgingTime")[0].getAttribute("hours");
            var catJudgeTime_mins = categories[selectedNodeNo].getElementsByTagName("judgingTime")[0].getAttribute("mins");
		    var judgingTime, judgingTime_hour, judgingTime_min;
		    if(!isNaN(catJudgeTime_hours) && catJudgeTime_hours !== ""){
		        catJudgeTime_hours = catJudgeTime_hours * 1;
		        if(catJudgeTime_hours > 0){
		            judgingTime_hour = catJudgeTime_hours +" hour"
		            if(catJudgeTime_hours > 1){
		                judgingTime_hour += "s"
		            }
		        } 
		    }
		    if(!isNaN(catJudgeTime_mins) && catJudgeTime_mins !== ""){
		        catJudgeTime_mins = catJudgeTime_mins * 1;
		        if(catJudgeTime_mins > 0){
		            judgingTime_min = catJudgeTime_mins +" minute"
		            if(catJudgeTime_mins > 1){
		                judgingTime_min += "s"
		            }
		        } 
		    }

		    judgingTime = judgingTime_hour;
		    if(judgingTime_min) judgingTime += " "+judgingTime_min;

		    // Output the category information.
		    var categoryInfo = "";
			categoryInfo += "<div class=\"downloads\" style=\"width: 90%; border: none;\">\n";
			categoryInfo += "<h3 class=\"bg\">Category Information: "+catTitle+"</h3>\n";
			categoryInfo += "<img src=\""+catImgSrc+"\" alt=\""+catTitle+"\" class=\"CategoryImage\" />";
		    categoryInfo += "<p><strong>Class</strong>"+catClass+"</p>\n";
		    //categoryInfo += "<p><strong>Category</strong>"+catTitle+"</p>\n";
		    categoryInfo += "<p><strong>Population</strong>"+population+"</p>\n";
		    categoryInfo += "<p><strong>Judging time</strong>"+judgingTime+"</p>\n";
		    categoryInfo += "<p><img src=\"graphics/info.gif\" alt=\"Information\" />&nbsp;&nbsp;&nbsp;All entrants will also have a further 15 minutes to be used for press and photo purposes only.</p>";
		    categoryInfo += "</div>\n";
			classInfoDiv.innerHTML = categoryInfo;
        }
        // End of IF statement.
    }
    // End of IF statement.
}
// End of function "showClassInfo".