$(document).ready(function() {
$('#sendRequest').click(function() {
	the_start_date = $('#dFrom').val();
	the_name = $('#your_name').val();
	the_number = $('#contact_number').val();
	the_email = $('#email_address').val();
	the_comments = $('#comments').val();
	the_trip_id = $('#trip_id').val();
	the_trip_name = $('#trip_name').val();
	
	
	if (the_name.length < 1 || the_number.length < 1 || the_start_date.length < 1) {
	    alert('Please make sure you have filled in all of the required fields.');
	} else if (!checkemail(the_email)) {
	    alert('Please input a valid email address.');	    
	} else {
	    $(this).attr('disabled', 'disabled');
	    alert('Your trip request is being sent...');
	    $.post(
		      '/pages/send_query',
      		{ trip_name: the_trip_name,
      		  trip_id: the_trip_id,
      		  desired_trip_start_date: the_start_date,
      		  name: the_name,
      		  contact_number: the_number,
      		  email_address: the_email,
      		  comments: the_comments
      		},
      		function (data) {
	          $(':input[type="text"]').val('');
      	    $('#sendRequest').attr('disabled', '');
      	    $('#sendRequest').val('Send Booking Request');
      	    alert('Your trip request has been submitted, thank you!'); 
      	    location.replace('/trips/thanks?id=' + $('#trip_id').val());
          }
	    );      
	}
});
    
    
$('.dateRange').datepicker({beforeShow: customRange}); 
 
// Customize two date pickers to work as a date range 
function customRange(input) { 
    return {minDate: (input.id == 'end-date' ? $('#start-date').datepicker('getDate') : null), 
        maxDate: (input.id == 'start-date' ? $('#end-date').datepicker('getDate') : null)}; 
} 
    
});


function bookBike() {
    var bike_id = $('#bikes_id').val();
    var pickup_date = $('#start-date').val();
    var dropoff_date = $('#end-date').val();
    var pickup_time = $('#time_hour').val() + ':' + $('#time_min').val();
    var dropoff_time = $('#return_hour').val() + ':' + $('#return_min').val();

    //var pickup_location = $('#pickup_location').val();
    var pickup_location = document.getElementById('pickup_location').options[document.getElementById('pickup_location').selectedIndex].value;

    var dropoff_location = $('#dropoff_location').val();
    var the_name = $('#your_name').val();
    var contact_number = $('#contact_number').val();
    var email_address = $('#email_address').val();
    var comments = $('#comments').val();
    
    terms_box = $('#terms').attr('checked');
    cancellation_box = $('#cancellation').attr('checked');
    safety_box = $('#safety').attr('checked');
    
    accessories = document.getElementsByName('accessory[]');
    accessory_list = '';
    for (i = 0; i < accessories.length; i++) {
        if (accessories[i].checked == true) {
            accessory_list += accessories[i].value + ', ';
        }
    }
    
    if (pickup_date.length < 1 || dropoff_date.length < 1) {
        alert('Please make sure you have selected a pick up and return date.');
    } else if (the_name.length < 1 || contact_number.length < 1) {
        alert('Please fill in your details.');
    } else if (terms_box != true || cancellation_box != true || safety_box != true) {
        alert('Please read and accept the terms, cancellation and safety policy before booking this bike.');
    } else {
        alert('Your bike rental request is being sent...');
        $.post(
            '/pages/send_query',
            { bike_id : bike_id,
                pickup_date : pickup_date,
                pickup_time : pickup_time,
                pickup_location : pickup_location,
                return_date : dropoff_date,
                return_time : dropoff_time,
                return_location : pickup_location,
                name : the_name,
                contact_number : contact_number,
                email_address : email_address,
                comments : comments
            },
            function (data) {
              alert('Your bike rental request has been submitted, thank you!');      
            }
        );
    }
}


function checkemail(str){
    var filter=/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i

    if (filter.test(str))
        testresults=true
    else{
	testresults=false
    }

    return (testresults)
}


function checkContact() {
    if ($('#name').val() == 'Your name*' || $('#name').val() == '') {
        $('#val1').fadeIn('2000');
        return false;
    }
    if ($('#number').val() == 'Phone*' || $('#number').val() == '') {
        $('#val2').fadeIn('2000');
        return false;
    }
    if ($('#mail').val() == 'Email Address*' || $('#mail').val() == '') {
        $('#val3').fadeIn('2000');
        return false;
    }
    
    if ($('#terms').attr('checked') != true) {
        alert('Please accept the terms');
        return false;
    }
    
}

function popUp() {
    var bike_id = $('#bikes_id').val();
    window.open('/bikes/details/' + bike_id, 'details', 'left=20,top=20,width=520,height=520,toolbar=0,resizable=0,scrollbars=0');
}


$(function()
{
	$('.date-pick').datePicker()
	$('#start-date').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#end-date').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
	$('#end-date').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#start-date').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);
});