<?php
error_reporting(E_ERROR | E_PARSE);

if (!class_exists('Authenticate')) require_once("common_cadline_libraries/crm_test.class.php");

$session        = isset($_GET['session']) ? $_GET['session'] : '';
$authCode       = isset($_GET['authCode']) ? $_GET['authCode'] : '';
$decodedSession = base64_decode($session);

if (!crm_hardlock::isEmergencyServer($_GET['secondary'])) {
    $auth = new Authenticate();
    $auth->setAuthCode($authCode);
    $auth->setAuthSession($session);
    $auth->auth();
}

$ctrID = isset($_GET['cnt']) ? $_GET['cnt'] : '';
$param = isset($_GET['param']) ? $_GET['param'] : '';

$btnClass       = 'blue';
$bodyClass      = 'default-body';
$privatekey     = Webform::GetWebformPrivateKey();
$decoded_param  = Webform::decode($privatekey, $decodedSession, $param); // Adat decode-olása
$decoded_param  = "?{$decoded_param}";
$ex             = Webform::get_valueFromStringUrl($decoded_param, "ex");
$trial          = Webform::get_valueFromStringUrl($decoded_param, "trial");
$seatID         = Webform::get_valueFromStringUrl($decoded_param, "seatid");
$se             = Webform::get_valueFromStringUrl($decoded_param, "se");
$custom_login   = Webform::get_valueFromStringUrl($decoded_param, "custom");
$dealers        = crm_hardlock::GetDealers();
$trialRequest   = crm_hardlock::getTrialRequest($seatID);

if (!crm_hardlock::isEmergencyServer($_GET['secondary']))
    $resource = new Resource($ctrID);

$linkClass = $se == 'LT' ? '' : 'yellow-txt';

if ($ctrID == 36) {
    $lost_name  = 'https://www.archline.hu/lost-user-name/';
    $lost_pwd   = 'https://www.archline.hu/elfelejtett-jelszo/';
    $favicon    = 'favicon_1.ico';
    $webPage    = 'https://www.archline.hu/';
    $lang       = 'hu-HU';
    $langFile   = 'hun';
    $error      = CRM_Error::serverDownHun;

    if (!crm_hardlock::isEmergencyServer($_GET['secondary'])) {
        $link = preg_replace("/\[LINK1\]([^\[]+)\[\/LINK1\]/", "<a class='{$linkClass}' href='https://www.archline.hu/jogi/adatvedelmi-nyilatkozat' target='_blank'>$1</a>", $resource->getDlgString('10758'));
        $link = preg_replace("/\[LINK2\]([^\[]+)\[\/LINK2\]/", "<a class='{$linkClass}' href='https://www.archline.hu/oktatas/workshopok/regisztracios-es-lemondasi-feltetelek' target='_blank'>$1</a>", $link);
    }
} else {
    $lost_name  = 'https://www.archlinexp.com/lost-user-name/';
    $lost_pwd   = 'https://www.archlinexp.com/lost-password/';
    $favicon    = 'favicon_2.ico';
    $webPage    = 'https://www.archlinexp.com/';
    $lang       = 'en-GB';
    $langFile   = 'eng';
    $error      = CRM_Error::serverDownEng;

    if (!crm_hardlock::isEmergencyServer($_GET['secondary']))
        $link = preg_replace("/\[LINK1\]([^\[]+)\[\/LINK1\]/", "<a class='{$linkClass}' href='https://www.archlinexp.com/legal/privacy-policy' target='_blank'>$1</a>", $resource->getDlgString('10758'));
}

if ($se == 'LT') {
    $btnClass   = 'lt-btn';
    $logo       = '/images/2022/02/07/logo.png';
    $bodyClass  = 'lt-body';
    $inputFocus = 'lt-input-focus';
} else {
    $btnClass   = 'custom-btn';
    $logo       = 'https://www.archlinexp.com/public/Logo_corner_up.png';
}

if ($_SERVER['SERVER_NAME'] == 'www.archline.hu')
    $captcha = '6Lc8nh8TAAAAAHn4ubhvltDcpcIyGqK7JKltJ0-0';
else
    $captcha = '6LeqnB8TAAAAAJXgoIg0gtTxwsjM4d22jZIs74hS';

$headerString = '11024';
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sign in</title>

    <link href="https://www.archlinexp.com/public/img/icons/<?= $favicon ?>" rel="shortcut icon" type="image/vnd.microsoft.icon" />
    <link rel="stylesheet" href="https://www.archline.hu/maintenance/css/bootstrap.min.css" />
    <link rel="stylesheet" href="public/css/style.css?time=<?= time() ?>" />

    <script src="https://www.archlinexp.com/media/jui/js/jquery.min.js"></script>
    <script src="https://www.archlinexp.com/media/plg_captcha_recaptcha/js/recaptcha.min.js?b2802ea557ed706aee0213f83ec66152" type="text/javascript"></script>
    <script src="https://www.google.com/recaptcha/api.js?onload=JoomlaInitReCaptcha2&render=explicit&hl=<?= $lang ?>" type="text/javascript"></script>
