var startLocationOk=false;
var endLocationOk=false;
var startLocationPoiOrNumber=false;
var endLocationPoiOrNumber=false;
function rentHubcheckAndSetTime(timefrom, cb){
rentHubcheckAndSetTimeFor("pickup", timefrom);
if(typeof cb!='undefined'&&typeof cb=='function'){
cb();
}}
function rentHubcheckAndSetTimeFor(forKey, forceTime){
var $=jQuery;
var time=$("#rsf_" + forKey + "_time").val();
if(typeof forceTime!='undefined'&&forceTime!=null&&forceTime!=''){
time=forceTime;
}
if($("#rsf_"+forKey+"_date").length<=0){
return;
}
var day=document.querySelector("#rsf_"+forKey+"_date")._flatpickr;
if(typeof day=='undefined'||day.selectedDates.length==0){
return;
}
day=day.selectedDates[0];
day=day.getDay();
$("#rsf_"+forKey+"_time").find("option").remove();
if(day==0){
day=7;
}
var time_accepted=renthub_config.opening.opening_time[day];
if(time_accepted!=''){
time_accepted=time_accepted.split(",");
for(var i=0;i<time_accepted.length;i++){
var range=time_accepted[i];
range=range.split("-");
var startHours=range[0].split(":")[0];
var startMinutes=range[0].split(":")[1];
var current=startHours+":"+startMinutes;
do{
$("#rsf_"+forKey+"_time").append("<option value='"+current+"'>"+current+"</option>");
if(startMinutes=='00'){
startMinutes='30';
}else{
startMinutes='00';
startHours++;
}
startHours="0"+startHours;
startHours=startHours.slice(-2);
current=startHours+":"+startMinutes;
} while(current<=range[1]);
}}
if($("#rsf_"+forKey+"_time"+" option[value='"+time+"']").length>0){
$("#rsf_"+forKey+"_time").val(time);
}}
(function ($){
var numberPoiMandatory=renthub_config.number_poi_mandatory=='no' ? false:true;
$(document).ready(function (){
var dateFormat=renthub_config.locale.js_date_format.replace("yyyy",'Y').replace("yy",'y').replace("dd","d").replace("mm","m");
if($("#renthub_search_form").length<=0){
return;
}
if($("#renthub_search_form").data("valid-search")===true){
startLocationOk=true;
endLocationOk=true;
startLocationPoiOrNumber=true;
endLocationPoiOrNumber=true;
}
if($("#rsf_type").length > 0){
$(".renthub_search_category").click(function (){
var selected_category=$(this).data("category-id");
$("#rsf_type").val(selected_category);
});
}
if($("#rsf_pickup").length > 0){
$("#rsf_pickup").change(function (){
var pickup=$("#rsf_pickup").val();
if(pickup!=''&&$("#rsf_dropoff option[value='" + pickup + "']").length > 0){
$("#rsf_dropoff").val(pickup);
}});
var daysOfWeekDisabled=[];
for (var i=1; i <=7; i++){
if(renthub_config.opening.opening_time[i]==''){
daysOfWeekDisabled.push(i==7 ? 0:i);
}}
$("#rsf_pickup_date").flatpickr({
'dateFormat': dateFormat,
'locale': renthub_config.locale.locale,
'minDate': new Date(),
'disable': [function(date){
var closed=renthub_config.opening.closed_at;
var plainDate=date.getFullYear()+"-"+("0"+(date.getMonth()+1)).slice(-2)+"-"+("0"+(date.getDate())).slice(-2);
for(var i=0; i<daysOfWeekDisabled.length; i++){
if(date.getDay()==daysOfWeekDisabled[i]){
return true;
}}
for(i=0; i < closed.length; i++){
if(closed[i]==plainDate){
return true;
}}
return false;
}],
"onChange":function(selectedDates, dateStr, instance){
if(selectedDates.length>0){
var newDate=new Date(selectedDates[0]);
newDate.setDate(newDate.getDate() + parseInt(renthub_config.opening.min_days));
}
rentHubcheckAndSetTime();
}});
rentHubcheckAndSetTime(renthub_config.pickup_time);
}
$("#rsf_pickup").keydown(function (){
startLocationOk=false;
startLocationPoiOrNumber=false;
});
$("#rsf_dropoff").keydown(function (){
endLocationOk=false;
endLocationPoiOrNumber=false;
});
$("#renthub_search_form").submit(function (e){
var preventForm=false;
if(!startLocationOk&&!preventForm){
$.toast({
text: renthub_js_trans.start_location_not_selected,
icon: 'error',
showHideTransition: 'fade',
allowToastClose: true,
hideAfter: 5000,
stack: 5,
position: 'top-center',
textAlign: 'left',
loader: false
});
preventForm=true;
}
if(!endLocationOk&&!preventForm){
$.toast({
text: renthub_js_trans.end_location_not_selected,
icon: 'error',
showHideTransition: 'fade',
allowToastClose: true,
hideAfter: 5000,
stack: 5,
position: 'top-center',
textAlign: 'left',
loader: false
});
preventForm=true;
}
if((!startLocationPoiOrNumber&&numberPoiMandatory)&&!preventForm){
$.toast({
text: renthub_js_trans.start_location_not_number_or_poi,
icon: 'error',
showHideTransition: 'fade',
allowToastClose: true,
hideAfter: 5000,
stack: 5,
position: 'top-center',
textAlign: 'left',
loader: false
});
preventForm=true;
}
if((!endLocationPoiOrNumber&&numberPoiMandatory)&&!preventForm){
$.toast({
text: renthub_js_trans.end_location_not_number_or_poi,
icon: 'error',
showHideTransition: 'fade',
allowToastClose: true,
hideAfter: 5000,
stack: 5,
position: 'top-center',
textAlign: 'left',
loader: false
});
preventForm=true;
}
if(preventForm){
e.preventDefault();
e.stopPropagation();
return false;
}});
});
google.maps.event.addDomListener(window, 'load', function (){
if($("#renthub_search_form").length<=0){
return;
}
var bound=renthub_utils.boundCountries;
if(bound!=''){
bound=bound.split(",");
}
var startAutocomplete=new google.maps.places.Autocomplete((document.getElementById("rsf_pickup")));
if(bound!=''){
startAutocomplete.setComponentRestrictions({'country': bound});
}
startAutocomplete.addListener('place_changed', function (){
var place=startAutocomplete.getPlace();
if(typeof place.geometry!=='undefined'){
var location=place.geometry.location;
$("#rsf_pickup_lat").val(location.lat());
$("#rsf_pickup_lng").val(location.lng());
startLocationOk=true;
if($.inArray("point_of_interest", place.types)!=-1){
startLocationPoiOrNumber=true;
}else{
for (var i=0; i < place.address_components.length; i++){
var addressComponent=place.address_components[i];
if($.inArray("street_number", addressComponent.types)!=-1){
startLocationPoiOrNumber=true;
break;
}}
}}else{
$.toast({
text: renthub_js_trans.start_location_not_valid,
icon: 'error',
showHideTransition: 'fade',
allowToastClose: true,
hideAfter: 5000,
stack: 5,
position: 'top-center',
textAlign: 'left',
loader: false
});
$("#rsf_pickup").val("");
}});
var endAutocomplete=new google.maps.places.Autocomplete((document.getElementById("rsf_dropoff")));
if(bound!=''){
endAutocomplete.setComponentRestrictions({'country': bound});
}
endAutocomplete.addListener('place_changed', function (){
var place=endAutocomplete.getPlace();
if(typeof place.geometry!=='undefined'){
var location=place.geometry.location;
$("#rsf_dropoff_lat").val(location.lat());
$("#rsf_dropoff_lng").val(location.lng());
endLocationOk=true;
if($.inArray("point_of_interest", place.types)!=-1){
endLocationPoiOrNumber=true;
}else{
for (var i=0; i < place.address_components.length; i++){
var addressComponent=place.address_components[i];
if($.inArray("street_number", addressComponent.types)!=-1){
endLocationPoiOrNumber=true;
break;
}}
}}else{
$.toast({
text: renthub_js_trans.end_location_not_valid,
icon: 'error',
showHideTransition: 'fade',
allowToastClose: true,
hideAfter: 5000,
stack: 5,
position: 'top-center',
textAlign: 'left',
loader: false
});
$("#rsf_dropoff").val("");
}});
});
})(jQuery);