<?php
    //echo '==>'.date("A", time()+28800).'--->'.date("h:i:s", time()+28800).'-//==>'.date("d-m-Y h:i:s A", time()+46800);
    $r_day = 0;
    if(date("A", time()+28800) == 'PM'){
        if(date("h:i:s", time()+28800) > '03:00:00'){
            $r_day = 1;
        }
    }
    if(Mage::getStoreConfig('excellence_custom/delivery/delivery_after') != ''){
        $final_delivery_day = Mage::getStoreConfig('excellence_custom/delivery/delivery_after');
    }
    
	
    ?>
    <?php
    $cur_date	= strtotime(date('m/d/Y 00:00:00', time()+28800));
    //echo '==>'.date('m/d/Y',$cur_date).'-->'.date("h:i:s", time()+28800);
    $end_date_p2 = strtotime("+$final_delivery_day day", $cur_date);
    $delivery_date = date('m/d/Y',$end_date_p2);
    //echo '==>'.$delivery_date;
    //$final_delivery_date = checkDeliverDate($delivery_date);
    
    //$cur_date	= strtotime(date('m/d/Y 00:00:00'));
    //$days_p2 = round(abs(strtotime($final_delivery_date) - $cur_date) / 86400);    
    
    /*function checkDeliverDate($date){
        //Get blocked dates
        $block_date_arr = array();
        $block_date = array();
        $block_date_arr = Mage::getStoreConfig('excellence_custom/delivery/block_date');
        if(count($block_date_arr) > 0){
            $block_date = explode(",", $block_date_arr);
        }
        
        $timestamp = strtotime($date);
        if(date("D",$timestamp) == 'Sun'){
            $new_date = strtotime("+1 day", $timestamp);
            $new_delivery_date = date('m/d/Y',$new_date);
            return checkDeliverDate($new_delivery_date);
        }elseif(in_array($date, $block_date)){
            $new_date = strtotime("+1 day", $timestamp);
            $new_delivery_date = date('m/d/Y',$new_date);
            return checkDeliverDate($new_delivery_date);
        }else{
            return $date;
        }
		$timestamp = strtotime($date); 
        if(in_array($date, $block_date)){
            $new_date = strtotime("+1 day", $timestamp);
            $new_delivery_date = date('m/d/Y',$new_date);
            return checkDeliverDate($new_delivery_date);
        }else{
            return $date;
        }
    }*/
    
    ?>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />

    <?php 
    $start = strtotime("today"); // your start/end dates here
    $end = strtotime("today + 3 years");
    
    //Get config setting
	$status = Mage::getStoreConfig('excellence_custom/delivery/delivery_status');
	$time_slots = Mage::getStoreConfig('excellence_custom/delivery/time_slot_1');
	$is_sunday = Mage::getStoreConfig('excellence_custom/delivery/delivery_sun');
	$block_date = Mage::getStoreConfig('excellence_custom/delivery/block_date');
	$delivery_after = Mage::getStoreConfig('excellence_custom/delivery/delivery_after'); 
	$delivery_msg = Mage::getStoreConfig('excellence_custom/delivery/delivery_msg');  
	
	//Check is sunday open or not and prepare the sunday array
	$blocked_days = '';
	$block_sun = '';
	if($is_sunday){
		$sunday = strtotime("sunday", $start);
		while($sunday <= $end) {
			$block_sun .= "'".date("d/m/Y", $sunday)."',";
			$sunday = strtotime("+1 weeks", $sunday);
		}
	}
	//$block_date = "'30/09/2014','01/10/2014','02/10/2014','03/10/2014','04/10/2014','05/10/2014','06/10/2014','07/10/2014'";
	//$blocked_days = "[".$block_sun.','.$block_date."]";
	$blocked_days = "[".$block_sun."]";
	
	
	//Prepare next 20 days working array 
	$start = strtotime("today");
	$dates=array();
	for($i = 1; $i<=20; $i++){		
		$new_day = date('d/m/Y', strtotime("+$i day", $start));
		$new_day_c = date('Y-m-d', strtotime("+$i day", $start)); 
		if($is_sunday){
			if(date("D",strtotime($new_day_c)) != 'Sun'){
				array_push($dates,"'".$new_day."'");
			}
		}else{
			array_push($dates,"'".$new_day."'");	
		}
	}
	/////////////////////////////////////////////////////
	/////////////// Only for live ///////////////////////
	////////////////////////////////////////////////////
	/*$block_dates = explode(",", $block_date);
	if(count($block_dates) > 0){
		$common_elements = array_intersect($dates, $block_dates);
		$final_dates = array_merge(array_diff($dates,$common_elements));
	}else{
		$final_dates = $dates;
	}*/
	$final_dates = $dates;
	
	//Find out the delivery date
	$delivery_date = $final_dates[$delivery_after];	
	
	//echo '<pre>';print_r($final_dates);
	
	$dateArr = explode('/', $delivery_date);	
	$final_delivery_date = date('d/m/Y', mktime(00, 00, 00, $dateArr[1], substr($dateArr[0],1), substr($dateArr[2],0,-1)));
	  
	//Calculate the days of delivery from today
	$daylen = 60*60*24;  
	$date2	= date('Y-m-d', time());
    $date1 =  date('Y-m-d', mktime(00, 00, 00, $dateArr[1], substr($dateArr[0],1), substr($dateArr[2],0,-1)));
	//$date1 =  date('Y-m-d', mktime(00, 00, 00, 10, 7, 2014));
	  
	$days_p2 = (strtotime($date1)-strtotime($date2))/$daylen;
    ?>

