<?php


// elaborate recursive form descriptions

function elaborate_desc($desc)
{

    if ($desc == 'Arec') return 'Tracking in A';

    if ($desc == 'Brec') return 'Tracking in B';

    if ($desc == 'Amix') return 'Mixing in A';

    if ($desc == 'Bmix') return 'Mixing in B';

    if ($desc == 'Aedit') return 'Editing in A';

    if ($desc == 'Bedit') return 'Editing in B';

    if ($desc == 'Amast') return 'Mastering in A';

    if ($desc == 'Bmast') return 'Mastering in B';

    if ($desc == 'Aprod') return 'Production in A';

    if ($desc == 'Bprod') return 'Production in B';

    if ($desc == 'piano') return 'Piano Tuning';

    if ($desc == 'data') return 'Data Transfer/Backup';

    if ($desc == 'shoot') return 'Photo/Video Shoot';

    if ($desc == 'hdd') return 'Hard Disk Drive';

    if ($desc == 'rent') return 'Equipment Rental';

    if ($desc == 'hire') return 'Work for Hire';

    if ($desc == 'food') return 'Food / Catering';

    if ($desc == 'other') return '';

}

// recursive form description element

function form_desc($field)
{

    global $data;

    $form = '

	  <select name="' . $field . '" id="' . $field . '" onchange="pianotuning(document.form.' . $field . '.value,\'' . $field . '\');do_the_math(document.form.subtot_notax.value,document.form.tax.value,document.form.deposit.value);">

		<option ' . (isset($_GET['do']) && $_GET['do'] == 'edit' && !isset($data[$field]) ? '' : 'selected="selected"') . '></option>

		<option value="Arec"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'Arec' ? ' selected="selected"' : '') . '>Tracking in A</option>

		<option value="Brec"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'Brec' ? ' selected="selected"' : '') . '>Tracking in B</option>

		<option value="Amix"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'Amix' ? ' selected="selected"' : '') . '>Mixing in A</option>

		<option value="Bmix"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'Bmix' ? ' selected="selected"' : '') . '>Mixing in B</option>

		<option value="Aedit"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'Aedit' ? ' selected="selected"' : '') . '>Editing in A</option>

		<option value="Bedit"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'Bedit' ? ' selected="selected"' : '') . '>Editing in B</option>

		<option value="Amast"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'Amast' ? ' selected="selected"' : '') . '>Mastering in A</option>

		<option value="Bmast"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'Bmast' ? ' selected="selected"' : '') . '>Mastering in B</option>

		<option value="Aprod"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'Aprod' ? ' selected="selected"' : '') . '>Production in A</option>

		<option value="Bprod"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'Bprod' ? ' selected="selected"' : '') . '>Production in B</option>

		<option value="piano"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'piano' ? ' selected="selected"' : '') . '>Piano Tuning</option>

		<option value="data"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'data' ? ' selected="selected"' : '') . '>Data Transfer/Backup</option>

		<option value="shoot"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'shoot' ? ' selected="selected"' : '') . '>Photo/Video Shoot</option>

		<option value="hdd"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'hdd' ? ' selected="selected"' : '') . '>Hard Disk Drive</option>

		<option value="rent"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'rent' ? ' selected="selected"' : '') . '>Equipment Rental</option>

		<option value="hire"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'hire' ? ' selected="selected"' : '') . '>Work for Hire</option>

		<option value="food"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'food' ? ' selected="selected"' : '') . '>Food / Catering</option>

		<option value="other"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && $data[$field] == 'other' ? ' selected="selected"' : '') . '>other</option>

      </select>

';

    return $form;

}


// recursive timer description element

