<?php
  if(!isset($_SESSION)){
	session_start();
  }

  include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/sessionStartGeneral.php");
  
  require_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/class.brdata.inc.php");
 
  
  if(isset($loggedIn) && $loggedIn === true){
    $brdataObj = new brdata();
  
    $theUser = [];
    $customerReceipts = [];
    
    $theUser = $brdataObj->getUser($_SESSION['FrequentShopperNum']);
    $theUserRoles = $brdataObj->getUserRoles($_SESSION['FrequentShopperNum']);
	$securityQuestions = $brdataObj->getSecurityQuestions();
	$availableSubscriptions = $brdataObj->getAvailableEmailSubscriptions();

    require_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/class.rewards.inc.php");
    
	$rewardsUserObj = new rewards($theUser['Phone']);
    list($userCredit, $userPoints, $accountError) = $rewardsUserObj->getUserCreditPoints($theUser['Phone']);

    //build Customer Subscriptions Available select option markup
    /* $custSubscriptnAvail = array();
    $customerSubscriptionsAvailable = $rewardsUserObj->getCustomerSubscriptionsAvailable();
    $customerSubscriptionsAvailableCount = count($customerSubscriptionsAvailable);    
    
    $customerSubscriptions = $rewardsUserObj->getCustomerSubscriptions();
    $customerSubscriptionsCount = count($customerSubscriptions); */

//helper function determines if element is already in array

/* function array_keys_exist($keys, $array){
foreach($keys as $key){
if(!array_key_exists($key, $array))return false;
}
return true;
}

if (isset($customerSubscriptionsAvailable[0]['DisplayName']))
{
foreach($customerSubscriptionsAvailable as $available)
{
$custSubscriptnAvail[] = $available['DisplayName'];
}
} */



/* if (isset($customerSubscriptions[0]['DisplayName']))
{
foreach($customerSubscriptions as $subscription)
{
if (!array_keys_exist($subscription, $custSubscriptnAvail))
{
$custSubscriptnAvail[] = $subscription['DisplayName'];
}
}
} */

/* $counter = 1;
$csaHTML = ""; */

//$custSubscriptnAvail foreach loop to generate option tag HTML markup

/* if (count($custSubscriptnAvail) > 0) {
    foreach($custSubscriptnAvail as $csa)
    {
    $csaHTML .= '<option value="' . $counter . '">'. $csa .'</option>';
    $counter++;
    }
}
else {

$csaHTML = '<option value="0">' ."You have no reward program options" .'</option>';
} */

}else{

    /* if ($_SERVER['SERVER_NAME'] == "valleythayne-local")
    {
    header("Location: http://". $_SERVER['HTTP_HOST'] ."/login.php?l=false" );
    }
    else {
    header("Location: https://" . $_SERVER['SERVER_NAME'] . "/login.php?l=false");
        } */

        if ($_SERVER['SERVER_NAME'] == "valleythayne-local")
    {
        header("Location: http://". $_SERVER['HTTP_HOST'] . "/about-rewards.php" );
        die();
    }
    else {
        header("Location: https://" . $_SERVER['SERVER_NAME'] . "/about-rewards.php");
        die();
    }
}

$theError = false;
?>
<?php
  $pageTitle = "User Information";
  $pageDescription = "Your user information";
  $pageKeywords = "";
?>
<?php
  include_once($_SERVER['DOCUMENT_ROOT'] . "/common/header.php"); 
?>

<style>
.myForm {
    display: flex;
    background: beige;
    /* padding: 1.2em; */
    /* width: 36em; */
}

.myForm-2 {
    display: flex;
    background: beige;
    /* padding: 1.2em; */
    /* width: 35em; */
}

