/* script */

function chauffeur_init ()
{
	if (!$('chauffeur_form')) {
		return;
	}

	hideLoader(null);
	hideChauffeurLoader(null);

	/* change destination 'on load' & 'on change' */
	updateDestinationOutput();
	Event.observe('chauffeur_form_destination', 'change', updateDestinationOutput);

	/* recalculate button */
	Event.observe('chauffeur_form_calculate', 'click', calculate);
	if ($('chauffeur_form_submit_delete'))
		calculate();

	/* submit button */
	Event.observe('chauffeur_form_submit', 'click', function (e) {
		calculate();
		if (!canSubmit) {
			Event.stop(e);
			return;
		}

		$$('#chauffeur_form input.text').each(function(item) {
			if (item.title == item.value)
				item.value = '';
		});
		$$('#chauffeur_form textarea').each(function(item) {
			if (item.title == item.value)
				item.value = '';
		});
	});
}
Event.onDOMReady(chauffeur_init);


function showLoader() {
	$('loader').show();
}
function hideLoader(event) {
	$('loader').hide();
}

function showChauffeurLoader() {
	$('cloader').show();
}
function hideChauffeurLoader(event) {
	$('cloader').hide();
}

function updateDestinationOutput ()
{
	showLoader();
	url = '/'+ lang +'/?ajax=getLocation';

	new Ajax.Updater('destination-output', url, {
		method: 'get',
		parameters: { id: $F('chauffeur_form_destination') },
		onSuccess: hideLoader
	});
}

var canSubmit = false;
function calculate ()
{
	showChauffeurLoader();
	url = '/'+ lang +'/?ajax=getLocationCombination';
	var pinCode = 0;
	/*
	if ($('chauffeur_form_pin_code').title != $F('chauffeur_form_pin_code'))
		pinCode = 1;
	*/
	new Ajax.Request(url, {
		method: 'get',
		parameters: {
			from_id: $F('chauffeur_form_pick_up'),
			to_id: $F('chauffeur_form_destination'),
			passangers: $F('chauffeur_form_passanegers'),
			pin_code: pinCode
		},
		onSuccess: function (ret) {
			$('recalculate-failed').hide();
			$('recalculate-success').show();
			$('recalculate-success').innerHTML = ret.responseText;
			canSubmit = true;
			hideChauffeurLoader();
		},
		onFailure: function (ret) {
			$('recalculate-success').hide();
			$('recalculate-failed').show();
			$('recalculate-failed').innerHTML = ret.responseText;
			canSubmit = false;
			hideChauffeurLoader();
		}
	});
}
