<?php
  if(!isset($_SESSION)){
	session_start();
  }
  
  $httpsVersion = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http';
  
  
  include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/sessionStartGeneral.php");
  
  require_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/class.brdata.inc.php");
  require_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/class.rewards.inc.php");

  $brdataObj = new brdata();
  $availableSubscriptions = $brdataObj->getAvailableEmailSubscriptions();
  $theError = false;
  
  if(isset($_GET['update']) && $_GET['update'] == "true"){
	if(!isset($_SESSION['LoggedIn']) || !isset($_SESSION['FS_JWT']) || !isset($_SESSION['LoggedIn'])){
    //   header("Location: https://" . $_SERVER['SERVER_NAME'] . "?error=noAccess");
    echo "<script>location.href='index.php';</script>";
	}
	
	$update = true;
	$theUser = $brdataObj->getUser($_SESSION['FrequentShopperNum']);
    $stores = $brdataObj->getStores();
    $rewardSubscriptions = $brdataObj->getRewardSubscriptions();
	
	if(isset($_POST['whichForm']) && $_POST['whichForm'] == "update"){
	  $theResult = $brdataObj->updateUser();
	  
	  if(isset($theResult['success']) && $theResult['success'] === true){
        // header("Location: " . $httpsVersion . "://" . $_SERVER['SERVER_NAME'] . "/account.php");
        echo "<script>location.href='account.php';</script>";
	  }else{
		$theError = true;
		
		if($theResult['errorCode'] == "000000004"){
		  $errorMessage = "An unknown error occured.";
		}else if($theResult['errorCode'] == "000000003"){
		  $errorMessage = $theResult['reason'];
		}else if($theResult['errorCode'] == "13"){
		  $errorMessage = "Server Error. Please try again in a few minutes.";
		}else{
		  if(isset($theResult['reason'])){
			$errorMessage = $theResult['reason'];
		  }else{
			$errorMessage = "Unknown error occured.";
		  }
		}
	  
	  }
	}
	
  }else{
	if(!isset($_POST['whichForm']) && isset($_SESSION['LoggedIn']) && isset($_SESSION['Username']) && $_SESSION['LoggedIn'] === 1){
    
    //   header("Location: " . $httpsVersion . "://" . $_SERVER['HTTP_HOST'] . "/account.php");
      //echo "<h1>YES, YOU ARE LOGGED IN</h1>";
      echo "<script>location.href='account.php';</script>";
	  die();
	}
	$update = false;
	
	if(isset($_POST['whichForm']) && $_POST['whichForm'] == "register"){
	  if(!empty($_POST['email']) && !empty($_POST['password'])){
		$theResult = $brdataObj->registerUser();
		
		if(isset($theResult) && $theResult === true){

        $rewardSubscriptions = $brdataObj->getRewardSubscriptions(); 

        if ($_SERVER['SERVER_NAME'] == "valleythayne-local")
        {
            header("Location: http://". $_SERVER['HTTP_HOST'] . "/account.php" );
        }
        else {
            header("Location: https://" . $_SERVER['SERVER_NAME'] . "/account.php");
        }
         
          die();
          
		}else{
		  $theUser = [];
		  $theUser['FirstName'] = $_POST['firstname'];
		  $theUser['LastName'] = $_POST['lastname'];
		  $theUser['Address1'] = $_POST['address1'];
		  $theUser['Address2'] = $_POST['address2'];
		  $theUser['City'] = $_POST['city'];
		  $theUser['ZipCode'] = $_POST['zipcode'];
		  $theUser['Phone'] = $_POST['phone'];
		  $theUser['SecondaryPhone'] = $_POST['secondaryPhone'];
		  $theUser['Birthday'] = $_POST['birthdate'];
		  $theUser['Email'] = $_POST['email'];
		  $theUser['UserEmailSubscriptions'] = $_POST['optOutEmail'];
          $theUser['OptOutEmail'] = $_POST['optOutEmail'];
          $theUser['RewardSubscriptions'] = $_POST['rewardSubscriptions'];
		  
		  $securityQuestions = $brdataObj->getSecurityQuestions();
          $stores = $brdataObj->getStores();
          $rewardSubscriptions = $brdataObj->getRewardSubscriptions();
		  
		  $theError = true;
		  
		  if(isset($theResult['errorCode']) && isset($theResult['reason'])){
			if($theResult['errorCode'] == 14){
			  //User already created
              $errorMessage = $theResult['reason'];
              
            //   header("Location: " . $httpsVersion . "://" . $_SERVER['SERVER_NAME'] . "/login?user=y");
            echo "<script>location.href=/login.php?user=y;</script>";

			}else{
			  $errorMessage = $theResult['reason'];
			}
		  }else{
			$errorMessage = "Unknown error occured.";
		  }
		}
	  }
	}else{
		$theError = false;
		$securityQuestions = $brdataObj->getSecurityQuestions();
        $stores = $brdataObj->getStores();
        $rewardSubscriptions = $brdataObj->getRewardSubscriptions();
	}
  }
  
  
  
