
function isLeapYear(year) {
	return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
}

function validate_email (value) {
	if (value.match("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}\$")) {
		return true;
	}
	return false;
}

function validate_time (value) {
	//match hh:mm
	if (value.match("^([01][0-9]|2[0-3]):[0-5][0-9]\$")) {
		return true;
	}
	return false;
}

function validate_month(value) {
	value = parseInt(value);
	if ((value > 0) && (value < 13)) {
		return true;
	}
	return false;
}

function validate_day(value, month) {
	value = parseInt(value);
	month = parseInt(month);
	var lastDay = 0;
	switch (month) {
		case 2:
			lastDay = 29;
			break;
		case 4:
			lastDay = 30;
			break;
		case 6:
			lastDay= 30;
			break;
		case 9:
			lastDay = 30;
			break;
		case 11:
			lastDay = 30;
			break;
		default:
			lastDay = 31;
	}

	if ((value > 0) && (value <= lastDay)) {
		return true;
	}
	return false;
}

function validate_date(value) {
	var day = parseInt(value.substr(0,2));
	var month = parseInt(value.substr(3,2));
	var year = parseInt(value.substr(6,4));
	if ( (validate_day(day,month)) && (validate_month(month)) ) {
		return true;
	}
	return false;
}

function validate_accredidationForm() {
	var errors = false;
	$('accredidation_form_errors').select('.errortext').each(function (item) {
		item.hide();
	});

	//arrivalDate
	var arrivalDate = $F('arrival_date');
	if (!validate_date(arrivalDate)) {
		errors = true;
		$('error_arrival_date').show();
	}

	//arrivalDate
	/*
	var arrivalDateMonth = $F('arrival_date_month');
	var arrivalDateDay = $F('arrival_date_day');

	if (! validate_day(arrivalDateDay, arrivalDateMonth ) || ! validate_month(arrivalDateMonth)) {
		errors = true;
		$('error_arrival_date').show();
	}
	*/

	//departureDate
	var departureDate = $F('departure_date');
	if (!validate_date(departureDate)) {
		errors = true;
		$('error_departure_date').show();
	}

	//departureDate
	/*
	var departureDateMonth = $F('departure_date_month');
	var departureDateDay = $F('departure_date_day');

	if (! validate_day(departureDateDay, departureDateMonth) || ! validate_month(departureDateMonth)) {
		errors = true;
		$('error_departure_date').show();
	}
	*/

	if ($F('arrival_time')) {
		//arrivalTime
		var arrivalTime = $F('arrival_time');

		if (arrivalTime.length > 0 && ! validate_time(arrivalTime) ) {
			errors = true;
			$('error_arrival_time').show();
		}
	}

	if ($F('departure_time')) {
		//departureTime
		var departureTime = $F('departure_time');

		if (departureTime.length > 0 && ! validate_time(departureTime) ) {
			errors = true;
			$('error_departure_time').show();
		}
	}

	//radioGroup_trade
	var radioGroup_tradeInputs_oneSelected = false;
	$$("#accredidation_form .radio_group_trade input").each( function (item) {
		if ($F(item))
			radioGroup_tradeInputs_oneSelected = true;
	});
	if (! radioGroup_tradeInputs_oneSelected) {
		errors = true;
		$('error_trade').show();
	}

	//address
	//organisation:
	if (!($F('organization_name')
			&& $F('organization_address')
			&& $F('organization_zip')
			&& $F('organization_city')
			&& $F('organization_country')
			&& $F('organization_email')
			&& validate_email($F('organization_email')))) {

		if (! ($F('address')
				&& $F('zip')
				&& $F('city')
				&& $F('country')
				&& $F('email')
				&& validate_email($F('email')))) {
					errors = true;
					$('error_address').show();
		}
	}

	if (($F('organization_email')) && (!validate_email($F('organization_email')))) {
		errors = true;
		$('error_email').show();
	}

	if (($F('email')) && (!validate_email($F('email')))) {
		errors = true;
		$('error_email').show();
	}

	if (errors) {
		$('accredidation_form_errors').show();
		return false;
	}

	$('accredidation_form_errors').hide();
	return true;
}


Event.observe(window, "load", function () {
	//init pressform (display)
	$F('trade_press') ? $('press_form').show() : $('press_form').hide();
	//init display of trade_others_text
	$F('trade_others') ? $('trade_others_text').show() : $('trade_others_text').hide();

	radioGroup_tradeInputs = $$("#accredidation_form .radio_group_trade input");
	if (radioGroup_tradeInputs.first()) {
		radioGroup_tradeInputs.each( function (item) {
			item.observe("click", function (event) {
				element = Event.element(event);

				//handling event for press-forms
				(element.id == 'trade_press') ? $('press_form').show() : $('press_form').hide();

				//handling event for showing "others" textfield
				$F('trade_others') ? $('trade_others_text').show() : $('trade_others_text').hide();
			});
		});
	}

	//init press_other display
	$F('press_other') ? $('press_other_text').show() : $('press_other_text').hide();
	if ($('press_other')) {
		$('press_other').observe("click", function (event) {
			$F('press_other') ? $('press_other_text').show() : $('press_other_text').hide();
		});
	}
});
