<?
require("functions.php");
require("class.phpmailer.php");

// Event Fee
if(date("U") > 1390608000){
  $eventFee = array("One year Membership + Lanyard - $15.00");
}else{
  $eventFee = array("One year Membership + Lanyard - $9.00");
}
  
// Additional Items
$additionalItemsSelect = array("lanyard" => "Coastercrew Lanyard - $5.00",
                         "patch" => "Coastercrew Patch - $2.00",
                         "clock" => "Coaster Clock - $20.00",
                         "als1" => "ALS Association Donation - $1.00",
                         "als5" => "ALS Association Donation - $5.00",
                         "als10" => "ALS Association Donation - $10.00");

// Settings   
$eventName = "Coastercrew Membership";              
$maxGuests = 3;
$actionPage = "/ki-central";

if($_SERVER['REQUEST_METHOD'] != "POST"){

require("header.php");
?>

<center><h1><? echo $eventName; ?></h1></center>

<div class="desc">
<dl>
  <dt>Membership</dt>
    <dd>Badge, online profile, access to all events, discount on Coastercrew merchandise, discount on InPark Magazine, and discount on Haunted House Magazine.</dd>
  <dt>Family Membership</dt>
    <dd>Register up to four family members at the same address!
  <dt>Platinum Membership</dt>
    <dd>All the perks of a standard membership plus a backpack, lanyard, membership badge, and access to discounts and coaster events at Holiday World.  Extras are for each family member.</dd>
 </dl>
 </div>

<div class="clear"></div>
<br />
<br />
<form id="contactform" class="rounded" method="post" action="<? echo $actionPage; ?>"  target="status">
<h3>Primary Member</h3>

<div class="field">
	<label for="fullname">Name:</label>
  	<input type="text" class="input" name="fullname" id="fullname"  onkeyup="attendeeName()" onchange="attendeeName()" />
	<p class="hint">Enter your name.</p>
</div>

<div class="field">
	<label for="email">Email:</label>
  	<input type="text" class="input" name="email" id="email" />
	<p class="hint">Enter your email.</p>
</div>

<div class="field">
	<label for="phone">Phone Number:</label>
  	<input type="text" class="input" name="phone" id="phone" />
	<p class="hint">Enter your phone number. Ex: 555-555-5555</p>
</div>

<div class="field">
	<label for="phone">Forum Username:</label>
  	<input type="text" class="input" name="forumusername" id="forumusername" />
	<p class="hint">Enter your KI Central forum username</p>
</div>

<input type="text" class="matt" name="username" id="username" />

<div class="field">
	<label for="badge">Badge Number:</label>
  	<input type="text" class="input" name="badge" id="badge" />
	<p class="hint">If renewing.</p>
</div>

<div class="field">
	<label for="address">Address:</label>
  	<textarea class="input textarea" name="address" id="address"></textarea>
	<p class="hint">Full mailing address.</p>
</div>

<div class="field">
	<label for="birthday">Birthday:</label>
  	<input type="text" class="input" name="birthday" id="birthday" value="mm/dd/yyyy" />
	<p class="hint">Enter your birthday</p>
</div>

<div class="field">
	<label for="homepark">Home Park:</label>
  	<input type="text" class="input" name="homepark" id="homepark" />
	<p class="hint">Enter your home park.</p>
</div>

<div class="field">
	<label for="attendeeEventFee">Membership Type:</label>
  	<select id="attendeeEventFee" name="attendeeEventFee" onchange="updateCost('attendeeEventFee','attendee')">
      <option value="">Please select...</option>
      <?
      foreach ($eventFee AS $k=>$v){
        echo "<option value=\"".$v."\">".$v."</option>";
      }
      ?>
    </select> 
</div>


<div class="field">
	<label for="additionalItems">Additional Items:</label>
  	<div class="fieldText">
      <?
      foreach ($additionalItemsSelect AS $k=>$v){
        echo "<input type=\"checkbox\" name=\"additionalItems[]\" onchange=\"updateCostcb('attendee_".$k."','attendee')\"  id=\"attendee_".$k."_cb\" value=\"".$v."\"><div id=\"attendee_".$k."\">".$v."</div> <br />";
      }
      ?>
    </div>
</div>

<div class="clear"></div>

<div id="family_title" style="display: none;"><h3>Family</h3></div>

<?
for ($i=1; $i<=$maxGuests; $i++){
?>
<div id="guest<? echo $i; ?>" style="display: none;">
  <h4>#<? echo $i; ?> </h4>
  <div class="field">
    <label for="guest<? echo $i; ?>_fullname">Name:</label>
      <input type="text" class="input" name="guest<? echo $i; ?>_fullname" id="guest<? echo $i; ?>_fullname"  onkeyup="guestName(<? echo $i; ?>)" onchange="guestName(<? echo $i; ?>)" />
    <p class="hint">Enter your guest's name.</p>
  </div>
  
  <div class="field">
    <label for="guest<? echo $i; ?>_birthday">Birthday:</label>
      <input type="text" class="input" name="guest<? echo $i; ?>birthday" id="guest<? echo $i; ?>_birthday" value="mm/dd/yyyy" />
    <p class="hint">Enter your birthday.</p>
  </div>
  
  
  <div class="field">
    <label for="additionalItems">Additional Items:</label>
      <div class="fieldText">
        <?
        foreach ($additionalItemsSelect AS $k=>$v){
          echo "<input type=\"checkbox\" name=\"guest".$i."additionalItems[]\" onchange=\"updateCostcb('guest".$i."_".$k."','guest1')\"  id=\"guest".$i."_".$k."_cb\" value=\"".$v."\"><div id=\"guest".$i."_".$k."\">".$v."</div> <br />";
        }
        ?>
      </div>
  </div>

</div>

<div class="clear"></div>
<?
}
?>


<a href="javascript:addGuest()" class="button" id="addGuestButton" style="float:left; display: none;">Add Family Member</a>


<div class="clear"></div>

<h3>Payment Summary</h3>

<table id="paysummary" class="summary">
  <tbody>
    <tr>
      <th>Name</th>
      <th style="text-align:right;">Cost</th>
    </tr>
    <tr>
      <td><span id="attendeeName"></span></td>
      <td style="text-align:right;">$<span id="attendeeCost">0.00</span></td>
    </tr>
    
    <?
    for ($i=1; $i<=$maxGuests; $i++){
    ?>
    <tr id="payrowguest<? echo $i; ?>" style="display: none;">
      <td><span id="guest<? echo $i; ?>Name"></span></td>
      <td style="text-align:right;">$<span id="guest<? echo $i; ?>Cost">0.00</span></td>
    </tr>
    <?
    }
    ?>
    <tr>
      <td>&nbsp;</td>
      <td style="text-align:right;"><b>Total:</b> $<span id="totalCost">0.00</span></td>
    </tr>
  </tbody>
</table>

<div class="clear"></div>

<div class="center">
<input type="submit" name="Submit"  class="button" value="Submit" ID="submitButton" onclick="this.disabled=true; this.form.submit();" />
</div>

</form>  

<div class="clear"></div>
<br />
<center>
<iframe width="300" height="60" name="status" frameborder="0" hspace="0" vspace="0" SCROLLING="NO"></iframe>
</center>
        
<?
require("footer.php");
?>

<?
}elseif(isset($_POST['totalCostPass'])){
  
  require("header.php");
  
  $totalCost = $_POST['totalCostPass'];
  $subid = $_POST['subid'];
  
  ?>
  <div class="success">Thank your for your registration!</div>
  
  <center><h1>Payment Information</h1></center>
  
  <div class="descLeft">

  <center><h2>Check</h2></center>
  Send check payable to Coastercrew for $<? echo number_format($totalCost,2); ?> to:<br />
  Coaster Crew<br />
  C/O Tim Holloran<br />
  3 Germano Dr.<br />
  Danvers, MA 01923<br /><br />
  Please write "<? echo $subid; ?>" in the memo.
  
  </div>

  <div class="descLeft">
  
  <center><h2>Credit Card / Paypal</h2></center>
    
  <center>
  <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="clintnovak@gmail.com">
    <input type="hidden" name="lc" value="US">
    <input type="hidden" name="item_name" value="Coastercrew Registration Fee">
    <input type="hidden" name="item_number" value="<? echo $subid; ?>">
    <input type="hidden" name="amount" value="<? echo number_format($totalCost,2); ?>">
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="button_subtype" value="services">
    <input type="hidden" name="shipping" value="0.00">
    <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHosted">
    <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
    <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
  </form>
  <br />
  You do <b>not</b> need a PayPal account to pay via PayPal.
  </center>
    
  </div>
        
  <?
  
  require("footer.php");

}else{
  ?>
  <html>
  <head>
    <link href="/style.css" rel="stylesheet" type="text/css" media="screen" />
  </head>
  <body style="background:#ffffff;">
  <?
  $reenable = "
              <script type=\"text/javascript\">
              window.onload=function(){
                      parent.document.getElementById('submitButton').disabled = false;
              }
              </script>
              ";
                            
  $errorPre = "$reenable <div class=\"validation\">";
  $errorPost = "</div>";
  
  // Remove spam bots
  if($_POST['username']){
    die();
  }

  while(list($k,$v)=each($_POST)){
    if(!is_array($v)){
      if(get_magic_quotes_gpc()){
        $$k = htmlentities(strip_tags(stripslashes($v)));
      }else{
        $$k = htmlentities(strip_tags($v));
      }
    }else{
      $$k = $v;
    }
  }
  if(empty($fullname)){
    die("$errorPre Name for primary member is missing  $errorPost");
  }elseif(empty($email)){
    die("$errorPre Email for primary member is missing  $errorPost");
  }elseif(empty($phone)){
    die("$errorPre Phone number for primary member is missing  $errorPost");
  }elseif(empty($address)){
    die("$errorPre Address for primary member is missing  $errorPost");
  }elseif($attendeeEventFee == ""){
    die("$errorPre Membership type for primary member is missing  $errorPost");
  }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    die("$errorPre Invalid email address for primary member $errorPost");
  }elseif(empty($birthday)){
    die("$errorPre Birthday for primary member is missing  $errorPost");
  }elseif(empty($homepark)){
    die("$errorPre Home Park for primary member is missing  $errorPost");
  }elseif($birthday == "mm/dd/yyyy"){
    die("$errorPre Birthday for primary member is missing  $errorPost");
  }elseif(empty($forumusername)){
    die("$errorPre KI Central forum username missing  $errorPost");
  }
  
  for ($i=1; $i<=$maxGuests; $i++){
        $varnameName = "guest".$i."_fullname";
        $varnameBirthday = "guest".$i."birthday";
        if($$varnameName != ""){
          if(empty($$varnameName)){
            die("$errorPre Name for family member ".$i." is missing  $errorPost");
          }elseif(empty($$varnameBirthday)){
            die("$errorPre Birthday for family member ".$i." is missing  $errorPost");
          }elseif($$varnameBirthday == "mm/dd/yyyy"){
            die("$errorPre Birthday for family member ".$i." is missing  $errorPost");
          }
        }
  }

  $totalCost = 0;
  
  $subid = rand_string(5);
  
  $message = "<html><body>";
  
  $message .= "
  <b>Submission ID:</b> ".$subid."<br />
  <br />
  <b>Name:</b> ".$fullname." <br />
  <b>Email:</b> ".$email." <br />
  <b>Forum Username:</b>  ".$forumusername." <br />
  <b>Phone:</b> ".$phone." <br />
  <b>Badge:</b> ".$badge." <br />
  <b>Address:</b> <br /> ".nl2br($address)."<br />
  <b>Birthday:</b> ".$birthday." <br />
  <b>Home Park:</b> ".$homepark." <br />
  <b>Membership Type:</b> ".$attendeeEventFee." <br />
  ";
  
  $cost = explode("- $", $attendeeEventFee);
  $totalCost += $cost[1];
   
  if(!empty($additionalItems)){
    $message .= "
    <b>Additonal Items:</b> <br />";
    foreach( $additionalItems as $key => $value){
      $message .= $value . "<br />";
      $cost = explode("- $", $value);
      $totalCost += $cost[1];
    }
  }
   
  $message .= "<br />";
  
  for ($i=1; $i<=$maxGuests; $i++){
        $varnameName = "guest".$i."_fullname";
        $varnameBirthday = "guest".$i."birthday";
        $varnameAdditionalItems = "guest".$i."additionalItems";
        if($$varnameName != ""){
          $message .= "
            <b>Family ".$i." Name: </b> ".$$varnameName." <br />
            <b>Family ".$i." Birthday: </b> ".$$varnameBirthday." <br />
          ";
          
          if(!empty($$varnameAdditionalItems)){
            $message .= "
            <b>Additonal Items:</b> <br />";
            foreach($$varnameAdditionalItems as $key => $value){
              $message .= $value . "<br />";
              $cost = explode("- $", $value);
              $totalCost += $cost[1];
            }
          }
          
          $message .= "<br />";
        }
  }

  $message .= "<b>Total Cost:</b> $".number_format($totalCost,2)."";
  
  $message .= "</body></html>";
  
  $emailTo = array($email,'hollorantimothy@yahoo.com','matthew.rydzik@gmail.com');
  //$emailTo = array($email,'matthew.rydzik@gmail.com');
  
  foreach( $emailTo as $key => $value){
    $mail = new PHPMailer();
    $mail->AddAddress($value, $value);
    $mail->SetFrom('tim@coastercrew.net', 'Coaster Crew');
    $mail->AddReplyTo('hollorantimothy@yahoo.com', 'Coaster Crew');
    $mail->Subject = "KI Central: " . $eventName . " Registration";
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
    $mail->MsgHTML($message);
    $mail->Send();;
  }     
  ?>
   <form name="forward" id="forward" action="<? echo $actionPage; ?>" method="POST" enctype="multipart/form-data" target="_parent">
      <input type="hidden" name="totalCostPass" value="<? echo $totalCost;?>">
      <input type="hidden" name="subid" value="<? echo $subid;?>">
      <input type="submit" style="display:none;">
    </form>
    <script type="text/javascript">
            document.forward.submit();
    </script>

  </body>
  </html>
  <?
  
}
?>  
