var formcheck;
var selected_form_number;

window.addEvent('load', function() {
	try
	{
		new DatePicker('.date_toggled', {
			pickerClass: 'datepicker_vista',
			allowEmpty: true,
			days: ['Zondag', 'Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag'],
			months: ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'October', 'November', 'December'],
			timePicker: false,
			format: 'd-m-Y',
			inputOutputFormat: 'd-m-Y'
		});
		
		formcheck = new FormCheck('wondermand_optie_form', {});
		availability_form();
	}
	catch(err) {}
});

function set_selected_form_number(form_number)
{
	selected_form_number = form_number;
	alter_form();
}


function alter_form(){
	if(selected_form_number == 1){
		$('form_id').value				= 1;
		availability_form();
	}
	else if (selected_form_number == 2) {
		$('form_id').value				= 2;
		option_form();
	}
	else if (selected_form_number == 3) {
		$('form_id').value				= 3;
		reservation_form();
	}
}

//function that determines what fields we show 
//if we only want to show the form for people to inform the availability of the act
function availability_form()
{
	toggle_event_info('none');
	toggle_contact_availability('');
	toggle_contact_option('none')
	toggle_contact_reservation('none');
	toggle_location_option('');
	toggle_location_reservation('none');
	unregister_all_fields();
	register_availability_fields();
}

//function that determines what fields we show 
//if we only want to show the form for people who want to place an option on an act
function option_form()
{
	toggle_event_info('none');
	toggle_contact_availability('');
	toggle_contact_option('');
	toggle_contact_reservation('none');
	toggle_location_option('');
	toggle_location_reservation('none');
	unregister_all_fields();
	register_option_fields();
}

//function that determines what fields we show 
//if people want to immediatly book the act
function reservation_form()
{
	toggle_event_info('');
	toggle_contact_availability('');
	toggle_contact_option('');
	toggle_contact_reservation('');
	toggle_location_option('');
	toggle_location_reservation('');
	unregister_all_fields();
	register_reservation_fields();
}

function toggle_event_info(style)
{
	$$('.event_info').each( function (el) {
		el.style.display = style;
	});
}

function toggle_contact_availability(style)
{
	$$('.contact_availability').each( function (el) {
		el.style.display = style;
	});
}

function toggle_contact_option(style)
{
	$$('.contact_option').each( function (el) {
		el.style.display = style;
	});
}

function toggle_contact_reservation(style)
{
	$$('.contact_reservation').each( function (el) {
		el.style.display = style;
	});
	
	new DatePicker('.start_time_toggled', {
		pickerClass: 'datepicker_vista',
		allowEmpty: true,
		timePickerOnly: true,
		format: 'H:i',
		inputOutputFormat: 'H:i'
	});
	
	new DatePicker('.end_time_toggled', {
		pickerClass: 'datepicker_vista',
		allowEmpty: true,
		timePickerOnly: true,
		format: 'H:i',
		inputOutputFormat: 'H:i'
	});
	
	new DatePicker('.event_prepare_done_toggle', {
		pickerClass: 'datepicker_vista',
		allowEmpty: true,
		timePickerOnly: true,
		format: 'H:i'
	});
	
	new DatePicker('.event_close_up_toggle', {
		pickerClass: 'datepicker_vista',
		allowEmpty: true,
		timePickerOnly: true,
		format: 'H:i'
	});
}

function toggle_location_option(style)
{
	formcheck.dispose($('location_name'));
	formcheck.dispose($('location_address'));
	formcheck.dispose($('location_zipcode'));
	formcheck.dispose($('location_name'));
	formcheck.dispose($('location_notify_address'));
	formcheck.dispose($('location_contact_name'));
	formcheck.dispose($('location_contact_telephone'));	
	
	$$('.location_option').each( function (el) {
		el.style.display = style;
	});
}

function toggle_location_reservation(style)
{
	formcheck.register($('location_name'), 9);
	formcheck.register($('location_address'), 10);
	formcheck.register($('location_zipcode'), 11);
	formcheck.register($('location_city'), 12);
	formcheck.register($('location_name'), 13);
	formcheck.register($('location_notify_address'), 14);
	formcheck.register($('location_contact_name'), 15);
	formcheck.register($('location_contact_telephone'), 16);
		
	$$('.location_reservation').each( function (el) {
		el.style.display = style;
	});
}

function update_prepare_done_field()
{
	$('event_prepare_done_text').value = $('event_prepare_done_dropdown').getSelected()[0].value;
	$('event_prepare_done_text').focus();
	
	
}

function update_event_close_up_field()
{
	$('event_close_up_text').value = $('event_close_up_dropdown').getSelected()[0].value;
	$('event_close_up_text').focus();
}

