<?php


/*

Copyright 2018 Murray Hayes

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/


session_start();

include_once "utils.php";
include_once "DOMUtils.php";
include_once "consts.php";
include_once "database.php";
include_once "applicationFormUtils.php";



if (isset($_SESSION['cookieMonster']))
//if (true)
{
  $doc = returnDoc();
  
  $root = returnRoot($doc);
  $root = $doc->appendChild($root);
  $root->appendChild(generateHead($doc));
  
  $body = $doc->createElement('body');
  $root->appendChild($body);
  
  $body->appendChild(generateMastHead($doc, $baseDir));
  
  if (!isset($_SESSION['login']) || $_SESSION['login'] === FALSE)
      
  /*
   *  Insert content here.
   */

    if ($_SERVER['REQUEST_METHOD'] === 'POST')
    {
      if (isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email']) && isset($_POST['monthly_fees'])
          && isset($_POST['application_date']) && isset($_POST['password1']) && isset($_POST['password2'])
          && ($_POST['password1'] === $_POST['password2']))
      {
        try
        {
          /*
           
          $newMemberID = insertIntoMemberTable();
          $newMemberID = returnIdFromUsername($_)
          */
          if (insertIntoMemberTable())
          {
            /*
             * Print Application Form PDF
             */
            $newMemberID = returnIdFromUsername($_POST['first_name'] . "." . $_POST['last_name']);
            $pdf = new ApplicationForm(getMemberRowData($newMemberID));
            $pdf->Output();
            
            //printApplicationForm();
          }
        }
        catch (Exception $e)
        {
          if ($e->getCode() == 23000)
          {
            $h1 = $doc->createElement('h1');
            $h1->appendChild($doc->createTextNode('A user by that exact name already exists.'));
            $body->appendChild($h1);
          }
        }
      }
      else
      {
        print ("short\n");
      }
    }
    else
    {
      $editorDiv = $doc->createElement('div');
      $editorDiv->setAttribute('id', 'MemberSignupDiv');
      $form = $doc->createElement('form');
      $form->setAttribute('action', 'signup.php');
      $form->setAttribute('method', 'post');
      $label = $doc->createElement('H3');
      $label->appendChild($doc->createTextNode("New Member Signup"));
      $form->appendChild($label);
      
      $fieldSet = $doc->createElement('fieldset');
      //$fieldSet->setAttribute('style', 'width:200px; max-width:300px;'); //margin-top:0.5em;');
      $fieldSetDiv = $doc->createElement('div');
      
      
      $label = $doc->createElement('label', 'First Name:');
      $label->setAttribute('for', 'first_name');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'first_name');
      $input->setAttribute('autocomplete', 'off');
      $input->setAttribute('required', 'required');
      if (isset($_POST['first_name']))
        $input->setAttribute('value', $_POST['first_name']);
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
    
      $label = $doc->createElement('label', 'Middle Name:');
      $label->setAttribute('for', 'middle_name');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'middle_name');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['middle_name']))
        $input->setAttribute('value', $_POST['middle_name']);
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Last Name:');
      $label->setAttribute('for', 'last_name');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'last_name');
      $input->setAttribute('autocomplete', 'off');
      $input->setAttribute('required', 'required');
      if (isset($_POST['last_name']))
        $input->setAttribute('value', $_POST['last_name']);
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Prefered Name:');
      $label->setAttribute('for', 'prefered_name');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'prefered_name');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['prefered_name']))
        $input->setAttribute('value', $_POST['prefered_name']);
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Nick Name:');
      $label->setAttribute('for', 'nick_name');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'nick_name');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['nick_name']))
        $input->setAttribute('value', $_POST['nick_name']);
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'EMail Address:');
      $label->setAttribute('for', 'email');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'email');
      $input->setAttribute('name', 'email');
      $input->setAttribute('autocomplete', 'off');
      $input->setAttribute('class', 'MemberEditorInputField');
      $input->setAttribute('required', 'required');
      if (isset($_POST['email']))
        $input->setAttribute('value', $_POST['email']);
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
/*      
      $label = $doc->createElement('label', 'Expiration Date:');
      $label->setAttribute('for', 'expire');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'date');
      $input->setAttribute('name', 'expire');
      $input->setAttribute('autocomplete', 'off');
        if (isset($_POST['expire']))
      $input->setAttribute('value', $_POST['expire']);
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Current Start Date:');
      $label->setAttribute('for', 'current_start');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'date');
      $input->setAttribute('name', 'current_start');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['current_start_date']))
        $input->setAttribute('value', $_POST['current_start_date']);
      $input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
  */
      
      $label = $doc->createElement('label', 'Application Date:');
      $label->setAttribute('for', 'application_date');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'date');
      $input->setAttribute('name', 'application_date');
      $input->setAttribute('autocomplete', 'off');
      $currentTime = date('Y-m-d');
      if (isset($_POST['application_date']))
        $input->setAttribute('value', $_POST['application_date']);
      else
        $input->setAttribute('value', "$currentTime");
      $input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
  
      /*
      $label = $doc->createElement('label', 'Status:');
      $label->setAttribute('for', 'status');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'status');
      //$input->setAttribute('autocomplete', 'off');
      if (isset($_POST['status']))
        $input->setAttribute('value', $_POST['status']);
      $input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      */
      
      $label = $doc->createElement('label', 'Monthly Rate:');
      $label->setAttribute('for', 'monthly_fees');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('select');
      //$input->setAttribute('type', 'select');
      $input->setAttribute('name', 'monthly_fees');
      $option = $doc->createElement('option');
      $option->setAttribute('value', '55');
      $option->setAttribute('selected', 'selected');
      $option->appendChild($doc->createTextNode("$55.00"));
      $input->appendChild($option);
      $option = $doc->createElement('option');
      $option->setAttribute('value', '35.00');
      $option->appendChild($doc->createTextNode("$35.00"));
      $input->appendChild($option);
      
