$(document).ready(function() {
    var $pickup = $('#ride_pickup');
    var $destination = $('#ride_destination');

    // AJAX Pickup Address checker
    $pickup.blur(function() {
        checkLocation($(this), 'Pickup', '#step1_pickup_msg');
    });
    $pickup.focus(function() {
        resetField($(this), 'Pickup');
    });

    //AJAX Destination Address checker
    $destination.blur(function() {
        checkLocation($(this), 'Destination', '#step1_destination_msg');
    });
    $destination.focus(function() {
        resetField($(this), 'Destination');
    });

    //Validation
    $('#ride_submit').click(function() {
        var allOk = true;
        var pickup = $('#ride_pickup').val();
        if (pickup == 'Pickup' || pickup == '') {
            allOk = false;
            $('#step1_pickup_msg').html(messages.location.invalid_address).effect('highlight', {}, 2000);
        }
        var destination = $('#ride_destination').val();
        if (destination == 'Destination' || destination == '') {
            allOk = false;
            $('#step1_destination_msg').html(messages.location.invalid_address).effect('highlight', {}, 2000);
        }

        var date = $('#ride_date').val();
        if (date == 'Date' || date == '') {
            allOk = false;
            $('#step1_date_msg').html(messages.ride.pickup_date_invalid).effect('highlight', {}, 2000);
        } else {
            $('#step1_date_msg').html('');
        }

        var passengers = $('#step1_passengers').val();
        if (passengers == 'Passengers' || passengers == '0' || passengers == '') {
            $('#step1_passengers_msg').html(messages.ride.passengers_required).effect('highlight', {}, 2000);
        } else {
            $('#step1_passengers_msg').html('');
        }
        if (allOk) {
            $('#processing h3').text(messages.tip);
            $('#processing').show();
        }
        return allOk;
    });
});

function resetField(el, title) {
    if (el.val() == title) {
        el.val('');
    }
}
