/* Form validation.
 * For each element in the form, if it has the attribute required and it's set
 * to true, if it's empty or it's null, add the error class to mark it.
 * Otherwise, remove it, just in case it had it from the previous check.
 * All elements that should be checked depending on whether a checkbox or a
 * radio button is checked or not, should be having their attribute 'required'
 * set to true by now (through observers on events).
 */
function validate_step_one(f)
{
	// Blank values validation
	var BOOK_MIN_HOURS_IN_ADVANCE = 48;
	var error = false;
	Form.getElements(f).each(
		function(element)
		{
			// The try/catch is here to prevent $F() from failing when there are no options in a select
			try
			{
				if (element.getAttribute('required') == "true" && ($F(element).blank() || $F(element) == '-1'))
				{
					$(element).addClassName('error');
					if (!error) element.focus(); // To set the focus on the first error
					error = true;
				}
				else $(element).removeClassName('error');
			}
			catch (e) {}
		}
	);
	if (error)
	{
		window.alert(MARKED_VALUES_CANNOT_BE_BLANK);
		return false;
	}

	// Dates validation
	if (!isDateField($F('start_date')))
	{
		window.alert(START_DATE_IS_NOT_VALID);
		return false;
	}
	if (!isDateField($F('end_date')))
	{
		window.alert(END_DATE_IS_NOT_VALID);
		return false;
	}

	// Check whether the pick-up date is not in the past.
	// Also check that we will have time enough for the reservation to be processed.
	var d = Date.parseExact($F('start_date'), "yyyy-MM-dd");
	var now = new Date();
	d.setHours(d.getHours() - BOOK_MIN_HOURS_IN_ADVANCE);
	error = (d.getTime() > now.getTime()) ? false : true;
	if (error)
	{
		window.alert(ERROR_BOOK_IN_ADVANCE);
		return false;
	}

	return true;
}

/*
 * Custom made date validation field
 */
function isDateField (txt)
{
	try
	{
		var d = Date.parseExact(txt, "yyyy-MM-dd");
		return true;
	}
	catch(e)
	{
		return false;
	}
}

function validate_step_two(f)
{
	// At least one of the tariffs must have value greater than zero
	// Error is true by default. When we find a tariff with a value,
	// we will set it to false. Finally we'll return the value.
	var error = true;
	Form.getElements(f).each(
		function(element)
		{
			// The try/catch is here to prevent $F() from failing when there are no options in a select
			try
			{
				if (element.name.startsWith('ser_id_') && $F(element) != '0')
				{
					error = false;
				}
			}
			catch (e) {}
		}
	);
	if (error)
	{
		window.alert(AT_LEAST_ONE_TARIFF_MUST_BE_CHOSEN);
		return false;
	}

	return true;
}

function validate_step_three(f)
{
	// Blank values validation
	var error = false;
	Form.getElements(f).each(
		function(element)
		{
			// The try/catch is here to prevent $F() from failing when there are no options in a select
			try
			{
				if (element.getAttribute('required') == "true" && ($F(element).blank() || $F(element) == '-1'))
				{
					$(element).addClassName('error');
					if (!error) element.focus(); // To set the focus on the first error
					error = true;
				}
				else $(element).removeClassName('error');
			}
			catch (e) {}
		}
	);
	if (error)
	{
		window.alert(MARKED_VALUES_CANNOT_BE_BLANK);
		return false;
	}

	return true;
}

function validate_contact(f)
{
	// Blank values validation
	var error = false;
	Form.getElements(f).each(
		function(element)
		{
			// The try/catch is here to prevent $F() from failing when there are no options in a select
			try
			{
				if (element.getAttribute('required') == "true" && ($F(element).blank() || $F(element) == '-1'))
				{
					$(element).addClassName('error');
					if (!error) element.focus(); // To set the focus on the first error
					error = true;
				}
				else $(element).removeClassName('error');
			}
			catch (e) {}
		}
	);
	if (error)
	{
		window.alert(MARKED_VALUES_CANNOT_BE_BLANK);
		return false;
	}

	return true;
}
