$(document).ready(function(){
	var auto_complete_url = "/ajax/static_data/get_destinations/";

	var ac_options = {
		mustMatch: false, 
		matchCase: false,
		minChars: 2,
		autoFill: false,
		max: 30,
		formatItem: function(data, position, total_rows){
			var obj = eval('(' + data + ')'); 
			return obj.text;
		},
		formatResult: function(data, position, total_rows){
			var obj = eval('(' + data + ')');
			return obj.text;
		}
	};
		
	var now = new Date();
	now.setDate(now.getDate() + 3);
	defaults = {
			buttonText: "Pick a date",
			dateFormat: 'yy-mm-dd',
			minDate: now,
			onSelect: function(dateText){
				$("#return_date").datepicker("option", "minDate", $(this).datepicker("getDate"));
			}
	};
		
	$("#arrival_date").datepicker(defaults);
	$("#arrival_time").timepickr();
	
	defaults.onSelect = function(){};
	
	$("#return_date").datepicker(defaults);
	$("#return_time").timepickr();
	//$("#return_date").datepicker('disable');
	
	$("#one_way").click(function(){
		//alert("asasa+");
		if($(this).is(":checked")){						
			$("#return_date").datepicker('disable');
			$("#return_time").attr('disabled', 'disabled');
			$("#return_time, #return_date").removeClass('required');
			$(".returnDiv").slideUp();
		}else{
			$("#return_date").datepicker('enable');
			$("#return_time").removeAttr('disabled');
			$("#return_time, #return_date").addClass('required');
			$(".returnDiv").slideDown();
		}
	});
	
	$("select[name='arrival_type']").change(function(){
		
		var tooltips = {
				'airport': "Please fill Flight Departure Local date and time",
				'port': "Always use  Departure Local time of ferry or cruise ship.",
				'hotel': "Look for your driver at the lobby of the Hotel.  Pick up time will be arranged according to your request.",
				'other': "Please notice that for international flights pick up time will be 3 hours prior departure and for domestic flights 2 hours prior departure"
		};
		
		selected = $(this).children("option:selected");

		if(this.name == 'arrival_type'){
			auto_complete_url_1 = auto_complete_url + selected[0].value;
			$('#destination_from').autocomplete(
						auto_complete_url_1, 
						ac_options).result(enter_value);
		}
				
//		if(selected[0].value != ""){
//			$(this).next('input:disabled').removeAttr('disabled');
//		}else{
//			$(this).next('input').attr('disabled','disabled');
//		}
		
		tmp_1 = $("select[name='arrival_type']").val();

		if( tmp_1 == 'airport' ){
			tmp_string = tooltips.airport;
		}else if( tmp_1 == 'port'){
			tmp_string = tooltips.port;
		}else if(tmp_1 == 'hotel'){
			tmp_string = tooltips.hotel;
		}else{
			tmp_string = tooltips.other;
		}
		$(".conTip").show();		
		$('#highlight_1').highlightFade({'start' : '#ffffcc', 'end': '#dce7f0' , 'speed':500});
		$('#highlight_1').html( "<b>Tip: " + tmp_string + "</b>");		
	});
	
	$("input[name='destination_from_h']").change(function(){
		
		$("option:first-child", $("select[name=destination_to]")).text("Please select a location");
		
		url = "/ajax/static_data/get_destinations_to/";
		post_data = {'from': this.value};
		$.post(url, post_data, function(data){
			first = $('#destination_to option:first');
			$('#destination_to').empty();
			$.each(data, function(id, value){
				$('#destination_to').append('<option value="' + id + '">' + value + "</option>");
			});
			$('#destination_to').prepend(first);
			$('#destination_to').removeAttr('disabled');
			
		},'json');
	});
	
	$("#destination_from").focus(function(){
		
		if($("select[name=arrival_type]").val() == ''){
			$("select[name=arrival_type]").addClass('error');
			$("select[name=arrival_type]").focus();
		}
		
	});
	
	$("a.SearchHideLink").click(function(){
		
		if($("#div_search_form").is(":visible")){
			$("#div_search_form").slideUp('slow');
		}else{
			$("#div_search_form").slideDown('slow');
		}
		
	});
	
	$('#review_booking_ref_num, #arrival_date, #arrival_time, #return_date, #return_time').focus(function(){
		if(this.value == this.title)
			this.value = '';
	});

	$('#review_booking_ref_num, #arrival_date, #arrival_time, #return_date, #return_time').blur(function(){
		if(this.value == '')
			this.value = this.title;
	});
			
	$("#review_booking_ref_num").triggerHandler('blur');
	$("#arrival_date").triggerHandler('blur');
	$("#arrival_time").triggerHandler('blur');
	$("#return_date").triggerHandler('blur');
	$("#return_time").triggerHandler('blur');
	
	$("#search_form").validate({
		rules:{
			destination_from_h: {
				required: function(element){
					if( element.value == '' ){
						
						$("#destination_from").val('');
						$("#destination_from").addClass('error');
						$("#destination_from").next("span.error").remove();
						$("#destination_from").after("<span class='error tooltip' style='margin-left:5px;' title='Please enter a valid pickup loaction. !!NOTICE!! that the text field is an autocoplete field, so please enter the three first letters of the location of your desire and wait for the the list with the valid locations to be appeared.'><img src='" + base_url + "public/images/icons/36x36/Information.png' width='16' height='16' alt='error' /></span>");
						
						return false;
					}else{
						return true;
					}
					
				}
			}
		},
		ignoreTitle: true,
		errorElement:'label',
		errorLabelContainer: "div.error",
		highlight: function(element, errorClass){		
			$(element).addClass('error');
		},
		unhighlight: function(element, errorClass){
			$(element).removeClass('error');
		},
		invalidHandler: function(form, validator){
			 var errors = validator.numberOfInvalids();
			 
			 if(errors){
				var message = errors == 1
		        				? 'You missed 1 field. It has been highlighted'
		        				: 'You missed ' + errors + ' fields. They have been highlighted';
				$("div.error strong").html(message);					 
			 }else {
				 $("div.error").hide();
		     }		 
		}
	});
	
	$("#one_way").triggerHandler("change");
});

function enter_value(evt, data){

	var obj = eval('(' + data + ')');
	
	var id = this.id
	var value = obj.value;
	
	$("input[name='" + id + "_h']").val(value);
	$("input[name='" + id + "_h']").change();

}