window.addEvent ('load', function() {

	var ajaxScripts =  "http://www.musicinstituteofchicago.org/scripts/ajax_scripts.php";

	//Assign the onChange handler to each classchoice select form item
	var list = $$('#seasonlist .classChoice');	
	list.each(function(element) {
		
		//perform the classcheck function on Change	
		element.addEvent('change', function() { 

			//determine course id by parsing the id of the element			   		
			var elName = element.getProperty('id');
			var nameArr = elName.split("_");
			var course_id = nameArr[1]; 


			//set interface response areas based on course_id
			statusArea = "status_"+course_id;
			submitBtn = "sub_"+course_id; 	

			//determine the classnum by getting the value of the element
			var classnum = element.get('value');
			
			//create an object for the ajax querystring
			var qData = {
				type: "classcheck",
				cid: classnum
				};
 
			// convert the object into a querystring
 			//var qData = Object.toQueryString(qData);
			var qData = 'type=classcheck&cid='+classnum;
			
			//send the ajax query and perform status checks			
			var ajaxRequest = new Request({	
				
				url:ajaxScripts,	
				data: qData, 
				method: 'get', 
				evalScripts: true,
				onFailure: function(xhr) { 
					alert('Sorry, there was an error processing your request.');
					//console.log('XHR Data=',xhr); 
					},				
				onSuccess: function(response) {
					//create a JS object of the found record
					var qResult = createRecordObject(response);
	
					//check for location and status
					//properties are: record object, field, comparison value, allow enrollment boolean, feedback 				
					enrollmentCheck (
						qResult,
						"status", 
						"capacity",
						"false", 
						"This class has reached its enrollment capacity. Please select a different day and time."
						);

					enrollmentCheck (
						qResult,
						"status", 
						"cancelled", 
						"false",
						"This class has been cancelled. Please select a different class or call 847.905.1500 for more info"
						);

					enrollmentCheck (
						qResult,
						"status", 
						"audition", 
						"true",
						"This class requires an audition. Please call 847.905.1500 to set one up after enrollment."
						);

					enrollmentCheck (
						qResult,
						"location", 
						"Winnetka Commuity House",
						"false", 
						"Please call the Winnetka Community House directly to register for this class: 847.446.0537"
						);													
				}					
				
			}).send();
		
		});
			
	});

});

function enrollmentCheck (chosenClass, property, value, allowEnrollment, feedback) {
	if (chosenClass[property] === value) {
		if (allowEnrollment=='false') {	
			$(submitBtn).addClass('hide'); 
		} else {
			$(submitBtn).removeClass('hide');
		}
		$(statusArea).set('text',feedback);		
		$(statusArea).addClass('alert');
	} else {
		if (chosenClass[property] == 'normal') {
			$(submitBtn).removeClass('hide');
			$(statusArea).empty();						
		} else {
			//check for a property of custom, and if true
			//see if the class still allows registration
			//and show or hide the add to cart button appropriately
			//then show the custom message stored in the table record
			if (chosenClass[property] == 'custom') {
				if (chosenClass.custom_allowEnrollment=='false') {	
					$(submitBtn).addClass('hide'); 
				} else {
					$(submitBtn).removeClass('hide');
				}
				$(statusArea).set('text',chosenClass.custom_message);		
				$(statusArea).addClass('alert');
			}
		}
	}
}

function createRecordObject(response) {
	//Returns a JS object of the found record with
	//table fields as properties of the object
	//using = and | as delimeters
	var qResult = new Object;
	var records = response.split('|');
	for (var i = 0;i<records.length;i++) {
				
		var properties = records[i].split('=');		
		var key = properties[0];
		var val = properties[1];
		
		qResult[key] = val;
	}
	return qResult;
}