/*
//select box names
course
location
class
*/

var select_course = 'course';
var select_location = 'location';
var select_class = 'courseclass';

var optionTest = false; //if false, means can't do dynamic options changes


//function to check if an element is in the array
function in_array(needle, haystack){
	for (var i in haystack){
		if(needle == haystack[i]){
			return true;
		}
	}
	return false;
}

//check for array keys
function array_key_exists(needle, haystack){
	if(typeof(haystack[needle]) != "undefined"){	
		return true;
	}
	return false;
}

function init()
{
	//removes browser doesn't support this script message and fires popCourse function
	optionTest = true;
	lgth = document.getElementById(select_course).options.length - 1;
	document.getElementById(select_course).options[lgth] = null;
	if(document.getElementById(select_course).options[lgth]){
		optionTest = false;
	}
	
	popCourse(1);
	popLocation(1);
	popLevel();
}


//Fill Course select box with courses
function popCourse(){
	var argv = popCourse.arguments;
	
	var box_course = document.getElementById(select_course);
	var box_location = document.getElementById(select_location);
	
	var selected_course = box_course.options.selectedIndex;
	var selected_location = box_location.options.selectedIndex;
	
	if(selected_course != -1){
		selected_course = box_course.options[selected_course].value;
	}
	if(selected_location != -1){
		selected_location = box_location.options[selected_location].value;
	}
	
	box_course.options.length = 0;
	
	var x = 0;
	
	if(argv[0]){
		//defined so first time getting here
		box_course.options[x] = new Option("Please choose a course -->", "ER");
		x++;
	}else{
		box_course.options[x] = new Option("All Topics", "ER");
		x++;
		box_location.options[0].text = "All Locations";
	}

	var location_code = box_location.value.substring(0,2);
	var location_id = box_location.value.replace(location_code,"");
	
	//if no location chosen, list all. 
	//if location chosen then need to filter
	if(location_code == "LG"){
		//location general
		if(array_key_exists(location_id, arr_locationCourses)){
			var matrix = arr_locationCourses[location_id];
		}
	}else if(location_code == "LT"){
		//location town
		if(array_key_exists(location_id, arr_locationTown)){
			var matrix = arr_locationTown[location_id];
		}
	}else{
		//show all
		
	}
	
	
	if(location_code == "LG" || location_code == "LT"){
		//location general
		if(typeof(matrix) != "undefined"){
			for (var i in arr_courseType){
			    if(array_key_exists(i, matrix)){
			    	box_course.options[x] = new Option("All " + arr_courseType[i] + " courses", "CG" + i);
			    	
					if(selected_course != -1 && ("CG" + i) == selected_course){
						selected_course = x;
					}
					x++;
			    }
			}
		}
	}else{
		//show all
		for (var i in arr_courseType){
		    box_course.options[x] = new Option("All " + arr_courseType[i] + " courses", "CG" + i);
		    if(selected_course != -1 && ("CG" + i) == selected_course){
				selected_course = x;
			}
		    x++;
		}
	}	
	
	box_course.options[x] = new Option("", -1);
	x++;
	
	//course matrix per town
	if(location_code == "LG"){
		//course general category
		if(array_key_exists(location_id, arr_locationCourses)){
			var matrix = arr_locationCourses[location_id]; //category id
		}
	}else if(location_code == "LT"){
		//course title
		if(array_key_exists(location_id, arr_locationTown)){
			var matrix = arr_locationTown[location_id];
		}
	}
	
	if(location_code == "LG" || location_code == "LT"){
		//location general
		if(typeof(matrix) != "undefined"){
			for (var i in arr_title){
				if(in_array(i, matrix)){
			    	box_course.options[x] = new Option(arr_title[i], "CT" + i);
			    	if(selected_course != -1 && ("CT" + i) == selected_course){
						selected_course = x;
					}
			    	x++;
			    }
			}
		}
	}else{
		//show all
		for (var i in arr_title){
		    box_course.options[x] = new Option(arr_title[i], "CT" + i);
		    if(selected_course != -1 && ("CT" + i) == selected_course){
				selected_course = x;
			}
		    x++;
		}
	}
	
	if(selected_course == -1){
		selected_course = 0;
	}
	box_course.options.selectedIndex = selected_course;
	popLevel();
}