@media only screen and (max-width: 768px) {

    html,
    body {
        height: 100% !important;
        width: 100% !important;
        display: contents !important;
        padding-left: 20px !important;
        margin: 5px !important;
    }

    .textarea {
        width: auto;
        max-width: auto;
        background-color: #eee;
        overflow: hidden;
        height: 150px;
        max-height: 150px;
        border: 1px solid #ccc;
        overflow-y: auto;
    }

    .alignButtons {
        text-align: center;
    }

    .wrapper {
        max-width: 100% !important;
        /* margin: 10px auto 0 auto !important; */
        margin: auto;
        margin-left: 10px;
        margin-right: 10px;
        display: block !important;
        padding: 15px !important;
    }

    .form-row {
        /* padding: 10px 0; */
        display: flex;
        flex-wrap: wrap;
        flex-basis: 85%;
    }

    .form-row label {
        padding-right: 10px;
        flex: 1;
        flex-basis: 40%;
    }

    .form-row input {
        flex: 1;
        flex-basis: 45%;
    }

    #customerReceiptsbtn {
        height: 25px !important;
        padding-bottom: 25px !important;
        margin-bottom: 15px !important;
        margin-top: 15px !important;
    }

    #updateAccountbtn {
        /*       margin-top: 10ps;
        margin-bottom: 10px; */
        margin: 0px !important;
        padding: 0px !important;
        margin-bottom: 10px !important;
        height: 25px !important;
    }

    #changePWbtn1 {
        /* margin-bottom: 10px; */
        margin: 0px !important;
        padding: 0px !important;
        margin-bottom: 25px !important;
        height: 25px !important;
    }

    #changeSecurity {
        /* margin-bottom: 10px; */
        margin: 0px !important;
        padding: 0px !important;
        height: 25px !important;
    }

    .imgButton {
        margin-bottom: 15px !important;
    }

}

@media only screen and (min-width: 768px) {

    /*  .wrapper {
        display: grid;
        grid-template-areas: "customerInfo customerOptions other";
        grid-template-columns: auto .1fr .1fr;
        grid-template-rows: auto;
        grid-column-gap: 8px;
        margin: 5px;
    } */

    html,
    body {
        height: 100% !important;
        width: 90% !important;
        display: contents !important;
        padding-left: 20px !important;
        margin: 20px;
    }

    .wrapper {

        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -webkit-box-pack: justify;
        -ms-flex-pack: justify;
        justify-content: space-between;
        -webkit-box-orient: vertical;
        -webkit-box-direction: normal;
        -ms-flex-direction: column;
        flex-direction: column;
        margin-left: 70px;
        height: 100% !important;

    }

    .row {
        display: flex;
        flex-direction: row;
        /* flex-wrap: wrap; */
        width: 90%;
        /*  justify-content:center;
        text-align:center; */
    }

    .column {
        display: flex;
        flex-direction: column;
        flex-basis: 100%;
        flex: 1;
        align-item: center;
    }

    div.controls {
        height: 20px;
    }

    .form-row {
        /* padding: 10px 0; */
        display: flex;
        flex-wrap: wrap;
        flex-basis: 100%;
        height: 20px;
    }

    .form-row label {
        padding-right: 10px;
        flex: 1;
        height: 20px;
    }

    .form-row input {
        flex: 1;
    }

    .alignButtons {
        text-align: center;
    }

    .myForm-2 label input {
        margin-right: 10px;
    }

    div#changePWcontain,
    div#changeSecurityContain {
        width: 200px;
    }

    input#oldpassword,
    input#newPW,
    input#securityanswer,
    select#securityquestion.input-xlarge {
        width: 180px;
    }


    .column-a {
        display: flex;
        flex-direction: column;
        flex: 30%;
        max-width: 30%;
        padding: 0 4px;
        justify-content: space-between;
        flex-wrap: wrap;
        margin-right: 20px;
        margin-left: 10px;
        background-color: beige;
    }

    .column-b {
        display: flex;
        flex-direction: column;
        flex: 30%;
        max-width: 30%;
        padding: 0 4px;
        justify-content: space-between;
        flex-wrap: wrap;
        margin-right: 20px;
        background-color: beige;
        /* max-height: 95% !important; */
    }

    .column-c {
        display: flex;
        flex-direction: column;
        flex: 30%;
        max-width: 30%;
        padding: 0 4px;
        justify-content: space-between;
        flex-wrap: wrap;
    }

    #colRewardsDDL {
        max-height: 35px;
    }

    #colProgramsDDL {
        max-height: 30px;

    }

    #colEmailSubscriptions {
        max-height: 300px !important;
        ;
        /* padding-top:5px !important;; */
        /* margin-top: 5px !important; */
    }

    #contentRow {
        max-height: 100%;
    }

    .sectionGraphics {
        max-height: 200px;
        /* padding-bottom: 10px !important; */
    }

    #customerReceiptsbtn {
        height: 25px !important;
    }

    #updateAccountbtn {
        /*       margin-top: 10ps;
        margin-bottom: 10px; */
        margin: 0px !important;
        padding: 0px !important;
        margin-bottom: 10px !important;
        height: 25px !important;
    }

    #changePWbtn1 {
        /* margin-bottom: 10px; */
        margin: 0px !important;
        padding: 0px !important;
        margin-bottom: 25px !important;
        height: 25px !important;
    }

    #changeSecurity {
        /* margin-bottom: 10px; */
        margin: 0px !important;
        padding: 0px !important;
        height: 25px !important;
    }

    .anchorTagPadding {
        padding: 0px !important;
        margin: 0px !important;
        margin-bottom: 5px !important;
    }

    .textarea {
        width: 100%;
        max-width: 100%;
        background-color: #eee;
        overflow: hidden;
        height: 60px;
        max-height: 60px;
        border: 1px solid #ccc;
        overflow-y: auto;
    }


}

