function wbsf_edit_form()
{
    /* Load Attributes Here */
    $wbsf_token = $_REQUEST['wbsf_token'];
    $form_type = $_REQUEST['form_type'];
    $entry = wbsf_get_entry_by_token($form_type, $wbsf_token);
    $entry_id = $entry->edi;
    $form_id = $entry->fid;
    $form = wbsf_get_form($form_id);
    $entry_mode = $_REQUEST['wbsf_entry_mode'];
    if (!$entry_mode) {
        $entry_mode = 'submit';
    }
    if (isset($_POST['wbsf-update-submit']) && !empty($_POST['wbsf-update-submit'])) {
        $entry_id = $_POST['eid'];
        if (!empty($entry_id) && !empty($wbsf_token) && !empty($form_type)) {
            $data = array();
            if ($form_type == 1) {
                $child_names = $_POST['child_name'];
                $child_dobs = $_POST['child_dob'];
                $data['parent_name'] = $_POST['parent_name'];
                $data['email'] = $_POST['email'];
                $data['parent_dob'] = $_POST['parent_dob'];
                $data['address'] = $_POST['address'];
                $data['city'] = $_POST['city'];
                $data['state'] = $_POST['state'];
                $data['zip'] = $_POST['zip'];
                $data['children'] = wbsf_serialize($child_names, $child_dobs);
            } else {
                if ($form_type == 2) {
                    $child_names = $_POST['child_name'];
                    $child_dobs = $_POST['child_dob'];
                    $data['parent_name'] = $_POST['parent_name'];
                    $data['email'] = $_POST['email'];
                    $data['parent_dob'] = $_POST['parent_dob'];
                    $data['address'] = $_POST['address'];
                    $data['city'] = $_POST['city'];
                    $data['state'] = $_POST['state'];
                    $data['zip'] = $_POST['zip'];
                    $data['children'] = wbsf_serialize($child_names, $child_dobs);
                } else {
                    if ($form_type == 3) {
                        $data['school_name'] = $_POST['school_name'];
                        $data['school_type'] = $_POST['school_type'];
                        $data['school_zip'] = $_POST['school_zip'];
                        $data['parent_name'] = $_POST['parent_name'];
                        $data['email'] = $_POST['email'];
                        $data['parent_dob'] = $_POST['parent_dob'];
                        $data['address'] = $_POST['address'];
                        $data['city'] = $_POST['city'];
                        $data['state'] = $_POST['state'];
                        $data['zip'] = $_POST['zip'];
                    }
                }
            }
            $data['fid'] = $_POST['fid'];
            $data['token'] = $wbsf_token;
            $data['confirm_status'] = 1;
            $entry_id = wbsf_update_entry($form_type, $entry_id, $data);
            if ($entry_id) {
                wbsf_mail_user($form_type, $entry_id);
                wbsf_mail_admin($form_type, $entry_id);
                $url = wbsf_url('wbsf_token=' . $wbsf_token . '&wbsf_entry_mode=success&form_type=' . $form_type);
                wbsf_redirect($url);
            }
        }
    }
    if ($wbsf_token) {
        ?>
<div class="row" id="wbsf_entry_form">
	<div class="col-md-8">
	<h3><?php 
        echo $form->form_title;
        ?>
</h3>

	<?php 
        require_once 'edit_form_type_' . $form_type . '.php';
        //Load Form
        if ($entry_mode == 'success' && !empty($entry)) {
            ?>
	<div class="wbsf_form_fieldset wbsf_success">
			<h4 class="wbsf_form_fieldset_title"><?php 
            echo $entry->parent_name;
            ?>
,</h4>
			<div class="alert alert-info">Thank you! Your registration is confirmed for	<?php 
            echo $form->form_title;
            ?>
</div>
			<p>Your Registration ID: <strong><?php 
            echo $entry->token;
            ?>
</strong></p>
			<p>A confirmation email has been sent with additional details of your registration.</p>
	</div>
	<p>We have sent you an confirmation email to your email address. Please print it or the copy below, and bring it in to a Bookmans location to get started.</p>
	<p><a class="printLink" href="javascript:window.print()">Print your confirmation</a></p>
	
	<?php 
        }
        ?>
	</div>
</div>
<?php 
    }
    // END IF
}
function wbsf_has_entries($form_id)
{
    $form = wbsf_get_form($form_id);
    $entries = wbsf_get_entries($form->form_type, $form_id);
    $count = count($entries);
    if ($count > 0) {
        return true;
    } else {
        return false;
    }
}
function wbsf_entries()
{
    global $wpdb;
    $url = wbsf_get_url('entries');
    //Current page url
    /******************
     Common Requests
    ******************/
    $action = $_REQUEST['action'];
    $form_id = $_REQUEST['fid'];
    $entry_id = $_REQUEST['eid'];
    $form_type = $_REQUEST['form_type'];
    $step = 'entries';
    //Reset to Step
    $status = $_REQUEST['status'];
    if ($action == 'view' && !empty($entry_id) && !empty($form_type)) {
        $entry = wbsf_get_entry($form_type, $entry_id);
    }
    /******************
     Delete Entry Step
    ******************/
    if ($action == 'delete' && !empty($entry_id) && !empty($form_type)) {
        $status = wbsf_delete_entry($form_type, $entry_id);
        if ($status == 1) {
            wbsf_redirect($url);
        }
    }
    ?>
<div class="wrap wbsf_wrap">
	<div class="add_new"><div id="icon-edit-pages" class="icon32"><br></div><h2><?php 
    _e('Bookmans Signup Forms');
    ?>
</h2></div>
	<div class="wbsf_container">
		<?php 
    wbsf_status_info($step, $status);
    ?>
		<?php 
    if ($action == 'view') {
        //Form Add/Edit Area
        ?>
			<div class="wbsf_add_new"><a class="button button-primary" href="<?php 
        echo $url;
        ?>
"><?php 
        _e('Go Back');
        ?>
</a></div>
			<div class="wbsf_form_add">
				<?php 
        require_once 'entry_view_' . $form_type . '.php';
        ?>
			</div>
		<?php 
    } else {
        if (!$action) {
            // List of forms
            ?>
			<div class="wbsf_add_new">
				<form name="wbsf_filter_entries" method="post" action="">
					<select id="form_list" name="fid" class="wbsf_select regular-text large data-required">
					<?php 
            $forms_list = wbsf_get_forms($form_status = 1);
            if (count($forms_list)) {
                foreach ($forms_list as $form_list) {
                    ?>
						<option value="<?php 
                    echo $form_list->fid;
                    ?>
" <?php 
                    if ($form_list->fid == $form_id) {
                        echo 'selected="selected"';
                    }
                    ?>
><?php 
                    echo $form_list->form_title;
                    ?>
</option>
					<?php 
                }
            }
            ?>
					</select>
					<input type="submit" class="button-primary" name="wbsf_filter_entries" value="<?php 
            _e('Filter');
            ?>
" />
				</form>
			</div>
			<table class="table table-striped table-bordered" id="wbsf_dyntable">
		        <colgroup>
		            <col class="con0" />
		            <col class="con1" />
		            <col class="con0" />
		            <col class="con1" />
		            <col class="con0" />
		            <col class="con1" />
		            <col class="con0" />
		            <col class="con1" />
		        </colgroup>
		        <thead>
		            <tr>
		                <th class="head0"><?php 
            _e('Name');
            ?>
</th>
						<th class="head0"><?php 
            _e('Email');
            ?>
</th>
		                <th class="head0"><?php 
            _e('Registered ID');
            ?>
</th>
		                <th class="head1"><?php 
            _e('Assoc. Form');
            ?>
</th>
		                <th class="head1"><?php 
            _e('Form Type');
            ?>
</th>
		                <th class="head1"><?php 
            _e('Status');
            ?>
</th>
		                <th class="head1"><?php 
            _e('View');
            ?>
</th>
		                <th class="head1"><?php 
            _e('Delete');
            ?>
</th>
		            </tr>
		        </thead>
		        <tbody>
					<?php 
            $entries = wbsf_get_entries(1, $form_id);
            if (count($entries)) {
                foreach ($entries as $entry) {
                    $form = wbsf_get_form($entry->fid);
                    ?>
		            <tr class="grade<?php 
                    if ($entry->confirm_status == 0) {
                        echo 'U';
                    } else {
                        echo 'X';
                    }
                    ?>
">
		                <td><?php 
                    echo $entry->parent_name;
                    ?>
</td>
 					    <td><?php 
                    echo $entry->email;
                    ?>
</td>
		                <td><?php 
                    echo $entry->token;
                    ?>
</td>
		                <td><a href="<?php 
                    echo $url;
                    ?>
&fid=<?php 
                    echo $entry->fid;
                    ?>
" class="button"><?php 
                    echo $form->form_title;
                    ?>
</a></td>
		                <td><?php 
                    echo wbsf_form_type($form->form_type);
                    ?>
</td>
		                <td><?php 
                    if ($entry->confirm_status == 1) {
                        echo 'Verified';
                    } else {
                        echo 'Not Verified';
                    }
                    ?>
</td>
		                <td><a href="<?php 
                    echo $url;
                    ?>
&action=view&eid=<?php 
                    echo $entry->eid;
                    ?>
&form_type=<?php 
                    echo $form->form_type;
                    ?>
" class="button"><?php 
                    _e('View');
                    ?>
</a></td>
		                <td><a href="<?php 
                    echo $url;
                    ?>
&action=delete&eid=<?php 
                    echo $entry->eid;
                    ?>
&form_type=<?php 
                    echo $form->form_type;
                    ?>
" class="button wbsf_delete"><?php 
                    _e('Delete');
                    ?>
</a></td>
		            </tr>
							<?php 
                }
            }
            ?>
					<?php 
            $entries = wbsf_get_entries(2, $form_id);
            if (count($entries)) {
                foreach ($entries as $entry) {
                    $form = wbsf_get_form($entry->fid);
                    ?>
		            <tr class="grade<?php 
                    if ($entry->confirm_status == 0) {
                        echo 'U';
                    } else {
                        echo 'X';
                    }
                    ?>
">
		                <td><?php 
                    echo $entry->parent_name;
                    ?>
</td>
 					    <td><?php 
                    echo $entry->email;
                    ?>
</td>
		                <td><?php 
                    echo $entry->token;
                    ?>
</td>
		                <td><a href="<?php 
                    echo $url;
                    ?>
&fid=<?php 
                    echo $entry->fid;
                    ?>
" class="button"><?php 
                    echo $form->form_title;
                    ?>
</a></td>
						<td><?php 
                    echo wbsf_form_type($form->form_type);
                    ?>
</td>
		                <td><?php 
                    if ($entry->confirm_status == 1) {
                        echo 'Verified';
                    } else {
                        echo 'Not Verified';
                    }
                    ?>
</td>
		                <td><a href="<?php 
                    echo $url;
                    ?>
&action=view&eid=<?php 
                    echo $entry->eid;
                    ?>
&form_type=<?php 
                    echo $form->form_type;
                    ?>
" class="button"><?php 
                    _e('View');
                    ?>
</a></td>
		                <td><a href="<?php 
                    echo $url;
                    ?>
&action=delete&eid=<?php 
                    echo $entry->eid;
                    ?>
&form_type=<?php 
                    echo $form->form_type;
                    ?>
" class="button wbsf_delete"><?php 
                    _e('Delete');
                    ?>
</a></td>

		            </tr>
							<?php 
                }
            }
            ?>
					<?php 
            $entries = wbsf_get_entries(3, $form_id);
            if (count($entries)) {
                foreach ($entries as $entry) {
                    $form = wbsf_get_form($entry->fid);
                    ?>
		            <tr class="grade<?php 
                    if ($entry->confirm_status == 0) {
                        echo 'U';
                    } else {
                        echo 'X';
                    }
                    ?>
">
		                <td><?php 
                    echo $entry->parent_name;
                    ?>
</td>
 					    <td><?php 
                    echo $entry->email;
                    ?>
</td>
		                <td><?php 
                    echo $entry->token;
                    ?>
</td>
		                <td><a href="<?php 
                    echo $url;
                    ?>
&fid=<?php 
                    echo $entry->fid;
                    ?>
" class="button"><?php 
                    echo $form->form_title;
                    ?>
</a></td>
						<td><?php 
                    echo wbsf_form_type($form->form_type);
                    ?>
</td>
		                <td><?php 
                    if ($entry->confirm_status == 1) {
                        echo 'Verified';
                    } else {
                        echo 'Not Verified';
                    }
                    ?>
</td>
		                <td><a href="<?php 
                    echo $url;
                    ?>
&action=view&eid=<?php 
                    echo $entry->eid;
                    ?>
&form_type=<?php 
                    echo $form->form_type;
                    ?>
" class="button"><?php 
                    _e('View');
                    ?>
</a></td>
		                <td><a href="<?php 
                    echo $url;
                    ?>
&action=delete&eid=<?php 
                    echo $entry->eid;
                    ?>
&form_type=<?php 
                    echo $form->form_type;
                    ?>
" class="button wbsf_delete"><?php 
                    _e('Delete');
                    ?>
</a></td>

		            </tr>
							<?php 
                }
            }
            ?>
		        </tbody>
      		</table>
		<?php 
        }
    }
    ?>
			
		
	</div> <!-- end container -->
</div> <!-- end wrapper -->
<?php 
}
function wbsf_forms()
{
    global $wpdb;
    $url = wbsf_get_url('forms');
    //Current page url
    /******************
     Common Requests
    ******************/
    $action = $_REQUEST['action'];
    $step = 'forms';
    //Reset to Step
    $status = $_REQUEST['status'];
    if (!$status && !$action) {
        $status = 'info';
        //Reset to Status for Step
    }
    $form_id = $_REQUEST['fid'];
    if ($action == 'delete' && !empty($form_id)) {
        $status = wbsf_delete_form($form_id);
        if ($status == 1) {
            wbsf_redirect($url);
        }
    }
    if ($action == 'edit' && !empty($form_id)) {
        $form = wbsf_get_form($form_id);
    }
    /**************
     Create Form Step
    ***************/
    if (isset($_REQUEST['form_add'])) {
        if (!empty($_REQUEST['form_add'])) {
            $data = array();
            $data['form_title'] = $_POST['form_title'];
            $data['form_type'] = $_POST['form_type'];
            $data['form_prefix'] = $_POST['form_prefix'];
            $data['form_status'] = 1;
            $form_id = wbsf_create_form($data);
            if ($form_id) {
                wbsf_redirect($url);
            }
        }
    }
    /**************
     Update Form Step
    ***************/
    if (isset($_REQUEST['form_update'])) {
        if (!empty($_REQUEST['form_update'])) {
            $data = array();
            $data['form_title'] = $_POST['form_title'];
            $data['form_type'] = $_POST['form_type'];
            $data['form_prefix'] = $_POST['form_prefix'];
            $data['form_status'] = 1;
            $form_id = wbsf_update_form($form_id, $data);
            if ($form_id) {
                wbsf_redirect($url);
            }
        }
    }
    ?>
<div class="wrap wbsf_wrap">
	<div class="add_new"><div id="icon-edit-pages" class="icon32"><br></div><h2><?php 
    _e('Bookmans Signup Forms');
    ?>
</h2></div>
	<div class="wbsf_container">
		<?php 
    wbsf_status_info($step, $status);
    ?>
		<?php 
    if ($action == 'edit' || $action == 'add') {
        //Form Add/Edit Area
        ?>
			<div class="wbsf_form_add">
				<form method="post" name="form_action" action="" id="form_action">
					<table id="general-tab" class="form-table wbsf_setting_table">
						<tr valign="top">
							<th scope="row"><?php 
        _e('Form Title');
        ?>
</th>
							<td><input type="text" id="form_title" name="form_title" id="wbsf-submit" class="wbsf_input regular-text data-required" value="<?php 
        echo $form->form_title;
        ?>
"  /></td>
						</tr>
				<tr valign="top"><th scope="row"><?php 
        _e('Form Type');
        ?>
</th>
					<td><select id="form_type" class="regular-text large data-required" name="form_type">
							<option value=""><?php 
        echo esc_attr(__('Select'));
        ?>
</option>
							<?php 
        for ($i = 1; $i <= 3; $i++) {
            ?>
							<option value="<?php 
            echo $i;
            ?>
" <?php 
            if ($i == $form->form_type) {
                echo 'selected="selected"';
            }
            ?>
><?php 
            echo wbsf_form_type($i);
            ?>
</option>
							<?php 
        }
        ?>
						</select>
					</td>
				</tr>
				<tr valign="top">
					<th scope="row"><?php 
        _e('Registered ID Prefix');
        ?>
</th>
					<td><input type="text" id="form_prefix" name="form_prefix" id="wbsf-submit" class="wbsf_input regular-text data-required" value="<?php 
        echo $form->form_prefix;
        ?>
"  /></td>
				</tr>
			</table>

		<p class="submit">
					<?php 
        if ($action == 'add') {
            ?>
<input type="submit" id="wbsf-submit" class="button-primary" name="form_add" value="<?php 
            _e('Create');
            ?>
" /><?php 
        } else {
            ?>
<input type="submit" class="button-primary" id="wbsf-submit" name="form_update" value="<?php 
            _e('Update');
            ?>
" /><?php 
        }
        ?>
					<a class="button" href="<?php 
        echo $url;
        ?>
"><?php 
        _e('Cancel');
        ?>
</a>
		</p>
				</form>
			</div>
		<?php 
    } else {
        if (!$action) {
            // List of forms
            ?>
			<div class="wbsf_add_new"><a class="button button-primary" href="<?php 
            echo $url;
            ?>
&action=add"><?php 
            _e('Add new');
            ?>
</a></div>
			<table class="table table-striped table-bordered" id="wbsf_dyntable">
		        <colgroup>
		            <col class="con0" />
		            <col class="con1" />
		            <col class="con0" />
		            <col class="con1" />
		        </colgroup>
		        <thead>
		            <tr>
		                <th class="head0"><?php 
            _e('Form Title');
            ?>
</th>
						<th class="head0"><?php 
            _e('Short Code');
            ?>
</th>
		                <th class="head1"><?php 
            _e('Entries');
            ?>
</th>
						<th class="head1"><?php 
            _e('Form Type');
            ?>
</th>
		                <th class="head0"><?php 
            _e('Edit');
            ?>
</th>
		                <th class="head1"><?php 
            _e('Delete');
            ?>
</th>
		            </tr>
		        </thead>
		        <tbody>
						<?php 
            $forms = wbsf_get_forms($form_status = 1);
            if (count($forms)) {
                foreach ($forms as $form) {
                    ?>
		            <tr class="gradeX">
		                <td><?php 
                    echo $form->form_title;
                    ?>
</td>
 					    <td><input type="text" value="[wbsf form=<?php 
                    echo $form->fid;
                    ?>
]" class="shortcode"  /></td>
		                <td><a href="<?php 
                    echo wbsf_get_url('entries');
                    ?>
&fid=<?php 
                    echo $form->fid;
                    ?>
" class="button"><?php 
                    _e('View Entries');
                    ?>
 (<?php 
                    echo wbsf_get_entries_count($form->fid);
                    ?>
)</a></td>
		                <td><?php 
                    echo wbsf_form_type($form->form_type);
                    ?>
</td>
		                <td><a href="<?php 
                    echo $url;
                    ?>
&action=edit&fid=<?php 
                    echo $form->fid;
                    ?>
" class="button"><?php 
                    _e('Edit');
                    ?>
</a></td>
		                <td><a href="<?php 
                    echo $url;
                    ?>
&action=delete&fid=<?php 
                    echo $form->fid;
                    ?>
" class="button wbsf_delete"><?php 
                    _e('Delete');
                    ?>
</a></td>
		            </tr>
							<?php 
                }
            }
            ?>
		        </tbody>
      		</table>
		<?php 
        }
    }
    ?>
			
		
	</div> <!-- end container -->
</div> <!-- end wrapper -->
<?php 
}