function popLocation(){
	//remove first option if ER and replace with ALL Topics etc
	var argv = popLocation.arguments;
	
	var box_course = document.getElementById(select_course);
	var box_location = document.getElementById(select_location);
	
	var selected_course = box_course.options.selectedIndex;
	var selected_location = box_location.options.selectedIndex;
	
	if(selected_course != -1){
		selected_course = box_course.options[selected_course].value;
	}
	if(selected_location != -1){
		selected_location = box_location.options[selected_location].value;
	}
	
	box_location.options.length = 0;

	var course_code = box_course.value.substring(0,2);
	var course_id = box_course.value.replace(course_code,"");
	
	var x = 0;
	
	if(argv[0]){
		//defined so first time getting here
		box_location.options[x] = new Option("Please choose a location -->", "ER");
		x++;
	}else{
		box_location.options[x] = new Option("All Locations", "ER");
		x++;
		box_course.options[0].text = "All Topics";
	}
	
	if(course_code == "CG"){
		//course general category
		if(array_key_exists(course_id, arr_courseTypeLocation)){
			var matrix = arr_courseTypeLocation[course_id];
		}
	}else if(course_code == "CT"){
		//course title
		if(array_key_exists(course_id, arr_locationMatrix)){
			var matrix = arr_locationMatrix[course_id];
		}
	}else{
		//show all
	}
	
	if(course_code == "CG" || course_code == "CT"){
		//general course matrix
		if(typeof(matrix) != "undefined"){
			for (var i in arr_location){
				//check against arr_locationMatrix
				if(in_array(i, matrix)){
					box_location.options[x] = new Option("All " + arr_location[i] + " region venues", "LG" + i);
					if(selected_location != -1 && ("LT" + i) == selected_location){
						selected_location = x;
					}
					x++;
				}
			}
		}
	}else{
		for (var i in arr_location){
			//check against arr_locationMatrix
			box_location.options[x] = new Option("All " + arr_location[i] + " region venues", "LG" + i);
			if(selected_location != -1 && ("LT" + i) == selected_location){
				selected_location = x;
			}
			x++;
		}
	}
	
	box_location.options[x] = new Option("", -1);
	x++;
	
	//town matrix per course
	if(course_code == "CG"){
		//course general category
		if(array_key_exists(course_id, arr_courseTypeTown)){
			var matrix = arr_courseTypeTown[course_id]; //category id
		}
	}else if(course_code == "CT"){
		//course title
		if(array_key_exists(course_id, arr_townMatrix)){
			var matrix = arr_townMatrix[course_id];
		}
	}
	
	if(course_code == "CG" || course_code == "CT"){
		//general course matrix
		if(typeof(matrix) != "undefined"){
			for (var i in arr_town){
			    if(in_array(i, matrix)){
					box_location.options[x] = new Option(arr_town[i], "LT" + i);
					if(selected_location != -1 && ("LT" + i) == selected_location){
						selected_location = x;
					}
				    x++;
			    }
			}
		}
	}else{
		for (var i in arr_town){
			//check against arr_locationMatrix
			box_location.options[x] = new Option(arr_town[i], "LT" + i);
			if(selected_location != -1 && ("LT" + i) == selected_location){
				selected_location = x;
			}
			x++;
		}
	}
	
	if(selected_location == -1){
		selected_location = 0;
	}
	box_location.options.selectedIndex = selected_location;
	popLevel();
}


function popLevel(){
	//there needs to be some check on the courses to see what levels can be shown
	var box_class = document.getElementById(select_class);
	var box_course = document.getElementById(select_course);
	var box_location = document.getElementById(select_location);

	var selected_course = box_course.options.selectedIndex;
	var selected_location = box_location.options.selectedIndex;
	
	var course_code = box_course.value.substring(0,2);
	var course_id = box_course.value.replace(course_code,"");
	
	var location_code = box_location.value.substring(0,2);
	var location_id = box_location.value.replace(location_code,"");
	
	box_class.options.length = 0;
	var x = 0;
	for (var i in arr_class){
		show = false;
		if( (course_code == "CT" || course_code == "CG") || (location_code == "LT" || location_code == "LG")){
			if(course_code == "CT"){
				if(in_array(course_id, arr_courseLevelDesc[i])){
					show = true;
					if(location_code == "LT"){
						if(!in_array(location_id, arr_courseLevelTown[i])){
							show = false;
						}
					}
					if(location_code == "LG"){
						if(!in_array(location_id, arr_courseLevelLocation[i])){
							show = false;
						}
					}
				}
			}
			if(course_code == "CG"){
				if(in_array(course_id, arr_courseLevelType[i])){
					show = true;
					if(location_code == "LT"){
						if(!in_array(location_id, arr_courseLevelTown[i])){
							show = false;
						}
					}
					if(location_code == "LG"){
						if(!in_array(location_id, arr_courseLevelLocation[i])){
							show = false;
						}
					}
				}
			}
			if(location_code == "LT"){
				if(in_array(location_id, arr_courseLevelTown[i])){
					show = true;
					
					if(course_code == "CG"){
						if(!in_array(course_id, arr_courseLevelType[i])){
							show = false;	
						}
					}
					if(course_code == "CT"){
						if(!in_array(course_id, arr_courseLevelDesc[i])){
							show = false;
						}
					}
				}
				
			}
			if(location_code == "LG"){
				if(in_array(location_id, arr_courseLevelLocation[i])){
					show = true;
					if(course_code == "CG"){
						if(!in_array(course_id, arr_courseLevelType[i])){
							show = false;	
						}
					}
					if(course_code == "CT"){
						if(!in_array(course_id, arr_courseLevelDesc[i])){
							show = false;
						}
					}
				}
			}
		}else{
			//show anyway
			show = true;
		}
		
		if(show == true){
			box_class.options[x] = new Option(arr_class[i], i);
		    x++;	
		}
	}
}