/*end of @media only screen and (min-width: 768px)*/

.modal {
    display: none;
    /* Hidden by default */
    position: fixed;
    /* Stay in place */
    z-index: 1;
    /* Sit on top */
    padding-top: 100px;
    /* Location of the box */
    left: 0;
    top: 0;
    width: 100%;
    /* Full width */
    height: 100%;
    /* Full height */
    overflow: auto;
    /* Enable scroll if needed */
    background-color: rgb(0, 0, 0);
    /* Fallback color */
    background-color: rgba(0, 0, 0, 0.4);
    /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
    background-color: #fefefe;
    margin: auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
}

/* The Close Button */
.close {
    color: #aaaaaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

#changeSecurity {
    height: 25px !important;
}

/* .anchorTagPadding {

    padding: 0px !important;
    padding-bottom: 5px !important;
}
 */
</style>

<?php require($_SERVER['DOCUMENT_ROOT'] . '/common/mainNav.php'); ?>



<div class="wrapper">
    <div class="row" id="contentRow">

        <div class="column-a" style="font-family: Helvetica Neue, Helvetica, Arial, sans-serif; font-size: 12px;">

            <h3 style="color:#f79e1e;">Personal Information</h3>

            <div class="column">

                <div class="form-row">
                    <label for="firstname">First Name : </label>
                    <!--<input type="text" name="firstname" id="firstname" required>-->
                    <div class="controls">
                        <?php echo $theUser['FirstName']; ?>
                    </div>
                </div>

                <div class="form-row">
                    <label for="lastname">Last Name : </label>
                    <!--<input type="text" name="last_name" id="last_name" required>-->
                    <div class="controls">
                        <?php echo $theUser['LastName']; ?>
                    </div>
                </div>

                <div class="form-row">
                    <label for="mystore">My Store : </label>
                    <!--<input type="text" name="my_store" id="my_store" required>-->
                    <div class="controls">
                        <?php echo isset($_SESSION['MyStoreAFS']['store_name']) ? $_SESSION['MyStoreAFS']['store_name'] : '' ?>
                        (<?php echo $_SESSION['MyStore']; ?>)
                    </div>
                </div>

                <div class="form-row">
                    <label for="birthdate">Birth Date : </label>
                    <!--<input type="date" name="birth_date" id="birth_date" required>-->
                    <div class="controls">
                        <?php echo $theUser['Birthday']; ?>
                    </div>
                </div>

            </div>

            <h3 style="color:#f79e1e;">Contact Information</h3>
            <div class="column">

                <div class="form-row">
                    <label for="phone">Phone 1 :
                        <span class="hideObject text-error" id="phoneErrorMsg"> - Provide a phone number</span>
                    </label>
                    <!-- <input type="tel" name="phone_number" id="phone_number" required> -->
                    <div class="controls">
                        <?php echo '('.substr($theUser['Phone'], 0, 3).') '.substr($theUser['Phone'], 3, 3).'-'.substr($theUser['Phone'],6); ?>
                    </div>

                </div>

                <div class="form-row">

                    <label for="phone">Phone 2
                        <span class="hideObject text-error" id="phoneErrorMsg"> - Provide a phone number</span> :
                    </label>
                    <!-- <input type="tel" name="second_phone_number" id="second_phone_number" required> -->
                    <div class="controls">
                        <?php echo '('.substr($theUser['SecondaryPhone'], 0, 3).') '.substr($theUser['SecondaryPhone'], 3, 3).'-'.substr($theUser['SecondaryPhone'],6); ?>
                    </div>

                </div>

                <div class="form-row">

                    <label for="email">Email
                        <span class="hideObject text-error" id="emailErrorMsg"> - Provide an email</span> :
                    </label>
                    <!-- <input type="email" name="email_address" id="email_address" required> -->
                    <div class="controls">
                        <span id="userEmail"><?php echo $theUser['Email']; ?></span>
                    </div>
                </div>

            </div>

            <h3 style="color:#f79e1e;">Address Information</h3>
            <div class="column">

                <div class="form-row">
                    <label for="address1">Street Address : </label>
                    <!--<input type="text" name="street_address" id="street_address" required>-->
                    <div class="controls">
                        <?php echo $theUser['Address1']; ?>
                    </div>
                </div>

                <div class="form-row">
                    <label for="address2">Apt/Building : </label>
                    <!--<input type="text" name="apt_bldg" id="apt_bldg">-->
                    <div class="controls">
                        <?php echo $theUser['Address2']; ?>
                    </div>
                </div>

                <div class="form-row">
                    <label for="city">City : </label>
                    <!--<input type="text" name="city" id="city" required>-->
                    <div class="controls">
                        <?php echo $theUser['City']; ?>
                    </div>
                </div>

                <div class="form-row">
                    <label for="state">State : </label>
                    <!-- <input type="text" name="state" id="state" required> -->
                    <div class="controls">
                        <?php echo $theUser['State']; ?>
                    </div>
                </div>

                <div class="form-row">
                    <label for="zipcode">Zip Code : </label>
                    <!-- <input type="text" name="zip_code" id="zip_code" required> -->
                    <div class="controls">
                        <?php echo $theUser['ZipCode']; ?>
                    </div>
                </div>

            </div>
        </div>
        <div class="column-b" style="font-family: Helvetica Neue, Helvetica, Arial, sans-serif; font-size: 12px;">
            <h3 style="color:#f79e1e;">My Rewards</h3>
            <div class="column" id="colRewardsDDL">
                <?php
                if (!isset($theUser['RewardSubscriptions']) || count($theUser['RewardSubscriptions']) == 0)
                {               
                    echo 'Grocery Rewards';                   
                }
                else {                    
                echo $theUser['RewardSubscriptions'][0]['RewardSubscriptionName'];
                }
                ?>
            </div>

            <h3 style="color:#f79e1e;">Programs</h3>
            <div class="column" id="colProgramsDDL">
                <div class="control-group">
                    <?php                     
                    if (strlen($theUserRoles) == 0)
                    {
                        echo "User has no roles.";
                    }
                    else {
                        $userRolesArray = explode(',' , $theUserRoles );
                        foreach($userRolesArray as $userRole)
                        {
                            echo $userRole . '<br>';
                        }
                        // echo $_SESSION['userRoles']; 
                    }
                    ?>
                </div>
            </div>

            <h3 style="color:#f79e1e;">Email Subscriptions</h3>
            <div class="column" id="colEmailSubscriptions">            


                <?php
            if(isset($theUser['OptOutEmail']) && $theUser['OptOutEmail'] == "True"){
                $checkedStatus = "";
            }else{
                $checkedStatus = "checked";
            }
            ?>
                <!--
												<label class="checkbox">
													<input type="checkbox" name="optOutEmail" id="optOutEmail" value="false" <?php echo $checkedStatus; ?>> I want to receive email about members-only deals, special offers and savings events.
												</label>
												-->
                <?php
                foreach($availableSubscriptions as $subscription){
                    //check to see if the user is subscribed
                    $userSubscribed = false;
                    
                    if (isset($theUser['UserEmailSubscriptions']) ) {
                    foreach($theUser['UserEmailSubscriptions'] as $userSubscription){
                            if($userSubscription['EmailSubscriptionID'] == $subscription['EmailSubscriptionID']){
                                $userSubscribed = true;
                            }
                        }
                    }
            ?>
                <label class="checkbox">
                    <input type="checkbox" name="optOutEmail[]"
                        id="optOutEmail_<?php echo $subscription['EmailSubscriptionID']; ?>"
                        value="<?php echo $subscription['EmailSubscriptionID']; ?>"
                        <?php echo $userSubscribed ? 'checked' : ''; ?>>
                    <?php echo $subscription['EmailSubscriptionProgram']; ?>
                </label>
                <?php
                }
            ?>

                <a href="" class="btn btn-success btn-block hideObject" id="submitEmailSubscriptions">Save Email
                    Preferences</a>

                <p class="alert alert-success hideObject" id="optOutSuccess">You have updated your email options</p>
                <p class="alert alert-warning hideObject" id="optOutError">Something went wrong and your preference
                    couldn't
                    be saved</p>
                <p class="alert alert-info hideObject" id="processingEmailOpt">Processing your email subscription
                    preference
                </p>

            </div>


        </div>
        <div class="column-c" style="font-family: Helvetica Neue, Helvetica, Arial, sans-serif; font-size: 12px;">

            <a href="/includes/customer-receipts.php" id="customerReceiptsbtn" class="btn btn-primary btn-block">View
                Receipts</a>
            <!-- <a href="/customer-receipts.php" id="customerReceiptsbtn" class="btn btn-primary btn-block">View Receipts</a> -->

            <!-- <div class="sectionGraphics"> -->

            <a href="/rewards.php" id=>
                <img src="/Kickstrap/img/ValleyMarket_FuelSiteGraphics_ValleyRewards_Button.png"
                    alt="Thayne Valley Rewards button" class="imgButton">
            </a>

            <!-- </div> -->

            <p>

                <a href="/user-update" id="updateAccountbtn" class="btn btn-primary btn-block">Update Account
                    Information</a>

                <br>

                <a href="#" id="changePWbtn1" class="btn btn-primary btn-block">Change Password</a>

                <a href="#" id="changeSecurity" class="btn btn-primary btn-block">Change Security Question /
                    Answer</a>

                <div id="changePWcontain" class="hideObject" style="padding:19px;text-align:center;">
                    <span class="hideObject text-success" id="newPWSuccessMsg"></span>
                    <span class="hideObject text-error" id="newPWErrorMsg"></span>
                    <form method="POST" action="#" name="changePWform" id="changePWform" class="cf"
                        accept-charset="UTF-8">
                        <input type="hidden" name="phone" value="<?php echo $theUser['Phone']; ?>">
                        <input type="hidden" name="email" value="<?php echo $theUser['Email']; ?>">
                        <label class="control-label" for="oldpassword">Old Password: </label>
                        <input type="password" name="oldpassword" id="oldpassword">
                        <br>
                        <label class="control-label" for="newPW">New Password: </label>
                        <input type="password" name="newPW" id="newPW">
                        <br><br>
                        <div class="btn-group">
                            <button id="submitBtn" name="submitBtn" class="btn btn-primary btn-large">Submit</button>
                            <button id="submitCancel" name="submitCancel"
                                class="btn btn-default btn-large">Cancel</button>
                        </div>
                        <br><br><br>
                    </form>
                </div>



                <div id="changeSecurityContain" class="hideObject" style="padding:19px;text-align:center;">
                    <span class="hideObject text-success" id="securitySuccessMsg"></span>
                    <span class="hideObject text-error" id="securityErrorMsg"></span>
                    <form method="POST" action="#" name="changeSecurityForm" id="changeSecurityForm" class="cf"
                        accept-charset="UTF-8">
                        <input type="hidden" name="phone" value="<?php echo $theUser['Phone']; ?>">
                        <input type="hidden" name="frequentshoppernum"
                            value="<?php echo $_SESSION['FrequentShopperNum']; ?>">
                        <input type="hidden" name="email" value="<?php echo $theUser['Email']; ?>">
                        <div class="control-group">
                            <label class="control-label" for="securityquestion">Security Question</label>
                            <div class="controls">
                                <select id="securityquestion" name="securityquestion" class="input-xlarge"
                                    style="width:106%;">
                                    <?php echo $securityQuestions; ?>
                                </select>
                            </div>
                        </div>
                        <br>
                        <label class="control-label" for="securityanswer">Security Answer: </label>
                        <input type="text" name="securityanswer" id="securityanswer">
                        <br><br>
                        <div class="btn-group">
                            <button id="submitBtn" name="submitBtn" class="btn btn-primary btn-large">Submit</button>
                            <button id="submitCancelSecurity" name="submitCancelSecurity"
                                class="btn btn-default btn-large">Cancel</button>
                        </div>
                        <br><br><br>
                    </form>
                </div>

            </p>


        </div>

    </div>