</head>

<body class="<?= $bodyClass ?>">
    <a href="<?= $webPage ?>" target="_blank"><img src="<?= $logo ?>" style="margin-top: 1% !important;" /></a>

    <div class="container">
        <?php if (crm_hardlock::isEmergencyServer($_GET['secondary'])) : ?>
            <h1 id="al_error"><?= $error ?></h1>
        <?php else : ?>
            <div class="regContainer form-div" style="margin-top: -50px;">
                <div id="alert-div" class="alert alert-danger"></div>

                <form id="regForm" action="progress.php" method="POST">
                    <input type="hidden" name="action" value="get_trial" />
                    <input type="hidden" name="session" value="<?= $session ?>" />
                    <input type="hidden" name="ctrID" value="<?= $ctrID ?>" />
                    <input type="hidden" name="seatID" value="<?= $seatID ?>" />
                    <input type="hidden" name="se" value="<?= $se ?>" />
                    <input type="hidden" name="prod" value="<?= $_GET['prod'] ?>" />
                    <input type="hidden" name="ver" value="<?= $_GET['ver'] ?>" />

                    <div class="header-div">
                        <h2><?= $resource->getDlgString('12107') ?></h2>
                    </div>

                    <div class="row">
                        <div class="form-group col-md-6">
                            <input id="jform_firstname" data-id="reg-data" type="text" class="form-control <?= $inputFocus ?>" placeholder="<?= $resource->getDlgString('10739') ?>" name="firstname" value="<?= $trialRequest['firstname'] ?>" required />
                        </div>

                        <div class="form-group col-md-6">
                            <input id="jform_lastname" data-id="reg-data" type="text" class="form-control <?= $inputFocus ?>" placeholder="<?= $resource->getDlgString('10756') ?>" name="lastname" value="<?= $trialRequest['lastname'] ?>" required />
                        </div>
                    </div>

                    <div class="form-group">
                        <input id="jform_email" data-id="reg-data" type="email" class="form-control <?= $inputFocus ?>" placeholder="<?= mb_substr($resource->getDlgString('5496'), 0, -1) ?>" name="email" value="<?= $trialRequest['email'] ?>" required />
                    </div>

                    <div class="form-group">
                        <input id="jform_phone" data-id="reg-data" type="text" class="form-control <?= $inputFocus ?>" placeholder="<?= $resource->getDlgString('11174') ?>" name="phone" value="<?= $trialRequest['phone'] ?>" required />
                    </div>

                    <div class="form-group">
                        <select name="distID" class="form-control" onmousedown="if(this.options.length>6){this.size=6;}" onchange='this.size=0;' onblur="this.size=0;" required>
                            <option value="">------ <?= $resource->getDlgString('11003') ?> ------</option>
                            <?php foreach ($dealers as $dealer) : ?>
                                <option value="<?= $dealer['id'] ?>" <?= $dealer['id'] == $trialRequest['distID'] ? 'selected' : '' ?>>
                                    <?= $dealer['name'] ?>
                                </option>
                            <?php endforeach; ?>
                            <option value="-1"><?= $resource->getDlgString('11012') ?></option>
                        </select>
                    </div>

                    <div class="checkbox">
                        <label><input type="checkbox" value="1" name="jform[gdpr][]" required /><?= $link ?></label>
                    </div>

                    <?php if ($_SERVER['REMOTE_ADDR'] != '106.120.112.154'): ?>
                        <div class="control-group">
                            <div class="controls">
                                <div id="jform_captcha" class="g-recaptcha required" data-sitekey="<?= $captcha ?>" data-theme="light" data-size="normal" required></div>
                            </div>
                        </div>
                    <?php endif; ?>

                    <input id="regBtn" type="submit" name="regSubmit" value="<?= $resource->getDlgString('12108') ?>" class="<?= $btnClass ?> reg-text" />
                </form>
            </div>
        <?php endif; ?>
    </div>

    <script>
        $(document).ready(function() {
            $("#jform_firstname").focus();

            $("#regForm").submit(function() {
                let email = $("#jform_email").val();

                if (!validateEmail(email)) {
                    $("#alert-div").html("<b><?= $resource->getDlgString('12106') ?></b>");
                    $("#alert-div").show();
                    return false;
                }

                return true;
            });

            const validateEmail = (email) => {
                return email.match(
                    /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
                );
            };
        });
    </script>
</body>

</html>