var manageCourseData={

	load: function() //this method loads data into dropdownlist for services and also adds onchange event handler
	{
		
		//find the date input and configure
		var datesValue=$("#main .trainingCourseDate input").before("<select id='dateList'><option value='0'>Please select</option></select>")[0];
		var datesList=$("#dateList")[0];
		datesList.onchange=function(){
				manageCourseData.datechange(this);
			};
		//find the select list for names
		var servicesValue=$("#main .trainingCourse input").before("<select id='courseList'><option value='0'>Please select</option></select>")[0];
		var servicesList=$("#courseList")[0];
		
		servicesList.length=1;
		//loop through data object
		
		for (var service in data)
		{
			//create option element in #item_name with value=service and text=service.category_text
			servicesList.options[servicesList.options.length]= new Option(data[service]['category_text'],data[service]['category_text']);
		}
		
		//add onchange event handler to #item_name
		
		
		
		
		servicesList.onchange=function(){
			manageCourseData.servicechange();
		};
		servicesList.selectedIndex=1;
		servicesList.onchange();
		 
	},
	
	servicechange: function()
	{
		var servicesValue=$("#main .trainingCourse input")[0];
		var servicesList=$("#courseList")[0];
		var servicesKey=servicesList.options[servicesList.selectedIndex].value.replace(/[^\w\d]/g,"_");
		
		//find key
		var servicesProperties=data[servicesKey];
		
		//setup price and dates fields
		var servicePrice=$("#main .trainingCoursePrice input")[0];
		servicePrice.readOnly=true;
		var datesValue=$("#main .trainingCourseDate input")[0];
		var datesList=$("#dateList")[0];
			
		//deal with null entry
		if (!servicesProperties)
		{
			servicePrice.value="0";
			datesValue.length=0;
			datesList.onchange();
			return;
		}
		
		
		//now put price property in price field
		
		
		servicePrice.value=servicesProperties.price;
		
		//load date options into date selectllist using same method as above
		 
		//delete existing options
		datesList.options.length=0;
		var i;
		for (i=0; i<servicesProperties.date.length; i++)
		{
			datesList.options[datesList.options.length]= new Option(servicesProperties['date'][i],servicesProperties['date'][i]);
		}
		
		//update input field
		servicesValue.value=servicesList.options[servicesList.selectedIndex].text;
		//update dates
		datesList.onchange();

	},
	
	datechange: function(dateList)
	{
		var datesValue=$("#main .trainingCourseDate input")[0];
		datesValue.value=dateList.options[dateList.selectedIndex].text;
	}

};
$(document).ready(function(){
manageCourseData.load();
});