<?php
/*
  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
*/ 
 include(DIR_WS_INCLUDES."date_format_js.php");
 ?>
		<script language='javascript'>  
			var max_bookings=<?php echo (float)$service_info["max_bookings"];?>;
			var time_length=<?php echo (float)$service_info["service_total_time"];?>;
			var book_type="<?php echo $booking_type;?>";
			var book_granular="<?php echo (int)$granular;?>";			
			var start_time="00:00";
			var end_time="23:59";
			var tempCheck=1;
			var cur_date="<?php echo format_date(date("Y-m-d",$current_date_string));?>";
			var cur_hour="<?php echo date("H",$current_date_string).':00';?>";
			var span_days="<?php echo $span_days;?>";
			var include_resource="<?php echo $include_resource;?>";
			var multiple_purchase="<?php echo $service_info["multiple_purchase"];?>";
			var book_buffer="<?php echo $book_buffer;?>";
			var book_buffer_minute="<?php echo $book_buffer_minute;?>";
			if(!time_length) time_length=1; 
			// get block dates and timings
			var nb_dates=Array();
			<?php  /*$count=0; 
			 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 $count;?>]="<?php echo $nb_sdate.'#'.$nb_sdate.'#'.$nbstime.'#'.'24:00:00';?>";
					  nb_dates[<?php echo $count=$count+1;?>]="<?php echo $nb_sdate.'#'.$nb_sdate.'#'.'00:00:00'.'#'.$nbetime;?>";	
			  <?php }else {?>
					  nb_dates[<?php echo $count;?>]="<?php echo $nb_sdate.'#'.$nb_sdate.'#'.$nbstime.'#'.$nbetime;?>";
		   <?php    }
		  			$count++;
			  }
			 $date_begin=$week_start_date;
			 $date_end=$week_end_date;
			 if($view=='month'){
			 	$date_begin=$month_start_date;
			 	$date_end=$month_end_date;
			 }
			 $bk_query=tep_db_query("select no_booking_start_date,no_booking_end_date,no_booking_start_time,no_booking_end_time,recurring_type,recurring_details from ".TABLE_SERVICES_NO_BOOKING_DAYS ." snd where snd.service_id='".$service_id."' and (('$date_begin'>=no_booking_start_date and '$date_begin'<=no_booking_start_date) OR  ('$date_end'>=no_booking_start_date and '$date_end'<=no_booking_end_date) OR ('$date_begin'<=no_booking_start_date and '$date_end'>=no_booking_end_date)) order by no_booking_start_date,no_booking_end_date,no_booking_start_time,no_booking_end_time");
			 while($nbk=tep_db_fetch_array($bk_query)){
			 	if($nbk['recurring_type']=='DD'){
				 	$nbstime=$nbk['no_booking_start_time'];
				 	$nbetime=$nbk['no_booking_end_time'];
				 	  if($span_days=='Y' && $nbetime<$nbstime){?>
				 	  	nb_dates[<?php echo $count;?>]="<?php echo $nbk['no_booking_start_date'].'#'.$nbk['no_booking_end_date'].'#00:00:00'.'#'.$nbetime;?>"; 
				 	  	nb_dates[<?php echo $count=$count+1;?>]="<?php echo $nbk['no_booking_start_date'].'#'.$nbk['no_booking_end_date'].'#'.$nbstime.'#24:00:00';?>";
				<?php }else {
						if($nbetime<$nbstime)$nbetime='24:00:00';?>
				 		nb_dates[<?php echo $count;?>]="<?php echo $nbk['no_booking_start_date'].'#'.$nbk['no_booking_end_date'].'#'.$nbstime.'#'.$nbetime;?>";
			    <?php
			 		  } 
			    }  
			   $count++;
			 }	 */?>
			 // get block dates and timings
			var baby=new Array();
			var final_data="<table height='90%' border=0 width=100% align=center><tr><td class=main height=70 valign=top>";
			final_data+="<?php echo '<b>' . addslashes(stripslashes(TEXT_SERVICES_NAME)) . '</b><br>' . str_replace("\n","<br>",addslashes(stripslashes($service_name))) . '<p>'; ?>";
			<?php if ($service_info["conditions"] !=''){ ?>
				 final_data+="<?php echo '<b>' . addslashes(stripslashes(TEXT_CONDITIONS)) . '</b><br>' . str_replace("\n","<br>",addslashes(stripslashes($service_info["conditions"]))) .  '<p>';?>";
			<?php } ?>
				final_data+="</table><table width='100%'><tr><td align='right' class='main'><a href='javascript:window.close();'>[ close ]</a></td></tr></table>";
				baby[1]=final_data;
				function showConditions(){
					var handle;
					var feats;
					vtop=screen.availHeight /2 - screen.availHeight /6;
					vleft=screen.availWidth/2 - screen.availWidth/6;
					feats="height=350,width=350,location=no,menubar=no,resizable=no,scrollbars=yes,toolbar=no,status=no,left=" + vleft + ",top=" + vtop ;	
					handle=window.open("","conditions",feats);
					handle.document.open();
					handle.document.write('<html><head><title>Conditions</title><base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>"><link rel="stylesheet" type="text/css" href="<?php echo TEMPLATE_STYLE;?>"></head>');
					handle.document.write('<body class="conditions">');
					handle.document.write(baby[1]);
					handle.document.write('</body></html>');
					handle.document.close();		
				}
				function validateForm(){
					var frm = document.forms["select_service"];
					var error_result="";
					if (book_type=="D"){
						if (frm.start_times.value==""){
							error_result="<?php echo addslashes(ERR_SELECT_START_TIME);?>";
						} else if (frm.end_times.value==""){
							error_result="<?php echo addslashes(ERR_SELECT_END_TIME);?>";
						}
					} else {
						if (frm.start_date.value=="" || frm.start_time.value==""){
							error_result="<?php echo addslashes(ERR_SELECT_START_TIME);?>";
						} else if (frm.end_times.value==""){
							error_result="<?php echo addslashes(ERR_SELECT_END_TIME);?>";
						}
					}
					if (error_result==""){
						if (frm.products_qty && isNaN(parseInt(frm.products_qty.value)) || parseInt(frm.products_qty.value)<=0 || frm.products_qty.value==''){
							error_result="<?php echo addslashes(ERR_EMPTY_HOW_MANY);?>";
						}else if(document.getElementById('agreed') && !document.getElementById('agreed').checked)
							error_result="<?php echo addslashes(ERROR_AGREE);?>";
					}
					if (error_result!=""){
						alert(error_result);
						return false;
					}
					if (book_type=='D'){
						frm.start_date.value=getDatePart(frm.start_times.value,"D");
						frm.start_time.value=getDatePart(frm.start_times.value,"H");
					}
					frm.end_date.value=getDatePart(frm.end_times.value,"D");
					frm.end_time.value=getDatePart(frm.end_times.value,"H");
					return true;
				}
				function get_diff_minutes(stime,etime){
					splt1=stime.split(":");
					splt2=etime.split(":");
					smin=parseInt(splt1[0],10)*60+parseInt(splt1[1],10);
					emin=parseInt(splt2[0],10)*60+parseInt(splt2[1],10);
					return emin-smin;
				}
				function add_options(textArr,valueArr,optionElement){
					// clear the previous details
					while(optionElement.options.length>0){
						optionElement.remove(optionElement.options.length - 1);
					}
					//create the new details
					for (icnt=0;icnt<textArr.length;icnt++){
						  var option = document.createElement('option');
						  option.text = textArr[icnt];
						  option.value = valueArr[icnt];
						  try {
							optionElement.add(option, null); // standards compliant; doesn't work in IE
						  }
						  catch(ex) {
							optionElement.add(option); // IE only
						  }
					}
				}
				function add_start_times(){
					if(nb_dates_string!="") nb_dates=nb_dates_string.split('{{}}');
					var tempDate=convert_to_sdate(document.select_service.start_date.value);
					var i=cnt=0;
					var hour;
					var arr_text=Array();
					var arr_value=Array();
					var dis_time_format='';
					dis_time_format="<?php if(defined('TIME_FORMAT')) echo TIME_FORMAT; ?>";
					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);
					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=date_format(n_dates[0],'Y-m-d','Y-m-d',true).getTime();
							 	 n_edate=date_format(n_dates[1],'Y-m-d','Y-m-d',true).getTime();
							 	 nb_s_time=n_dates[2];
							 	 nb_e_time=n_dates[3];
							 	 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;
								 splt_etime=nb_e_time.split(":");
								 tep_date_string=date_format(tempDate,'','',true).getTime();
								 if(n_sdate<=tep_date_string && n_edate>=tep_date_string){
								 	if(splt_stime_mt<=i && i<=splt_etime_mt || splt_stime_mt==i)
								   	  i=splt_etime_mt;
							     }	 
						      }
						   }
						}
						// loop for no booking date and time
						 buf_min=0;
					  	 if(i>=1440) break;
					  	 var tem_hour=tem_hr="";
					  	 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());		
						     //if(hr==12) hr=24;
						     tem_hr=hr;
						     if(dis_time_format=='24' && hr==12) tem_hr=12;						     
						     else if(hr==12) tem_hr=24;
						     //if(hr==00) 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!=""){
									if(dis_time_format!="" && dis_time_format=='24')  										
										arr_text[cnt]=tem_hour;
									else 
										arr_text[cnt]=convert_to_stime(tem_hour);
									arr_value[cnt]=hour;
						     		cnt++;
						     		if(cnt>24) break;
							    }
					     }
						 if(book_type=='H') i=parseInt(i+slot);
						 else i=parseInt(i+60);
						 if(i>1440) break;
					}														
					if(arr_text.length<=0 && arr_value.length<=0 && (book_type=='H' || book_type=='M') && document.select_service.start_time && document.select_service.start_date){
						document.select_service.start_date.remove(document.select_service.start_date.options.selectedIndex);
						add_start_times();	
					}
					 
					if (arr_text.length>0 && arr_value.length>0 && (book_type=='H' || book_type=='M')){
					  add_options(arr_text,arr_value,document.select_service.start_time);
					  add_end_times();
					}
				}
				
				function add_end_times(){
					var arr_text=Array();
					var arr_value=Array();
					var	dis_time_format='';
					dis_time_format="<?php if(defined('TIME_FORMAT')) echo TIME_FORMAT; ?>";					
					//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.select_service.start_times.value,"D");
						var check_date=tempDate;
						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.select_service.start_time.value;
						var display_tempTime_s=document.select_service.start_time.value;
						var tempDate=document.select_service.start_date.value;
						var check_date=tempDate;
						var tempDateS=document.select_service.start_date.value;
						var check_date=tempDateS;
						if (!tempTime_s) {
							document.select_service.start_time.remove(document.select_service.start_time.options.selectedIndex)
							if(document.select_service.start_time.options.length<0) document.select_service.start_date.remove(document.select_service.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=check_date.split("-");							
							hour=parseInt(tempTime_splt[0],10);
							//Display Hour
							display_tempTime_splt=display_tempTime_s.split(":");
							display_hour=parseInt(display_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
							}
							hour=((hour<10)?"0"+hour:hour);
							minute=parseInt(tempTime_splt[1],10)+mins;
							minute=((minute<10)?"0"+minute:minute);
							var dt=new Date(parseInt(tempdate_splt[0]),tempdate_splt[1]-1,tempdate_splt[2],hour,minute,0);
							//alert(tempdate_splt[0]+ " "+parseInt(tempdate_splt[1])-1+" "+tempdate_splt[2]+" "+hour+" "+minute);
							//Display Hour
							var display_dt=new Date(tempdate_splt[0],tempdate_splt[1]-1,tempdate_splt[2],hour,minute,(minute=="59")?60:1);
							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=(getyear+"-"+getmonth+"-"+getday);
								set_tempDate=(set_getyear+"-"+set_getmonth+"-"+set_getday);
								tempTime_s=((dt.getHours()<10)?"0"+dt.getHours():dt.getHours()) + ":"+ dt.getMinutes();
								display_date=(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();//Display_hour
								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());
							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;
								dis_date=(display_date>tempDate)?display_date:tempDate;
								var display_time=convert_to_stime(display_tempTime_s);
								if(dis_time_format!="" && dis_time_format=='24') display_time=display_tempTime_s;
								arr_text[jcnt]=date_format(dis_date,'Y-m-d','')+ " " +display_time;  //Display Hour
								arr_value[jcnt]=set_tempDate+" "+tempTime_s;
								elapsed+=mins;
							if (elapsed>maxs) break;
							jcnt++;
						}
					}
					if(arr_text.length>0) add_options(arr_text,arr_value,document.select_service.end_times);					
				}
				function getDatePart(source,type){
					var result="";
					if (source!=""){
						splt=source.split(" ");
						if (type=="D")
							result=splt[0];
						else
							result=splt[1];
					}
					return result;
				}
				function convert_to_sdate(sdate){
					format="<?php echo EVENTS_DATE_FORMAT;?>";
					splt=sdate.split("-");
					switch(format){
						case "d-m-Y":
							rdate=splt[2]+"-"+splt[1]+"-"+splt[0];
							break;
						case "m-d-Y":
							rdate=splt[1]+"-"+splt[2]+"-"+splt[0];
							break;
						case "Y-m-d":
							rdate=sdate;
							break;
					}
					return rdate;
				}
				function convert_to_stime(stime){
					splt=stime.split(":");
					hour=parseInt(splt[0],10);
					minutes=parseInt(splt[1],10);
					if (hour>12 || hour==12){
						if (hour>12) hour=hour-12;
						rtime=" PM";
					} else 
						rtime=" AM";
					if (hour<10) hour="0"+hour;
					if (minutes<10) minutes="0"+minutes;
					rtime=((hour==00)?12:hour)+":"+minutes+rtime;
					return rtime;
				}
			
			
			function dateAdd(p_Interval, p_Number, p_Date){	
				if(isNaN(p_Number)){return false;}
				//convert date elements
				if (book_type=="H" || book_type=='M'){
					splt=p_Date.split(":");
					m=parseInt(splt[1],10);
					if (tempCheck==0) {
					  m--;
					  tempCheck=1;					  
					}
						var dt=new Date(1976,1,1,parseInt(splt[0],10),m);
				} else {
					splt=p_Date.split("-");
					var dt=new Date(parseInt(splt[0],10),parseInt(splt[1],10)-1,parseInt(splt[2],10));
				}
								
				p_Number = new Number(p_Number);
				switch(p_Interval.toLowerCase()){
					case "yyyy": {// year
						dt.setFullYear(dt.getFullYear() + p_Number);
						break;
					}
					case "q": {		// quarter
						dt.setMonth(dt.getMonth() + (p_Number*3));
						break;
					}
					case "m": {		// month
						dt.setMonth(dt.getMonth() + p_Number);
						break;
					}
					case "y":		// day of year
					case "d":		// day
					case "w": {		// weekday	
						dt.setDate(dt.getDate() + p_Number);
						break;
					}
					case "ww": {	// week of year
						dt.setDate(dt.getDate() + (p_Number*7));
						break;
					}
					case "h": {		// hour
						dt.setHours(dt.getHours() + p_Number);
						break;
					}
					case "n": {		// minute
						dt.setMinutes(dt.getMinutes() + p_Number);
						break;
					}
					case "s": {		// second
						dt.setSeconds(dt.getSeconds() + p_Number);
						break;
					}
					case "ms": {		// second
						dt.setMilliseconds(dt.getMilliseconds() + p_Number);
						break;
					}
					default: {
						return "invalid interval: '" + p_Interval + "'";
					}
				}
				if (book_type=="H" || book_type=='M') {
					hour=dt.getHours();
					minute=dt.getMinutes();
					if (hour<10) hour="0"+hour;
					if (minute<10) minute="0"+minute;
					return hour+":"+minute;
				} else {
					day=dt.getDate();
					month=dt.getMonth()+1;
					if (day<10) day="0"+day;
					if (month<10) month="0"+month;
					return dt.getFullYear()+"-"+month+"-"+day;
				}
			}
			
			function datePrepare(p_sDate,p_Type, p_Interval,p_upTo){
				var dt=date_format(p_sDate,'','',true);
				if (!dt) return;
                			var valuedate;
				var textdate;
				p_Interval = new Number(p_Interval);

				var dateArray=Array(); // first entry contains the text and second contains the value
				var jcnt=0;
				for (icnt=0;icnt<=p_upTo;icnt++){	
					switch(p_Type.toLowerCase()){
						case "yyyy":
							dt.setFullYear(dt.getFullYear() + p_Interval);
							break;
						case "H": 
							dt.setFullYear(dt.getFullYear() + (6));
							break;
						case "q": 
							dt.setMonth(dt.getMonth() + (3));
							break;
						case "m": 
							dt.setMonth(dt.getMonth() + p_Interval);
							break;
						case "y":		// day of year
						case "d":		// day
						case "w":
							dt.setDate(dt.getDate() + p_Interval);
							break;
					} 
					valuedate=date_format(dt,'','Y-m-d',false);
					//textdate=date_format(dt,'','',false);
						//dateArray[jcnt*2]=textdate;
						dateArray[jcnt]=valuedate;
						jcnt++;
				}
				return dateArray;
			}					
		
		function get_service_detail_view(service_id,vw,mn,yr,dy){ 
			var service_params="";
			if(!vw) vw='<?php echo SERVICES_CALENDAR_STYLE; ?>';
			if(!mn)mn=""; 
			if(!yr)yr="";
			if(!dy)dy="";
			if(document.getElementById("service_detail_view_display"))	document.getElementById("service_detail_view_display").style.visibility='';
			service_params="&view="+vw+"&month="+mn+"&year="+yr+"&day="+dy+"&service_id="+service_id;
			command="<?php echo tep_href_link(FILENAME_SERVICES_INFO,'command=service_detail_view','NONSSL',true,false);?>"+service_params;
			do_get_command(command);
		}
		function do_result(result){
			if(result!="") result_splt=result.split('{{{}}}');
			if(document.getElementById("service_detail_view_display")) document.getElementById("service_detail_view_display").style.visibility='hidden';
			service_element=document.getElementById("service_detail_view");
			if(service_element && result!='') service_element.innerHTML=result_splt[0];
			nb_dates_string=result_splt[1];
			add_start_times();
			add_end_times();
		}	
</script>
