*/
 define('GLM_MEMBERS_BILLING_MEMBER_MENU', true);
 require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/data/dataInvoices.php';
-// require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/data/dataPayments.php';
 require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/billingSupport.php';
 
-// Load Billing data abstract
-// require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/data/dataBilling.php';
-
 class GlmMembersFront_billing_renew // extends GlmDataBilling
 {
 
 
     }
 
-    public function modelAction($actionData = false)
+    public function modelAction( $actionData = false )
     {
         // Initialize Variables Here
-        $fromMemberMenu     = false;
-        $this->memberID     = false;
-        $memberData         = false;
-        $memberName         = false;
-        $haveMember         = false;
-        $haveAccount        = false;
-        $option             = 'renew';
-        $view               = 'renew';
-        $account            = false;
-        $accountID          = 0;
-        $account_data       = false;
-        $balance_due        = false;
-        $invoiceHtml        = false;
-        $management         = false;
-        $messages           = array();
-        $paymentSuccess     = false;
-        $paymentError       = false;
-        $hasBillingAccount  = false;
-        $employees          = false;
-        $member_invoice     = false;
-        $payable_types      = false;
-        $account_status     = false;
+        $fromMemberMenu    = false;
+        $this->memberID    = false;
+        $memberData        = false;
+        $memberName        = false;
+        $haveMember        = false;
+        $haveAccount       = false;
+        $option            = 'renew';
+        $view              = 'renew';
+        $account           = false;
+        $accountID         = 0;
+        $account_data      = false;
+        $balance_due       = false;
+        $invoiceHtml       = false;
+        $management        = false;
+        $messages          = array();
+        $paymentSuccess    = false;
+        $paymentError      = false;
+        $hasBillingAccount = false;
+        $employees         = false;
+        $member_invoice    = false;
+        $payable_types     = false;
+        $account_status    = false;
+        $unregistered      = false;
 
         // For lockedToMember.
         $lockedToMember = false;
         $lockedWhereT   = 'true';
         $lockedWhere    = 'true';
 
+        // Check for unregistered.
+        if ( isset( $actionData['request']['unregistered'] ) && filter_var( $actionData['request']['unregistered'], FILTER_VALIDATE_BOOLEAN ) ) {
+            $unregistered = filter_var( $actionData['request']['unregistered'], FILTER_VALIDATE_BOOLEAN );
+        }
+
+        echo '<pre>$unregistered: ' . print_r( $unregistered, true ) . '</pre>';
+
         // Call in the support class
         $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config );
-        // echo '<pre>$this->config: ' . print_r( $this->config, true ) . '</pre>';
+
+        // Check if the user is logged in.
         if ( isset( $this->config['loggedInUser'] )
             && isset( $this->config['loggedInUser']['contactUser'] )
             && isset( $this->config['loggedInUser']['contactUser']['ref_dest'] )
         ) {
             $this->memberID = $this->config['loggedInUser']['contactUser']['ref_dest'];
+        } elseif ( $unregistered ) {
+            $option = 'renew';
         } else {
             $option = 'noaccount';
         }
 
         // Check if there's a logged in user who is locked to their own entity.
         $lockedToMember = apply_filters('glm_members_locked_to_member_id', false);
-        // echo '<pre>$lockedToMember: ' . print_r( $lockedToMember, true ) . '</pre>';
+
         if ($lockedToMember) {
             $memberID       = $lockedToMember;
             $this->memberID = $memberID;
             $option = $_REQUEST['option'];
         }
 
-        require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataAccounts.php';
-        $Accounts = new GlmDataAccounts( $this->wpdb, $this->config );
-        $accountID = $this->wpdb->get_var(
-            $this->wpdb->prepare(
-                "SELECT id
-                   FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
-                  WHERE ref_dest = %d",
-                $this->memberID
-            )
-        );
-        $account_status = apply_filters( 'glm-billing-get-account-status', '', $accountID );
+        if ( isset( $memberID ) && $memberID ) {
+            require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataAccounts.php';
+            $Accounts = new GlmDataAccounts( $this->wpdb, $this->config );
+            $accountID = $this->wpdb->get_var(
+                $this->wpdb->prepare(
+                    "SELECT id
+                       FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
+                      WHERE ref_dest = %d",
+                    $this->memberID
+                )
+            );
+            if ( isset( $accountID ) && $accountID ) {
+                $account_status = apply_filters( 'glm-billing-get-account-status', '', $accountID );
+            }
+        }
 
         switch ( $option ) {
         case 'noaccount':
             $view = 'noaccount';
             break;
+
         case 'renew':
             // TODO: only the renew form if the member is not active
             if ( $account_status == 'Active' ) {
                 $view = 'nonrenew';
+            } elseif ( !$account_status && !$unregistered ) {
+                $view = 'noaccount';
             } else  {
                 $view = 'renew';
-                // echo '<pre>$this->memberID: ' . print_r( $this->memberID, true ) . '</pre>';
 
+                if ( $unregistered ) {
+                    $lockedToMember = true;
+                }
 
                 // Get list of payable invoice_types
                 $payable_types = $BillingSupport->getAllPayableInvoiceTypes();
                 if ( $member_invoice_id ) {
                     $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id );
                 }
-                // echo '<pre>$member_invoice: ' . print_r( $member_invoice, true ) . '</pre>';
 
-                // Get a list of this accounts employees. If they have any.
-                $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
-                // echo '<pre>$employees: ' . print_r( $employees, true ) . '</pre>';
+                if ( $this->config['settings']['allow_employees'] ) {
+                    // Get a list of this accounts employees. If they have any.
+                    $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+                }
 
                 // Load DataClass for Management.
                 require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataManagement.php';
                 $Management = new GlmDataBillingManagement( $this->wpdb, $this->config );
                 $management = $Management->getEntry( 1 );
 
-                // Need to see if there's an account for this member.
-                require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataAccounts.php';
-                $account = $Accounts->editEntry( $accountID );
+                if ( $accountID ) {
+                    // Need to see if there's an account for this member.
+                    require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataAccounts.php';
+                    $account = $Accounts->editEntry( $accountID );
+                }
             }
 
             break;
             $error = false;
             $view  = 'renewMembership';
 
