//////////////////////////////////////////////////////
//
//	Be very careful editing this file,
//	save a backup version to your computer 
// 	to use in the event of mistakes
// 	when editing.
//
//////////////////////////////////////////////////////
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

$(document).ready(function() {
	
	function Course(courseTypeId,cityId,startDate){
		this.courseTypeId  = courseTypeId;
		this.cityId = cityId;
		this.startDate = startDate;
	}
	
	var courseType = new Array('AlignmentIntensive','BreakthroughIntensive','BuildingExecutive','ExecutiveChallenge','ExecutiveMastery','LeveragingGenius');
	var city = new Array('Boston','Chicago','Houston','London','Philadelphia','SanFrancisco','Washington','Arlington','Springfield');
	var courses = new Array ();
	
	courses.push(new Course(0,7,'September 21, 2009'));
	courses.push(new Course(1,8,'July 14, 2009'));
	courses.push(new Course(1,8,'October 20, 2009'));
	courses.push(new Course(2,4,'March 3, 2009'));
	courses.push(new Course(2,4,'August 4, 2009'));
	courses.push(new Course(3,0,'October 20, 2009'));
	courses.push(new Course(3,1,'October 13, 2009'));
	courses.push(new Course(3,2,'July 22, 2009'));
	courses.push(new Course(3,3,'July 2, 2009'));
	courses.push(new Course(3,4,'October 7, 2009'));
	courses.push(new Course(3,5,'October 27, 2009'));
	courses.push(new Course(3,6,'October 22, 2009'));
	courses.push(new Course(4,0,'July 23, 2009'));
	courses.push(new Course(4,1,'July 16, 2009'));
	courses.push(new Course(4,2,'July 23, 2009'));
	courses.push(new Course(4,3,'To be announced'));
	courses.push(new Course(4,4,'July 15, 2009'));
	courses.push(new Course(4,5,'July 20, 2009'));
	courses.push(new Course(4,6,'July 29, 2009'));
	courses.push(new Course(5,4,'To be announced'));
	
	var courseKey = getQueryKey('course');
		 //parseInt(courseKey);
	$('select#courseSelect').val(courseKey);
	
	// get the index of the courseType
	function getCourseTypeIndex (courseTypeArr,courseKeyStr ){
		var courseTypeIndex = null;
		for (var i =  0; i < courseTypeArr.length;  i++){
			//checking if the course type is equal to the course key
			if (courseKeyStr == courseTypeArr[i]){
				courseTypeIndex = i;
				i = courseTypeArr.length;
			}
		}
		return courseTypeIndex;
	}
	
	// creating a new array with all the courses that match the course type index
	function getCoursesByCourseTypeIndex(coursesArr,courseTypeIndexInt){
		var tempCourses = new Array();
		for ( var i = 0; i < coursesArr.length; i++){
			// looping through t to find matching courseType
			if (courseTypeIndexInt == coursesArr[i].courseTypeId){
				tempCourses.push(coursesArr[i]);
			}
		}
		return tempCourses;
	}
	
	// get the index of the city
	function getCityIndex(cityArr,cityStr){
		var cityIndex = null;
		for ( var i = 0; i < cityArr.length; i++){
			if (cityArr[i] == cityStr){
				cityIndex = i;
				i = cityArr.length;
			}
		}
		return cityIndex;
	}
	
	// build out the city select options with correct cities
	function buildCityOptions(tempCoursesArr){
		citySelect.empty();
		citySelect.append('<option value=" ">Please select a city</option>');
		var noDuplicates = new Array();
		for ( var i = 0; i < tempCoursesArr.length; i++) {
			if ( !noDuplicates.inArray(tempCourses[i].cityId)){
				citySelect.append('<option value="' + city[tempCourses[i].cityId]  + '">' +  city[tempCourses[i].cityId] + '</option>');
				noDuplicates.push(tempCourses[i].cityId);
			}
		}
		
	}
	
	var dateSelect = $('select[name=Date]');
	var citySelect = $('select[name=City]');
	
	var courseTypeIndex = getCourseTypeIndex(courseType,courseKey);
	var tempCourses = getCoursesByCourseTypeIndex(courses,courseTypeIndex);
	buildCityOptions(tempCourses);	
	
	
	$('select[name=City]').change(function(){
		var cityIndex = getCityIndex(city,$(this).val());
		dateSelect.empty();
		dateSelect.append('<option value=" ">Please choose a starting date</option>');
		for( var i = 0; i < tempCourses.length; i++){
			if(cityIndex == tempCourses[i].cityId){
				dateSelect.append('<option value="' + tempCourses[i].startDate  + '">' +  tempCourses[i].startDate + '</option>');
			}
		}
	});
	
	$('select[name=Course]').change(function(){
		courseTypeIndex = getCourseTypeIndex(courseType,$(this).val());
		tempCourses = getCoursesByCourseTypeIndex(courses,courseTypeIndex);
		buildCityOptions(tempCourses);
	});
	
	
	
	
});
