<?php
//$u = $users->get('admin');
//$u->of(false);
//$u->pass = '1234560';
//$u->save();

function showDate($date, $type = 'normal')
{
    if (!$date) return '';
    $months = array(
        1 => _x('January', '-'),
        2 => _x('February', '-'),
        3 => _x('March', '-'),
        4 => _x('April', '-'),
        5 => _x('May', '-'),
        6 => _x('June', '-'),
        7 => _x('July', '-'),
        8 => _x('August', '-'),
        9 => _x('September', '-'),
        10 => _x('October', '-'),
        11 => _x('November', '-'),
        12 => _x('December', '-'),
    );
    $short_months = array(
        1 => _x('JAN', '-'),
        2 => _x('FEB', '-'),
        3 => _x('MAR', '-'),
        4 => _x('APR', '-'),
        5 => _x('MAY', '-'),
        6 => _x('JUN', '-'),
        7 => _x('JUL', '-'),
        8 => _x('AUG', '-'),
        9 => _x('SEP', '-'),
        10 => _x('OCT', '-'),
        11 => _x('NOV', '-'),
        12 => _x('DEC', '-'),
    );
    if (!is_int($date)) $date = strtotime($date);
    list($y, $m, $d) = explode('-', date('Y-m-d', $date));
    $m = (int)$m;
    if ($type == 'normal') $date = $d . ' ' . $months[$m] . ' ' . $y;
    else if ($type == "noyear") $date = '<strong>' . $d . '</strong> ' . $months[$m];
    else if ($type == "short") $date = $d . ' ' . $short_months[$m];
    return $date;
}

$action = $input->post('action');

if ($action) :
    $result = array('success' => null, 'errors' => array());
    if ($session->CSRF->validate()) :
        if ($action == "subscription") :
            $fields = array('type', 'name', 'surname', 'position', 'website', 'email', 'company', 'phone', 'mobile');
            foreach ($fields as $field):
                if ($field == "email") :
                    $$field = $sanitizer->email($input->post($field));
                else:
                    $$field = $sanitizer->text($input->post($field));
                endif;
                if (empty($$field)) $result['errors'][$field] = _x('Field is required', '-');
            endforeach;
            if (count($result['errors']) == 0):

                require_once('../modules/mail/mail.php');
                $caption = _x('Subscription', '-');
                $content = _x('Type', '-') . ": $type<br/>"
                    . _x('Name', '-') . ": $name<br/>"
                    . _x('Surname', '-') . ": $surname<br/>"
                    . _x('Position', '-') . ": $position<br/>"
