/**
 * Reservation first step javascript file
 */
$(document).ready(function() {
	$(function() {
		$( "#date_arrive" ).datepicker({ changeMonth: true, changeYear:true, yearRange : current_year + ':' + (current_year+1), showButtonPanel:false, animate: 'fast', dateFormat : 'yy-mm-dd'});
		$( "#date_leave" ).datepicker({ changeMonth: true, changeYear:true, yearRange : current_year + ':' + (current_year+1), showButtonPanel:false, animate: 'fast', dateFormat : 'yy-mm-dd'});
	});
	
	var id_pickup_location = $("#id_pickup_location").val();
	var id_return_location = $("#id_return_location").val();
	
	if( $("#id_pickup_location").val() != $("#id_return_location").val() )
		$("p.alert").show();
	else
		$("p.alert").hide();
	
	$("#id_return_location").change(function(){
		if( $("#id_pickup_location").val() != $("#id_return_location").val() )
			$("p.alert").show();
		else
			$("p.alert").hide();
		
		displayLocationAddress('return_location' , $("#id_return_location").val());
	});
	
	$("#id_pickup_location").change(function(){
		if( id_return_location == '' || id_return_location == id_pickup_location ){
			$("#id_return_location").val($("#id_pickup_location").val());
			displayLocationAddress('return_location' , $("#id_return_location").val());
		}
		if( $("#id_pickup_location").val() != $("#id_return_location").val() )
			$("p.alert").show();
		else
			$("p.alert").hide();
		displayLocationAddress('pickup_location' , $("#id_pickup_location").val());
	});
});

function displayLocationAddress( _id, _id_location )
{
	var content_html = '';
	if( _id_location == '' ){
		$("#" + _id).html('');
		return true;
	}
	
	jQuery.each(json_locations, function(ind, val){
		if( val.id == _id_location )
		{
			content_html += 'Address: ' + val.address + '<br />';
			content_html += 'Tel: ' + val.phone + '<br />';
			content_html += 'Fax: ' + val.fax + '<br />';
			content_html += 'Mo. - Fr. : ' + val.schedule_lv + '<br />';
//			alert(val.schedule_s);return true;
			if( typeof val.schedule_s != 'undefined' && $.trim(val.schedule_s).length > 0 )
				content_html += 'Sat. : ' + val.schedule_s + '<br />';
			if( typeof val.schedule_d != 'undefined' && $.trim(val.schedule_d).length > 0 ){
				content_html += 'Sun. : ' + val.schedule_d;
//				alert($.trim(val.schedule_d).length);
		}
		}
	});
	$("#" + _id).html(content_html);
	
}

/*
 * Validate form for step1
 */
function validateStep1()
{
	var ok = true;
	var id_start_location = $("#id_pickup_location").val();
	var id_end_location = $("#id_return_location").val();
	var start_date = $("#date_leave").val();
	var start_hour = $("#time_hour_leave").val();
	var start_min  = $("#time_min_leave").val();
	var start_location = $("#id_pickup_location").val();
	
	var end_date = $("#date_arrive").val();
	var end_hour = $("#time_hour_arrive").val();
	var end_min  = $("#time_min_arrive").val();
	var end_location = $("#id_return_location").val();
	
	var asd = start_date.split('-');
	var new_start_date = asd[1] + '/' + asd[2] + '/' + asd[0];
	var asd = end_date.split('-');
	var new_end_date = asd[1] + '/' + asd[2] + '/' + asd[0];
	
	var start_date_full = new_start_date + ' ' + (start_hour == 24 ? 0 : start_hour) + ':' + start_min + ':00';
	var end_date_full = new_end_date + ' ' + (end_hour == 24 ? 0 : end_hour) + ':' + end_min + ':00';
	var current_date = new Date();
	current_date = current_date.format("mm/dd/yyyy");
	
	var diff_start_date = dateDiff(current_date, new_start_date, 'days');
	var diff_end_date = dateDiff(new_start_date, new_end_date, 'days');
	
	if( diff_start_date < 0 ){
		ok = false;
		$("#err_start_date").show();
		$("#date_leave").addClass('invalid');
	}
	else{
		$("#err_start_date").hide();
		$("#date_leave").removeClass('invalid');
	}
	
	if( diff_end_date < 0 ){
		ok = false;
		$("#err_end_date").show();
		$("#date_arrive").addClass('invalid');
	}
	else{
		$("#err_end_date").hide();
		$("#date_arrive").removeClass('invalid');
	}
	
	var today=new Date();
	
	var diff_start_datetime = dateDiff(today.getTime(), start_date_full, 'hours');
	
	if( parseInt(diff_start_datetime, 10) < 2 ){
		ok = false;
		$("#time_hour_leave").addClass('invalid');
		$("#time_min_leave").addClass('invalid');
		$("#err_start_time").show();
	}
	else{
		$("#time_hour_leave").removeClass('invalid');
		$("#time_min_leave").removeClass('invalid');
		$("#err_start_time").hide();
	}
	
	var rent_period = dateDiff(start_date_full, end_date_full, 'minutes');
	
	if( parseInt(rent_period, 10) <= 0 )
	{
		ok = false;
		$("#time_hour_arrive").addClass('invalid');
		$("#time_min_arrive").addClass('invalid');
		$("#err_end_time").show();
	}
	else{
		$("#time_hour_arrive").removeClass('invalid');
		$("#time_min_arrive").removeClass('invalid');
		$("#err_end_time").hide();
	}
	
	if( id_start_location == '' ){
		ok = false;
		$("#err_start_location").show();
		$("#id_pickup_location").addClass('invalid');
	}
	else{
		$("#err_start_location").hide();
		$("#id_pickup_location").removeClass('invalid');
	}
	
	if( id_end_location == '' ){
		ok = false;
		$("#err_end_location").show();
		$("#id_return_location").addClass('invalid');
	}
	else{
		$("#err_end_location").hide();
		$("#id_return_location").removeClass('invalid');
	}
	
	rent_period = dateDiff(start_date_full, end_date_full, 'hours');
	
	if( id_start_location != id_end_location && rent_period < 72 ){
		ok = false;
		$("#err_end_date").show();
		$("#date_arrive").addClass('invalid');
		$("#time_hour_arrive").addClass('invalid');
		$("#time_min_arrive").addClass('invalid');
	}
	else{
		$("#err_end_date").hide();
		$("#date_arrive").removeClass('invalid');
		$("#time_hour_arrive").removeClass('invalid');
		$("#time_min_arrive").removeClass('invalid');
	}

	if( ok == true ){
		document.forms['reservation-step1'].submit();
	}
	
}
