<?php
//get shop page's title
$t = $site->pageTitle($s['shop_page']);
$seo_pageTitle = seo_str($t);
//if user doesnt have any items in cart, send back to cart page
$check = $mysql->num_rows($mysql->query("ID", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
if ($check == 0)
{
    header("Location: /{$s['shop_page']}/$seo_pageTitle/Basket");
    exit;
}
$shopSettings = $site->shop_settings('hide_delivery_section,termsNconditions_box_checkout,where_heard,where_heard_enable');
//save form
if (isset($_POST['first_name']))
{
    $form->cache_form();
    $post = $form->clean_fields($_POST);
    $form->check($post['first_name'], 'first name');
    $form->check($post['last_name'], 'last name');
    //$form->check($post['phone_number'], 'phone number');
    $form->check($post['email_address'], 'email address');
    if ($shopSettings['hide_delivery_section'] == 'n')
    {
        $form->check($post['address_line1'], 'address line 1');
        $form->check($post['town'], 'town');
        $form->check($post['postcode'], 'postcode');
        $form->check($post['dev_firstname'], 'Delivery first name');
        $form->check($post['dev_lastname'], 'Delivery last name');
    }
    if ($shopSettings['termsNconditions_box_checkout'] == 'y' && $post['termsNconditions'] != 'y')
    {
        $form->error[] = 'You must agree to our terms and conditions before you can place this order';
    }
    if (sizeof($form->error) == 0)
    {
        $post['country'] = (empty($post['country'])) ? 'AU' : $post['country'];
        $ip = $form->clean_fields($_SERVER['REMOTE_ADDR']);
        $user_agent = $form->clean_fields($_SERVER['HTTP_USER_AGENT']);
        $post['where_heard'] = ($post['where_heard'] == 'Select') ? '' : $post['where_heard'];
        $newsletter = ($post['newsletter'] == 'y') ? 'y' : 'n';
        //check if order is in db
        $check = $mysql->num_rows($mysql->query("ID", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'n' LIMIT 1"));
        if ($check == 0)
        {
            $check = $mysql->num_rows($mysql->query("ID", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
            if ($check == 0)
            {
                $mysql->insert("fs_shop_orderdata", "owner,temp,shipped,stamp", "{$_SESSION['owner']},y,n,".time());
            }
        }
        //update order
        $mysql->update("first_name,last_name,phone_number,email_address,address_line1,address_line2,town,state,postcode,country,company_name,dev_firstname,dev_lastname,dev_date,cardmsg,ip,user_agent,where_heard,newsletter,notes", "{$post['first_name']},{$post['last_name']},{$post['phone_number']},{$post['email_address']},{$post['address_line1']},{$post['address_line2']},{$post['town']},{$post['state']},{$post['postcode']},{$post['country']},{$post['company_name']},{$post['dev_firstname']},{$post['dev_lastname']},{$post['dev_date']},{$post['cardmsg']},$ip,$user_agent,{$post['where_heard']},$newsletter,{$post['notes']}", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1");
        header("Location: /{$s['shop_page']}/$seo_pageTitle/Payment");
        exit;
    }
    $error_msg = $form->show_error();
}
//order form data holder
$holder = $_SESSION['form'];
if (!is_array($_SESSION['form']))
{
    $q = $mysql->query("first_name,last_name,phone_number,email_address,address_line1,address_line2,town,state,postcode,country,company_name,dev_firstname,dev_lastname,dev_date,cardmsg,where_heard,newsletter,notes", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1");
    if ($mysql->num_rows($q) == 1)
    {
        $holder = $mysql->get_array($q);
        $holder = array_map('stripslashes', $holder);
        $holder = array_map('trim', $holder);
    }
}
//page html
$html = '<h1>Checkout</h1><p id="shopCrumbs"><a href="/'.$s['shop_page'].'/'.$seo_pageTitle.'">'.$t.'</a> &gt; <a href="/'.$s['shop_page'].'/'.$seo_pageTitle.'/Basket">Basket</a> &gt; <strong>Your information</strong></p>'.$error_msg.'<form action="" method="post" name="checkout" id="checkout">
<h2>Your details</h2>
<table border="0" cellspacing="0" cellpadding="0">
<tr><th>First name</th><td><input name="first_name" type="text" id="first_name" size="32" class="required" value="'.$holder['first_name'].'" /></td></tr>
<tr><th>Last name</th><td><input name="last_name" type="text" id="last_name" size="32" class="required" value="'.$holder['last_name'].'" /></td></tr>
<tr><th>Phone number</th><td><input name="phone_number" type="text" id="phone_number" size="32" value="'.$holder['phone_number'].'" /></td></tr>
<tr><th>Email address</th><td><input name="email_address" type="text" id="email_address" size="32" class="required validate-email" value="'.$holder['email_address'].'" /></td></tr>';
//where did you hear about us field
if ($shopSettings['where_heard_enable'] == 'y')
{
    $heard = str_replace("\n", ",", $shopSettings['where_heard']);
    $html .= '<tr><th>Where did you hear about us?</th><td><select name="where_heard" id="where_heard">'.$form->select_post_fix($heard, $heard, $holder['where_heard']).'</select></td></tr>';
}
//
$html .= '</table>';
if ($shopSettings['hide_delivery_section'] == 'n')
{
    $html .= '<h2>Delivery details</h2>
<table border="0" cellspacing="0" cellpadding="0">
<tr><th>Company name</th><td><input name="company_name" type="text" id="company_name" size="32" value="'.$holder['company_name'].'" /></td></tr>
<tr><th>Delivery first name</th><td><input name="dev_firstname" type="text" id="dev_firstname" size="32" class="required" value="'.$holder['dev_firstname'].'" /></td></tr>
<tr><th>Delivery last name</th><td><input name="dev_lastname" type="text" id="dev_lastname" size="32" class="required" value="'.$holder['dev_lastname'].'" /></td></tr>
<tr><th>Delivery address line 1</th><td><input name="address_line1" type="text" id="address_line1" size="32" class="required" value="'.$holder['address_line1'].'" /></td></tr>
<tr><th>Delivery address line 2</th><td><input name="address_line2" type="text" id="address_line2" size="32" value="'.$holder['address_line2'].'" /></td></tr>
<tr><th>Delivery suburb / town</th><td><input name="town" type="text" id="town" size="32" class="required" value="'.$holder['town'].'" /></td></tr>
<tr><th>Delivery state</th><td><input name="state" type="text" id="state" size="32" value="'.$holder['state'].'" /></td></tr>
<tr><th>Delivery postcode</th><td><input name="postcode" type="text" id="postcode" size="32" class="required" value="'.$holder['postcode'].'" /></td></tr>';
}
$list = $site->countryList();
$selected = (empty($holder['country'])) ? 'AU' : $holder['country'];
//if fastway or ego is being used as the shipping method, only allowed AU
$shippingMethod = $site->shop_settings('shipping_method,shipping_postoffice_use_express');
if ($shippingMethod['shipping_method'] == 'fastway' || $shippingMethod['shipping_method'] == 'ego')
{
    $list = ['code' => [0 => 'AU'], 'name' => ['AU' => 'Australia']];
}
if ($shopSettings['hide_delivery_section'] == 'n')
{
    $html .= '<tr><th>Delivery country</th><td><select name="country" id="country">'.$form->select_post_fix(implode(",", $list['code']), implode(",", $list['name']), $selected).'</select></td></tr>';
    //if site is using au post office
    if ($shippingMethod['shipping_method'] == 'postoffice' && $shippingMethod['shipping_postoffice_use_express'] == 'y')
    {
        $s = (empty($holder['notes'])) ? 'Standard' : $holder['notes'];
        $html .= '<tr><th>Delivery method</th><td><select name="notes">'.$form->select_post_fix('Standard,Express', 'Standard,Express', $s).'</select></td></tr>';
    }
    else
    {
        $html .= '<input type="hidden" name="notes" value="Standard" />';
    }
//
    $html .= '</table>';
}
//Misc
$settings = $site->shop_settings('show_delivery_date,show_cardmsg_field,termsNconditions_box_checkout');
if ($settings['show_delivery_date'] == 'y' || $settings['show_cardmsg_field'] == 'y' || $settings['termsNconditions_box_checkout'] == 'y')
{
    $html .= '<h2>Misc</h2>';
}
$html .= '<table border="0" cellspacing="0" cellpadding="0">';
//Delivery date
if ($settings['show_delivery_date'] == 'y')
{
    $html .= '<tr><th>Delivery date<br /><small>DD/MM/YYYY format</small></th><td><input type="text" id="dev_date" name="dev_date" class="required validate-date-au" size="12" value="'.$holder['dev_date'].'" /></td></tr>';
}
//card message / comments box
if ($settings['show_cardmsg_field'] == 'y')
{
    $val = $mysql->get_array($mysql->query("value", "fs_shop_settings", "`name` = 'custom_msg_field_lbl' LIMIT 1"));
    $html .= '<tr><th valign="top" id="msgFieldth">'.stripslashes($val['value']).'</th><td><textarea id="cardmsg" name="cardmsg" rows="3">'.$holder['cardmsg'].'</textarea></td></tr>';
}
//m2news newsletter checkbox
$settings = $site->shop_settings('m2news_signup_checkout,m2news_signup_checkout_group');
if ($settings['m2news_signup_checkout'] == 'y' && $settings['m2news_signup_checkout_group'] > 0)
{
    $checked = ($holder['newsletter'] == 'y') ? ' checked' : '';
    $checked = ($holder['newsletter'] == '') ? ' checked' : $checked;
    $html .= '<tr><th>&nbsp;</th><td><input type="checkbox" id="newsletter" name="newsletter" value="y" style="border:none;width:auto;"'.$checked.' /><label for="newsletter">I would like to subscribe to the newsletter</label></td></tr>';
}
//I agree to terms checkbox
$settings = $site->shop_settings('termsNconditions_box_checkout,termsNconditions_page_url');
if ($settings['termsNconditions_box_checkout'] == 'y')
{
    $checked = ($holder['termsNconditions'] == 'y') ? ' checked' : '';
    $html .= '<tr><th>&nbsp;</th><td><input type="checkbox" id="termsNconditions" name="termsNconditions" value="y" style="border:none;width:auto;"'.$checked.' /><label for="termsNconditions">I have read the <a href="'.$settings['termsNconditions_page_url'].'" target="_blank">terms and conditions</a></label></td></tr>';
}
//end of table
$html .= '<tr><th>&nbsp;</th><td><input type="submit" name="button" id="button" value="Next step" class="button" /></td></tr></table></form><script>new Validation(\'checkout\', {immediate:true});</script>';
//merge with template
$mainhtml .= stripslashes($crumbs.$html);
//add js in the head
$mainhtml = str_replace("</head>", '<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script><script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/scriptaculous/1.8.3/scriptaculous.js"></script><script type="text/javascript" src="/inc/shop/src/validation.js"></script></head>', $mainhtml);
//include this add-ons's overwrite css file from the template being used, if there is one
$t = settings('template');
if (file_exists('templates/'.$t['template'].'/addons/shop.css'))
{
    $mainhtml = str_replace("</head>", '<link rel="stylesheet" type="text/css" href="/templates/'.$t['template'].'/addons/shop.css" /></head>', $mainhtml);
}
else
{
    $mainhtml = str_replace("</head>", '<link rel="stylesheet" type="text/css" href="https://www.freesites.com.au/addons/shop.css" /></head>', $mainhtml);
}
//end
