<?php include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/sessionStartGeneral.php") ?>

<?php
  $pageTitle = "Terms and Conditions";
  $pageDescription = "We have awesome giveaways. Know the rules before entering.";
  $pageKeywords = "Giveaway, terms, conditions ";
?>
<?php include_once($_SERVER['DOCUMENT_ROOT'] . "/common/header.php") ?>
<script src="/Kickstrap/js/jquery.h5validate.js" type="text/javascript"></script>
<?php include_once($_SERVER['DOCUMENT_ROOT'] . "/common/mainNav.php")?>
<?php
  if($_POST){
	$action = "submitted";
  }else{
	$action = "not submitted";
  }
?>    
    
    
    <section class="container">
      <div id="blankBuffer" class="carousel-buffer"></div>
      
      <section class="container" id="mainContent">
        <article class="row">
          <div id="termsContainer" class="span8 leftSide">
            <h1>Promotion Terms and Conditions</h1>
            <?php
              if($action == "submitted"){
                include_once($_SERVER['DOCUMENT_ROOT'] . "/admin/inc/class.features.inc.php");
                
                $storeObj = new stores();
                $theEntry = $storeObj->newNoPurchaseEntry();
                
                if($theEntry['action'] == "added"){
                  $theMessage = "You have successfully submitted {$theEntry['name']} using the email {$theEntry['email']} into the {$theEntry['thePromotion']} Giveaway. Good luck!";
                  $theClass = "alert-success";
                }//end if($theEntry['action'] == "added")
                else{
                  $theMessage = "You have already entered {$theEntry['name']} using the email {$theEntry['email']} into the {$theEntry['thePromotion']} Giveaway.";
                  $theClass = "alert-error";
                }
            ?>
                <div id="noEditError">
                  <div class="alert <?php echo $theClass; ?>">
                    <a class="close" data-dismiss="alert" href="#">x</a><?php echo $theMessage; ?>
                  </div>
                </div>
            <?php
              }//end if($action == "submitted")
            
              //include("https://afsshareportal.com/siteContent/corporate/terms.php");
              $url = "https://afsshareportal.com/lookUpFeatures.php?action=getTerms&website_url=" . urlencode(WEB_URL);
              
              //$url = "https://afsshareportal.com/lookUpFeatures.php?action=getTerms&website_url=yourplusrewards.com";
              
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_HEADER, false);
              $result=curl_exec($ch);
              
              //echo $result . "<br>";
              
              $theTerms = json_decode($result, true);
              
              curl_close($ch);
              if(count($theTerms) > 0){
				foreach($theTerms as $term){
                
				  if(strpos($term['tc_stores'], 'yourplusrewards.com') === false){//if yourplusrewards.com is not in the string
            ?>
                <div id="<?php echo $term['tc_identifier']; ?>" class="aPromotion">
                  <h3><?php echo $term['tc_name']; ?></h3>
                  <div class="offsetPadLeft">
                    <?php echo $term['tc_terms']; ?>
                  </div>
                  
			<?php
					if($term['tc_needNoPurchase'] == true){
			?>
					  <div class="offsetPadLeft">
						<p>No Purchase Necessary.</p>
						<p class="noPurchBtn btn">Click here to enter.</p>
						<form action="/terms" name="noPurchaseForm" method="post" class="noPurchaseForm hideObject">
						  <br>
						  <label for="thePromotion">Promotion Name</label>
						  <input type="text" name="thePromotion" id="thePromotion" value="<?php echo $term['tc_identifier']; ?>" readonly />
						  <?php include("https://afsshareportal.com/common/noPurchaseForms/" . $term['tc_noPurchaseTemplate'] . ".php"); ?>
						</form>
					  </div>
			<?php
					}//end if($term['tc_needNoPurchase'] === true)
			?>
					</div>
			<?php
				  }else{//if yourplusrewards.com is in the string
            ?>
                <div id="<?php echo $term['tc_identifier']; ?>" class="aPromotion">
                  <h3><?php echo $term['tc_name']; ?></h3>
                  <div class="offsetPadLeft">
                    <p>This promotion is run through our PLUS program.</p>
                    <p><a href="http://yourplusrewards.com/terms#<?php echo $term['tc_identifier']; ?>" class="btn">Click here to view terms and conditions.</a></p>
                  </div>
                </div>
            <?php
				  }//end if(strpos($term['tc_stores'], 'yourplusrewards.com') === false)
				}//end foreach($theTerms as $term)
			  }//end if(count($theTerms) > 0)
			  else{
			?>
			  <h4>There are no current promotions.</h4>
			<?php
			  }
            ?>
          </div><!--end left side div-->
            
          <div class="span4 rightSide">
            <div class="paddingHelpRight">
              <?php include("includes/sidebar.php"); ?>
            </div>
          </div><!-- end right side div -->
        </article>
        
      </section>
    </section>
      
    
    
    
    <?php include_once($_SERVER['DOCUMENT_ROOT'] . "/common/footer.php"); ?>
  <script>
    $(".theStoreName").text("Peterson's Fresh Market");
    
	var hash = window.location.hash;
	if (hash !== "") {
	  $(hash + " .noPurchaseForm").removeClass("hideObject");
	}
	
    $(".noPurchBtn").bind("click", function(){
	  $(this).siblings(".noPurchaseForm").removeClass('hideObject');
	});
    
    function getStoreLocations(){
        $.ajax({
            type: "POST",
            url: "/admin/lookUp.php",
            data: "action=getStoreLocations",
            dataType: "json",
            success: function(theResponse){
                var theHTML = "";
                //console.log(theResponse);
                if(theResponse.length > 1){
                  $.each(theResponse, function(){
                    theHTML += "<option value='" + this.store_id + "'>" + this.store_name + "</option>";
                  });
                  
                  $(".noPurchaseForm #location").append(theHTML);
                }else{
                  $(".noPurchaseForm #location").closest("div").remove();
                }
				
				theHTML = "";
            }
        });
    }
    
    getStoreLocations();
   
    $('.noPurchaseForm, .noPurchaseForm2').h5Validate();
    
    
    $("#rightSidebarCollapse h4").bind("click", function(){
      $this = $(this).children("i");
      $siblings = $(this).parents(".accordion-group").siblings();
      $("h4", $siblings).children("i").removeClass("icon-minus-sign").addClass("icon-plus-sign");
      if($this.hasClass("icon-plus-sign")){
        $this.removeClass("icon-plus-sign").addClass("icon-minus-sign");
      }else{
        $this.removeClass("icon-minus-sign").addClass("icon-plus-sign");
      }
    });
   
   
   ks.ready(function() {
      // JavaScript placed here will run only once Kickstrap has loaded successfully.
      
   });
  </script>
  
</body>
</html>
