<?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");
  $brdataObj = new brdata();
  
  $theUser = [];
  
  if(isset($loggedIn) && $loggedIn === true){
	$theUser = $brdataObj->getUser($_SESSION['FrequentShopperNum']);
	$securityQuestions = $brdataObj->getSecurityQuestions();
	$availableSubscriptions = $brdataObj->getAvailableEmailSubscriptions();

	//require_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/class.rewards.inc.php");
	// $rewardsUserObj = new rewards($theUser['Phone']);
	
	// $rewardsReceipts = $rewardsUserObj->getUserOrderSummary();
	
	//list($userCredit, $userPoints, $accountError) = $rewardsUserObj->getUserCreditPoints($theUser['Phone']);
  }else{
	header("Location: https://" . $_SERVER['SERVER_NAME'] . "/login?l=false");
  }
  
  $theError = false;
?>
<?php
  $pageTitle = "User Information";
  $pageDescription = "Your user information";
  $pageKeywords = "";
?>
<?php
  include_once($_SERVER['DOCUMENT_ROOT'] . "/common/header.php"); 
?>

<style>
	  body #contentWrapper #sf-wrapper #bodyPadding #trueContentWrapper #mainContent .span12{
			width: 95%;
	  }

	  body #contentWrapper #sf-wrapper #bodyPadding #trueContentWrapper #mainContent #rewardsSC{
			width: 97%;
	  }

	  body #bodyPadding #rewardsSC {
	  	margin-left:0;
	  }

	  #accountDisplayContainer .accountSection {
	  	width:46%;
	  	margin: 0 2%;
	  	float: left;
	  	text-align: left;
	  }

	  #accountDisplayContainer .accountSection .accountBtn {
	  	margin-bottom: 10px;
	  }

	  #sideNavigation .active {
	  	/*border-bottom:2px solid #00669e;*/
	  	background-color: #f5f5f5;
		background-image: linear-gradient(
			to top,
			rgba(100,100,100,.25),
			#f5f5f5 25%
		);
	  }
	  
	  .control-label {
		font-weight:900;
	  }
	  
	  .controls, .control-label{
		display: inline;
	  }

	  h6.pointsEarned {
	  	background:#00669e;
	  	padding:12px 20px;
	  	color:#fff;
	  	font-size:18px;
	  	margin-bottom: 30px;
	  }

	  body #bodyPadding #schoolCentsNav, body #bodyPadding #rewardsSC {
	  	display: block !important;
	  }

	  /*
	  
	  
	  .hideObject{
		display: none !important;
	  }
	  
	  @media (max-width: 780px) {
		#changePWcontain label {
		  display: block;
		}
	  }
	  
	  #mainContent .well {
		margin-right: 1.5%;
		width: 27.5%;
	  }
	  
	  .col-container.col-container-table {
		display: table;
		width: 100% !important;
		border-collapse: separate;
		border-spacing: 20px;
	  }
	  
	  .col-container.col-container-table .well {
		display: table-cell;
	  }
	  
	  .col-container-table .well a {
		height: 100%;
		padding:19px;
		display: block;
		text-align: center;
	  }
	  
	  .col-container-table .well.noPadding {
		padding: 0;
	  }
	  
	  @media (max-width: 780px) {
		#mainContent .well {
		  margin-right: auto;
		  width: 98% !important;
		}
		
		.col-container.col-container-table {
		  display: block;
		  width: 100% !important;
		  border-collapse: unset;
		  border-spacing: 0px;
		}
		
		#mainContent .col-container.col-container-table .well {
		  display: block;
		  width:93% !important;
		}
		
		body #contentWrapper #footerContain {
		  margin-top:60px;
		}
	  }
	  
	  #addSecondaryForm .controls, #addSecondaryForm .control-label {
		display: block;
	  }
	  
	  .secondaryBtns {
		padding: 0 !important;
	  }
	  
	  #secondaryUserContain {
		min-height: 150px;
	  }
	  */
</style>

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

