<div class="menu-wrapper wrapper" id="checkoutpage">
    <div class="container-fluid <?php echo $this->config->item('website_style') == 'singlepage' ? 'inner' : 'inner-customer-dashboard' ?>">
        <h1 class="main section-title text-center">Checkout </h1>
        <div class="row">
            <div class="col-sm-4">
                <div class="checkout-cart-part" id="cartHolder">
                    <?php $this->load->view('onlineorder/cart_filler') ?>
                </div>
            </div>
            <div class="col-sm-8">
                <div class="checkout-information-part">
                    <h4 class="text-center text-uppercase"><?php $order_type == 'online_delivery' ? print 'Delivery Address' : print 'Collection Address' ?></h4>
                    <?php echo form_open('onlineorder/checkout_verification', array('id' => 'checkoutVerificationForm')) ?>
                    <?php if ($order_type == 'online_delivery'): ?>
                        <div class="form-group">
                            <label for="inputname">Name</label>
                            <input type="hidden" name="name" value="<?php echo $primary[0]->name ?>"/>
                            <input name="namealt" value="<?php echo $primary[0]->name ?>" type="text" class="form-control form-control-small input-sm"  placeholder="Enter name">
                        </div>
                        <div class="form-group">
                            <label for="inputname">Phone</label>
                            <input type="hidden" name="phone" value="<?php echo $primary[0]->phone ?>"/>
                            <input name="phonealt" value="<?php echo $primary[0]->phone ?>" type="text" class="form-control form-control-small" placeholder="Enter name">
                        </div>
                        <div style="border:1px solid #ccc;border-radius: 10px;">
                            <div style="padding: 10px;">
                                <div style="background: #F2F2F2; padding: 3px 10px; border-radius: 10px;">
                                    <h3 class="text-center"><strong>Current Delivery Address</strong></h3>
                                    <div class="row" id="chekoutHouseInformation">
                                        <div class="col-sm-4">
                                            <div class="form-group">
                                                <label for="checkout_house">House Number</label>
                                                <input name="house" value="<?php isset($delivery_address['house']) ? print $delivery_address['house'] : print $primary[0]->house ?>" type="text" class="form-control form-control-small check-primary" id="checkout_house">
                                            </div>
                                        </div>
                                        <div class="col-sm-4">
                                            <div class="form-group">
                                                <label for="checkout_floor_or_apt">Floor/Apt</label>
                                                <input name="floor_or_apt" value="<?php isset($delivery_address['floor_or_apt']) ? print $delivery_address['floor_or_apt'] : print $primary[0]->floor_or_apt ?>" type="text" class="form-control check-primary" id="checkout_floor_or_apt">
                                            </div>
                                        </div>
                                        <div class="col-sm-4">
                                            <div class="form-group">
                                                <label for="checout_building">Building</label>
                                                <input name="building"  value="<?php isset($delivery_address['building']) ? print $delivery_address['building'] : print $primary[0]->building ?>" type="text" class="form-control check-primary" id="checkout_building">
                                            </div>
                                        </div>
                                    </div>

                                    <div class="row">
                                        <div class="col-sm-4">
                                            <div class="form-group">
                                                <label for="checkout_street">Street</label>
                                                <input name="street" value="<?php isset($delivery_address['street']) ? print $delivery_address['street'] : print $primary[0]->street ?>" type="text" class="form-control form-control-small check-primary" id="checkout_street" placeholder="Enter Street address">
                                            </div>
                                        </div>
                                        <div class="col-sm-4">
                                            <div class="form-group">
                                                <label for="checkout_postcode">Postal Code</label>
                                                <input name="postcode" value="<?php echo $delivery_postcode ?>" type="text" class="form-control form-control-small check-primary" id="checkout_postcode" placeholder="Enter Postal Code">
                                            </div>
                                        </div>
                                        <div class="col-sm-4">
                                            <div class="form-group">
                                                <label for="checkout_city">City</label>
                                                <input name="city" value="<?php isset($delivery_address['city']) ? print $delivery_address['city'] : print $primary[0]->city ?>" type="text" class="form-control check-primary" id="checkout_city" placeholder="Enter city" >
                                            </div>
                                        </div>
                                        <input name="country" type="hidden" class="form-control" id="checout_country" value="<?php isset($delivery_address['country']) ? print $delivery_address['country'] != null ? $delivery_address['country'] : 'UK'  : print $primary[0]->country != null ? '' : 'UK'  ?>" readonly="true">
                                    </div>
                                </div>
                                <div class="row" style="margin: 10px auto;">
                                    <div class="checkbox col-sm-6">
                                        <label data-toggle="collapse" data-target="#ChangeDeliveryAddress">
                                            <input type="checkbox"> Do you want to change delivery address?
                                        </label>
                                    </div>
                                    <div class="collapse col-sm-6" id="ChangeDeliveryAddress">
                                        <select class="dropdown cl_op_field form-control form-control-small"  id="changeAddressID" onchange="changeAddress()">
                                            <option value="0" selected="true">Select Delivery Address</option>
                                            <option value="primary">Primary</option>
                                            <option value="add_new">Add New Address</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php else: ?>
                        <div class="form-group">
                            <label for="restaurantName">Restaurant Name</label>
                            <input type="text" class="form-control form-control-small input-sm" id="restaurantName" placeholder="Enter name" value="<?php echo $this->config->item('gk_name') ?>" disabled="true">
                        </div>
                        <div class="form-group">
                            <label for="inputname">Phone</label>
                            <input type="text" class="form-control form-control-small input-sm" id="inputname" placeholder="Enter name" value="<?php echo $this->config->item('gk_phone') ?>" disabled="true">
                        </div>
                        <div class="form-group" >
                            <label for="inputGift" class="control-label">Address</label>
                            <textarea class="form-control form-control-large" rows="2" disabled="true"><?php echo $this->config->item('gk_address') ?></textarea>
                        </div>
                    <?php endif; ?>
                    <div class="row">
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label for="DeliveryDate"><?php echo $order_type == 'online_collection' ? 'Collection' : 'Delivery' ?> Date</label>
                                <input type="text" class="form-control form-control-small input-sm" id="DeliveryDate" disabled="true" value="<?php echo date($this->config->item('dateformat'), strtotime('today')) ?>">
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label for="DeliveryTime"> <?php echo $order_type == 'online_collection' ? 'Collection' : 'Delivery' ?> Time</label>
                                <select class="form-control input-sm" name="delivery_time" style="color: #000000" onchange="getDeliveryTime(this.value)" id="deliveryTimeOption">
                                    <?php if (strtotime($delivery_time) <= strtotime("12:01 pm")): ?>
                                        <option value="12:01 pm" <?php $saved_delivery_time == '12:01 pm' ? print'selected="selected"' : '' ?>>12:00 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("12:05 pm")): ?>
                                        <option value="12:05 pm" <?php $saved_delivery_time == '12:05 pm' ? print'selected="selected"' : '' ?>>12:05 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("12:10 pm")): ?>
                                        <option value="12:10 pm" <?php $saved_delivery_time == '12:10 pm' ? print'selected="selected"' : '' ?>>12:10 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("12:15 pm")): ?>
                                        <option value="12:15 pm" <?php $saved_delivery_time == '12:15 pm' ? print'selected="selected"' : '' ?>>12:15 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("12:20 pm")): ?>
                                        <option value="12:20 pm"  <?php $saved_delivery_time == '12:20 pm' ? print'selected="selected"' : '' ?> >12:20 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("12:25 pm")): ?>
                                        <option value="12:25 pm"  <?php $saved_delivery_time == '12:25 pm' ? print'selected="selected"' : '' ?>>12:25 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("12:30 pm")): ?>
                                        <option value="12:30 pm"  <?php $saved_delivery_time == '12:30 pm' ? print'selected="selected"' : '' ?>>12:30 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("12:35 pm")): ?>
                                        <option value="12:35 pm"  <?php $saved_delivery_time == '12:35 pm' ? print'selected="selected"' : '' ?>>12:35 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("12:40 pm")): ?>
                                        <option value="12:40 pm"  <?php $saved_delivery_time == '12:40 pm' ? print'selected="selected"' : '' ?>>12:40 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("12:45 pm")): ?>
                                        <option value="12:45 pm"  <?php $saved_delivery_time == '12:45 pm' ? print'selected="selected"' : '' ?>>12:45 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("12:50 pm")): ?>
                                        <option value="12:50 pm"  <?php $saved_delivery_time == '12:50 pm' ? print'selected="selected"' : '' ?>>12:50 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("12:55 pm")): ?>
                                        <option value="12:55 pm"  <?php $saved_delivery_time == '12:55 pm' ? print'selected="selected"' : '' ?>>12:55 pm</option>
                                    <?php endif; ?>
                                    
                                    <?php if (strtotime($delivery_time) <= strtotime("12:00 pm")): ?>
                                        <option value="01:00 pm" <?php $saved_delivery_time == '01:00 pm' ? print'selected="selected"' : '' ?>>01:00 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:05 pm")): ?>
                                        <option value="01:05 pm" <?php $saved_delivery_time == '01:05 pm' ? print'selected="selected"' : '' ?>>01:05 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:10 pm")): ?>
                                        <option value="01:10 pm" <?php $saved_delivery_time == '01:10 pm' ? print'selected="selected"' : '' ?>>01:10 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:15 pm")): ?>
                                        <option value="01:15 pm" <?php $saved_delivery_time == '01:15 pm' ? print'selected="selected"' : '' ?>>01:15 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:20 pm")): ?>
                                        <option value="01:20 pm"  <?php $saved_delivery_time == '01:20 pm' ? print'selected="selected"' : '' ?> >01:20 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:25 pm")): ?>
                                        <option value="01:25 pm"  <?php $saved_delivery_time == '01:25 pm' ? print'selected="selected"' : '' ?>>01:25 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:30 pm")): ?>
                                        <option value="01:30 pm"  <?php $saved_delivery_time == '01:30 pm' ? print'selected="selected"' : '' ?>>01:30 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:35 pm")): ?>
                                        <option value="01:35 pm"  <?php $saved_delivery_time == '01:35 pm' ? print'selected="selected"' : '' ?>>01:35 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:40 pm")): ?>
                                        <option value="01:40 pm"  <?php $saved_delivery_time == '01:40 pm' ? print'selected="selected"' : '' ?>>01:40 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:45 pm")): ?>
                                        <option value="01:45 pm"  <?php $saved_delivery_time == '01:45 pm' ? print'selected="selected"' : '' ?>>01:45 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:50 pm")): ?>
                                        <option value="01:50 pm"  <?php $saved_delivery_time == '01:50 pm' ? print'selected="selected"' : '' ?>>01:50 pm</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:55 pm")): ?>
                                        <option value="01:55 pm"  <?php $saved_delivery_time == '01:55 pm' ? print'selected="selected"' : '' ?>>01:55 pm</option>
                                    <?php endif; ?>
                                     <?php if (strtotime($delivery_time) <= strtotime("02:00 pm")): ?>
                                        <option value="02:00 pm"  <?php $saved_delivery_time == '02:00 pm' ? print'selected="selected"' : '' ?>>02:00 pm</option>
                                    <?php endif; ?>    
                            
                                    <?php if ($time_slot): ?>
                                        <?php foreach ($time_slot as $aslot): ?>
                                            <?php if (strtotime($delivery_time) <= strtotime($aslot)): ?>
                                                <option value='<?php echo $aslot ?>'  <?php $saved_delivery_time == $aslot ? print'selected="selected"' : '' ?>><?php echo $aslot ?></option>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                    <?php endif; ?>   
                                                
                                    <?php if (strtotime($delivery_time) <= strtotime("00:05 am")): ?>
                                        <option value="00:05 am"  <?php $saved_delivery_time == '00:05 am' ? print'selected="selected"' : '' ?>>00:05 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("00:10 am")): ?>
                                        <option value="00:10 am"  <?php $saved_delivery_time == '00:10 am' ? print'selected="selected"' : '' ?>>00:10 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("00:15 am")): ?>
                                        <option value="00:15 am"  <?php $saved_delivery_time == '00:15 am' ? print'selected="selected"' : '' ?>>00:15 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("00:20 am")): ?>
                                        <option value="00:20 am"  <?php $saved_delivery_time == '00:20 am' ? print'selected="selected"' : '' ?>>00:20 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("00:25 am")): ?>
                                        <option value="00:25 am"  <?php $saved_delivery_time == '00:25 am' ? print'selected="selected"' : '' ?>>00:25 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("00:30 am")): ?>
                                        <option value="00:30 am"  <?php $saved_delivery_time == '00:30 am' ? print'selected="selected"' : '' ?>>00:30 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("00:35 am")): ?>
                                        <option value="00:35 am"  <?php $saved_delivery_time == '00:35 am' ? print'selected="selected"' : '' ?>>00:35 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("00:40 am")): ?>
                                        <option value="00:40 am"  <?php $saved_delivery_time == '00:40 am' ? print'selected="selected"' : '' ?>>00:40 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("00:45 am")): ?>
                                        <option value="00:45 am"  <?php $saved_delivery_time == '00:45 am' ? print'selected="selected"' : '' ?>>00:45 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("00:50 am")): ?>
                                        <option value="00:50 am" <?php $saved_delivery_time == '00:50 am' ? print'selected="selected"' : '' ?>>00:50 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("00:55 am")): ?>
                                        <option value="00:55 am"  <?php $saved_delivery_time == '00:55 am' ? print'selected="selected"' : '' ?>>00:55 am</option>
                                    <?php endif; ?>  
                                    <?php if (strtotime($delivery_time) <= strtotime("01:00 am")): ?>
                                        <option value="01:00 am"  <?php $saved_delivery_time == '01:00 am' ? print'selected="selected"' : '' ?>>01:00 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:05 am")): ?>
                                        <option value="01:05 am" <?php $saved_delivery_time == '01:05 am' ? print'selected="selected"' : '' ?>>01:05 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:10 am")): ?>
                                        <option value="01:10 am" <?php $saved_delivery_time == '01:10 am' ? print'selected="selected"' : '' ?>>01:10 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:15 am")): ?>
                                        <option value="01:15 am" <?php $saved_delivery_time == '01:15 am' ? print'selected="selected"' : '' ?>>01:15 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:20 am")): ?>
                                        <option value="01:20 am" <?php $saved_delivery_time == '01:20 am' ? print'selected="selected"' : '' ?>>01:20 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:25 am")): ?>
                                        <option value="01:25 am" <?php $saved_delivery_time == '01:25 am' ? print'selected="selected"' : '' ?>>01:25 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:30 am")): ?>
                                        <option value="01:30 am" <?php $saved_delivery_time == '01:30 am' ? print'selected="selected"' : '' ?>>01:30 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:35 am")): ?>
                                        <option value="01:35 am" <?php $saved_delivery_time == '01:35 am' ? print'selected="selected"' : '' ?>>01:35 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:40 am")): ?>
                                        <option value="01:40 am" <?php $saved_delivery_time == '01:40 am' ? print'selected="selected"' : '' ?>>01:40 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:45 am")): ?>
                                        <option value="01:45 am" <?php $saved_delivery_time == '01:45 am' ? print'selected="selected"' : '' ?>>01:45 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:50 am")): ?>
                                        <option value="01:50 am" <?php $saved_delivery_time == '01:50 am' ? print'selected="selected"' : '' ?>>01:50 am</option>
                                    <?php endif; ?>
                                    <?php if (strtotime($delivery_time) <= strtotime("01:55 am")): ?>
                                        <option value="01:55 am" <?php $saved_delivery_time == '01:55 am' ? print'selected="selected"' : '' ?>>01:55 am</option>
                                    <?php endif; ?>      
                                    <?php if (strtotime($delivery_time) <= strtotime("02:00 am")): ?>
                                        <option value="02:00 am" <?php $saved_delivery_time == '02:00 am' ? print'selected="selected"' : '' ?>>02:00 am</option>
                                    <?php endif; ?>      
                                </select>
                            </div>
                        </div>

                    </div>
                    <div class="checkbox">
                        <label data-toggle="collapse" data-target="#promo">
                            <input type="checkbox"> I have a promo code
                        </label>
                    </div>
                    <div class="collapse" id="promo">
                        <div class="form-group">
                            <label for="inputpromo" class="control-label">Promo Code</label>
                            <div class="form-inline">
                                <input id="txtpromocode" <?php !empty($promotion) ? print'disabled' : print 'false' ?>  placeholder="Enter promo code" value="<?php !empty($promotion) ? print $promotion['code'] : print'' ?>" class="discount-input form-control" type="text">
                                <input value="<?php !empty($promotion) ? print 'Remove' : print'Apply' ?>" onclick="usepromocode(this.id)" type="button" id="PromotionBtn" class="btn btn-sm btn-primary gradient round">
                            </div>
                        </div>
                    </div>
                    <div class="checkbox">
                        <label data-toggle="collapse" data-target="#gift">
                            <input type="checkbox"> Any Special Information
                        </label>
                    </div>
                    <div class="form-group collapse" id="gift">
                        <label for="checkout_delivery_note" class="control-label">Your Message</label>
                        <textarea name="delivery_note" id="checkout_delivery_note" class="form-control form-control-large" rows="3"></textarea>
                    </div>
                    <div class="form-group">
                        <label>Payment Type</label>
                        <div class="row radio">
                            <div class="col-sm-12">
                                <label>
                                    <input data-error="#onlineMethodError"  value="cod" onchange="paymentType(this.value)" id="pmt-cod" name="online_method" <?php isset($online_method) && $online_method == 'cod' ? print 'checked="true"' : !isset($online_method) && empty($online_methods) ? print 'checked="true"' : print''  ?> type="radio">Cash
                                </label>
                            </div>
                        </div>
                        <?php if (!empty($online_methods)): ?>
                            <?php foreach ($online_methods as $onMethod): ?>
                                <div class="row radio">
                                    <div class="col-sm-12">
                                        <label>
                                            <input data-error="#onlineMethodError" <?php isset($online_method) && $online_method == $onMethod->title ? print 'checked="true"' : print'' ?>  onchange="paymentType(this.value)"  value="<?php echo $onMethod->title ?>" name="online_method" type="radio">Card
                                        </label>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                        <div id="onlineMethodError"></div>
                        <div class="row">
                            <div class="col-sm-12" style="padding: 10px 0px;">
                                <div class="col-sm-6" style="padding: 10px 0px;">
                                    <input class="btn btn-sm sharp" type="submit" value='Confirm Order' onclick="checkoutVerification('checkoutVerificationForm')" />
                                    &nbsp;&nbsp;&nbsp;&nbsp;<a href="<?php echo $this->config->item('website_style') == 'singlepage' ? base_url() . '#food-menu' : base_url() . 'onlineorder' ?>" class="btn btn-sm btn-alt sharp hidden-xs">Cancel to back menu</a>
                                </div>
                                <div class="col-sm-6 hidden-md hidden-sm hidden-lg" style="padding: 10px 0px;">
                                    <a href="<?php echo $this->config->item('website_style') == 'singlepage' ? base_url() . '#food-menu' : base_url() . 'onlineorder' ?>" class="btn btn-sm sharp btn-alt">Cancel to back menu</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php echo form_close(); ?>
                </div>
            </div>
        </div>
        <div class="row hr"><hr/></div>
    </div>