+            // Check for account_number
+            // If given then this is from unregistered user.
+            // Have to verify that this number does match with billing_account.
+            if ( isset( $_REQUEST['account_number'] ) && $account_number = filter_var( $_REQUEST['account_number'], FILTER_SANITIZE_STRING ) ) {
+                // Verify account number.
+                $accountID = $this->wpdb->get_var(
+                    $this->wpdb->prepare(
+                        "SELECT id
+                           FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
+                          WHERE account_number = %s",
+                        $account_number
+                    )
+                );
+            }
+
             $member_invoice_id = $BillingSupport->getMembersInvoiceTypeByRefDest( $this->memberID );
             if ( $member_invoice_id ) {
                 $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id );
                 $error = true;
             }
 
-            $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+            if ( $this->config['settings']['allow_employees'] ) {
+                $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+            }
 
             $invoice_data = array(
                 'account_id'     => $accountID,
                 $error = true;
             }
 
+            // Start database transaction
+            $this->wpdb->show_errors();
+            $this->wpdb->query('START TRANSACTION');
+
             // Before we create an invoice for this member see if they already have one.
             // If we find one then delete it.
             $old_invoice_ids = $this->wpdb->get_results(
                     $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id );
                 }
 
-                // Get a list of this accounts employees. If they have any.
-                $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+                if ( $this->config['settings']['allow_employees'] ) {
+                    // Get a list of this accounts employees. If they have any.
+                    $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+                }
 
                 // Load DataClass for Management.
                 require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataManagement.php';
                 // Need to see if there's an account for this member.
                 $account = $Accounts->editEntry( $accountID );
             } else {
+
                 // Look at the payment option given
                 $payment_option = filter_var( $_REQUEST['payment_option'], FILTER_SANITIZE_STRING );
 
                             $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id );
                         }
 
-                        // Get a list of this accounts employees. If they have any.
-                        $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+                        if ( $this->config['settings']['allow_employees'] ) {
+                            // Get a list of this accounts employees. If they have any.
+                            $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
+                        }
 
                         // Load DataClass for Management.
                         require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataManagement.php';
 
                 // If there's no error reported then show the invoice
                 if ( !$error ) {
+                    $this->wpdb->query( 'COMMIT' );
                     // Now need to show the invoice.
                     $view = 'viewInvoice';
 
                             );
                         }
                     }
+                } else {
+                    $this->wpdb->query( 'ROLLBACK' );
                 }
             }
 
 
                     <input id="glm_member_name" type="text" name="ref_name" value="">
                 </td>
             </tr>
+            <tr>
+                <th class="glm-required" align="right">Account Number</th>
+                <td>
+                    <input type="text" name="account_number" value="">
+                </td>
+            </tr>
             <tr>
                 <th class="glm-required" align="right">Anniversary Date</th>
                 <td>
         line_items        = [],                                   // Array holding line_items json objects
         allFields         = $( [] ).add( line_item_type ),        // Array holding the form fields - add line_item form
         ref_name          = $( 'input[name="ref_name"' ),
+        account_number    = $( 'input[name="account_number"' ),
         anniversary_date  = $( 'input[name="anniversary_date"' ),
         renewal_date      = $( 'input[name="renewal_date"' ),
         email             = $( 'input[name="email"' ),
         lineTips          = $( '.validateTips' ),
         accountTips       = $( '.validateAccountTips' ),
         customTips        = $( '.validateCustomTips' ),