<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
    
  jQuery(function() {
    var unavailableDates =<?php echo  $blocked_days ;?>;
    function unavailable(date) {
		day = (date.getDate() < 10) ? '0'+date.getDate() : date.getDate();
		mon = ((date.getMonth() + 1) < 10) ? '0'+(date.getMonth() + 1) : (date.getMonth() + 1);		
        dmy = day + "/" + mon + "/" + date.getFullYear();
        if (jQuery.inArray(dmy, unavailableDates) == -1) {
            return [true, ""];
        } else {
            return [false, "", "Unavailable"];
        }
    }
    
    jQuery( "#delivery_date" ).datepicker({ 
            dateFormat: "dd/mm/yy", 
            minDate: +<?php echo $days_p2; ?>, 
            beforeShowDay: unavailable,
			onSelect: function(date) {
			  jQuery('#selected_delivery_date').val(date);
			}			
        });
    });
  
  //Set system generated date to form for further use.
  setTimeout("jQuery('.ui-state-active').click();", 1500);
  
  
</script>
  <style>
#ui-datepicker-div {
	font-size: 10px;
	font-family: "Trebuchet MS", "Arial", "Helvetica", "Verdana", "sans-serif";
}    
    table {
	font-size: 1em;
}

.demo-description {
	clear: both;
	padding: 10px;
	font-size: 1.3em;
	line-height: 1.4em;
}

.ui-draggable, .ui-droppable {
	background-position: top;
}
.ui-datepicker-trigger{
    margin-top: 7px;
}
.ui-datepicker-today span{ color:red !important; }
  </style>
<script>	
	jQuery(document).ready(function() {
        jQuery("#free_shipping_method").click(function() {
			jQuery('.lbl-title').html('Self-Collection Date');
			jQuery('.del-msg-cus').hide();
			
			jQuery('#del-tm').hide('required-entry');
			jQuery('#del-lo').hide('required-entry');
			jQuery('#timeslot').removeClass('required-entry');
			jQuery('#location').removeClass('required-entry');
			
			jQuery(".col-main").mCustomScrollbar("update");
			
			jQuery('#co-billing-form .form-list li.control').hide();
		});
		jQuery("#flat_shipping_method").click(function() {
			jQuery('.lbl-title').html('Delivery Date');
			jQuery('.del-msg-cus').show();
			
			jQuery('#del-tm').show('required-entry');
			jQuery('#del-lo').show('required-entry');
			jQuery('#timeslot').addClass('required-entry');
			jQuery('#location').addClass('required-entry');
			
			jQuery(".col-main").mCustomScrollbar("update");			
			
			jQuery('#co-billing-form .form-list li.control').show();
		});
		
		jQuery('#checkout-step-login .col2-set .col-1 .buttons-set #onepage-guest-register-button').click(function() {
			if(jQuery('#free_shipping_method:checked').val()){
				jQuery('#free_shipping_method').click();
			}else{
				jQuery('#flat_shipping_method').click();
			}			
		});
    });	
	setTimeout("jQuery('#free_shipping_method').click();", 1000);