</div>


<script>
$("[id^=optOutEmail]").on("touch click", function(e) {
    $("#submitEmailSubscriptions").removeClass("hideObject");
});

$("#submitEmailSubscriptions").on("touch click", function(e) {
    e.preventDefault();

    $("#submitEmailSubscriptions").addClass("hideObject");
    $("#processingEmailOpt").removeClass("hideObject");

    email = $("#userEmail").text();
    subscriptions = [];

    $("[id^=optOutEmail]").each(function() {

        $this = $(this);
        id = $this.val();
        checkedStatus = typeof $this.attr("checked") !== "undefined" ? true : false;

        //need to switch the checked status to match what brdata is expecting:
        //true means they are opted out
        //false means they are subscribed
        if (checkedStatus == true) {
            checkedStatus = "false";
        } else {
            checkedStatus = "true";
        }

        subscriptions.push({
            'emailsubscriptionid': id,
            'email': email,
            'optoutemail': checkedStatus
        });
    });
    subscriptions = JSON.stringify(subscriptions);

    $.ajax({
        type: "POST",
        url: "/admin/lookUp.php",
        data: {
            "action": "changeBrEmailOptIn",
            "subscription": subscriptions,
            "email": email,
        },
        dataType: "json",
        success: function(theResponse) {
            $("#processingEmailOpt").addClass("hideObject");

            if (theResponse.success === true) {
                $("#optOutSuccess").removeClass("hideObject");

                setTimeout(function() {
                    $("#optOutSuccess").addClass("hideObject");
                }, 3000);
            } else {
                //revert the check box and display the error alert
                if (checkedStatus === false) {
                    $this.prop('checked', '');
                } else {
                    $this.prop('checked', 'checked');
                }

                $("#optOutError").removeClass("hideObject");

                setTimeout(function() {
                    $("#optOutError").addClass("hideObject");
                }, 3000);
            }
        }
    });
});