?>
<?php
  $pageTitle = "User Registration";
  $pageDescription = "Signup for our Valley Market site";
  $pageKeywords = "About Us, Wyoming, Fresh Foods, Grocery Store, Supermarket, Local, Low Price, Great Service, Weekly Specials";
?>
<?php
  include_once($_SERVER['DOCUMENT_ROOT'] . "/common/header.php"); 
?>

<script src='https://www.google.com/recaptcha/api.js'></script>
<script src="/Kickstrap/js/jquery-ui-1.9.0.custom.min.js"></script>


<style>
/* html,
body {
    height: 100% !important;
    width: 98% !important;
    display: contents !important;
    padding-left: 20px !important;
} */

.box {
    /*background-color: #444;*/
    /*background-color: #dcdcdc;*/
    /*color: #fff;*/
    border-radius: 5px;
    padding: 10px;
    /*font-size: 150%;*/
}

.headerSection {
    margin-left: 20px;
    margin-right: 20px;
}

.myForm>* {
    font: 1.4em 'Consolas', sans-serif;
}



/* .myForm>label,
.myForm>fieldset {
    grid-column: labels;
    grid-row: auto;
}

.myForm>input,
.myForm>select,
.myForm>textarea,
.myForm>button {
    grid-column: controls;
    grid-row: auto;
    padding: .4em;
    border: 0;
} */

/*.myForm>fieldset {
    grid-column: span 2;
}*/

/*.myForm>button {
    background: darkkhaki;
    color: white;
}*/

/*.myForm>textarea {
    min-height: 3em;
}*/