</script>
<form id="co-excellence-form" action="">     
	<div class="del-box"> 
		<div id="self-col-box">
        <input type="radio" name="excellence[shipping_method]" id="free_shipping_method" checked="checked" value="freeshipping_freeshipping">
			<label><?php echo $this->__('Self-Collection') ?></label>			
		</div>
		<?php if($status){ ?>
		<div id="del-cho-box">
		
        <input type="radio" name="excellence[shipping_method]" id="flat_shipping_method" value="flatrate_flatrate">	
			<label><?php echo $this->__('Delivery') ?></label>					
		</div>
		<?php } ?>
        <div style="clear:both;"></div>
        <span class="del-msg-cus"><em>*</em><?php echo $delivery_msg; ?></span>
		<div class="del-date-box">
			
			<div class="del-bx">
				<span class="lbl-title"></span>
				<div id="delivery_date"></div>
			</div>
		</div>
	</div> 
<fieldset>
    <ul class="form-list">
    <li id="excellence-form">
        <fieldset>
            <ul>
                <li class="wide">                    
                    <div class="input-box" id="del-tm">
						<label for="excellence:like" class="required"><em>*</em><?php echo $this->__('Time Slot') ?></label>
                        <select class="required-entry" name="excellence[timeslot]" id="timeslot">
                            <option value=''><?php echo $this->__('Please Choose..');?></option>
                            <?php $timeslots = explode(';',$time_slots); ?>
							<?php if(count($timeslots) > 0){?>
								<?php foreach($timeslots as $k=>$v){ ?>
								<option value='<?php echo $v;?>'><?php echo $v;?></option> 
								<?php } ?>
							<?php } ?>
                        </select>
                    </div>
                </li>
				<li class="wide">                    
                    <div class="input-box" id="del-lo">
						<label for="excellence:like" class="required"><em>*</em><?php echo $this->__('Location') ?></label>
                        <select class="required-entry" name="excellence[location]" id="location">
                            <option value=''><?php echo $this->__('Please Choose..');?></option>
                            <option value='Within CBD'><?php echo $this->__('Within CBD');?></option> 
							<option value='Outside CBD'><?php echo $this->__('Outside CBD');?></option> 
							<option value='Multiple Floors Per Building'><?php echo $this->__('Multiple Floors Per Building');?></option> 
                        </select>
						<p class="note"><em>*</em>Postal codes starting with 01,02,03,04,05,06,07,08 are within CBD.</p>
                    </div>
                </li>
            </ul>
        </fieldset>
     </li>
    </ul>
    <div class="buttons-set" id="excellence-buttons-container">
        <p class="required"><?php echo $this->__('* Required Fields') ?></p>
        <button type="button" title="<?php echo $this->__('Continue') ?>" class="button" onclick="excellence.save()"><span><span><?php echo $this->__('Continue') ?></span></span></button>
        <span class="please-wait" id="excellence-please-wait" style="display:none;">
            <img src="<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif') ?>" alt="<?php echo $this->__('Loading next step...') ?>" title="<?php echo $this->__('Loading next step...') ?>" class="v-middle" /> <?php echo $this->__('Loading next step...') ?>
        </span>
    </div>
</fieldset>
<input type="hidden" name="excellence[delivery_date]" id="selected_delivery_date">
</form>
<script type="text/javascript">
//<![CDATA[
    var excellence = new ExcellenceMethod('co-excellence-form','<?php echo $this->getUrl('custom/onepage/saveExcellence') ?>');
    var excellenceForm = new VarienForm('co-excellence-form');
//]]>
</script>
