var registration = {
	
	currentSection : false,
	
	sections : false,
	
	DATA_ID : false,
	
	initSections : function (section_container) 
	{	
		var container = $(section_container);
		
		
		this.sections = container.getElementsByClassName('section');
		
		if (!$('progress_bar')) return;
		
		this.progress = $('progress_bar').getElementsByClassName('progress_item');
		
		
		$A(this.sections).each(function(section, i)
		{	
			/* hide all sections */
			section.hide();
			
			/* show first section on load */
			if (i == 0) 
			{
				/* show */
				section.show();
				this.progress[i].setStyle({'fontWeight' : 'bold'});
				
				/* set current section */
				this.currentSection = i;
			}
					
		}.bind(this));
	},
	
	nextSection : function (exec_function, params) 
	{	
		/* exec any function given with the nextSection */
		if (exec_function) 
		{	
			eval("registration."+exec_function+"("+params+");");
		}
		
		var nextSection = this.currentSection + 1;
		
		if (this.sections[nextSection]) 
		{	
			if (formValidation.check_fields('quickie_form')) {
			
				$A(this.progress).each(function(progress_item, i) {
					progress_item.setStyle({'fontWeight' : 'normal'});
				});
			
				/* hide current section */
				this.sections[this.currentSection].hide();
				
				/* show next section */
				this.sections[nextSection].show();
			
				this.progress[nextSection].setStyle({'fontWeight' : 'bold'});
			
				if ($('abfile')) {
					formupload.go();
				}
				
			} else {
			
				return false;
			
			}
			
			/* set current section */
			this.currentSection = nextSection;
			
			/* create summary if needed */
			if (this.sections[nextSection].id == 'summary')
			{
				registration.createSummary(registration.DATA_ID);
			}
			
			/* get hotel dates if needed */
			if (this.sections[nextSection].id == 'hotels')
			{
				registration.get_hotel_dates(registration.DATA_ID);
			}
			
			/* get hotel dates if needed */
			if (this.sections[nextSection].id == 'tours')
			{
				registration.get_tours(registration.DATA_ID);
			}
			
		}
	},
	
	previousSection : function (exec_function, params) 
	{	
		$A(this.progress).each(function(progress_item, i) {
			progress_item.setStyle({'fontWeight' : 'normal'});
		});
	
		/* exec any function given with the nextSection */
		if (exec_function) 
		{	
			eval("registration."+exec_function+"();");
		}
		
		var previousSection = this.currentSection - 1;
		
		if (this.sections[previousSection]) 
		{	
			/* hide current section */
			this.sections[this.currentSection].hide();
			
			/* show previous section */
			this.sections[previousSection].show();
			
			/* set current section */
			this.currentSection = previousSection;
			
			this.progress[previousSection].setStyle({'fontWeight' : 'bold'});
		}
	},
	
	get_hotel_dates : function (data) {
		
		new Ajax.Request('/registrations/get_hotel_dates/data='+data+'/', {
		
			onComplete : function(transport) {
				
				var dates = transport.responseText.evalJSON();
				
				/* min dates */
				dates.min.each(function(date_min){
				
					var newOption = document.createElement('option');
					newOption.value = date_min.value;
					newOption.innerHTML = date_min.label;
					
					$('hotel_list_min').appendChild(newOption);
				});
				
				/* max dates */
				dates.max.each(function(date_max){
				
					var newOption = document.createElement('option');
					newOption.value = date_max.value;
					newOption.innerHTML = date_max.label;
					
					$('hotel_list_max').appendChild(newOption);
				});
			}
		});
	},
	
	get_hotels : function (data, min, max) {
		
		new Ajax.Updater('hotel_listing', '/registrations/get_hotels/data='+data+'/min='+min+'/max='+max, { evalScripts : true });
	},
	
	get_tours : function (data) {
		
		new Ajax.Updater('tour_listing', '/registrations/get_tours/data='+data, { evalScripts : true });
	},
	
	createSummary : function (data) {
		
		new Ajax.Updater('summary_listing', '/registrations/create_summary/data='+data, { evalScripts : true, postBody : Form.serialize('quickie_form') });
		
	},
	
	complete : function (data) {
		
		if ($('terms').checked == false) {
			alert('Please agree with the terms and conditions');
		} else {	
			new Ajax.Updater('summary_listing', '/registrations/complete_registration/data='+data, { evalScripts : true, postBody : Form.serialize('quickie_form') });
		}
		
	},
	
	lodging : function (value) {
		if (value.empty()) {
			$('hotel_1').show();
			$('hotel_2').show();
			$('hotel_3').show();
		} else {
			$('hotel_1').hide();
			$('hotel_2').hide();
			$('hotel_3').hide();
		}
	}
};