-        allAccountFields  = $( [] ).add( ref_name ) .add( anniversary_date ) .add( renewal_date )
+        allAccountFields  = $( [] ).add( ref_name ) .add( account_number ) .add( anniversary_date ) .add( renewal_date )
             .add( email ) .add( billing_addr1 ) .add( billing_addr2 ) .add( billing_city )
             .add( billing_state ) .add( billing_zip ) .add(billing_county)
             {if $settings.billing_contact_name_enabled}.add( billing_contact_name ){else}.add( billing_fname ).add( billing_lname ){/if}
         allAccountFields.removeClass( 'ui-state-error' );
 
         valid = valid && checkRequired( ref_name, accountTips, 'Name is required!' );
+        valid = valid && checkRequired( account_number, accountTips, 'Account Number is required!' );
         valid = valid && checkRequired( anniversary_date, accountTips, 'Anniversary Date is required!' );
         valid = valid && checkRequired( email, accountTips, 'Email is required!' );
         valid = valid && checkRegexp( email, emailRegex, 'Use valid email!', accountTips );
                     // Close the dialog
                     newAccountDialog.dialog( 'close' );
                 } else {
-                    //console.log( 'return', msg );
+                    // Check for unqique account number
+                    if ( msg.fieldFail.account_number ) {
+                        alert( 'Account Number must be unique!' );
+                    }
+                    console.log( 'return', msg );
                 }
             });
         }
 
         <input type="hidden" name="account_id" value="{$account_data.id}" />
         <input type="hidden" id="total_renew_amount" name="total_renew_amount" value="" />
 
-
         {if $member_invoice}
-        <div class="glm-billing-field">
-            <div class="glm-billing-label glm-required">
-                Membership Class
+            <div class="glm-billing-field">
+                <div class="glm-billing-label glm-required">
+                    Membership Class
+                </div>
+                <div class="glm-billing-input">
+                    {if $settings.allow_membership_choice}
+                        {foreach $payable_types as $type}
+                            <label>
+                                <input type="radio" name="member_renewing" data-amount="{$type.amount}" value="{$type.id}"
+                                {if $type.id == $member_invoice.id}checked{/if}
+                                />
+                                {$type.name} {$type.amount}
+                            </label>
+                        {/foreach}
+                    {else}
+                        <input type="hidden" name="member_renewing" data-amount="{$member_invoice.amount}" value="{$member_invoice.id}" />
+                        {$member_invoice.name} {$member_invoice.amount}
+                    {/if}
+                </div>
             </div>
-            <div class="glm-billing-input">
-                {if $settings.allow_membership_choice}
-                    {foreach $payable_types as $type}
-                        <label>
-                            <input type="radio" name="member_renewing" data-amount="{$type.amount}" value="{$type.id}"
-                            {if $type.id == $member_invoice.id}checked{/if}
-                            />
-                            {$type.name} {$type.amount}
-                        </label>
-                    {/foreach}
-                {else}
-                    <input type="hidden" name="member_renewing" data-amount="{$member_invoice.amount}" value="{$member_invoice.id}" />
-                    {$member_invoice.name} {$member_invoice.amount}
-                {/if}
+        {else}
+            <div class="glm-billing-field">
+                <div class="glm-billing-label glm-required">
+                    Member Billing #
+                </div>
+                <div class="glm-billing-input">
+                    <input name="account_number" value="" />
+                </div>
+            </div>
+            <div class="glm-billing-field">
+                <div class="glm-billing-label glm-required">
+                    Membership Class
+                </div>
+                <div class="glm-billing-input">
+                    {if $settings.allow_membership_choice}
+                        {foreach $payable_types as $type}
+                            <label>
+                                <input type="radio" name="member_renewing" data-amount="{$type.amount}" value="{$type.id}" />
+                                {$type.name} {$type.amount}
+                            </label>
+                        {/foreach}
+                    {/if}
+                </div>
             </div>
-        </div>
         {/if}
 
         {if $employees}
             </div>
         </div>
 
-{* Include the Payment Form template *}
-{include file='common/billing/paymentForm.html'}
+        {* Include the Payment Form template *}
+        {include file='common/billing/paymentForm.html'}
 
         <input class="button button-primary" type="submit" value="Renew">
 
         currency: 'USD',
         minimunFractionDigits: 2
     });
+
+    // Generate the total for the renewal.
     function getPageTotal(){
 
         // Calculate the total for this page.
         if ( $('input[name=member_renewing]:checked').length ) {
             var member_renewing_amount = parseFloat( $('input[name=member_renewing]:checked').data('amount') );
         } else {
-            var member_renewing_amount = parseFloat( $('input[name=member_renewing]').data('amount') );
+            var member_renewing_amount = parseFloat( $('input[name=member_renewing]:first').data('amount') );
+            $('input[name=member_renewing]:first').prop( 'checked', true );
         }
 
+        // console.log( 'member_renewing_amount', member_renewing_amount );
+
         // Get each employee and add their amounts.
         $('input[name^=employees]').each(function(){
             var isChecked = $(this).prop('checked');