function form_clock($field)
{

    global $data;

    $start_time = [];
    if (isset($data[$field])) {
        $start_time = explode(':', $data[$field]);
    }

    $form = '

	  <select name="h_' . $field . '" id="h_' . $field . '" onchange="timers(document.form.h_' . $field . '.value,\'h\',\'' . $field . '\');do_the_math(document.form.subtot_notax.value,document.form.tax.value,document.form.deposit.value);">

        <option ' . (isset($_GET['do']) && $_GET['do'] == 'edit' && !isset($start_time[0]) ? '' : 'selected="selected"') . '></option>

        <option value="01"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[0] ?? null) == '01' ? ' selected="selected"' : '') . '>01</option>

        <option value="02"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[0] ?? null) == '02' ? ' selected="selected"' : '') . '>02</option>

        <option value="03"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[0] ?? null) == '03' ? ' selected="selected"' : '') . '>03</option>

        <option value="04"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[0] ?? null) == '04' ? ' selected="selected"' : '') . '>04</option>

        <option value="05"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[0] ?? null) == '05' ? ' selected="selected"' : '') . '>05</option>

        <option value="06"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[0] ?? null) == '06' ? ' selected="selected"' : '') . '>06</option>

        <option value="07"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[0] ?? null) == '07' ? ' selected="selected"' : '') . '>07</option>

        <option value="08"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[0] ?? null) == '08' ? ' selected="selected"' : '') . '>08</option>

        <option value="09"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[0] ?? null) == '09' ? ' selected="selected"' : '') . '>09</option>

        <option value="10"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[0] ?? null) == '10' ? ' selected="selected"' : '') . '>10</option>

        <option value="11"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[0] ?? null) == '11' ? ' selected="selected"' : '') . '>11</option>

        <option value="12"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[0] ?? null) == '12' ? ' selected="selected"' : '') . '>12</option>

	  </select><select name="m_' . $field . '" id="m_' . $field . '" onchange="timers(document.form.m_' . $field . '.value,\'m\',\'' . $field . '\');do_the_math(document.form.subtot_notax.value,document.form.tax.value,document.form.deposit.value);">

        <option ' . (isset($_GET['do']) && $_GET['do'] == 'edit' && !isset($start_time[1]) ? '' : 'selected="selected"') . '></option>

        <option value="00"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[1] ?? null) == '00' ? ' selected="selected"' : '') . '>00</option>

        <option value="30"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[1] ?? null) == '30' ? ' selected="selected"' : '') . '>30</option>

	  </select><select name="ap_' . $field . '" id="ap_' . $field . '" onchange="timers(document.form.ap_' . $field . '.value,\'ampm\',\'' . $field . '\');do_the_math(document.form.subtot_notax.value,document.form.tax.value,document.form.deposit.value);">

        <option ' . (isset($_GET['do']) && $_GET['do'] == 'edit' && !isset($start_time[2]) ? '' : 'selected="selected"') . '></option>

        <option value="am"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[2] ?? null) == 'am' ? ' selected="selected"' : '') . '>am</option>

        <option value="pm"' . (isset($_GET['do']) && $_GET['do'] == 'edit' && ($start_time[2] ?? null) == 'pm' ? ' selected="selected"' : '') . '>pm</option>

	  </select><input type="hidden" name="' . $field . '" id="' . $field . '" />

';

    return $form;

}


// make adjustments for the print page

function print_no_bgcol($bgcol)
{

    if (!isset($_GET['do']) || (isset($_GET['do']) && $_GET['do'] != 'print'))

        return 'bgcolor="#' . $bgcol . '"';

}

function whosedate($dateu)
{

    /*

            if ( $_GET['room'] == '1' && is_int(week_number($dateu)/2) ) // A room, even week

                return 'Lou';

            else if ( $_GET['room'] == '2' && is_int(week_number($dateu)/2) ) // B room, even week

                return 'Fran';

            else if ( $_GET['room'] == '1' && !is_int(week_number($dateu)/2) ) // A room, uneven week

                return 'Fran';

            else if ( $_GET['room'] == '2' && !is_int(week_number($dateu)/2) ) // B room, uneven week

                return 'Lou';

    */

    if ($_GET['login'] == 'duffharris')

        return 'duffharris';

    else if ($_GET['login'] == 'gregrecchia')

        return 'gregrecchia';

    else

        return $_GET['login'];

}

// create new invoice form