/*      $input->setAttribute('type', 'number');
      $input->setAttribute('name', 'monthly_fees');
      $input->setAttribute('autocomplete', 'off');
      $input->setAttribute('step', '0.01');
      $input->setAttribute('pattern', '^\d+(\.|\,)\d{2}$');
      $input->setAttribute('value', '55');*/
      $input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Birth Date:');
      $label->setAttribute('for', 'birth_date');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'date');
      $input->setAttribute('name', 'birth_date');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['birth_date']))
        $input->setAttribute('value', $_POST['birth_date']);
      //$input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Minor:');
      $label->setAttribute('for', 'minor');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'checkbox');
      $input->setAttribute('name', 'minor');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['minor']) /* ||  ($_POST['birth_date'] - 18)  */)
        $input->setAttribute('value', $_POST['minor']);
      /* $input->setAttribute('required', 'required'); */
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Guardian:');  
      $label->setAttribute('for', 'guardian');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'guardian');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['guardian']))
        $input->setAttribute('value', $_POST['guardian']);
      //$input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Street Address:');
      $label->setAttribute('for', 'address');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'address');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['street_address']))
        $input->setAttribute('value', $_POST['street_address']);
      //$input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'City:');
      $label->setAttribute('for', 'city');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'city');
      //$input->setAttribute('autocomplete', 'off');
      if (isset($_POST['city']))
        $input->setAttribute('value', $_POST['city']);
      //$input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Province:');
      $label->setAttribute('for', 'province');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'province');
      //$input->setAttribute('autocomplete', 'off');
      if (isset($_POST['province']))
        $input->setAttribute('value', $_POST['province']);
      //$input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Postal Code:');
      $label->setAttribute('for', 'postal_code');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'postal_code');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['postal_code']))
        $input->setAttribute('value', $_POST['postal_code']);
      //$input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Phone:');
      $label->setAttribute('for', 'phone');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'phone');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['phone']))
        $input->setAttribute('value', $_POST['phone']);
      //$input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Emergency Contact Name:');
      $label->setAttribute('for', 'emergency_name');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'emergency_name');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['emergency_contact_name']))
        $input->setAttribute('value', $_POST['emergency_contact_name']);
      //$input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      $label = $doc->createElement('label', 'Emergency Contact Number:');
      $label->setAttribute('for', 'emergency_number');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'text');
      $input->setAttribute('name', 'emergency_number');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['emergency_contact_number']))
        $input->setAttribute('value', $_POST['emergency_contact_number']);
      //$input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
      
      
      $label = $doc->createElement('label', 'Password:');
      $label->setAttribute('for', 'password1');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'password');
      $input->setAttribute('name', 'password1');
      $input->setAttribute('autocomplete', 'off');
      $input->setAttribute('required', 'required');
      if (isset($_POST['password1']))
        $input->setAttribute('value', $_POST['password1']);
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
        
      
      $label = $doc->createElement('label', 'Confirm:');
      $label->setAttribute('for', 'password2');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'password');
      $input->setAttribute('name', 'password2');
      $input->setAttribute('autocomplete', 'off');
      $input->setAttribute('required', 'required');
      $fieldSetDiv->appendChild($input);
      $label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);
        
      
      $label = $doc->createElement('label', 'Member Notes:');
      $label->setAttribute('for', 'member_notes');
      $label->setAttribute('class', 'MemberEditorInputLabel');
      $fieldSetDiv->appendChild($label);
      $input = $doc->createElement('textarea');
      //$input->setAttribute('type', 'textarea');
      $input->setAttribute('name', 'member_notes');
      $input->setAttribute('rows', '3');
      $input->setAttribute('cols', '50');
      $input->setAttribute('autocomplete', 'off');
      if (isset($_POST['member_notes']))
        $input->setAttribute('value', $_POST['member_notes']);
      //$input->setAttribute('required', 'required');
      
      /* A child node is required to create a proper closing tag for <textarea>*/
      $input->appendChild($doc->createTextNode(""));
      $fieldSetDiv->appendChild($input);
      /*$label = $doc->createElement('br');
      $fieldSetDiv->appendChild($label);*/
      
      $fieldSet->appendChild($fieldSetDiv);
      
      $form->appendChild($fieldSet);
      
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'reset');
      $form->appendChild($input);
      
      $input = $doc->createElement('input');
      $input->setAttribute('type', 'submit');
      $form->appendChild($input);
      $editorDiv->appendChild($form);
      $body->appendChild($editorDiv);
    }
  $body->appendChild(generateFooter($doc));
  
  outputDoc($doc);
}
else
{
  generateCookieMonster();
}

?>