/* .myForm {
    display: grid;
    grid-template-columns: [labels] auto [controls] 1fr;
    grid-auto-flow: row;
    grid-gap: .8em .5em;
    background: beige;
    padding: 1.2em;
    width: 36em;
}

.myForm-2 {
    display: grid;
    grid-template-columns: 1fr;
    grid-auto-flow: row;
    grid-gap: .8em .5em;
    /*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-left: 10px !important;
    }

    body #contentWrapper {
        margin-left: 20px !important;
        padding-left 20px !important;
        margin-right: 20px !important;

    }

    .alignButtons {
        /* text-align: center; */
    }

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

    .pageHeader {
        text-align: center;
    }

    .alignButtons {
        text-align: center;
        /*display: flex;
        align-items: center;
        justify-content: center;*/
    }

    /*     div#termsBtnContainer.control-group {
        display: flex;
        justify-content: center;
        width: 200px;
    } */

    div.box.terms {
        display: flex;
        justify-content: center;
        width: 200px;
        margin-left: 70px;
    }

    div.box.submit_data {
        display: block;
        margin: auto;
        width: 200px;
    }

    /*     div.controls>span {
        display: block;
        margin: auto;
        width: 200px;
        text-align: center;
    } */

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

    .subscription label {
        margin: 0px 20px 0px 3px;
    }

    .subscription input {
        margin-right: 10px;
    }

    input #password {
        width: 270px;
    }

    .fieldgroup {
        float: left !important;
        width: auto !important;
        margin-left: 3em !important;
    }

    #termsModal{
        
        height:90%;
    }

    /* .modal-body {
        position: relative !important;
        overflow-y: scroll !important;
        max-height: 92% !important;
        height:80%;
        width: 80%;
        max-width:80%;
        padding: 5px !important;
        margin-top: 5px;
        margin-bottom: 10px;
        margin-left: 25px;
    } */

    


}

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

    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: 100px !important;
        margin-right: 100px !important;
        padding-left: 50px !important;
        padding-right: 50px !important;
        max-width: 90% !important;
    }

    .parent {
        display: flex;
        /* background-color: transparent; */
        color: #fff;
        /* border: 1px solid #333; */
        align-items: center;
        width: 95%;
    }

    .space-between {
        justify-content: space-between !important;
    }


    .sectionHeader {
        margin-left: 10px !important;
    }

    #imgHero {
        margin-left: 40px !important;
        margin-right: 40px !important;
        max-width: 90% !important;
    }

    .alignButtons {
        text-align: center;
        width: 200px;
    }

    /* div#termsBtnContainer.control-group{
      display: flex;
      justify-content: center;
      width: 200px;
      
    } */

    div.box.submit_data {
        display: block;
        margin: auto;
        width: 200px;
    }

    div.box.terms {
        display: block;
        margin: auto;
        width: 200px;
    }

    .alignContent {
        text-align: center;
    }

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

    .subscription {
        text-align: center;
    }

    .subscription input {
        margin-right: 5px;
    }


    .checkbox input[type="checkbox"] {
        float: left;
        margin-left: 5px;
    }

    /*   input[type="radio"] {
        display: inline-block !important;
    } */

    .personal-1,
    .personal-2,
    .contact-1,
    .contact-2,
    .address-1,
    .address-2,
    .account-1,
    .account-2 {
        display: flex;
        flex-direction: row;
        padding-bottom: 0px !important;
        margin-bottom: 0px !important;
        /* justify-content: flex-start; */
        /*grid-template-columns: 1fr 1fr;
    grid-auto-flow:row;
    width:36em;*/
    }

    .subscription {

        display: flex;
        flex-direction: row;
        /* justify-content: space-around; */
        flex-wrap: wrap !important;
    }

    .storeRewards {
        display: flex;
        flex-direction: row;
        justify-content: center;
        /*align-items: center;*/
        /*grid-template-columns: 1fr 1fr;
    grid-auto-flow:row;
    width:36em;*/
    }

    .personal-1 label,
    .personal-2 label,
    .storeRewards label,
    .contact-1 label,
    .contact-2 label,
    .address-1 label,
    .address-2 label,
    .account-1 label,
    .account-2 label {
        width: 175px;
        color: black;
        font-size: 13px;
        font-family: Helvetica Neue, Helvetica, Arial, sans-serif
    }

    .personal-1 input,
    .personal-2 input,
    .storeRewards select,
    .contact-1 input,
    .contact-2 input,
    .address-1 input,
    .address-2 input,
    .account-1 input,
    .account-2 input,
    .account-2 select {
        /* margin-right: 20px; */
        width: 288.98px;       
    }

    div.alert.alert-info {
        width: 250.98px;
        /* text-align: */
    }

    #password {
        width: 256px;
    }

    #city {
        width: 270px;
    }

    input#birthdate {
        /* padding-left: 6px !important;
    padding-right: 6px; */
        padding: 0px;
        background-color: white;

    }

    #spanCalendar{
        background-color: aliceblue;
    }

    .datepicker {
        padding: 0px !important;
    }

    div#datepicker_birthday.input-append.date.datepicker.control {
        padding: 0px;
    }

    div#divPerks,
    div#divStores {
        width: 302.96px;
    }

    /*    div#divPerks{
        display: flex !important;
        flex-direction: row !important;
    } */

    .subscriptionOptions {
        border: none;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        break-before: always;
        margin: 0 0 5px 0;
    }

    .floatBlock {
        margin: 0 5px 0 0;
    }

    select#mystore.input-xlarge.required,
    select#rewardSubscriptions {
        width: 302.96px;
    }

    div.alert.alert-info {
    max-width: auto !important;
    width: auto !important;
    /* text-align: end; */
    /* align-self: end; */
    margin-top: 40px;
    }

    #bdateMessage{
        padding-top: 8px;
        padding-right: 15px;
        padding-bottom: 8px;
        padding-left: 14px;
    }

}
#iconCalendar{
    display: inline-block !important;
    width: auto !important;
    height: auto !important;
    background-image: none !important;
    background-position: 0 !important;
    background-repeat: no-repeat !important;
    line-height: normal !important;
    background-color: #f9f9f9 !important;
    color: blue !important;
}

   
</style>

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