$("#accountInformationContainer a").on("touch click", function(e) {
    e.preventDefault();

    $("#rewardsContentContainer .mainSection").addClass('hideObject');
    $("#accountDisplayContainer").removeClass('hideObject');
    $("#sideNavigation .well").removeClass('active');
    $("#accountInformationContainer").addClass("active");
});

/* $("#receiptsLink").on("touch click", function(e) {
    e.preventDefault();

    $("#rewardsContentContainer .mainSection").addClass('hideObject');
    $("#receiptsDisplayContainer").removeClass('hideObject');
    $("#sideNavigation .well").removeClass('active');
    $("#allReceiptsContainer").addClass("active");
}); */

//Receipts Code
/* $(".receiptLink").on("touch click", function(e) {
    e.preventDefault();

    var id = $(this).closest('.receiptLinkContainer').attr('id').replace('receiptLink_', '');

    $("#rewardsContentContainer .mainSection").addClass('hideObject');
    $("#receiptsDisplayContainer").removeClass('hideObject');
    $("#receiptsDisplayContainer .oneReceipt").addClass("hideObject");
    $("#fullReceipt_" + id).removeClass("hideObject");
    $("#sideNavigation .well").removeClass('active');
    $("#allReceiptsContainer").addClass("active");
}); */

$("#optOutEmail").change(function() {
    var optOutVal = "true";

    if (this.checked) {
        optOutVal = "false";
    }

    var email = $("#userEmail").text();
    var fsid = $("#frequentShopperID").data('fsid');

    $.ajax({
        type: "POST",
        url: "/admin/lookUp.php",
        data: "action=changeBrEmailOptIn&optOutEmail=" + optOutVal + "&email=" + email,
        //data: "action=changeBrEmailOptIn&optOutEmail=" + optOutVal + "&frqShopperNo=" + fsid,
        dataType: "json",
        success: function(theResponse) {

            if (theResponse.success === true) {
                $("#optOutSuccess").removeClass("hideObject");

                setTimeout(function() {
                    $("#optOutSuccess").addClass("hideObject");
                }, 3000);
            }
        }
    });
});