<section class="container">
	<div id="blankBuffer"></div>
	<section class="row" id="mainContent">
		<p id="frequentShopperID" class="hideObject" data-fsid="<?php echo $theUser['FrqShopperNo']; ?>"></p>
		<div class="span12">
			<h1>Your Account</h1>
			<div class="col-container col-container-table">
				<div class="tab-pane span12" id="rewardsSC">
					<div class="span4 ml-0" id="sideNavigation">
						<div class="well active" id="accountInformationContainer">
							<h4><a href="#">Account Information</a></h4>
						</div>
						<div class="well" id="perksLinkContainer">
							<h4><a href="/plus">My Plus</a></h4>
						</div>
						<!-- <div class="well" id="allReceiptsContainer">
							<h4><a href="#" id="receiptsLink">Receipts</a> <span class="icon-question-sign pull-right"></span></h4>
							<?php
								if(!isset($rewardsReceipts['error'])){
									foreach($rewardsReceipts as $receipt){
										?>
										<div class="receiptLinkContainer clearfix" id="receiptLink_<?php echo $receipt['TransactionID']; ?>">
											<p class="pull-left">
												<a href="#" class="receiptLink"><?php echo $receipt['SiteName']; ?></a>
												<br>
												<span><?php echo date("m/d/Y H:i:s", strtotime($receipt['TransactionDate'])); ?></span>
											</p>
											<p class="receiptPrice pull-right">
												$<?php echo number_format($receipt['Amount'], 2); ?>
											</p>
										</div>
										<?php
									}
								}else{
							?>
								<h4>You have no receipts</h4>
							<?php
								}
							?>
						</div> -->

						<!--
						<div class="well" id="redeemedCouponsContainer">
							<h4><a href="#">Redeemed Coupons</a> <span class="icon-question-sign pull-right"></span></h4>
						</div>
						-->
					</div>
					<div class="span8" id="rewardsContentContainer">
						<div class="well mainSection" id="accountDisplayContainer">
							<div class="panel text-center">
								<div class="panel-body">
									<div class="accountSection">
										<h3>Personal <br>Information</h3>
										<!-- Text input-->
										<div class="control-group">
											<label class="control-label" for="firstname">First Name: </label>
											<div class="controls">
												<?php echo $theUser['FirstName']; ?>
												<!--<input id="firstname" name="firstname" type="text" placeholder="First Name" class="input-xlarge" required="">-->

											</div>
										</div>

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

											</div>
										</div>


										<!-- Select Basic -->
										<div class="control-group">
											<label class="control-label" for="mystore">My Store: </label>
											<div class="controls">
												<?php echo isset($_SESSION['MyStoreAFS']['store_name']) ? $_SESSION['MyStoreAFS']['store_name'] : '' ?> (<?php echo $_SESSION['MyStore']; ?>)
											</div>
										</div>

										<!-- Text input-->
										<div class="control-group">
											<label class="control-label" for="birthdate">Birth Date: </label>
											<div class="controls">
												<?php echo $theUser['Birthday']; ?>
												<!--<input id="birthdate" name="birthdate" type="text" placeholder="MM/DD/YYYY" class="input-xlarge">-->

											</div>
										</div>

										<br>

										<h3>Address <br>Information</h3>
										<!-- Text input-->
										<div class="control-group">
											<label class="control-label" for="address1">Street Address: </label>
											<div class="controls">
												<?php echo $theUser['Address1']; ?>
												<!--<input id="address1" name="address1" type="text" placeholder="Address" class="input-xlarge" required="">-->

											</div>
										</div>

										<!-- Text input-->
										<div class="control-group">
											<label class="control-label" for="address2">Apartment / Building: </label>
											<div class="controls">
												<?php echo $theUser['Address2']; ?>
												<!--<input id="address2" name="address2" type="text" placeholder="" class="input-xlarge">-->
											</div>
										</div>

										<!-- Text input-->
										<div class="control-group">
											<label class="control-label" for="city">City: </label>
											<div class="controls">
												<?php echo $theUser['City']; ?>
												<!--<input id="city" name="city" type="text" placeholder="City" class="input-xlarge">-->

											</div>
										</div>

										<!-- Text input-->
										<div class="control-group">
											<label class="control-label" for="state">State: </label>
											<div class="controls">
												<?php echo $theUser['State']; ?>
												<!--<input id="state" name="state" type="text" placeholder="State" class="input-xlarge">-->

											</div>
										</div>

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

											</div>
										</div>

										<br>

										<h3>Programs</h3>
										<!-- Text input-->
										<div class="control-group">
											<?php echo $_SESSION['userRoles']; ?>
										</div>
									</div>

									<div class="accountSection">
										<h3>Contact <br>Information</h3>
										<!-- Text input-->
										<div class="control-group">
											<label class="control-label" for="phone">
												Phone Number <span class="hideObject text-error" id="phoneErrorMsg"> - Provide a phone number</span>
											: </label>
											<div class="controls">
												<?php echo '('.substr($theUser['Phone'], 0, 3).') '.substr($theUser['Phone'], 3, 3).'-'.substr($theUser['Phone'],6); ?>
												<!--<input id="phone" name="phone" type="text" placeholder="000-000-0000" class="input-xlarge">-->

											</div>
										</div>

										<!-- Text input-->
										<div class="control-group">
											<label class="control-label" for="phone">
												Secondary Phone Number <span class="hideObject text-error" id="phoneErrorMsg"> - Provide a phone number</span>
											: </label>
											<div class="controls">
												<?php echo '('.substr($theUser['SecondaryPhone'], 0, 3).') '.substr($theUser['SecondaryPhone'], 3, 3).'-'.substr($theUser['SecondaryPhone'],6); ?>
												<!--<input id="phone" name="phone" type="text" placeholder="000-000-0000" class="input-xlarge">-->

											</div>
										</div>

										<!-- Text input-->
										<div class="control-group">
											<label class="control-label" for="email">
												Email Address <span class="hideObject text-error" id="emailErrorMsg"> - Provide an email</span>
											: </label>
											<div class="controls">
												<span id="userEmail"><?php echo $theUser['Email']; ?></span>
												<!--<input id="email" name="email" type="text" placeholder="example@example.com" class="input-xlarge" required="">-->

											</div>
										</div>

										<br>
										
										<div class="control-group">
											<h3>Email Subscriptions</h3>
											<?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;
													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>

										<hr>

										
										<a href="/user-update" id="updateAccountbtn" class="btn btn-primary btn-block accountBtn">Update Account Information</a>
								
										
										<a href="#" id="changePWbtn1" class="btn btn-primary btn-block accountBtn">Change Password</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>
											
										<a href="#" id="changeSecurity" class="btn btn-primary btn-block accountBtn">Change Security Question / Answer</a>

										<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>
									</div>
									<div style="clear: both;"></div>
								</div>
							</div>
						</div>

						<!-- <div class="well mainSection hideObject" id="receiptsDisplayContainer">
							<div class="panel text-center">
								<div class="panel-body">
									<?php
									if(!isset($rewardsReceipts['error'])){
										
										$count = 0;

										foreach($rewardsReceipts as $receipt){
											
											$receiptComponents = explode("\n", $receipt['$POSReceipts$'][0]['TextData']);
											$receiptItems = array_slice($receiptComponents, 4, -16);
									?>
											<div id="fullReceipt_<?php echo $receipt['TransactionID']; ?>" class="oneReceipt <?php echo $count !== 0 ? 'hideObject' : ''; ?>">
												<h6 class="pointsEarned">Points Earned: <?php echo floor((float) $receipt['Amount']); ?></h6>
												<h5><?php echo $receiptComponents[0]; ?></h5>
												<h4><?php echo $receiptComponents[1]; ?></h4>
												<p class="mb-30"><?php echo $receiptComponents[3]; ?></p>

									<?php
												foreach($receiptItems as $item){
													if(trim($item) !== ""){
														$itemComponents = explode("$", $item);
														?>
														<p class="clearfix text-left">
															<span class="pull-left">
																<span><?php echo strtoupper($itemComponents[0]); ?></span>
																<?php
																if(isset($itemComponents['qty']) && !is_null($itemComponents['qty'])){
																	?>
																	<br>
																	<span style="margin-left:20px;display: block;">
																		QTY
																		<?php
																		echo $itemComponents['qty'];
																		?>
																	</span>
																	<?php
																}
									?>
															</span>
															<span class="pull-right">$<?php echo $itemComponents[1]; ?></span>
														</p>
									<?php
													}
												}
									?>
												<div class="receiptTotalInfo mt-20">
													<p class="clearfix">
									<?php
														$subTotal = explode("$", $receiptComponents[count($receiptComponents)-16]);
									?>
														<span class="pull-left">SUB TOTAL</span>
														<span class="pull-right">$<?php echo $subTotal[1]; ?></span>
													</p>

													<p class="clearfix">
									<?php
														$totalTax = explode("$", $receiptComponents[count($receiptComponents)-15]);
									?>
														<span class="pull-left">TOTAL TAX</span>
														<span class="pull-right">$<?php echo $totalTax[1]; ?></span>
													</p>

													<p class="clearfix">
									<?php
														$total = explode("$", $receiptComponents[count($receiptComponents)-14]);
									?>
														<span class="pull-left">TOTAL</span>
														<span class="pull-right">$<?php echo $total[1]; ?></span>
													</p>
												</div>

												<div class="receiptTenderedInfo mt-20">
													<p class="clearfix">
									<?php
														$balanceDue = explode("$", $receiptComponents[count($receiptComponents)-12]);
									?>
														<span class="pull-left">BALANCE DUE</span>
														<span class="pull-right">$<?php echo $balanceDue[1]; ?></span>
													</p>

													<p class="clearfix">
									<?php
														$tendered = explode("$", $receiptComponents[count($receiptComponents)-11]);
									?>
														<span class="pull-left">TENDERED</span>
														<span class="pull-right">$<?php echo $tendered[1]; ?></span>
													</p>
												</div>

												<div class="receiptDateInfo mt-50">
													<p class="mb-20"><?php echo $receiptComponents[count($receiptComponents)-8]; ?></p>
													<p><?php echo $receiptComponents[count($receiptComponents)-6]; ?></p>
													<p><?php echo $receiptComponents[count($receiptComponents)-5]; ?></p>
													<p><?php echo $receiptComponents[count($receiptComponents)-4]; ?></p>
													<p><?php echo $receiptComponents[count($receiptComponents)-3]; ?></p>
												</div>
											</div>
									<?php
											$count++;
										}
									}else{
									?>
										<h3>No receipts to display</h3>
									<?php
									}
									?>
								</div>
							</div>
						</div> -->
					</div>
				</div>
			</div>
		</div>
		

		
		
		
		<!--<p>JWT: <?php echo $_SESSION['FS_JWT'] ?></p>-->
		<div style="clear:both;"></div>
		<?php
		  //echo "<pre>";
		  //var_dump($_SESSION['userInformation']);
		  //echo "</pre>";
		?>
	</section>
</section>
  
<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'); ?>