</div>
<script>
    $(document).ready(function () {
        loadCart('1');
    });
    function paymentType(value) {
        if ($.trim(value) !== 'cod') {
            processCCFee(value);
        } else {
            $.ajax({
                url: '<?php echo site_url('onlineorder/clear_ccfee') ?>',
                dataType: 'json',
                type: 'POST',
                success: function (output) {
                    if (output.success) {
                        loadCart('0');
                    }
                }
            });
        }
    }
    function processCCFee(gateway) {
        $.ajax({
            url: '<?php echo site_url('onlineorder/process_ccfee') ?>',
            dataType: 'json',
            type: 'POST',
            data: {
                gateway: gateway
            },
            success: function (output) {
                if (output.success) {
                    loadCart('0');
                }
            }
        });
    }
    function checkoutVerification(formId) {
        $('#' + formId).validate({
            submitHandler: function () {
                var myform = document.getElementById(formId);
                var formdata = new FormData(myform);
                $.ajax({
                    url: $('#' + formId).attr('action'),
                    data: formdata,
                    cache: false,
                    processData: false,
                    contentType: false,
                    type: 'POST',
                    dataType: 'json',
                    success: function (response) {
                        if (response.success) {
                            if (response.deliveryCondition) {
                                window.location.replace('<?php echo site_url('onlineorder/processing') ?>');
                            } else {
                                alert("Please meet up the delivery condition on minimum order amount for checkout");
                                window.setTimeout(function () {
                                    if (WrapperPage == 'singlepage') {
                                        window.location.replace('<?php echo base_url() . '#food-menu' ?>');
                                    } else {
                                        window.location.replace('<?php echo base_url() . 'onlineorder' ?>');
                                    }
                                }, 5000);
                            }

                        } else {
                            alert('Sorry! we do not provide delivery service this area');
                        }
                    }
                });
            },
            errorClass: "has-error",
            highlight: function (e) {
                $(e).closest('.form-group').addClass('has-error');
            },
            unhighlight: function (e) {
                $(e).closest('.form-group').removeClass('has-error');
            },
            rules: {
                namealt: {required: true, lettersonly: true},
                phonealt: {required: true, phone: true},
                city: "required",
                street: "required",
                postcode: {required: true, postcodeUK: true},
                delivery_time: "required",
                online_method: "required"
            },
            messages: {
                namealt: "<?php echo $this->lang->line('gkpos_valid_name_required') ?>",
                phonealt: "<?php echo $this->lang->line('gkpos_valid_phone_required') ?>",
                street: "<?php echo $this->lang->line('gkpos_customer_street_required') ?>",
                postcode: "<?php echo $this->lang->line('gkpos_customer_postcode_required') ?>",
                delivery_time: "Delivery Time is required field",
                online_method: "Please specify one of the payment methods"
            },
            errorPlacement: function (error, element) {
                errorPlacement(error, element);
            }
        });
    }


    function loadCart(line) {
        $.ajax({
            url: '<?php echo site_url("onlineorder/loadcart_checkout") ?>',
            type: "POST",
            success: function (output) {
                $('#cartHolder').html(output);
                if (typeof line !== 'undefined') {
                    $('.line').removeClass('current');
                    $('#line-' + line).addClass('cuurent');
                }
            }
        });
    }
    function increment_item(line) {
        $.ajax({
            url: '<?php echo site_url("onlineorder/increment_item") ?>',
            data: {
                line: line,
            },
            type: "POST",
            dataType: "json",
            beforeSend: function () {
                $('.please-wait').removeAttr('style').css({'display': 'inline'});
            },
            success: function (output) {
                $('.please-wait').removeAttr('style').css({'display': 'none'});
                if (output.success) {
                    loadCart(output.line);
                }
            }
        });
    }
    function reduce_item(line) {
        $.ajax({
            url: '<?php echo site_url("onlineorder/reduce_item") ?>',
            data: {
                line: line,
            },
            type: "POST",
            dataType: "json",
            beforeSend: function () {
                $('.please-wait').removeAttr('style').css({'display': 'inline'});
            },
            success: function (output) {
                $('.please-wait').removeAttr('style').css({'display': 'none'});
                if (output.success) {
                    loadCart(output.line);
                }
            }
        });
    }
    function remove_item(line) {
        $.ajax({
            url: '<?php echo site_url("onlineorder/remove_item") ?>',
            data: {
                line: line,
            },
            type: "POST",
            dataType: "json",
            beforeSend: function () {
                $('.please-wait').removeAttr('style').css({'display': 'inline'});
            },
            success: function (output) {
                $('.please-wait').removeAttr('style').css({'display': 'none'});
                if (output.success) {
                    loadCart(output.line);
                }
            }
        });
    }
    function getOrderType(orderType) {
        if (orderType == 'online_delivery') {
            openDeliveryPostcodeForm('checkout');
        } else {
            $.ajax({
                url: '<?php echo site_url("onlineorder/fixordertype") ?>',
                data: {
                    order_type: orderType
                },
                type: "POST",
                dataType: "json",
                success: function (output) {
                    if (output.success) {
                        window.location.reload();
                    }
                }
            });
        }
    }
    function changeAddress() {
        var address = document.getElementById("changeAddressID").value;
        if (address != '0') {
            if (address == 'add_new') {
                $.ajax({
                    url: '<?php echo site_url("onlineorder/reset_deliveryplan") ?>',
                    type: "POST",
                    success: function (output) {
                        var obj = $.parseJSON(output);
                        if (obj.success) {
                            $('.check-primary').removeAttr('readonly', true);
                            $('.check-primary').val('');
                            loadCart();
                        }
                    }
                });

            } else {
                if (address == 'primary') {
                    $.ajax({
                        url: '<?php echo site_url("onlineorder/get_primary_address") ?>',
                        type: "POST",
                        beforeSend: function () {
                            $('.please-wait').removeAttr('style').css({'display': 'inline'});
                        },
                        success: function (output) {
                            var obj = $.parseJSON(output);
                            if (obj.success) {
                                if (obj.deliveryCondtion) {
                                    $('.check-primary').val('');
                                    $('#checkout_street').val(obj.address.street);
                                    $('#checkout_city').val(obj.address.city);
                                    $('#checkout_postcode').val(obj.address.postcode);
                                    $('#checkout_house').val(obj.address.house);
                                    $('#checkout_floor_or_apt').val(obj.address.floor_or_apt);
                                    $('#checkout_building').val(obj.address.building);
                                    if ($.trim($('#checkout_street').val())) {
                                        $('#checkout_street.check-primary').attr('readonly', true);
                                    }
                                    if ($.trim($('#checkout_city').val())) {
                                        $('#checkout_city.check-primary').attr('readonly', true);
                                    }
                                    if ($.trim($('#checkout_postcode').val())) {
                                        $('#checkout_postcode.check-primary').attr('readonly', true);
                                    }
                                    if ($.trim($('#checkout_house').val())) {
                                        $('#checkout_house.check-primary').attr('readonly', true);
                                    }
                                    if ($.trim($('#checkout_floor_or_apt').val())) {
                                        $('#checkout_floor_or_apt.check-primary').attr('readonly', true);
                                    }
                                    if ($.trim($('#checkout_building').val())) {
                                        $('#checkout_building.check-primary').attr('readonly', true);
                                    }
                                    loadCart();
                                    $('.please-wait').removeAttr('style').css({'display': 'none'});
                                    if (!obj.deliveryPlan) {
                                        $('.check-primary').removeAttr('readonly');
                                        $('.check-primary').val('');
                                        ALERT_TITLE = "Warning!";
                                        alert('Sorry, We do not provide delivery service this area');
                                    }
                                } else {
                                    if (WrapperPage == 'singlepage') {
                                        window.location.replace('<?php echo base_url() . '#food-menu' ?>');
                                    } else {
                                        window.location.replace('<?php echo base_url() . 'onlineorder' ?>');
                                    }

                                }
                            } else {
                                $('.check-primary').removeAttr('readonly');
                                $('.check-primary').val('');
                            }

                        }
                    });
                } else {
                    $.ajax({
                        url: '<?php echo site_url("onlineorder/get_secondary_address") ?>',
                        type: "POST",
                        data: {
                            address: address
                        },
                        beforeSend: function () {
                            $('.please-wait').removeAttr('style').css({'display': 'inline'});
                        },
                        success: function (output) {
                            var obj = $.parseJSON(output);
                            if (obj.success) {
                                if (obj.deliveryCondtion) {
                                    $('#checkout_street').val(obj.address.street);
                                    $('#checkout_city').val(obj.address.city);
                                    $('#checkout_postcode').val(obj.address.postcode);
                                    $('#checkout_house').val(obj.address.house);
                                    $('#checkout_floor_or_apt').val(obj.address.floor_or_apt);
                                    $('#checkout_building').val(obj.address.building);
                                    $('.check-primary').attr('readonly', true);
                                    loadCart();
                                    $('.please-wait').removeAttr('style').css({'display': 'none'});
                                } else {
                                    window.location.replace('<?php echo base_url() . '#food-menu' ?>');
                                }

                            } else {
                                $('.check-primary').removeAttr('readonly');
                                $('.check-primary').val('');
                            }
                        }
                    });
                }

            }
        } else {
            $('.check-primary').removeAttr('readonly');
            $('.check-primary').val('');
        }
    }

    function getDeliveryTime(time) {
        $.ajax({
            url: '<?php echo site_url('onlineorder/manage_delivery_time') ?>',
            type: 'POST',
            dataType: 'json',
            data: {
                time: time
            },
            success: function (response) {
                $("#deliveryTimeOption option").each(function () {
                    if ($(this).text() == response.time) {
                        $(this).attr("selected", "selected");
                    } else {
                        $(this).removeAttr("selected");
                    }

                });
            }
        });
    }
</script>