function invoice($action, $data = '')
{

    global $id, $date, $login, $data;

    $month = substr($date, 4, 2);

    $day = substr($date, 6);

    $year = substr($date, 0, 4);

    $dateu = mktime(3, 0, 0, (float) $month, (float) $day, (float) $year);

    $datedb = explode('-', $data['date']);

    $form = '';

    if (!isset($_GET['do']) || (isset($_GET['do']) && $_GET['do'] != 'print')) {

        $form .= '

<div align="right" style="width:780px">[ <a href="index.php">Calendar System</a> ]<br />[ <a href="' . $_SERVER['PHP_SELF'] . '">Billing System</a> ]</div>

<hr align="left" width=780px" />

<i>Please ' . ($action == 'show' ? 'review' : ($action == 'edit' ? 'edit' : 'enter')) . ' your Invoice details below:</i>

<hr align="left" width=780px" />

<br />';

    }

    $form .= '

<form name="form" id="form" method="post">

<div style="padding-left:20px;">

Invoice No.: <a href="/calendar/view_entry.php?id=' . $id . '"><b>' . $id . '</b></a>' . ($action != 'show' ? '<input type="hidden" name="id" value="' . $id . '" />' : '') . '

<br />

Date: ' . ($action == 'show' ? '<b>' . $datedb[1] . '/' . $datedb[2] . '/' . $datedb[0] . '</b>' : ($action == 'edit' ? '<b>' . $datedb[1] . '/' . $datedb[2] . '/' . $datedb[0] . '</b><input type="hidden" name="date" value="' . $data['date'] . '">' : '<b>' . substr($date, 4, 2) . '/' . substr($date, 6) . '/' . substr($date, 0, 4) . '</b><input type="hidden" name="date" value="' . substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6) . '" />')) . '<br />

';

    if (!isset($_GET['do']) || (isset($_GET['do']) && $_GET['do'] != 'print')) {

        $form .= '

	Whose Date: ';

        if ($action == 'show') {

            $form .= '<b>' . $data['whose'] . '</b><br />';

        } else {

            $form .= '

		  <select name="whose" id="whose">

			<option value="Marc"' . ($action == 'edit' && $data['whose'] == 'Marc' ? ' selected="selected"' : ($action != 'edit' && whosedate($dateu) == 'Marc' ? ' selected="selected"' : '')) . '>Marc</option>

			<option value="Fran"' . ($action == 'edit' && $data['whose'] == 'Fran' ? ' selected="selected"' : ($action != 'edit' && whosedate($dateu) == 'Fran' ? ' selected="selected"' : '')) . '>Fran</option>

			<option value="Lou"' . ($action == 'edit' && $data['whose'] == 'Lou' ? ' selected="selected"' : ($action != 'edit' && whosedate($dateu) == 'Lou' ? ' selected="selected"' : '')) . '>Lou</option>

			<option value="Eric"' . ($action == 'edit' && $data['whose'] == 'Eric' ? ' selected="selected"' : ($action != 'edit' && whosedate($dateu) == 'Eric' ? ' selected="selected"' : '')) . '>Eric</option>

			<option value="Duff"' . ($action == 'edit' && $data['whose'] == 'duffharris' ? ' selected="selected"' : ($action != 'edit' && whosedate($dateu) == 'duffharris' ? ' selected="selected"' : '')) . '>Duff</option>

			<option value="Greg"' . ($action == 'edit' && $data['whose'] == 'gregrecchia' ? ' selected="selected"' : ($action != 'edit' && whosedate($dateu) == 'gregrecchia' ? ' selected="selected"' : '')) . '>Greg</option>

			<option value="studio"' . ($action == 'edit' && $data['whose'] == 'studio' ? ' selected="selected"' : '') . '>studio</option>

		  </select>

	<br />

	';

        }

        $form .= 'Who Engineered: ';

        if ($action == 'show') {

            $form .= '<b>' . $data['engineer'] . '</b><br />';

        } else {

            $form .= '

		  <select name="engineer" id="engineer">

			<option value="Marc"' . ($action == 'edit' && $data['engineer'] == 'Marc' ? ' selected="selected"' : ($action != 'edit' && $login == 'Marc' ? ' selected="selected"' : '')) . '>Marc</option>

			<option value="Fran"' . ($action == 'edit' && $data['engineer'] == 'Fran' ? ' selected="selected"' : ($action != 'edit' && $login == 'Fran' ? ' selected="selected"' : '')) . '>Fran</option>

			<option value="Lou"' . ($action == 'edit' && $data['engineer'] == 'Lou' ? ' selected="selected"' : ($action != 'edit' && $login == 'Lou' ? ' selected="selected"' : '')) . '>Lou</option>

			<option value="Eric"' . ($action == 'edit' && $data['engineer'] == 'Eric' ? ' selected="selected"' : ($action != 'edit' && $login == 'Eric' ? ' selected="selected"' : '')) . '>Eric</option>

			<option value="Duff"' . ($action == 'edit' && $data['engineer'] == 'duffharris' ? ' selected="selected"' : ($action != 'edit' && $login == 'duffharris' ? ' selected="selected"' : '')) . '>Duff</option>

			<option value="Greg"' . ($action == 'edit' && $data['engineer'] == 'gregrecchia' ? ' selected="selected"' : ($action != 'edit' && $login == 'gregrecchia' ? ' selected="selected"' : '')) . '>Greg</option>

			<option value="outside"' . ($action == 'edit' && $data['engineer'] == 'outside' ? ' selected="selected"' : '') . '>outside</option>

		  </select>

	<br />

	';

        }

    }

    $form .= 'Payment Type: ';

    if ($action == 'show') {

        $form .= '<b>' . str_replace('check', 'Check', str_replace('credit', 'Credit Card', str_replace('cash', 'Cash', str_replace('wire', 'Bank Wire', $data['payment'])))) . '</b><br />';

    } else {

        $form .= '

      <select name="payment" id="payment" onchange="verify_payment();">

        <option selected="selected"></option>

        <option value="unpaid"' . ($action == 'edit' && $data['payment'] == 'unpaid' ? ' selected="selected"' : '') . '>unpaid</option>

        <option value="check"' . ($action == 'edit' && $data['payment'] == 'check' ? ' selected="selected"' : '') . '>check</option>

        <option value="credit"' . ($action == 'edit' && $data['payment'] == 'credit' ? ' selected="selected"' : '') . '>credit</option>

        <option value="cash"' . ($action == 'edit' && $data['payment'] == 'cash' ? ' selected="selected"' : '') . '>cash</option>

        <option value="wire"' . ($action == 'edit' && $data['payment'] == 'wire' ? ' selected="selected"' : '') . '>wire</option>

	  </select>

<br />

';

    }

    $form .= '

<br />

<table class="main" style="clear:both;" width="500px" cellpadding="0" cellspacing="0" border="0" bordercolor="#000000">

  <tr><td class="entry">Artist Name:</td>			<td>' . ($action == 'show' ? '<b>' . stripslashes($data['artist']) . '</b>' : '<input type="text" name="artist" id="artist" size="25" 	' . ($action == 'edit' ? 'value="' . stripslashes($data['artist']) . '"' : (isset($_GET['client']) ? 'value="' . $_GET['client'] . '"' : '')) . ' onblur="InvoiceName();" />') . '</td></tr>

  <tr><td class="entry">Contact Name:</td>			<td>' . ($action == 'show' ? '<b>' . stripslashes($data['contact']) . '</b>' : '<input type="text" name="contact" id="contact" size="25" ' . ($action == 'edit' ? 'value="' . stripslashes($data['contact']) . '"' : (isset($_GET['client']) ? 'value="' . $_GET['client'] . '"' : '')) . '/>') . '</td>	</tr>

  <tr><td class="entry">Company Name:</td>			<td>' . ($action == 'show' ? '<b>' . stripslashes($data['company']) . '</b>' : '<input type="text" name="company" id="company" size="25" ' . ($action == 'edit' ? 'value="' . stripslashes($data['company']) . '"' : (isset($_GET['client']) ? 'value="' . $_GET['client'] . '"' : '')) . '/>') . '</td>	</tr>

  <tr><td class="entry">Address:</td>				<td>' . ($action == 'show' ? '<b>' . stripslashes($data['address']) . '</b>' : '<input type="text" name="address" id="address" size="25" ' . ($action == 'edit' ? 'value="' . stripslashes($data['address']) . '"' : '') . '/>') . '</td>	</tr>

  <tr><td class="entry">City, State:</td>			<td>' . ($action == 'show' ? '<b>' . stripslashes($data['city']) . '</b>' : '<input type="text" name="city" id="city" size="25" 		' . ($action == 'edit' ? 'value="' . stripslashes($data['city']) . '"' : '') . '/>') . '</td>		</tr>

  <tr><td class="entry">ZIP:</td>					<td>' . ($action == 'show' ? '<b>' . stripslashes($data['zip']) . '</b>' : '<input type="text" name="zip" id="zip" size="25" 		' . ($action == 'edit' ? 'value="' . stripslashes($data['zip']) . '"' : '') . '/>') . '</td>		</tr>

  <tr><td class="entry">Country:</td>				<td>' . ($action == 'show' ? '<b>' . stripslashes($data['country']) . '</b>' : '<input type="text" name="country" id="country" size="25" ' . ($action == 'edit' ? 'value="' . stripslashes($data['country']) . '"' : '') . '/>') . '</td>	</tr>

  <tr><td class="entry">Phone:</td>					<td>' . ($action == 'show' ? '<b>' . stripslashes($data['phone']) . '</b>' : '<input type="text" name="phone" id="phone" size="25" 	' . ($action == 'edit' ? 'value="' . stripslashes($data['phone']) . '"' : '') . '/>') . '</td>	</tr>

  <tr><td class="entry">E-Mail:<br /><br /></td>	<td>' . ($action == 'show' ? '<b>' . stripslashes($data['email']) . '</b><br /><br />' : '<input type="text" name="email" id="email" size="25" 	' . ($action == 'edit' ? 'value="' . stripslashes($data['email']) . '"' : '') . '/><br /><br />') . '</td>	</tr>

  <tr><td class="entry">Project</td>				<td>' . ($action == 'show' ? '<b>' . stripslashes($data['project']) . '</b>' : '<input type="text" name="project" id="project" size="50" ' . ($action == 'edit' ? 'value="' . stripslashes($data['project']) . '"' : '') . '/>') . '</td>	</tr>

  <tr><td class="entry">P.O.</td>					<td>' . ($action == 'show' ? '<b>' . stripslashes($data['po']) . '</b>' : '<input type="text" name="po" id="po" size="25" 			' . ($action == 'edit' ? 'value="' . stripslashes($data['po']) . '"' : '') . '/>') . '</td>	</tr>

</table>

</div>

<br />

<hr align="left" width=780px" />

<br />

<div style="width:780px;border: gray 1px solid;">

<table class="main" style="clear:both;" width="780px" cellpadding="' . ($action == 'show' ? '5' : '0') . '" cellspacing="0" border="0" bordercolor="#000000">

  <tr>

    <th bgcolor="#CCCCCC"><p style="text-align:center">Day</p></th>

    <th bgcolor="#CCCCCC"><p style="text-align:center">Start</p></th>

    <th bgcolor="#CCCCCC"><p style="text-align:center">End</p></th>

    <th bgcolor="#CCCCCC"><p style="text-align:center">Qty</p></th>

    <th bgcolor="#CCCCCC"><p style="text-align:center">Price/Qty</p></th>

    <th bgcolor="#CCCCCC"><p style="text-align:center">Description</p></th>

    <th bgcolor="#CCCCCC"><p style="text-align:right">Sub Total</p></th>

  </tr>

';

    for ($i = 1; $i < 11; $i++) {

        $form .= '

	  <tr>

		<td>' . ($action == 'show' ? '<p style="text-align:center">' . $data['l' . $i . '_day'] . '</p>' : '<input type="text" name="l' . $i . '_day" id="l' . $i . '_day"' . ($action == 'edit' ? ' value="' . $data['l' . $i . '_day'] . '"' : ($i == 1 ? 'value="' . substr($date, 4, 2) . '/' . substr($date, 6) . '"' : 'onfocus="fill_date(\'' . substr($date, 4, 2) . '/' . substr($date, 6) . '\',\'l' . $i . '_day\')";')) . ' size="6" maxlength="5" />&nbsp;&nbsp;') . '</td>

		<td>' . ($action == 'show' ? '<p style="text-align:center">' . str_replace(':am', ' am', str_replace(':pm', ' pm', $data['l' . $i . '_start'])) . '</p>' : form_clock('l' . $i . '_start') . '&nbsp;') . '</td>

		<td>' . ($action == 'show' ? '<p style="text-align:center">' . str_replace(':am', ' am', str_replace(':pm', ' pm', $data['l' . $i . '_end'])) . '</p>' : form_clock('l' . $i . '_end') . '&nbsp;') . '</td>

		<td>' . ($action == 'show' ? '<p style="text-align:right">' . $data['l' . $i . '_qty'] . '</p>' : '<input type="text" name="l' . $i . '_qty" id="l' . $i . '_qty"' . ($action == 'edit' ? ' value="' . $data['l' . $i . '_qty'] . '"' : '') . ' size="5" maxlength="4" onchange="l_subtotal(document.form.l' . $i . '_qty.value,document.form.l' . $i . '_rate.value,' . $i . ');do_the_math(document.form.subtot_notax.value,document.form.tax.value,document.form.deposit.value);" />') . '</td>

		<td>' . ($action == 'show' ? '<p style="text-align:right">' . (!empty($data['l' . $i . '_rate']) ? '$ ' . $data['l' . $i . '_rate'] : '') . '</p>' : '<input type="text" name="l' . $i . '_rate" id="l' . $i . '_rate"' . ($action == 'edit' ? ' value="' . $data['l' . $i . '_rate'] . '"' : '') . ' size="8" maxlength="7" onchange="l_subtotal(document.form.l' . $i . '_qty.value,document.form.l' . $i . '_rate.value,' . $i . ');do_the_math(document.form.subtot_notax.value,document.form.tax.value,document.form.deposit.value);" />&nbsp;') . '</td>

		<td>' . ($action == 'show' ? '<p style="text-align:center">' . elaborate_desc($data['l' . $i . '_desc']) . '</p>' : form_desc('l' . $i . '_desc')) . '</td>

		<td>' . ($action == 'show' ? '<p style="text-align:right">' . (!empty($data['l' . $i . '_subtot']) ? '$ ' . $data['l' . $i . '_subtot'] : '') . '</p>' : '<input type="text" name="l' . $i . '_subtot" id="l' . $i . '_subtot"' . ($action == 'edit' ? ' value="' . $data['l' . $i . '_subtot'] . '"' : '') . ' size="8" maxlength="7" />') . '</td>

	  </tr>

	';

    }

    $form .= '

  <tr>

    <td colspan="5">

';

    if (!isset($_GET['do']) || (isset($_GET['do']) && $_GET['do'] != 'print')) {

        if ($action == 'show') {

            $form .= '<dliv style="position:absolute;width:400px;font-style:italic;font-size:12px;">' . (!empty($data['notes']) ? '<b>Notes</b>:<br />' : '') . ' ' . nl2br($data['notes']) . '</div>';

//	} else if ( $action == 'edit' ) {

        } else {

            $form .= '<dliv style="position:absolute;width:500px;font-style:italic;font-size:12px;"><b>Notes</b>:<br /><textarea name="notes" rows="5" cols="55">' . $data['notes'] . '</textarea></div>';

        }

    }

    $form .= '

    </td>

    <td ' . print_no_bgcol('EEEEEE') . ' class="entry"><div align="right"><strong>SubTotal </strong></div></td>

    <td ' . print_no_bgcol('EEEEEE') . '>' . ($action == 'show' ? '<p style="text-align:right">$ ' . $data['subtot_notax'] . '</p>' : '<input name="subtot_notax" type="text" id="subtot_notax"' . ($action == 'edit' ? ' value="' . $data['subtot_notax'] . '"' : '') . ' size="8" maxlength="7" />') . '</td>

  </tr>

  <tr>

    <td colspan="5">&nbsp;</td>



    <td ' . print_no_bgcol('EEEEEE') . ' class="entry">

    	<div align="right">

    	<strong>Tax</strong>';

    if (!isset($_GET['do']) || (isset($_GET['do']) && $_GET['do'] != 'print')) {

        $form .= '

			<input type="checkbox" ' . ($action == 'show' ? ($data['addtax'] == 'checked' ? 'checked>' : '>') : ($action == 'edit' && $data['addtax'] == 'checked' ? 'checked' : '') . ' name="addtax" id="addtax" value="checked" onclick="addTax(document.form.subtot_notax.value,document.form.tax.value);do_the_math(document.form.subtot_notax.value,document.form.tax.value,document.form.deposit.value);" />') . '

        				  		   ' . ($action == 'show' ? (!empty($data['tax']) ? '(' . $data['tax'] . ')' : '') : '<input type="text" name="tax" id="tax" value="8.35%" size="6" maxlength="5" onchange="addTax(document.form.subtot_notax.value,document.form.tax.value);do_the_math(document.form.subtot_notax.value,document.form.tax.value,document.form.deposit.value);" />') . '

';

    }

    $form .= '

        </div>

    </td>

    <td ' . print_no_bgcol('EEEEEE') . '>' . ($action == 'show' ? '<p style="text-align:right">' . ($data['addtax'] == 'checked' ? '$ ' . $data['subtot_notax'] / 100 * $data['tax'] : '') . '</p>' : '<input type="text" name="taxes" id="taxes"' . ($action == 'edit' && $data['addtax'] == 'checked' ? ' value="' . $data['subtot_notax'] / 100 * $data['tax'] . '"' : '') . ' size="8" maxlength="7" />') . '</td>

  </tr>

  <tr>

    <td colspan="5">&nbsp;</td>

    <td ' . print_no_bgcol('EEEEEE') . ' class="entry"><div align="right"><strong>SubTotal + Taxes </strong></div></td>

    <td ' . print_no_bgcol('EEEEEE') . '>' . ($action == 'show' ? '<p style="text-align:right">' . (!empty($data['subtot_aftax']) ? '$ ' . $data['subtot_aftax'] : '') . '</p>' : '<input type="text" name="subtot_aftax" id="subtot_aftax"' . ($action == 'edit' ? ' value="' . $data['subtot_aftax'] . '"' : '') . ' size="8" maxlength="7" />') . '</td>

  </tr>

  <tr>

    <td colspan="5">&nbsp;</td>

    <td ' . print_no_bgcol('66FF66') . ' class="entry"><div align="right"><strong>Deposit </strong></div></td>

    <td ' . print_no_bgcol('66FF66') . '>' . ($action == 'show' ? '<p style="text-align:right">' . (!empty($data['deposit']) ? '$ ' . $data['deposit'] : '') . '</p>' : '<input type="text" name="deposit" id="deposit"' . ($action == 'edit' ? ' value="' . $data['deposit'] . '"' : '') . ' size="8" maxlength="7" onchange="grandtotal(document.form.deposit.value);" />') . '</td>

  </tr>

  <tr>

    <td colspan="5">&nbsp;</td>

    <td ' . print_no_bgcol('EEEEEE') . ' class="entry"><div align="right"><strong>Total Owed </strong></div></td>

    <td ' . print_no_bgcol('EEEEEE') . '>' . ($action == 'show' ? '<p style="text-align:right">$ ' . $data['totalowed'] . '</p>' : '<input type="text" name="totalowed" id="totalowed"' . ($action == 'edit' ? ' value="' . $data['totalowed'] . '"' : '') . ' size="8" maxlength="7" />') . '</td>

  </tr>

  <tr>

    <td colspan="5">&nbsp;</td>

    <td ' . print_no_bgcol('FF3333') . ' class="entry"><div align="right"><strong>TOTAL PAID </strong></div></td>

    <td ' . print_no_bgcol('FF3333') . '>' . ($action == 'show' ? '<p style="text-align:right">$ ' . $data['totalpaid'] . '</p>' : '<input type="text" name="totalpaid" id="totalpaid"' . ($action == 'edit' ? ' value="' . $data['totalpaid'] . '"' : '') . ' size="8" maxlength="7" />') . '</td>

  </tr>

</table>

</div>

';

    if (!isset($_GET['do']) || (isset($_GET['do']) && $_GET['do'] != 'print')) {

        if ($action == 'show') {

            $form .= '

		<input type="button" value="Edit Invoice" onclick="parent.location=\'' . $_SERVER['PHP_SELF'] . '?id=' . $_GET['id'] . '&do=edit' . '\';" />

		<input type="button" value="Print Invoice" onclick="PrintInvoice(invoice);" />

	';

        } else if ($action == 'edit') {

            $form .= '

		<input type="button" name="save" id="save" value="Save Invoice without Printing" onclick="return update();" />

		<input type="button" name="saveandprint" id="saveandprint" value="Save and (then) Print Invoice" onclick="return update_and_print();" />

	';

        } else {

            $form .= '

		<input type="button" name="save" id="save" value="Save Invoice without Printing" onclick="return add();" />

		<input type="button" name="saveandprint" id="saveandprint" value="Save and (then) Print Invoice" onclick="add_and_print();" />

	';

        }

        $form .= '
        
        <input type="hidden" name="csrf_form_key" value="' . $_SESSION['csrf_form_key'] . '" />

  </form>

<iframe name="invoice" id="invoice" src="' . $_SERVER['PHP_SELF'] . '?id=' . $_GET['id'] . '&do=print' . '" width="0" height="0" frameborder="0"></iframe>

	';

    }

    echo $form;

}