//                    . _x('Address', '-') . ": $address<br/>"
                    . _x('Website', '-') . ": $website<br/>"
                    . _x('Email', '-') . ": $email<br/>"
                    . _x('Company', '-') . ": $company<br/>"
                    . _x('Phone', '-') . ": $phone<br/>"
                    . _x('Mobile', '-') . ": $mobile<br/>";
                Mail::send($caption, $content, array('Ashraf.Hajiyev@siar.az'));

                $result['success'] = _x('Congratulations! Your order has been sent!', '-');
            endif;
        elseif ($action == "contacts") :
            $fields = array('name', 'price', 'time', 'email');
            foreach ($fields as $field):
                if ($field == "email") :
                    $$field = $sanitizer->email($input->post($field));
                else:
                    $$field = $sanitizer->text($input->post($field));
                endif;
                if (empty($$field)) $result['errors'][$field] = _x('Field is required', '-');
            endforeach;
            if (count($result['errors']) == 0):

                require_once('../modules/mail/mail.php');
                $caption = _x('Subscription', '-');
                $content = _x('Name', '-') . ": $name<br/>"
                    . _x('Price', '-') . ": $price<br/>"
                    . _x('Time', '-') . ": $time<br/>"
                    . _x('Email', '-') . ": $email";
                /*$content = "Hello I am $name please get back to me about $price. "
                    . "You can reach me $time via $email."
                    . "Looking forward headring from you soon";*/
                Mail::send($caption, $content, array('Ashraf.Hajiyev@siar.az'));

                $result['success'] = _x('Congratulations! Your order has been sent!', '-');
            endif;
        elseif ($action == 'shopper'):
            require_once('../modules/mail/mail.php');
            $caption = _x('Mystery shopper', '-');
            $content = "<div>Name: " . $input->post('name') . "</div>";
            $content .= "<div>Surname: " . $input->post('surname') . "</div>";
            $content .= "<div>Gender: " . $input->post('gender') == 'm' ? 'Male' : 'Female' . "</div>";
            $content .= "<div>Age: " . $input->post('age') . "</div>";
            $content .= "<div>Education: " . $input->post('education') == 'm' ? 'Master' : 'Bachelor' . "</div>";
            $content .= "<div>Marital status: " . $input->post('marital') == 'm' ? 'Married' : 'Single' . "</div>";
            $content .= "<div>Position: " . $input->post('position') . "</div>";
            $content .= "<div>Smartphone: " . $input->post('smartphone') . "</div>";
            $content .= "<div>Laptop: " . $input->post('laptop') . "</div>";
            $content .= "<div>City: " . $input->post('city') . "</div>";
            $content .= "<div>Home phone: " . $input->post('homephone') . "</div>";
            $content .= "<div>Mobile: " . $input->post('mobile') . "</div>";
            $content .= "<div>Email: " . $input->post('email') . "</div>";
            //Mail::send($caption, $content, array('Ashraf.Hajiyev@siar.az'));
            Mail::send($caption, $content, array('fuad@jis.az'));

            $result = array('success' => true, 'errors' => array());
        endif;
    endif;

    echo json_encode($result);
    exit;
endif;

$tokenName = $this->session->CSRF->getTokenName();
$tokenValue = $this->session->CSRF->getTokenValue();
$CSRF_STR = '<input type="hidden" id="_post_token" name="' . $tokenName . '" value="' . $tokenValue . '"/>';


$home = $pages->findOne('template=home');
$bodyClass = '';
if ($page->id == $home->id) $bodyClass = 'home-page';
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php echo $page->title; ?> | Siar</title>
    <base href="<?php echo $config->urls->templates; ?>"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta content="All" name="robots">
    <meta name="Author" content="JIS">
    <link id="ico" rel="shortcut icon" href="img/favicon.ico"/>
    <link rel="stylesheet" href="style/robo.css" type="text/css">
    <link rel="stylesheet" href="style/style.css" type="text/css">
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDYeaLfYR3NVRVbChwsRjxzLinIlBxToUo"></script>

    <script type="text/javascript" src="js/robo.js"></script>
    <script type="text/javascript" src="js/scripts.js"></script>
    <?php if (in_array($page->template, array('about', 'industries', 'library'))): ?>
        <link rel="stylesheet" href="style/swiper.css">
        <script src="js/swiper.min.js"></script>
    <?php endif; ?>
</head>
<body class="<?php echo $bodyClass; ?>">
<header>
    <div class="full_head">
        <div class="logo">
            <a href="<?php echo $home->url; ?>"></a>
        </div>
        <ul class="menu">
            <?php
            foreach ($home->children() as $item) :
                echo '<li'
                    . ($item->id == $page->id || $item->id == $page->closest('template=' . $item->template)->id ? ' class="active"' : '')
                    . '><a href="' . $item->url . '">' . $item->title . '</a></li>';
            endforeach;
            ?>
        </ul>
        <div class="footer">
            <sppan><?php echo sprintf(_x('© %s SIAR - All rights reserved', '-'), date('Y')); ?></sppan>
            <a href="http://www.jis.az/" class="jis" target="_blank">Site by <b>Jeykhun Imanov Studio</b> </a>
        </div>
    </div>
    <div class="icon_head">
        <div class="logo">
            <span></span>
        </div>
        <i class="icon-menu"></i>
    </div>
</header>