function handle_submission($invoice_type_id)
{
    global $wpdb;
    $user_id = $_POST["user-id"];
    // if on other's behalf, get id of person
    if ($_POST["others_behalf"]) {
        $user_id = $wpdb->get_var($wpdb->prepare("SELECT `ID` FROM `wp_users` WHERE `user_email` = %s;", $_POST["email"]));
    }
    // send user's invoive (or simply the first if submitting for multiple people)
    $invoice_id = generateInvoice($user_id, $invoice_type_id);
    generatePDF($invoice_type_id, $invoice_id, $user_id);
    emailInvoice($invoice_type_id, $invoice_id, $user_id);
    /* Submit the rest of the invoices */
    $num_registering = $_POST["num_registering"];
    for ($i = 1; $i < $num_registering; $i++) {
        $email = "email" . $i;
        $user_id = $wpdb->get_var($wpdb->prepare("SELECT `ID` FROM `wp_users` WHERE `user_email` = %s;", $_POST[$email]));
        $invoice_id = generateInvoice($user_id, $invoice_type_id);
        generatePDF($invoice_type_id, $invoice_id, $user_id);
        emailInvoice($invoice_type_id, $invoice_id, $user_id);
    }
    // for: generate invoice for each user specified (can be multiple)
    // return if success
    return '<p class="cstem_im_form_success"> Form Submitted Successfully. You should receive an email shortly. </p>';
}
function generatePDF($invoice_type_id, $invoiceid)
{
    global $wpdb;
    /* get form type to display in correct pdf format */
    $form_type = $wpdb->get_var($wpdb->prepare("SELECT form_type FROM wp_invoice_type WHERE invoice_type_id = %d", $invoice_type_id));
    $form_name = $wpdb->get_var($wpdb->prepare("SELECT invoice_name FROM wp_invoice_type WHERE invoice_type_id = %d", $invoice_type_id));
    $user_id = $_POST["user-id"];
    if ($_POST["others_behalf"]) {
        $user_email = $_POST["email"];
        $user_id = $wpdb->get_var($wpdb->prepare("SELECT `ID` FROM `wp_users` WHERE `user_email` = %s;", $user_email));
    }
    // if on other's behalf, get id of person
    $invoiceNameRow = $wpdb->get_row($wpdb->prepare("SELECT * FROM `wp_invoice_type` WHERE `invoice_type_id` = %d;", $invoice_type_id));
    $invoiceName = $invoiceNameRow->invoice_name;
    $subdateRow = $wpdb->get_row($wpdb->prepare("SELECT * FROM `wp_invoice_table` WHERE `invoice_no` = %d;", $invoiceid));
    $subdate = $subdateRow->invoice_creation_date;
    //$title = $contact_form->title;
    //$posted_data = $contact_form->posted_data;
    $single = true;
    $key = 'last_name';
    $user_last = get_user_meta($user_id, $key, $single);
    $key = 'first_name';
    $user_first = get_user_meta($user_id, $key, $single);
    $name = $user_first . ' ' . $user_last;
    $key = 'wp_school_id';
    $schoolid = get_user_meta($user_id, $key, $single);
    $key = 'wp_school';
    $school = get_user_meta($user_id, $key, $single);
    $key = 'wp_district';
    $district = get_user_meta($user_id, $key, $single);
    $key = 'wp_county';
    $county = get_user_meta($user_id, $key, $single);
    $key = 'wp_school_address';
    $schooladdr = get_user_meta($user_id, $key, $single);
    $key = 'wp_school_city';
    $schoolcity = get_user_meta($user_id, $key, $single);
    $key = 'wp_school_state';
    $schoolstate = get_user_meta($user_id, $key, $single);
    $key = 'wp_school_zipcode';
    $schoolzip = get_user_meta($user_id, $key, $single);
    $key = 'wp_school_phone_number';
    $schoolphone = get_user_meta($user_id, $key, $single);
    $key = 'wp_phone_number';
    $phone = get_user_meta($user_id, $key, $single);
    $school1 = ' ' . $school;
    $schooladdr1 = ' ' . $schooladdr;
    $schoolcsz1 = ' ' . $schoolcity . ', ' . $schoolstate . ' ' . $schoolzip;
    $user_info = get_userdata($user_id);
    $email = $user_info->user_email;
    ob_start();
    ?>
	<!-- ***** BEGIN PDF Page Generation ***** -->
	<?php 
    if ($form_type == "invoice") {
        ?>
	<page>
	<table style="width:100%"><tr>
		<td style="width:50%">
			<h4>Invoice</h4>
			<?php 
        echo $invoiceName;
        ?>
 <br/>
			Quote Number:	<?php 
        printf("%08d", $invoiceid);
        ?>
<br/>
			Quote Date:	<?php 
        echo $subdate;
        ?>
<br/>
		</td>

		<td style="width:50%">
			<img style="float:right; vertically-align:top;" src="wp-content/uploads/2013/11/cropped-center_logo_big.jpg">
		</td>
	</tr></table>

	<div>
	<h4>Customer Information</h4>
	Name: <?php 
        echo $name;
        ?>
<br/>
	Address: <div>
		<?php 
        echo $school1;
        ?>
<br/>
		<?php 
        echo $district;
        ?>
<br/>
		<?php 
        echo $county;
        ?>
<br/>
		<?php 
        echo $schooladdr1;
        ?>
<br/>
		<?php 
        echo $schoolcsz1;
        ?>
<br/>
		Work:<?php 
        echo $schoolphone;
        ?>
<br/>
		Cell:<?php 
        echo $phone;
        ?>
<br/>
		<?php 
        echo $email;
        ?>
<br/>
		</div><br/>
	</div>
	<div>
	<h4>Payment Information</h4>
	Please send <u>Purchase Order or Check</u>, payable to <i>The University of California Regents</i>, by mail, fax, or email to: <br/><br/>
	Heidi Espindola, Program Manager<br/>
	UC Davis C-STEM Center<br/>
	University of California, Davis<br/>
	One Shields Avenue<br/>
	2132 Bainer Hall<br/>
	Davis, CA 95616<br/>
	(530) 752-9082<br/>
	Fax: (530) 752-4158 <br/>
	orders@c-stem.ucdavis.edu<br/><br/>
	<b>Orders will not be processed until PO or check has been received.<br/>Please note that we do not accept credit cards.</b><br/><br/>
	</div>
	<br/><br/>

	<table cellspacing="0" style="width: 100%; border: solid 1px black; background: #E7E7E7; text-align: center; font-size: 10pt;">
		<tr>
			<th style="width: 46%"> Description </th>
			<th style="width: 34%"> Value </th>
			<th style="width: 10%"> Unit Price </th>
			<th style="width: 10%"> Net Price </th>
		</tr>
	</table>
    <table cellspacing="0" style="width: 100%; border: solid 1px black; background: #F7F7F7; text-align: center; font-size: 10pt;">
        
    	<?php 
        $results = $wpdb->get_results($wpdb->prepare("SELECT `wp_invoice_data`.`field_value`, `wp_invoice_data`.`field_cost`, `wp_invoice_info`.`field_description`, `wp_invoice_info`.`field_type`\n\t\t\tFROM `wp_invoice_data`\n\t\t\tINNER JOIN `wp_invoice_info` ON `wp_invoice_data`.`field_name` = `wp_invoice_info`.`field_name`\n\t\t\tWHERE `wp_invoice_data`.`invoice_no` = %d\n\t\t\tGROUP BY `wp_invoice_data`.`field_name`\n\t\t\tORDER BY `wp_invoice_info`.`field_order`;", $invoiceid));
        $total;
        foreach ($results as $row) {
            if ($row->field_type == "textboxHidden") {
                continue;
            }
            $tableRow = '<tr><td style="width: 46%; text-align:left; padding-top: 5px; padding-bottom: 5px;">';
            $tableRow .= $row->field_description;
            $tableRow .= '</td>';
            $tableRow .= '<td style="width: 34%; padding-top: 5px; padding-bottom:5px; ">';
            $tableRow .= $row->field_value;
            $tableRow .= '</td>';
            $tableRow .= '<td style="width: 10%; padding-top: 5px; padding-bottom:5px; ">$';
            if (is_numeric($row->field_value) && $row->field_value != 0) {
                $unitCost = $row->field_cost / $row->field_value;
            } else {
                $unitCost = $row->field_cost;
            }
            $costFormatted = sprintf("%.2f", $unitCost);
            $tableRow .= $costFormatted . '</td>';
            $tableRow .= '<td style="width: 10%; padding-top: 5px; padding-bottom:5px; ">$';
            $costFormatted = sprintf("%.2f", $row->field_cost);
            $tableRow .= $costFormatted;
            $tableRow .= '</td></tr>';
            $total += $row->field_cost;
            echo $tableRow;
        }
        // foreach
        ?>
    </table>	
    <table cellspacing="0" style="width: 100%; border: solid 1px black; background: #E7E7E7; text-align: center; font-size: 10pt;">
    	<tr>
    		<th style="width: 80%; "></th>
    		<th style="width: 10%; ">Total : </th>
    		<th style="width: 10%; ">$<?php 
        printf("%.2f", $total);
        ?>
</th>
    	</tr>
	</table>

	</page>

	<?php 
    } elseif ($form_type == "report" || $form_type == "waiver") {
        ?>
	<page>
	<table style="width:100%"><tr>
		<td style="width:50%">
			UC Davis Center for Integrated Computing and STEM Education
			<br/>University of California, Davis
			<br/>One Shields Avenue
			<br/>2132 Bainer Hall
			<br/>Davis, CA 95616
		</td>

		<td style="width:50%">
			<img style="float:right; vertically-align:top;" src="wp-content/uploads/2013/11/cropped-center_logo_big.jpg">
		</td>
	</tr></table>

	<div>
	<h4><?php 
        echo $form_name;
        ?>
 Submission Information</h4>
	Name: <?php 
        echo $name;
        ?>
<br/>
	Address: <div>
		<?php 
        echo $school1;
        ?>
<br/>
		<?php 
        echo $district;
        ?>
<br/>
		<?php 
        echo $county;
        ?>
<br/>
		<?php 
        echo $schooladdr1;
        ?>
<br/>
		<?php 
        echo $schoolcsz1;
        ?>
<br/>
		Work:<?php 
        echo $schoolphone;
        ?>
<br/>
		Cell:<?php 
        echo $phone;
        ?>
<br/>
		<?php 
        echo $email;
        ?>
<br/>
		</div><br/>
	Date:<?php 
        $today = getdate();
        echo " ";
        echo $today["month"];
        echo " ";
        echo $today["mday"];
        echo ", ";
        echo $today["year"];
        ?>
<br/>	
	</div>
	<br/><br/>

	<?php 
        $results = $wpdb->get_results($wpdb->prepare("SELECT `wp_invoice_data`.`field_value`, `wp_invoice_data`.`field_cost`, `wp_invoice_info`.`field_description`, `wp_invoice_info`.`field_type`\n\t\t\tFROM `wp_invoice_data`\n\t\t\tINNER JOIN `wp_invoice_info` ON `wp_invoice_data`.`field_name` = `wp_invoice_info`.`field_name`\n\t\t\tWHERE `wp_invoice_data`.`invoice_no` = %d\n\t\t\tGROUP BY `wp_invoice_data`.`field_name`\n\t\t\tORDER BY `wp_invoice_info`.`field_order`;", $invoiceid));
        foreach ($results as $row) {
            if ($row->field_type == "textboxHidden") {
                continue;
            }
            echo '<span style="font-weight:bold">';
            echo $row->field_description;
            echo '</span><br />';
            echo stripcslashes($row->field_value);
            echo '<br /><br />';
        }
        ?>
	</page>

	<?php 
    }
    ?>
	<!-- ***** END PDF Page Generation ***** -->
	<?php 
    $content = ob_get_clean();
    //'wp-content/plugins/contact-form-7/html2pdf/Invoice.pdf'
    $file_name = 'Invoice_' . $invoiceid . '.pdf';
    if ($invoice_type_id == 8) {
        $file_name = 'Application_' . $invoiceid . '.pdf';
    }
    $invoice_file_name = 'wp-content/plugins/contact-form-7/html2pdf/' . $file_name;
    require_once ABSPATH . 'wp-content/plugins/contact-form-7/html2pdf/html2pdf.class.php';
    $html2pdf = new HTML2PDF('P', 'A4', 'fr');
    $html2pdf->pdf->SetProtection(array('print'), '', 'YouWillNeverGuess');
    $html2pdf->WriteHTML($content);
    $html2pdf->Output($invoice_file_name, 'F');
    emailInvoice($form_name, $invoice_type_id, $form_type, $invoiceid);
}