function unregister_all_fields()
{
	formcheck.dispose($('wensdatum'));
	formcheck.dispose($('event_start_time'));
	formcheck.dispose($('event_end_time'));
	formcheck.dispose($('event_occasion'));
	formcheck.dispose($('event_prepare_done_text'));
	formcheck.dispose($('event_close_up_text'));
	formcheck.dispose($('location_build_information'));
	formcheck.dispose($('location_height'));

	formcheck.dispose($('company_name'));
	formcheck.dispose($('aanhef'));
	formcheck.dispose($('contact_initials'));
	formcheck.dispose($('contact_name'));
	formcheck.dispose($('contact_address'));
	formcheck.dispose($('contact_zipcode'));
	formcheck.dispose($('contact_city'));
	formcheck.dispose($('contact_telephone'));
	formcheck.dispose($('contact_email'));

	formcheck.dispose($('location_name'));
	formcheck.dispose($('location_address'));
	formcheck.dispose($('location_zipcode'));
	formcheck.dispose($('location_city'));
	formcheck.dispose($('location_telephone'));
	formcheck.dispose($('location_notify_address'));
	formcheck.dispose($('location_contact_name'));
	formcheck.dispose($('location_contact_telephone'));
}

function register_availability_fields()
{
	formcheck.register($('wensdatum'), 1);	
	formcheck.register($('company_name'), 2);	
	formcheck.register($('aanhef'), 3);	
	formcheck.register($('contact_initials'), 4);	
	formcheck.register($('contact_name'), 5);	
	formcheck.register($('contact_email'), 6);	
	formcheck.register($('location_city'), 7);	
}

function register_option_fields()
{
	formcheck.register($('wensdatum'), 1);	
	formcheck.register($('company_name'), 2);	
	formcheck.register($('aanhef'), 3);	
	formcheck.register($('contact_initials'), 4);	
	formcheck.register($('contact_name'), 5);	
	formcheck.register($('contact_telephone'), 6);	
	formcheck.register($('contact_email'), 7);	
	formcheck.register($('location_city'), 8);	
}

function register_reservation_fields()
{
	formcheck.register($('wensdatum'), 1);	
	formcheck.register($('event_start_time'), 2);	
	formcheck.register($('event_end_time'), 3);	
	formcheck.register($('event_occasion'), 4);	
	formcheck.register($('event_prepare_done_text'), 5);	
	formcheck.register($('event_close_up_text'), 6);	
	formcheck.register($('location_build_information'), 7);	
	formcheck.register($('location_height'), 8);	
	
	formcheck.register($('company_name'), 9);	
	formcheck.register($('aanhef'), 10);	
	formcheck.register($('contact_initials'), 11);	
	formcheck.register($('contact_name'), 12);	
	formcheck.register($('contact_address'), 13);	
	formcheck.register($('contact_zipcode'), 14);	
	formcheck.register($('contact_city'), 15);	
	formcheck.register($('contact_telephone'), 16);	
	formcheck.register($('contact_email'), 17);	
	
	formcheck.register($('location_name'), 18);	
	formcheck.register($('location_address'), 19);	
	formcheck.register($('location_zipcode'), 20);	
	formcheck.register($('location_city'), 21);	
	formcheck.register($('location_telephone'), 22);	
	formcheck.register($('location_notify_address'), 23);	
	formcheck.register($('location_contact_name'), 24);	
}

function toggle_customer_type(customer)
{
	alter_form();
	if(customer == 'consumer')
	{
		formcheck.dispose($('company_name'));
		$$('.customer_type')[0].style.display = 'none';
		$$('.existing_customer').each(function (el) {
			el.style.display = 'table-row';
		});
	}
	else if(customer == 'business')
	{	
		toggle_contact_availability('');
		toggle_contact_option('');
		toggle_contact_reservation('');
		register_option_fields()
		$$('.existing_customer').each(function (el) {
			el.style.display = 'table-row';
		});		
	}
	else if(customer == 'existing_customer')
	{
		formcheck.dispose($('contact_address'));
		formcheck.dispose($('contact_zipcode'));
		formcheck.dispose($('contact_city'));
		formcheck.dispose($('contact_city'));
		
		formcheck.register($('company_name'));
		$$('.customer_type')[0].style.display = '';
		$$('.existing_customer').each(function (el) {
			el.style.display = 'none'
		});
	}
	
}

function send_option_form()
{
	new Request({
		url: '/wondermand/verzend_optie',
		method: 'post',
		onComplete: function()
		{
			var result = JSON.decode(this.response.text);
			if(result['success'] == 1)
				$('article_body').innerHTML = result['notification'];
			else
				$('article_body').innerHTML = result['notification']+''+$('article_body').innerHTML;
		}
	}).post($('wondermand_optie_form'));
}

function remove_product(product_id)
{
	new Request({
		url: '/wondermand/verwijderen/'+product_id,
		method: 'get',
		onComplete: function()
		{
			$('wondermand_selected_products').innerHTML = this.response.text;
		}
	}).post($('wondermand_optie_form'));
}









