<?php
if ($_GET['start'] == 1)
{
	$hour_name = 'hour_from';
	$minnute_name = 'minute_from';
	
	$hour_default = date('H') + 1;
} else {
	$hour_name = 'hour_to';
	$minnute_name = 'minute_to';
	if(isset($rent_by_day))
	{
		$hour_default = date('H') + 1;
	}else{
		$hour_default = date('H') + 2;
	}
}
$selected_hour = $hour_default;
if ($_GET['change'] == "date")
{
	$selected_hour = (isset($FORM[$hour_name]) && strlen($FORM[$hour_name]) > 0) ? $FORM[$hour_name] : $hour_default;
} else if (isset($_GET['hour'])) {
	$selected_hour = $_GET['hour'];
}

$skip_hour = $skip_minute = array();
if (isset($tpl['day_limit']) && !empty($tpl['day_limit']))
{
	$start = $tpl['day_limit']['start'];
	$end = $tpl['day_limit']['end'];
	
	// 24h
	if ($start == '00:00:00' && $start == $end)
	{
		$end = '23:59:59';
	}
	
	list($start_hour, $start_minute,) = explode(":", $start);
	list($end_hour, $end_minute,) = explode(":", $end);
	
	$start_time = strtotime($start);
	$end_time = strtotime($end);
	
	$condition = NULL;
	foreach (range(0,23) as $i)
	{
		if ($_GET['start'] == 1)
		{
			$condition = ($i < $start_hour || $i > $end_hour || ($i == $end_hour && (int) $end_minute === 0));
		} else {
			$condition = ($i < $start_hour || $i > $end_hour /*|| ($i == $start_hour && (int) $start_minute === 55)*/);
		}
		
		if ($condition)
		{
			$skip_hour[] = $i;
		}
	}

	

	if ($start_hour > $selected_hour || $end_hour < $selected_hour)
	{
		$selected_hour = $start_hour;
	}
	
	for ($i = 0; $i < 60; $i += 5)
	{
		$time = strtotime($selected_hour.":".$i.":00");
		
		$condition = ($time < $start_time || $time >= $end_time);
		
		if ($condition)
		{
			$skip_minute[] = $i;
		}
	}
}

$time_ampm = 0;
if(strpos($tpl['option_arr']['o_time_format'], 'a') > -1)
{
	$time_ampm = 1;
}
if(strpos($tpl['option_arr']['o_time_format'], 'A') > -1)
{
	$time_ampm = 2;
}
$index = pjObject::escapeString($_GET['index']);
if ($_GET['change'] == "date")
{
	?>
	<div class="col-xs-4">
		<?php
		if($time_ampm == 0)
		{
			echo pjTime::factory()
				->attr('id', $hour_name . '_' . $index)
				->attr('name', $hour_name)
				->attr('class', 'form-control erFormField')
				->prop('selected', $selected_hour)
				->prop('skip', $skip_hour)
				->hour();
		}else{
			?>
			<select id="<?php echo $hour_name?>_<?php echo $index;?>" name="<?php echo $hour_name;?>" class="form-control erFormField">
				<?php
				foreach (range(0, 23) as $v)
				{
					if (in_array($v, $skip_hour)) continue;
					$hour_label = $v;
					if (strlen($v) == 1)
					{
					    $v = '0' . $v;
					}
					if((int) $v < 12)
					{
					    if($time_ampm == 1)
					    {
					        $hour_label = $v . ' am';
					    }else if($time_ampm == 2){
					        $hour_label = $v . ' AM';
					    }
					}else{
					    $map = array(13 => '01',14 => '02',15 => '03',16 => '04',17 => '05',18 => '06',19 => '07',20 => '08',21 => '09',22 => '10',23 => '11');
					    if($time_ampm == 1)
					    {
					        $hour_label = (int) $v == 12 ? $v . ' pm' : $map[$v] . ' pm';
					    }else if($time_ampm == 2){
					        $hour_label = (int) $v == 12 ? $v . ' PM' : $map[$v] . ' PM';
					    }
					}
					?><option value="<?php echo $v;?>"<?php echo !is_null($selected_hour) && $v == $selected_hour ? ' selected="selected"' : NULL;?>><?php echo $hour_label;?></option><?php
				}
				?>
			</select>
			<?php	
		}
		?>
	</div><!-- /.col-xs-3 -->
	<div class="col-xs-3 pjErItemMinute">
	<?php
}

	echo pjTime::factory()
		->attr('id', $minnute_name . '_' . $index)
		->attr('name', $minnute_name)
		->attr('class', 'form-control erFormField')
		->prop('step', 5)
		->prop('selected', (isset($FORM[$minnute_name]) ? $FORM[$minnute_name] : 0))
		->prop('skip', $skip_minute)
		->minute();

if ($_GET['change'] == "date")
{
	?></div><!-- /.col-xs-3 --><?php
}
?>