<div id="registrationLoadingContain" class="hideObject"
    style="background: rgba(0,0,0,.75);position: fixed;top:0;bottom:0;left:0;right:0;z-index:100;text-align: center;">
    <i class="fa fa-spinner fa-spin" style="font-size:70px;color:#fff;margin: 15% auto 0;display: block;"></i>
</div>

<div id="imgHero" class="wrapper">

    <div class="box hero">

        <img src="/Kickstrap/img/ValleyMarket_FuelSiteGraphics_Welcome_Header.png"
            alt="Welcome to Rewards Header image">
    </div>
    <div style="clear: both"><br></div>

    <div class="box pageHeader">

        <div class="sectionHeader">
            <h4 style="color:#f79e1e;">Register for rewards to start saving today. Choose between free groceries OR free
                fuel and don’t miss out on great deals
                by signing up for emails. Once registered, provide your phone number every time you shop to see your
                points
                add up.</h4>
        </div>

        <div style="clear: both"><br></div>

        <div class="alignContent">
            <h3>User Registration</h3>
        </div>

    </div>

    <?php
	  if($theError){
	?>
    <div class="alert alert-error">
        <a class="close" data-dismiss="alert" href="#">x</a><?php echo $errorMessage; ?>
        <?php
                if(isset($theResult['badInputs'])){
                foreach($theResult['badInputs'] as $inputName){
            ?>
        <span><?php echo $inputName; ?></span><br>
        <?php
                }
                }
            ?>
    </div>
    <?php
        }//end if($theError)
        
        if($update){
    ?>

    <form method="POST" action="user-update" name="updateform" id="updateform" class="cf" accept-charset="UTF-8" autocomplete="off">
        <!--<legend style="text-align: center;">Update User Information</legend>-->
        <input type="hidden" name="whichForm" id="whichForm" value="update" />
        <input type="hidden" name="frqShopperNo" id="frqShopperNo" value="<?php echo $theUser['FrqShopperNo']; ?>" />
        <?php
						  }else{
						?>
        <form method="POST" action="register.php" name="registerform" id="registerform" class="cf"
            accept-charset="UTF-8" autocomplete="off">
            <!--<legend style="text-align: center;">User Registration</legend>-->
            <input type="hidden" name="whichForm" id="whichForm" value="register" />
            <input type="hidden" name="acceptedTerms" id="acceptedTerms" value="false" />
            <?php
						  }
						?>

            <!-- production recaptcha site key -->
            <div class="g-recaptcha" data-sitekey="6LeHlbAUAAAAALMPzaK9zInC6yohvguYx68clL5q"
                data-callback="registerRecaptcha" data-size="invisible">
            </div>

            <!-- local recaptcha site key -->
            <!-- <div class="g-recaptcha" data-sitekey="6LeJ0rAUAAAAAK1zlQQTsF7CY4Jp8luuZUm9EpAu"
                data-callback="registerRecaptcha" data-size="invisible">
            </div> -->

            <div style="clear: both"><br></div>

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

            <!-- <div style="clear: both"><br></div> -->

            <div class="parent space-between box personal-1">

                <!--<div class="myForm">-->
                <div>
                    <label for="firstname">First Name*
                        <span class="hideObject text-error" id="firstnameErrorMsg"> - Provide a first name</span>
                    </label>

                    <input type="text" name="firstname" id="firstname" placeholder="First Name" class="required" autocomplete="given-name"
                        value="<?php echo isset($theUser['FirstName']) ? htmlspecialchars($theUser['FirstName']) : ''; ?>"
                        required>
                </div>
                <div>
                    <label for="lastname">Last Name*
                        <span class="hideObject text-error" id="lastnameErrorMsg"> - Provide a last name</span>
                    </label>

                    <input type="text" name="lastname" id="lastname" placeholder="Last Name" class="required" autocomplete="family-name"
                        value="<?php echo isset($theUser['LastName']) ? htmlspecialchars($theUser['LastName']) : ''; ?>"
                        required>

                </div>

                <!--</div>-->
            </div>

            <div class="parent space-between box personal-2">


                <!--<div class="myForm">-->
                <?php
            $useDate = date("m/d/Y");
            $setDate = false;
            if(isset($theUser['Birthday'])){
                $useDate = $theUser['Birthday'];
                $setDate = true;
            }
        ?>
                <div>
                    <label for="birthdate">Birth Date</label>
                    <div id="datepicker_birthday" class="input-append date datepicker controls"
                        data-date-format="mm/dd/yyyy" data-date="<?php echo $useDate; ?>" data-date-viewmode="years">
                        <input type="text" id="birthdate" name="birthdate"
                            value="<?php echo $setDate === true ? $useDate : ""; ?>" readonly="readonly" required />
                        <span class="add-on" id="spanCalendar"><i class="icon-calendar" id="iconCalendar"></i></span>
                    </div>
                </div>


                <?php
            if(!$update){
            ?>
                <div class="alert alert-info" id="bdateMessage">
                    Free medium <i>GELATO</i> for your birthday, yeah!!!
                </div>
                <?php
            }
        ?>
                <!--</div>-->
            </div>

            <div style="clear: both"><br></div>

            <div class="sectionHeader">
                <h3 style="color:#f79e1e;">Store & Rewards</h3>
            </div>

            <!-- <div style="clear: both"><br></div> -->

            <div class="parent space-between box storeRewards">

                <!--<div class="myForm">-->
                <div id="divStores">
                    <label for="stores">
                        My Store <?php echo $update ? '' : '*'; ?> <span class="hideObject text-error"
                            id="mystoreErrorMsg">
                            -
                            Choose a store</span>
                    </label>

                    <select id="mystore" name="mystore" class="input-xlarge <?php echo $update ? '' : 'required'; ?>">
                        <!-- <option value="0">Choose Your Store</option> -->
                        <?php
            echo $stores;
            ?>
                    </select>

                </div>

                <!--<select name="stores" id="stores">
            <option value="" selected="selected">Choose Your Store</option>
            <option value="Thayne">Thayne Valley Market</option>
        </select>-->
                <div><label>Choose Your Rewards*</label>
                    <div id="divPerks" class="subscriptionOptions">
                        <?php
                        echo $rewardSubscriptions;
                        ?>

                    </div>


                </div>

                <!--</div>-->
            </div>

            <!-- <div style="clear: both"><br></div> -->

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

            <!-- <div style="clear: both"><br></div> -->

            <div class="parent space-between box contact-1">

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

                    <label for="phone">
                        Phone 1*<span class="hideObject text-error" id="phoneErrorMsg"> - Provide a
                            phone
                            number</span>
                    </label>
                    <input type="tel" name="phone" id="phone" type="tel" placeholder="000-000-0000" maxlength="12"
                        class="input-xlarge required" autocomplete="tel"
                        value="<?php echo isset($theUser['Phone']) ? htmlspecialchars($theUser['Phone']) : ''; ?>"
                        required>
                </div>

                <div>
                    <label for="secondaryPhone">
                        Phone 2 <span class="hideObject text-error" id="secondaryPhoneErrorMsg"> -
                            Provide a phone number</span>
                    </label>

                    <input type="tel" name="secondaryPhone" id="secondaryPhone" placeholder="000-000-0000"
                        maxlength="12" class="input-xlarge" autocomplete="tel"
                        value="<?php echo isset($theUser['SecondaryPhone']) ? htmlspecialchars($theUser['SecondaryPhone']) : ''; ?>">
                </div>

                <!--</div>-->
            </div>

            <div class="parent space-between box contact-2">

                <!--<div class="myForm">-->
                <div>
                    <label for="email">
                        Email* <span class="hideObject text-error" id="emailErrorMsg"> - Provide an email</span>
                    </label>

                    <input type="email" name="email" id="email" placeholder="example@example.com"
                        class="input-xlarge required" autocomplete="email"
                        value="<?php echo isset($theUser['Email']) ? htmlspecialchars($theUser['Email']) : ''; ?>"
                        required>

                </div>

                <?php
            if(!$update){
        ?>
                <div class="alert alert-info">
                    Your email address is your new username.
                </div>
                <?php
            }
        ?>

                <!--</div>-->
            </div>

            <!-- <div style="clear: both"><br></div> -->

            <div class="parent space-between box address-1">
                <!--<div class="myForm">-->

                <div>
                    <label for="address1">Address* </label>
                    <input id="address1" name="address1" type="text" placeholder="Address" class="input-xlarge required" autocomplete="street-address"
                        value="<?php echo isset($theUser['Address1']) ? htmlspecialchars($theUser['Address1']) : ''; ?>">
                </div>

                <div>
                    <label for="address2">Apartment/Building </label>
                    <input id="address2" name="address2" type="text" placeholder="" class="input-xlarge" autocomplete="street-address"
                        value="<?php echo isset($theUser['Address2']) ? htmlspecialchars($theUser['Address2']) : ''; ?>">
                </div>

            </div>

            <div class="parent space-between box address-2">

                <div>
                    <label for="city">City* </label>
                    <input id="city" name="city" type="text" placeholder="City" class="input-xlarge" autocomplete="address-line1"
                        value="<?php echo isset($theUser['City']) ? htmlspecialchars($theUser['City']) : ''; ?>">
                </div>

                <div>
                    <label for="zipcode">Zip Code*</label>
                    <input id="zipcode" name="zipcode" type="text" placeholder="Zip Code" class="input-xlarge required" autocomplete="postal-code"
                        value="<?php echo isset($theUser['ZipCode']) ? htmlspecialchars($theUser['ZipCode']) : ''; ?>">
                </div>

            </div>

            <?php
    if(!$update){
    ?>

            <div style="clear: both"><br></div>

            <div class="sectionHeader">
                <h3 style="color:#f79e1e;">Account Security</h3>
            </div>
            <!-- <div style="clear: both"><br></div> -->

            <div class="parent space-between box account-1">

                <!--<div class="myForm">-->
                <div>
                    <label for="password">
                        Password* <span class="hideObject text-error" id="passwordErrorMsg"> - Provide a password</span>
                    </label>

                    <input type="password" name="password" id="password" class="input-xlarge required" placeholder="" autocomplete="new-password"
                        required>

                </div>

                <div>
                    <label for="confirmPassword">Confirm Password* </label>
                    <input type="password" name="confirmPassword" id="confirmPassword" class="input-xlarge required" autocomplete="new-password"
                        placeholder="" required>
                </div>


                <!--</div>-->
            </div>


            <div class="alignContent">
                <div class="alert alert-danger hideObject" id="confirmPasswordErrorMsg"><span>The passwords do not
                        match</span></div>
            </div>

            <div class="parent space-between box account-2">

                <div>
                    <label for="questions">Security Question*</label>

                    <select id="securityQuestion" name="securityQuestion" class="input-xlarge">
                        <?php echo $securityQuestions; ?>
                    </select>
                </div>
                <!--<select name="questions" id="questions">
            <option value="movie" selected="selected">What is your favorite movie?</option>
            <option value="mother">What is your mother's maiden name?</option>
            <option value="street">What was the name of the first street you lived on?</option>
        </select>-->

                <div>
                    <label for="securityAnswer">Security Question Answer*</label>
                    <input type="text" name="securityAnswer" id="securityAnswer" class="input-xlarge required" required>
                </div>

            </div>

            <!-- <div style="clear: both"><br></div> -->

            <div class="sectionHeader">
                <h3 style="color:#f79e1e;">Email options</h3>
            </div>

            <!-- <div style="clear: both"><br></div> -->

            <div class="box subscription">



                <!--<label>
            <input type="checkbox" name="subscriptions" id="subscriptions_weekly" value="weekly">Weekly Ad
            Promotions</label>

        <label>
            <input type="checkbox" name="subscriptions" id="subscriptions_storage" value="storage">Food
            Storage</label>
        <label> <input type="checkbox" name="subscriptions" id="subscriptions_baby" value="baby">Baby Club
        </label>
        <label> <input type="checkbox" name="subscriptions" id="subscriptions_food" value="food">Food Club
        </label>
        <label> <input type="checkbox" name="subscriptions" id="subscriptions_pet" value="pet">Pet Club </label>-->

                <?php
            foreach($availableSubscriptions as $subscription){
                if(isset($theUser)){
                    $checkedStatus = false;

                    foreach($theUser['UserEmailSubscriptions'] as $userSubscription){
                        if(isset($userSubscription['EmailSubscriptionID']) && $userSubscription['EmailSubscriptionID'] == $subscription['EmailSubscriptionID']){
                            $checkedStatus = true;
                        }
                    }
                }else{
                    $checkedStatus = true;
                }
            ?>
                <label class="checkbox">
                    <input type="checkbox" name="optOutEmail[]"
                        id="optOutEmail_<?php echo $subscription['EmailSubscriptionID']; ?>"
                        value="<?php echo $subscription['EmailSubscriptionID']; ?>"
                        <?php echo $checkedStatus ? "checked" : "" ; ?>>
                    <?php echo $subscription['EmailSubscriptionProgram']; ?>
                </label>
                <?php
            }
            ?>

            </div>

            <?php
        }
    ?>

            <div style="clear: both"><br></div>

            <div class="box terms">
                <?php
        if(!$update){
    ?>


                <!--   <div id="termsBtnContainer" class="control-group">
                    <span class="hideObject text-error" id="termsErrorMsg">You must accept the terms to register.</span>
                    <div class="controls">

                        <a href="#termsModal" id="termsBtn" name="termsBtn" class="btn btn-primary btn-block btn-large"
                            data-toggle="modal" role="button">View Terms</a>
                        <p style="text-align: center;"> * I have read and agree to the <b style="color:blue;">Terms and
                                Conditions</b>.</p>

                    </div>
                </div> -->

                <div id="termsBtnContainer" class="control-group">
                    <span class="hideObject text-error" id="termsErrorMsg">You must accept the terms to register.</span>
                    <div class="controls">
                        <a href="#termsModal" id="termsBtn" name="termsBtn" class="btn btn-primary btn-block btn-large"
                            data-toggle="modal" role="button">View Terms</a>
                        <p style="text-align: center;"> * I have read and agree to the <b style="color:blue;">Terms and
                                Conditions</b>.</p>
                    </div>
                </div>

                <!--   <div id="termsConditionsStmt" class="hideObject alignContent"> * I have read and agree to the <span> 
                <b style="color:blue;">
                Terms and Conditions
                <a href="/includes/termsConditions.php" style="color:blue;">Terms and Conditions</a>
                        </b></span> </div> -->

                <?php
        }
    ?>

            </div>

            <!-- <div style="clear: both"><br></div> -->

            <div class="box submit_data">


                <p class="alignButtons">
                    <!-- <span><a href="#" id="submitBtn" name="submitBtn"
                            class="btn btn-primary <?php $update === true ? "disabled" : "" ?>"
                            style="width:150px; margin-top:5px; margin-bottom:5px;">Submit</a></span> -->
                    <button id="submitBtn" name="submitBtn"
                        class="btn btn-default btn-block btn-large <?php $update === true ? "disabled" : "" ?>">Submit</button>
                </p>

            </div>

        </form>
        <div id="termsModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
            aria-hidden="true">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h3 id="myModalLabel">Terms and Conditions</h3>
            </div>
            <div class="modal-body">
                <?php include($_SERVER['DOCUMENT_ROOT'] . '/includes/termsConditions.php'); ?>
            </div>
            <div class="modal-footer">
                <!--<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>-->
            </div>
        </div>


        <!--</div>-->