$("#changePWbtn1, #changePWbtn2").on("click", function(e) {
    e.preventDefault();
    $("#changePWcontain").removeClass("hideObject");
    $(this).addClass("hideObject");
});

$("#submitCancel").on("click", function(e) {
    e.preventDefault();
    $("#changePWcontain").addClass("hideObject");
    $("#changePWbtn1, #changePWbtn2").removeClass("hideObject");
});

$("#changePWform").on("submit", function(e) {
    e.preventDefault();

    $.ajax({
        type: "POST",
        url: "/admin/lookUp.php",
        data: "action=changeBrPW&" + $("#changePWform").serialize(),
        dataType: "json",
        success: function(theResponse) {
            if (theResponse.success === "true") {
                $("#newPWErrorMsg").addClass("hideObject");
                $("#newPWSuccessMsg").text("You have successfully changed your password")
                    .removeClass('hideObject');

                setTimeout(function() {
                    $("#newPWSuccessMsg").addClass("hideObject");
                    $("#changePWcontain").addClass("hideObject");
                    $("#changePWbtn").removeClass("hideObject");
                    $("#oldpassword, #newPW").val("");
                }, 3000);
            } else {
                $("#newPWSuccessMsg").addClass("hideObject");
                $("#newPWErrorMsg").text(theResponse.reason).removeClass('hideObject');
            }

        }
    });
});

