<?php // Check to ensure this file is included in Freeway!
defined('_FEXEC') or die(); ?>
<script language="javascript" type="text/javascript">
		var nb_dates=Array();
 <?php  $booking_total=services_dates($service_id,'no_booking_dates');
  		$service_details=get_services_info((int)$HTTP_POST_VARS['service_id']);  
 		for($ic=0;$ic<sizeof($booking_total);$ic++){ 
			 $nbstime=$booking_total[$ic]['start_time'];
			 $nbetime=$booking_total[$ic]['end_time'];
			 $nb_sdate=$booking_total[$ic]['start_date'];
			 $nb_edate=$booking_total[$ic]['end_date'];
			if($nbstime=="12:00:00"){ $nbstime="00:00:00"; } 
			 if($nb_sdate<$nb_edate){?>
			   nb_dates[<?php echo $ic;?>]="<?php echo format_date($nb_sdate).'#'.$nbstime.'#'.'23:00:00';?>";
			   nb_dates[<?php echo $ic+1;?>]="<?php echo format_date($nb_sdate).'#'.'00:00:00'.'#'.$nbetime;?>";	
	  		<?php }else {?>
				nb_dates[<?php echo $ic;?>]="<?php echo format_date($nb_sdate).'#'.$nbstime.'#'.$nbetime;?>";
	  		<?php }
			} ?>
		    var attribute_array=Array();
			<?php $cou=0;
				  $pre_id=""; 	
				  $result = tep_db_query("SELECT * FROM " . TABLE_SERVICES_ATTRIBUTES . " sa LEFT JOIN " . TABLE_SERVICES_OPTIONS . " so ON so.services_options_id=sa.options_id LEFT JOIN " . TABLE_SERVICES_OPTIONS_VALUES . " sov ON sov.services_options_values_id=sa.options_values_id WHERE sa.service_id='" . (int)$service_id . "' and so.language_id='" . (int)$languages_id . "' and sov.language_id='" . (int)$languages_id . "' order by services_options_id");
				  $rows=tep_db_num_rows($result); 
				  if($rows){
				   while($attribute=tep_db_fetch_array($result)){
				    if($pre_id!=$attribute['services_options_id']){?>
						attribute_array[<?php echo $cou;?>]="<?php echo $attribute['services_options_id'];?>";
			<?php   	$pre_id=$attribute['services_options_id'];
						$cou++;
					 }
					} 
				 }
			?>		
			var max_bookings="";
			var time_length=1;
			var book_type="H";
			var book_granular=1;
			var service_quantity=1;
			var span_days="";
			var include_resource="";
			var multiple_purchase="";	
			var start_time="00:00";
			var end_time="23:59";			
			var tempCheck=1;
			var cur_hour="<?php echo date("H").':00';?>";
			var cur_date="<?php echo format_date(date("Y-m-d"));?>";								
			var dis_time_format="";
			dis_time_format="<?php if(defined('TIME_FORMAT')) echo TIME_FORMAT; ?>";
			function add_start_times(sel_time){
				if(document.getElementById("nb_dates") && document.getElementById("nb_dates").value!=""){
			   		var temp=document.getElementById("nb_dates").value;
			   		temp_arr=temp.split('?');
						for (cnt=0;cnt<temp_arr.length-1;cnt++)
							nb_dates[cnt]=temp_arr[cnt];					
				}else nb_dates=Array();
					if(document.getElementById("times_val")){
						splt=document.getElementById("times_val").value.split('{}');					
						if(splt.length>0){
							max_bookings=splt[0];
							time_length=parseInt(splt[1]);
							book_type=splt[2];
							book_granular=splt[3];
							service_quantity=splt[4];
							span_days=splt[5];
							include_resource=splt[6];
							multiple_purchase=splt[7];
						}
					}					
					if(!time_length) time_length=1;
					var tempDate;
					if(document.move_booking_confirm && document.move_booking_confirm.start_date) tempDate=convert_to_sdate(document.move_booking_confirm.start_date.value);
					var i=cnt=0;
					var hour;
					var arr_text=Array();
					var arr_value=Array();
					splt_cur_hour=cur_hour.split(":");						
					if(tempDate==cur_date)	i=(parseInt(splt_cur_hour[0])+1) * 60;
					var slot=parseInt(book_granular); //booking granular					
					if(book_type=='H') slot=parseInt(slot*60);			
					if(slot=="") slot=1;		
					while(i<=1440){
					    // loop for no booking date and time 
					    var no_book=false;
					    if(nb_dates.length>0){
						  for(j=0;j<nb_dates.length;j++){							 
						    if(nb_dates[j]){
						     n_dates=nb_dates[j].split("#");
							 n_sdate=n_dates[0];
							 nb_s_time=n_dates[1];
							 nb_e_time=n_dates[2];
							 splt_etime=nb_e_time.split(":");
							 splt_stime=nb_s_time.split(":");
							 splt_stime_mt=splt_stime[0] * 60;
							 splt_etime_mt=splt_etime[0] * 60;
							 if(n_sdate==tempDate){
							      if(splt_stime_mt<=i && i<=splt_etime_mt || splt_stime_mt==i) 	
							   	  	i=splt_etime_mt;
						     }	  
						    }
						  }
						} 
						// loop for no booking date and time	
						  	 if(i>=1440) break;
						  	 var mins=parseInt(time_length*60);
						 	 if(book_type=='M') mins=parseInt(time_length);
						 for(ict=0;ict<=59;ict+=slot){
					 	     var d_obj=new Date(1976,1,1,0,i,ict*60);
						  	 hr=((d_obj.getHours()==0)?00:d_obj.getHours());
							 hr=((hr<10)?"0"+hr:hr);
						     mt=((d_obj.getMinutes()<10)?"0"+d_obj.getMinutes():d_obj.getMinutes());
						     var tem_hr=hr;
						     if(dis_time_format=='24'){
						     	if(hr==12) tem_hr=12;
						     	//if(hr==00) tem_hr=24;
						     }else if(hr==12) tem_hr=24;
						     tem_hour=tem_hr +":"+mt;
						     hour=hr +":"+mt;
						     var n_obj=new Date(1976,1,1,hr,(mt+mins));
						     if(span_days=='N' && n_obj.getDate()=="2" && (n_obj.getMinutes()>0 || n_obj.getHours()>0)) hour="";
						     	if(hour!=""){
									var dis_time=convert_to_stime(tem_hour);
									if(dis_time_format!="" && dis_time_format=='24') dis_time=tem_hour;
											arr_text[cnt]=dis_time;
											arr_value[cnt]=hour;
							     			cnt++;
						     	}
						 }
						 if(book_type=='H') i=parseInt(i+slot);
						 else i=parseInt(i+60);						 
						 if(i>1440 || i==0) break;
					}
					if(arr_text.length<=0 && arr_value.length<=0 && (book_type=='H' || book_type=='M') && document.move_booking_confirm.start_time && document.move_booking_confirm.start_date){
						document.move_booking_confirm.start_date.remove(document.move_booking_confirm.start_date.options.selectedIndex);
						add_start_times();
					}
					if (document.move_booking_confirm && arr_text.length>0 && arr_value.length>0 && (book_type=='H' || book_type=='M')){
					  	add_options(arr_text,arr_value,document.move_booking_confirm.start_time);
					  	add_end_times();
					}
				}
				
				function add_end_times(){	
					if(document.getElementById("times_val")){
						splt=document.getElementById("times_val").value.split('{}');					
						if(splt.length>0){
							max_bookings=splt[0];
							time_length=parseInt(splt[1]);
							book_type=splt[2];
							book_granular=splt[3];
							service_quantity=splt[4];
							span_days=splt[5];
							include_resource=splt[6];
							multiple_purchase=splt[7];
						}
					}
					if(!time_length) time_length=1;
					var arr_text=Array();
					var arr_value=Array();
					//check if booking is day based or hours based
					var end_time='23:59';
					var send_time=convert_to_stime('00:00:00');
					if(dis_time_format!="" && dis_time_format=='24') 						
						send_time='00:00';
					if (book_type=="D"){
						var tempDate=getDatePart(document.move_booking_confirm.start_times.value,"D");
						jcnt=0;
						if (!tempDate) return;
						for (icnt=time_length;icnt<=max_bookings;icnt=icnt+time_length){
						    start_time_split=start_time.split(":");
						    hour=start_time_split[0];
						 if(hour==00){
							if(jcnt>=1)
							  	tempDate=dateAdd("d",time_length,tempDate);
							else 
								tempDate=dateAdd("d",time_length-1,tempDate);
					          }else { 
						   	tempDate=dateAdd("d",time_length,tempDate);
						 } 
							arr_value[jcnt]=tempDate+" "+end_time;
							tempDate_ed=dateAdd("d",1,tempDate);	
							arr_text[jcnt]=date_format(tempDate_ed,'Y-m-d','')+" "+send_time;
							jcnt++;
						}
					} else {
						var tempTime_s=document.move_booking_confirm.start_time.value;
						var display_tempTime_s=document.move_booking_confirm.start_time.value;
						var tempDate=document.move_booking_confirm.start_date.value;
						var check_date=tempDate;
						var tempDateS=document.move_booking_confirm.start_date.value;
						var check_date=tempDateS;
						if (!tempTime_s && document.move_booking_confirm) {
							document.move_booking_confirm.start_time.remove(document.move_booking_confirm.start_time.options.selectedIndex)
							if(document.move_booking_confirm.start_time.options.length<0) document.move_booking_confirm.start_date.remove(document.move_booking_confirm.start_date.options.selectedIndex)
						}
						if (book_granular<=0) book_granular=1;
						slot=book_granular;
						jcnt=0;
						elapsed=1;
						tempCheck=0;
						hour=0;
						var display_hour = 0;
						var display_minute = 0;
						var mins=time_length*60;
						var maxs=max_bookings*60;						
						if(book_type=='M'){
							mins=time_length;
							maxs=max_bookings;
						}
						while(true){
							tempTime_splt=tempTime_s.split(":");
							tempdate_splt=tempDate.split("-");
							hour=parseInt(tempTime_splt[0],10);			
							if(tempTime_s=="24:00"){
							    	hour=11+parseInt(time_length);
								    display_hour=11+parseInt(time_length);  //Display Hour
								}
								//else if(hour==23 && book_type=='H') mins-=1;
								else if(tempTime_splt[0]=="24"){
								  hour=12+parseInt(time_length);
								  display_hour=12+parseInt(time_length);  //Display Hour
							}
							//Display Hour
							hour=((hour<10)?"0"+hour:hour);
							hour=((hour<10)?"0"+hour:hour);							
							minute=parseInt(tempTime_splt[1],10)+mins;
							minute=((minute<10)?"0"+minute:minute);
							var dt=new Date(tempdate_splt[0],parseInt(tempdate_splt[1])-1,tempdate_splt[2],hour,minute,0);
							getday=dt.getDate();
							getmonth=dt.getMonth()+1;
							getyear=dt.getFullYear();
							set_getday=getday;
							set_getmonth=getmonth
							set_getyear=getyear;
							if(tempdate_splt[2]<getday && dt.getHours()==0 && dt.getMinutes()==0){
								set_getday=tempdate_splt[2];
								set_getmonth=tempdate_splt[1];
								set_getyear=tempdate_splt[0];
							}
							if(set_getday<10)   set_getday="0"+set_getday; 								
							if(set_getmonth<10) set_getmonth="0"+set_getmonth;							
							tempDate=tempDateS=(dt.getFullYear()+"-"+getmonth+"-"+getday);
							set_tempDate=(set_getyear+"-"+set_getmonth+"-"+set_getday);
							tempTime_s=((dt.getHours()<10)?"0"+dt.getHours():dt.getHours()) + ":"+ dt.getMinutes();
							var display_dt=new Date(tempdate_splt[0],tempdate_splt[1]-1,tempdate_splt[2],hour,minute,(minute=="59")?60:1);
							display_tempDate=(display_dt.getFullYear()+"-"+(display_dt.getMonth()+1)+"-"+display_dt.getDate());
							display_tempTime_s=((display_dt.getHours()<10)?"0"+display_dt.getHours():display_dt.getHours())+ ":" + ((display_dt.getMinutes()<10)?"0"+display_dt.getMinutes():display_dt.getMinutes());
							var dis_time=convert_to_stime(display_tempTime_s);
							if(dis_time_format!="" && dis_time_format=='24')  dis_time=display_tempTime_s;
							check_date_obj=date_format(check_date,'Y-m-d','Y-m-d',true);
							tempDate_obj=date_format(tempDate,'Y-m-d','Y-m-d',true);
							if (tempTime_s>end_time || (span_days=='N' && check_date_obj<tempDate_obj && (dt.getMinutes()>0 || dt.getHours()>0))) break;
								arr_text[jcnt]=date_format(display_tempDate,'Y-m-d','')+" "+dis_time;
								arr_value[jcnt]=set_tempDate+" "+tempTime_s;
								elapsed+=mins;
								if (elapsed>maxs) break;
							jcnt++;		
						} 		
					}
					if(arr_text.length>0) {
						document.move_booking_confirm.end_times.style.display='';
						add_options(arr_text,arr_value,document.move_booking_confirm.end_times);	
					}
				}				
	add_start_times();
	if(document.move_booking_confirm) document.move_booking_confirm.end_times.selectedIndex=add_end_times();
</script>