</div>
<!--end of "wrapper" class  -->



<script>
ks.ready(function() {
    var nowTemp = new Date();
    var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);

    var birthday = $("#datepicker_birthday").datepicker().on('changeDate', function(ev) {

        if (ev.viewMode == "days") {
            birthday.hide();
        }
    }).data('datepicker');

    $("#birthdate").on("touch click", function(e) {
        $("#birthdate i").click();
    });
});

function validatePassword() {
    var password1 = $("#registerform #password").val();
    var password2 = $("#registerform #confirmPassword").val();

    if (password1 == password2) {
        $("#registerform #confirmPasswordErrorMsg").hide();
        return true;
    } else {
        $("#registerform #confirmPasswordErrorMsg").show();
        return false;
    }

}

$(document).ready(function() {
    $("#registerform #password, #registerform #confirmPassword").keyup(validatePassword);

    var numLocations = $("#mystore option").length;

    if (numLocations == 2) {
        $("#mystore option").eq(1).attr("selected", true);
    }
});

function registerRecaptcha(token) {
    if ($("#registerform").length > 0) {
        $("#registerform").unbind("submit").submit();
    } else {
        $("#updateform").unbind("submit").submit();
    }
}

function validateForm($form) {
    //check if phone number or email is provided
    var phone = $("#phone", $form).val();
    var email = $("#email", $form).val();
    var zipCode = $("#zipcode", $form).val();
    var acceptedTerms = $("#registerform #acceptedTerms").val();
    var homeStore = $("#mystore option:selected").val();

    var isUpdate = "<?php echo $update ? 'true' : 'false'; ?>";

    if (isUpdate == "false") {
        var passwordsMatch = validatePassword();

        if (passwordsMatch === false) {
            $('html, body').animate({
                scrollTop: $("#registerform #password").offset().top - 100
            }, 1000);

            $("#registrationLoadingContain").addClass('hideObject');

            return false;
        }
    }

    if (homeStore == "0" && isUpdate == "false") {
        $("#mystoreErrorMsg").removeClass("hideObject");

        $('html, body').animate({
            scrollTop: $("#mystoreErrorMsg").offset().top - 100
        }, 1000);

        $("#registrationLoadingContain").addClass('hideObject');

        return false;
    }

    if (acceptedTerms == "false") {
        $("#termsErrorMsg, #termsBtnContainer").removeClass("hideObject");
        $("#registrationLoadingContain").addClass('hideObject');

        return false;
    }

    $("#termsErrorMsg, #termsBtnContainer").addClass("hideObject");

    validForm = true;

    var $requiredFields = $(".required", $form);

    $requiredFields.each(function() {
        $this = $(this);
        if (!$this.val() || $.trim($this.val()) === "") {
            $("#" + $this.attr("id") + "ErrorMsg").removeClass("hideObject");
            validForm = false;
        } else {
            $("#" + $this.attr("id") + "ErrorMsg").addClass("hideObject");
        }
    });

    if (zipCode.length != 5) {
        $("#" + $this.attr("id") + "ErrorMsg").removeClass("hideObject");
        validForm = false;
    }

    if (validForm) {
        grecaptcha.execute();
        return true;
    } else {
        $("#registrationLoadingContain").addClass('hideObject');
        return false;
    }

}