$("#changeSecurity").on("click", function(e) {
    e.preventDefault();
    $("#changeSecurityContain").removeClass("hideObject");
    $(this).addClass("hideObject");
});

$("#submitCancelSecurity").on("click", function(e) {
    e.preventDefault();
    $("#changeSecurityContain").addClass("hideObject");
    $("#changeSecurity").removeClass("hideObject");
});

$("#changeSecurityForm").on("submit", function(e) {
    e.preventDefault();

    $.ajax({
        type: "POST",
        url: "/admin/lookUp.php",
        data: "action=updateSecurityInfo&" + $("#changeSecurityForm").serialize(),
        cache: false,
        dataType: "json",
        success: function(theResponse) {
            if (theResponse.success === "true") {
                $("#securityErrorMsg").addClass("hideObject");
                $("#securitySuccessMsg").text(
                        "You have successfully changed your security question and answer")
                    .removeClass('hideObject');

                setTimeout(function() {
                    $("#securitySuccessMsg").addClass("hideObject");
                    $("#changeSecurityContain").addClass("hideObject");
                    $("#changeSecurity").removeClass("hideObject");
                    $("#securityanswer").val("");
                }, 3000);
            } else {
                $("#securitySuccessMsg").addClass("hideObject");
                $("#securityErrorMsg").text(theResponse.reason).removeClass('hideObject');
            }

        }
    });
});




$("#addSecondaryUser").on("click", function(e) {
    e.preventDefault();
    $("#addSecondaryContain").removeClass("hideObject");
    $("#secondaryAction").text("Add");
    $("#addSecondaryForm").attr("name", "addSecondaryForm");
    $("#addSecondaryForm #secondaryUserID").remove();
    $("#secondaryPasswordContainer").removeClass("hideObject");
    $(this).addClass("hideObject");
});

$("#secondarySubmitCancel").on("click", function(e) {
    e.preventDefault();
    $("#addSecondaryContain").addClass("hideObject");
    $("#addSecondaryUser").removeClass("hideObject");
});