$(".required").on('blur', function() {
    if ($(this).val()) {
        $("#" + $(this).attr('id') + "ErrorMsg").addClass("hideObject");
    }
});

$("#submitBtn").on('touch click', function(e) {
    e.preventDefault();
    $("#registrationLoadingContain").removeClass('hideObject');
    var theForm = $(this).closest("form");
    validateForm(theForm);
});

$("#acceptTermsBtn").on('touch click', function(e) {
    e.preventDefault();
    $("#registerform #acceptedTerms").val("true");
    $("#submitBtn").removeClass("disabled btn-default").addClass("btn-primary");
    $("#termsModal").modal("hide");
    $("#termsBtnContainer").addClass("hideObject");
    $("#termsBtnContainer").css("display", "");

});

$("#rejectTermsBtn").on('touch click', function(e) {
    e.preventDefault();
    $("#registerform #acceptedTerms").val("false");
    alert("You must accept the terms of conditions in order to create an account");
    $("#termsModal").modal("hide");
});

$(document).ready(function() {
    var selectedStore =
        '<?php echo isset($theUser['
    MyStore ']['
    MyStoreNumber ']) ? trim($theUser['
    MyStore ']['
    MyStoreNumber ']) : "false"; ?>';

    if (selectedStore != "false") {
        $("#mystore option[value='" + selectedStore + "']").prop('selected', 'selected');
    }

    if ($("#submitBtn").hasClass("disabled")) {
        $("#termsBtnContainer").removeClass("hideObject");
    }
});

   

</script>




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