$("#addSecondaryForm").on("submit", function(e) {
    e.preventDefault();

    var theFormName = $(this).attr('name');

    var theAction = "";

    if (theFormName == "updateSecondaryForm") {
        theAction = "updateSecondaryUser";
    } else {
        theAction = "addSecondaryUser";
    }

    $.ajax({
        type: "POST",
        url: "/admin/lookUp.php",
        data: "action=" + theAction + "&" + $("#addSecondaryForm").serialize(),
        dataType: "json",
        success: function(theResponse) {
            if (theResponse.reload !== undefined) {
                location.reload();
            } else if (theResponse.success === true) {
                $("#secondaryErrorMsg").addClass("hideObject");
                $("#secondarySuccessMsg").text("You have successfully added a secondary user")
                    .removeClass('hideObject');

                setTimeout(function() {
                    location.reload();
                }, 1000);
            } else {
                $("#secondarySuccessMsg").addClass("hideObject");
                $("#secondaryErrorMsg").text(theResponse.reason + " - " + theResponse
                    .AppUserRegisterSecondaryUser.reason).removeClass('hideObject');
            }

        }
    });
});

$(".updateSecondaryUser").on("click touchstart", function(e) {
    e.preventDefault();
    $this = $(this);
    $thisTR = $this.closest("tr");

    var secondaryID = $thisTR.data('secondaryid');
    $("#addSecondaryContain").removeClass("hideObject");
    $("#secondaryAction").text("Update");
    $("#addSecondaryForm").attr("name", "updateSecondaryForm");
    $('<input>').attr({
        type: 'hidden',
        id: 'secondaryUserID',
        name: 'secondaryUserID',
        value: secondaryID
    }).appendTo('#addSecondaryForm');
    $("#secondaryPasswordContainer").addClass("hideObject");

    var fullName = $thisTR.find(".secondaryFullName").text();
    var firstName = fullName.split(" ")[0];
    var lastName = fullName.split(" ")[1];
    var email = $thisTR.find(".secondaryEmail").text();
    var phone = $thisTR.find(".secondaryPhone").text();

    $("#addSecondaryForm #firstname").val(firstName);
    $("#addSecondaryForm #lastname").val(lastName);
    $("#addSecondaryForm #secondaryEmail").val(email);
    $("#addSecondaryForm #secondaryPhone").val(phone);

    $("html, body").animate({
        scrollTop: $("#addSecondaryContain").offset().top
    }, 1000);
});

$(".deleteSecondaryUser").on("click touchstart", function(e) {
    e.preventDefault();
    $this = $(this);
    $thisTR = $this.closest("tr");

    var secondaryID = $thisTR.data('secondaryid');
    var fullName = $thisTR.find(".secondaryFullName").text();
    var email = $thisTR.find(".secondaryEmail").text();

    $("#modalUserName").html(fullName);
    $("#modalUserEmail").html(email);
    $("#deleteSecondaryConfirm").data('userid', secondaryID);

    $("#deleteSecondaryModal").modal("show");
});

$("#deleteSecondaryConfirm").on("click touchstart", function(e) {
    var secondaryID = $(this).data('userid');

    $.ajax({
        type: "POST",
        url: "/admin/lookUp.php",
        data: "action=deleteSecondaryUser&secondaryUserID=" + secondaryID,
        dataType: "json",
        success: function(theResponse) {
            if (theResponse.reload !== undefined) {
                location.reload();
            } else if (theResponse.success === true) {
                $("#secondaryDeleteErrorMsg").addClass("hideObject");
                $("#secondaryDeleteSuccessMsg").text(
                    "You have successfully deleted the secondary user").removeClass(
                    'hideObject');

                setTimeout(function() {
                    location.reload();
                }, 1000);
            } else {
                $("#secondaryDeleteSuccessMsg").addClass("hideObject");
                $("#secondaryDeleteErrorMsg").text(theResponse.reason + " - " + theResponse
                    .AppUserRegisterSecondaryUser.reason).removeClass('hideObject');
            }

        }
    });
});
</script>




<?php require($_SERVER['DOCUMENT_ROOT'] . '/common/footer.php'); ?>