<?php
session_start();
// - arinfo.php
require_once "main.php";
ini_set('display_errors', 1);
Logger::configure('log4php.xml');
$log = Logger::getLogger('arinfo');
global $db;
//print_r(@$_GET);
$zid = $_SESSION['comnum'];
if ($_SESSION['access']<4){
    $log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum']);
    //cust
    if ($_GET['submitType']=='cust') {
            $result1 = bill_get_mrbatches($_SESSION['comnum']);
            echo 'Please choose a batch or click new batch.<br />';
            if (count($result1)>0){ ?>
                <table border="1">
                    <tr>
                        <th>Batch ID</th>
                        <th>Batch Date</th>
                        <th>Operation</th>
                    </tr>
                    <?php
                    for ($i=0;$i<count($result1);$i++) { ?>
                        <tr>
                            <td><?php echo $result1[$i]['id']; ?></td>
                            <td><?php echo $result1[$i]['rdate']; ?></td>
                            <td><button type="button" value="axrates" onClick="window.open('includes/downloadbatch.php?submitType=printbatch&id=<?php echo $result1[$i]['id']; ?>', '_blank'); return false;">Download to Read Software</button>
    <script>
        function setType(type)
        {
            //formName is the name of your form
            document.accinform.submitType.value = type;
        }
    </script>
    <form action="includes/uploadreads.php" method="post" target="_blank" enctype="multipart/form-data">
        <input type="hidden" name="batchid" value="<?php echo $result1[$i]['id'];?>">
        <input type="hidden" name="comnum" value=" <?php echo $_SESSION['comnum']?>">
        <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
        <!-- Name of input element determines name in $_FILES array -->
        <input name="csvfile" type="file" id="csvfile">
        <input name="upload" type="submit" id="upload" value="Upload From Read Software">
    </form>
                                 <button type="button" value="axrates" onClick="dynamic_Select('includes/arinfo.php', '#arinfo','batch', <?php echo $result1[$i]['id'];?>)">Enter Readings</button>
                                 <button type="button" value="axrates" onClick="dynamic_Select('includes/arinfo.php', '#arinfo','checkbatch', <?php echo $result1[$i]['id'];?>)">Check</button>
                                 <button type="button" value="axrates" onClick="window.open('includes/printbatch.php?submitType=reread&id=<?php echo $result1[$i]['id']; ?>', '_blank'); return false;">Print ReReads</button>
                                 <button type="button" value="axrates" onClick="dynamic_Select('includes/arinfo.php', '#arinfo','postbatch', <?php echo $result1[$i]['id'];?>)">Post</button>
                            </td>
                        </tr>
                <?php } ?>
                </table>
                <?php
            } else {
                echo 'There were no batches found!<br/>Please click the New Batch button<br/>';
            } ?>
        <button type="button" value="axrates1" onClick="dynamic_Select('includes/arinfo.php', '#arinfo','newbatch', '0')">New Batch</button>
    <?php
    }
    //newbatch
    else if ($_GET['submitType']=='newbatch') {
        echo '<script>alert("in NewBatch");</script>';
            $test = bill_insert_newbatch($_SESSION['comnum']);
            $result1 = bill_get_mrbatches($_SESSION['comnum']);
            echo 'Please choose a batch or click new batch.<br />';
            if (count($result1)>0){ ?>
                <table border="1">
                    <tr>
                        <th>Batch ID</th>
                        <th>Batch Date</th>
                        <th>Operation</th>
                    </tr>
                <?php
                for ($i=0;$i<count($result1);$i++) { ?>
                    <tr>
                        <td><?php echo $result1[$i]['id']; ?></td>
                        <td><?php echo $result1[$i]['rdate']; ?></td>
                        <td><button type="button" value="axrates" onClick="dynamic_Select('includes/arinfo.php', '#arinfo','printbatch', <?php echo $result1[$i]['id'];?>)">Print Reading Worksheet</button>
                            <button type="button" value="axrates" onClick="dynamic_Select('includes/arinfo.php', '#arinfo','batch', <?php echo $result1[$i]['id'];?>)">Enter Readings</button>
                            <button type="button" value="axrates" onClick="dynamic_Select('includes/arinfo.php', '#arinfo','checkbatch', <?php echo $result1[$i]['id'];?>)">Check</button>
                            <button type="button" value="axrates" onClick="dynamic_Select('includes/arinfo.php', '#arinfo','postbatch', <?php echo $result1[$i]['id'];?>)">Post</button>
                        </td>
                    </tr>
                <?php } ?>
                </table>
            <?php
             } else {
                echo 'There were no batches found!<br/>Please click the New Batch button<br/>';
                echo $_SESSION['comnum'];
             }
            ?>
            <button type="button" value="axrates1" onClick="dynamic_Select('includes/arinfo.php', '#arinfo','newbatch', '0',<?php echo $_SESSION['comnum'];?>)">New Batch</button>
        <?php
        }
    //batch
    else if ($_GET['submitType']=='batch') {
            $_SESSION['batch'] = $_GET['id'];
            $rows_per_page = bill_get_nmrows($_SESSION['comnum']);
            $nmrows = bill_get_nmread($_SESSION['comnum']);

            /*if (isset($_GET['pageno'])) {
               $pageno = $_GET['pageno'];
            } else {
               $pageno = 1;
            }*/
            $pageno = isset($_GET['pageno']) ? $pageno = $_GET['pageno'] : 1;
            $lastpage = ceil($nmrows/$rows_per_page);
            $pageno = (int)$pageno;
            if ($pageno > $lastpage) {
               $pageno = $lastpage;
            }
            if ($pageno < 1) {
               $pageno = 1;
            }
            $limit = ($pageno - 1) * $rows_per_page .',' .$rows_per_page;
            $result = bill_get_meters($_SESSION['comnum'], $limit);
            $datax = bill_get_reads($_SESSION['comnum'], $_SESSION['batch']);
        ?>
        <script>
               function setType(type)
               {
                  //formName is the name of your form
                  document.accinform1.submitType.value = type;
               }
        </script>
        <?php echo 'Page ' .$pageno.' of '.$lastpage; ?>
        <form name="accinform1" action="#" onsubmit="jQuery.ajax({url:'includes/arinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#arinfo').html(data);}});return false;">
            <table border="1">
                <tr>
                    <th>Account</th>
                    <th>Name</th>
                    <th>Meter Address</th>
                    <th>Last</th>
                    <th>Reading</th>
                </tr>
        <?php
                for ($i=0;$i<count($result);$i++) {
                    $datay = '';
                    $datar = '4';
                    $dataid = '';
                    for ($b=0;$b<count($datax);$b++) {
                        if ($result[$i]['account']==$datax[$b]['zid'] ) {
                            $datay = $datax[$b]['xread'];
                            $datar = '1';
                            $dataid = $datax[$b]['id'];
                        }
                    }
        ?>
                <tr>
                    <td><?php echo $result[$i]['account']; ?></td>
                    <td><?php echo $result[$i]['name']; ?></td>
                    <td><?php echo $result[$i]['maddr']; ?></td>
                    <td><?php echo @$result[$i]['lread']; ?></td>
                    <td><input type="text" name="x<?php echo $i; ?>" id="x<?php echo $i; ?>" value="<?php echo $datay; ?>">
                        <input type="hidden" name="z<?php echo $i; ?>" id="z<?php echo $i; ?>" value="<?php echo  $result[$i]['account']; ?>">
                        <input type="hidden" name="n<?php echo $i; ?>" id="n<?php echo $i; ?>" value="<?php echo  $datar; ?>">
                        <input type="hidden" name="id<?php echo $i; ?>" id="id<?php echo $i; ?>" value="<?php echo  $dataid; ?>">
                    </td>
               </tr>
        <?php } ?>
            </table>
                <input type="hidden" name="pageno" value="<?php echo $pageno; ?>">
                <input type="hidden" name="lastpage" value="<?php echo $lastpage; ?>">
                <input type="hidden" name="nrow" value="<?php echo $i; ?>">
                <input type="hidden" name="nmrows" value="<?php echo $nmrows; ?>">
                <input type="hidden" name="rowsperpage" value="<?php echo $rows_per_page; ?>">
                <input type="hidden" name="submitType">
                <input type="submit" onclick="setType('rcUpdate')" value="Add Above Readings and move to next page"/>
        </form>
    <?php
        }
    //rcupdate
    else if ($_GET['submitType']=='rcUpdate') {
            $pageno = $_GET['pageno']+1;
            $rows_per_page = $_GET['rowsperpage'];
            $nmrows = $_GET['nmrows'];
            $limit = ($pageno - 1) * $rows_per_page .',' .$rows_per_page;
            for ($i=0;$i<$_GET['nrow'];$i++) {
                $xa='x'.$i;
                $za='z'.$i;
                $na='n'.$i;
                $ida='id'.$i;
                //echo $_GET[$na].' - '.$_GET[$xa].' - '.$_GET[$za].' - '.$_GET[$ida]. ' x ';
                if ($_GET[$na]==4){
                    if ($_GET[$xa]>0){
                        $test = bill_insert_newread($_SESSION['comnum'], $_GET[$za], $_GET[$xa], $_SESSION['batch']);
                    }
                } else {
                    $test = bill_update_newread($_GET[$ida], $_GET[$xa]);
                }
            }
          //echo 'page no: '.$pageno.' -- '.$_GET['lastpage'].' x-x ';
            if ($pageno<=$_GET['lastpage']){
                $limit = ($pageno - 1) * $rows_per_page .',' .$rows_per_page;
                $result = bill_get_meters($_SESSION['comnum'], $limit);
                $datax = bill_get_reads($_SESSION['comnum'], $_SESSION['batch']);
    ?>
        <script>
           function setType(type)
           {
              //formName is the name of your form
              document.accinform1.submitType.value = type;
           }
        </script>
        <?php echo 'Page ' .$pageno.' of '.$_GET['lastpage']; ?>
        <form name="accinform1" action="#" onsubmit="jQuery.ajax({url:'includes/arinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#arinfo').html(data);}});return false;">
            <table border="1">
                <tr>
                    <th>Account</th>
                    <th>Name</th>
                    <th>Meter Address</th>
                    <th>Last</th>
                    <th>Reading</th>
                </tr>
    <?php
                for ($i=0;$i<count($result);$i++) {
                    $datay = '';
                    $datar = '4';
                    $dataid = '';
                    for ($b=0;$b<count($datax);$b++) {
                        if ($result[$i]['account']==$datax[$b]['zid'] ) {
                            $datay = $datax[$b]['xread'];
                            $datar = '1';
                            $dataid = $datax[$b]['id'];
                        }
                    }
     ?>
                 <tr>
                     <td><?php echo $result[$i]['account']; ?></td>
                    <td><?php echo @$result[$i]['name']; ?></td>
                    <td><?php echo @$result[$i]['maddr']; ?></td>
                    <td><?php echo @$result[$i]['lread']; ?></td>
                    <td><input type="text" name="x<?php echo $i; ?>" id="x<?php echo $i; ?>" value="<?php echo $datay; ?>">
                        <input type="hidden" name="z<?php echo $i; ?>" id="z<?php echo $i; ?>" value="<?php echo  $result[$i]['account']; ?>">
                        <input type="hidden" name="n<?php echo $i; ?>" id="n<?php echo $i; ?>" value="<?php echo  $datar; ?>">
                        <input type="hidden" name="id<?php echo $i; ?>" id="id<?php echo $i; ?>" value="<?php echo  $dataid; ?>">
                    </td>
                 </tr>
    <?php } ?>
            </table>
            <input type="hidden" name="pageno" value="<?php echo $pageno; ?>">
            <input type="hidden" name="lastpage" value="<?php echo $_GET['lastpage']; ?>">
            <input type="hidden" name="nrow" value="<?php echo $i; ?>">
            <input type="hidden" name="nmrows" value="<?php echo $nmrows; ?>">
            <input type="hidden" name="rowsperpage" value="<?php echo $rows_per_page; ?>">
            <input type="hidden" name="submitType"/>
            <input type="submit" onclick="setType('prevUpdate')" value="Add Above Readings and move to previous page"/>
            <input type="submit" onclick="setType('rcUpdate')" value="Add Above Readings and move to next page"/>
        </form>
    <?php
            } else echo '<h3>That is the end of the meter file.  Now you should click the check readings button.</h3>';
        }
    //checkbatch
    else if ($_GET['submitType']=='checkbatch') {
            $_SESSION['batch'] = $_GET['id'];
            $rows_per_page = bill_get_nmrows($_SESSION['comnum']);
            $nmrows = bill_get_nmread($_SESSION['comnum']);
            $datz = bill_get_company($_SESSION['comnum']);
            //} else {
            if (isset($_GET['pageno'])) {
               $pageno =$_GET['pageno'];
            } else {
               $pageno = 1;
            }
            $lastpage = ceil($nmrows/$rows_per_page);
            $pageno = (int)$pageno;
            if ($pageno > $lastpage) {
               $pageno = $lastpage;
            }
            if ($pageno < 1) {
               $pageno = 1;
            }
            $limit = '0,' .$rows_per_page;
            $result = bill_get_checkread($_SESSION['comnum'], $_SESSION['batch'], $limit);
            //print_r($result);
    ?>
            <script>
               function setType(type)
               {
                  //formName is the name of your form
                  document.accinform.submitType.value = type;
               }
            </script>
        <?php
            //$nm = bill_reset_check( $_SESSION['batch'] );
            //$nm = bill_get_checkread($_SESSION['comnum'], $_SESSION['batch'], $limit);
            //echo mysql_num_rows($result);
            if (empty($result)){
                    echo '<p>That is the end of the batch file.  You have checked all of the'.
                         ' meter readings in this batch.  Now that you are happy with'.
                         ' the readings, Click Manual Read and then choose the Post Readings button.'.
                         '  Otherwise, you should check them again.</p>';
                } else {
                    echo 'Page no: '.$pageno.' -of- '.$lastpage; ?>
            <p>After a reading is checked and updated, it will no longer show up in this listing, but will be posted when you post this batch</p>
            <form name="accinform" action="#" onsubmit="jQuery.ajax({url:'includes/arinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#arinfo').html(data);}});return false;">
                <table border="1">
                    <tr>
                        <th>Account</th>
                        <th>Name</th>
                        <th>Meter Address</th>
                        <th>Last</th>
                        <th>Reading</th>
                        <th>Checked</th>
                    </tr>
    <?php
                    for ($i=0;$i<count($result);$i++) { ?>
                    <tr>
                        <td><?php echo $result[$i]['zid']; ?></td>
                        <td><?php echo @$result[$i]['name']; ?></td>
                        <td><?php echo @$result[$i]['maddr']; ?></td>
                        <td><?php echo @$result[$i]['lread']; ?></td>
                        <td><input type="text" name="x<?php echo $i; ?>" id="x<?php echo $i; ?>" value="<?php echo $result[$i]['xread']; ?>"></td>
                        <td<?php
                            if ((@$result[$i]['xread']-@$result[$i]['lread'])>(@$datz[0]['highlimit'])) { ?>
                                style="background-color: blue;">
                            <?php
                            } else if (@$result[$i]['lread']>(@$result[$i]['xread'])) { ?>
                                style="background-color: red;">
                            <?php
                            } else { ?>
                                style="background-color: green;">
                            <?php } ?>
                            <input type="checkbox" name="ch<?php echo $i; ?>" id="ch<?php echo $i; ?>" checked="checked">
                            <input type="hidden" name="z<?php echo $i; ?>" id="z<?php echo $i; ?>" value="<?php echo $result[$i]['id']; ?>">
                        </td>
                  </tr>
                  <?php
                  } ?>
               </table>
               <input type="hidden" name="pageno" value="<?php echo $pageno; ?>"/>
               <input type="hidden" name="lastpage" value="<?php echo $lastpage; ?>"/>
               <input type="hidden" name="nrow" value="<?php echo $i; ?>"/>
               <input type="hidden" name="nmrows" value="<?php echo $nmrows; ?>"/>
               <input type="hidden" name="rowsperpage" value="<?php echo $rows_per_page; ?>" />
               <input type="hidden" name="submitType"/>
               <input type="submit" onclick="setType('batchUpdate')" value="Update Above Readings"/>
            </form>
<?php }
            }
    //batchupdate
    else if ($_GET['submitType']=='batchUpdate') {
            $pageno = $_GET['pageno']+1;
            $rows_per_page = $_GET['rowsperpage'];
            $nmrows = $_GET['nmrows'];
            //$limit = $pageno*$rows_per_page.',' .$rows_per_page;
            $limit = '0,' . $rows_per_page;
            for ($i=0;$i<$_GET['nrow'];$i++) {
                $xa='x'.$i;
                $cha='ch'.$i;
                $za='z'.$i;
                if (isset($_GET[$cha])){
                    $chb = 1;
                } else {
                    $chb = 2;
                }
                $checked = bill_update_check( $_GET[$za], $_GET[$xa], $chb );
            }
            //this is for ?
            //$sql = $sql.' ELSE xread END';
            if ($pageno>$_GET['lastpage']){
                echo 'No pages to display';
            } else {
                echo 'Page no: '.$pageno.' -of- '.$_GET['lastpage'];
            }

            if ($pageno<=$_GET['lastpage']){
                $result = bill_get_checkread($_SESSION['comnum'], $_SESSION['batch'], $limit);
              //echo 'no: '.count($result).' y-x ';
?>
            <script>
               function setType(type)
               {
                  //formName is the name of your form
                  document.accinform.submitType.value = type;
               }
            </script>
            <p>After a reading is checked and updated, it will no longer show up in this listing, but will be posted when you post this batch</p>
            <form name="accinform" action="#" onsubmit="jQuery.ajax({url:'includes/arinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#arinfo').html(data);}});return false;">
                <table border="1">
                    <tr>
                        <th>Account</th>
                        <th>Name</th>
                        <th>Meter Address</th>
                        <th>Last</th>
                        <th>Reading</th>
                        <th>Checked</th>
                    </tr>
<?php
                for ($i=0;$i<count($result);$i++) { ?>
                    <tr>
                        <td><?php echo $result[$i]['zid']; ?></td>
                        <td><?php echo @$result[$i]['name']; ?></td>
                        <td><?php echo @$result[$i]['maddr']; ?></td>
                        <td><?php echo @$result[$i]['lread']; ?></td>
                        <td><input type="text" name="x<?php echo $i; ?>" id="x<?php echo $i; ?>" value="<?php echo @$result[$i]['xread']; ?>"></td>
                        <td<?php
                            //original red checkbackground in the update section.
                            //if ($result[$i]['xread']>($result[$i]['lread'] 100)) {
                            //Added red background check from the inital state when form is first loaded below ??
                                if ((@$result[$i]['xread']-@$result[$i]['lread'])>(@$datz[0]['highlimit'])) { ?>
                                      style="background-color: red;">
                                <?php
                            } else if (@$result[$i]['lread']>(@$result[$i]['xread']-1)) { ?>
                                style="background-color: blue;">
                                <?php
                            } else { ?>
                                style="background-color: green;">
                            <?php
                            } ?>
                            <input type="checkbox" name="ch<?php echo $i; ?>" id="ch<?php echo $i; ?>" checked="checked">
                            <input type="hidden" name="z<?php echo $i; ?>" id="z<?php echo $i; ?>" value="<?php echo $result[$i]['id']; ?>">
                           </td>
                    </tr>
                <?php } ?>
                  </table>
                    <input type="hidden" name="pageno" value="<?php echo $pageno  ; ?>">
                    <input type="hidden" name="lastpage" value="<?php echo $_GET['lastpage']; ?>">
                    <input type="hidden" name="nrow" value="<?php echo $i; ?>">
                    <input type="hidden" name="nmrows" value="<?php echo $nmrows; ?>">
                    <input type="hidden" name="rowsperpage" value="<?php echo $rows_per_page; ?>">
                    <input type="hidden" name="submitType"/>
                    <input type="submit" onclick="setType('batchUpdate')" value="Update Above Readings"/>
            </form>
<?php
            } else {
                $nm = bill_reset_check( $_SESSION['batch'] );
                if ($nm>0){
                    echo '<p>That is the end of the batch file.  There are still '.$nm.
                        ' meter reading(s) that have not been accepted and checked. '.
                        ' You should go back and fix those that have not been accepted. When you are happy with'.
                        ' the readings, Click Manual Read and then choose the Post Readings button.'.
                        '  Otherwise, you should check them again.</p>';
                } else {
                    echo '<p>That is the end of the batch file.  You have checked all of the'.
                         ' meter readings in this batch.  Now that you are happy with'.
                         ' the readings, Click Manual Read and then choose the Post Readings button.'.
                         '  Otherwise, you should check them again.</p>';
                }
            }
        } //end batchupdate
    //postbatch
    else if ($_GET['submitType']=='postbatch') { ?>
        <script>
            $( function() {
            $( "#datepicker1" ).datepicker({altField: "#datepicker4"});
            } );
            $( function() {
            $( "#datepicker2" ).datepicker();
            } );
            $( function() {
            $( "#datepicker3" ).datepicker();
            } );
        </script>
            <form name="acx1inform" action="includes/billingpdf.php" target="_blank">
                <table>
                    <tr>
                        <td><label for="note1">Billing Notice to print on bill</label></td>
                    </tr>
                    <tr>
                        <td><input type="text" id="note1" name="note1"></td>
                    </tr>
                    <tr>
                        <td><label for="billingdate1">Bill Date to print on bill</label></td>
                    </tr>
                    <tr>
                        <td><input type="text" id="datepicker1" name="billingdate1"></td>
                    </tr>
                    <tr>
                        <td><label for="readingdate1">Reading Date to print on bill</label></td>
                    </tr>
                    <tr>
                        <td><input type="text" id="datepicker2" name="readingdate1"></td>
                    </tr>
                    <tr>
                        <td><label for="duedate1">Due Date to print on bill</label></td>
                    </tr>
                    <tr>
                        <td><input type="text" id="datepicker3" name="duedate1"></td>
                    </tr>
                    <tr>
                        <td>
                            <input type="hidden" name="id" value="<?php echo $_GET['id'] ?>">
                            <input type="submit" value="Print Bills" />
                        </td>
                    </tr>
                </table>
            </form>
            <form name="acz2inform" action="includes/postaccounts.php" target="_blank">
                <table>
                    <tr>
                        <td>If the bills printed correctly, you can now post the bills to the accounts.</td>
                    </tr>
                    <tr>
                        <td>
                            <input type="hidden" id="datepicker4" name="billingdate4">
                            <input type="hidden" name="id" value="<?php echo $_GET['id'] ?>">
                            <input type="submit" value="Complete Account Posting" />
                        </td>
                    </tr>
                </table>
            </form>
<?php }
    //updatepostings
    else if ($_GET['submitType']=='updatepostings') {
            echo 'Processing '.$i.' meter reads.';
            for ($i=0;$i<count($mrnow);$i++) {
                $sql = 'UPDATE customer, bill_history, mreadings SET abalance = '.$mrnow[$i]['zbalance'].', lmread = '
                .$mrnow[$i]['lmread'].', lmuse = '.$mrnow[$i]['lmuse'].', lread = '
                .$mrnow[$i]['lread'].', luse = '.$mrnow[$i]['luse'].',1 and customer.account = mtemp.zid order by customer.rorder';
                $st = $db->prepare($sql);
                $st->execute();
            }
        }
    //prevUpdate
    else if ($_GET['submitType']=='prevUpdate') {
            $pageno = $_GET['pageno']-1;
            $rows_per_page = $_GET['rowsperpage'];
            $nmrows = $_GET['nmrows'];
            $limit = ($pageno - 1) * $rows_per_page .',' .$rows_per_page;
            for ($i=0;$i<$_GET['nrow'];$i++) {
                $xa='x'.$i;
                $za='z'.$i;
                $na='n'.$i;
                $ida='id'.$i;
                //echo $_GET[$na].' - '.$_GET[$xa].' - '.$_GET[$za].' - '.$_GET[$ida]. ' x ';
                if ($_GET[$na]==4){
                    if ($_GET[$xa]>0){
                        $test = bill_insert_newread($_SESSION['comnum'], $_GET[$za], $_GET[$xa], $_SESSION['batch']);
                    }
                } else {
                    $test = bill_update_newread($_GET[$ida], $_GET[$xa]);
                }
            }
          //echo 'page no: '.$pageno.' -- '.$_GET['lastpage'].' x-x ';
            if ($pageno<=$_GET['lastpage']){
                $limit = ($pageno - 1) * $rows_per_page .',' .$rows_per_page;
                $result = bill_get_meters($_SESSION['comnum'], $limit);
                $datax = bill_get_reads($_SESSION['comnum'], $_SESSION['batch']);
    ?>
        <script>
           function setType(type)
           {
              //formName is the name of your form
              document.accinform1.submitType.value = type;
           }
        </script>
        <?php echo 'Page ' .$pageno.' of '.$_GET['lastpage']; ?>
        <form name="accinform1" action="#" onsubmit="jQuery.ajax({url:'includes/arinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#arinfo').html(data);}});return false;">
            <table border="1">
                <tr>
                    <th>Account</th>
                    <th>Name</th>
                    <th>Meter Address</th>
                    <th>Last</th>
                    <th>Reading</th>
                </tr>
    <?php
                for ($i=0;$i<count($result);$i++) {
                    $datay = '';
                    $datar = '4';
                    $dataid = '';
                    for ($b=0;$b<count($datax);$b++) {
                        if ($result[$i]['account']==$datax[$b]['zid'] ) {
                            $datay = $datax[$b]['xread'];
                            $datar = '1';
                            $dataid = $datax[$b]['id'];
                        }
                    }
     ?>
                 <tr>
                     <td><?php echo $result[$i]['account']; ?></td>
                    <td><?php echo @$result[$i]['name']; ?></td>
                    <td><?php echo @$result[$i]['maddr']; ?></td>
                    <td><?php echo @$result[$i]['lread']; ?></td>
                    <td><input type="text" name="x<?php echo $i; ?>" id="x<?php echo $i; ?>" value="<?php echo $datay; ?>">
                        <input type="hidden" name="z<?php echo $i; ?>" id="z<?php echo $i; ?>" value="<?php echo  $result[$i]['account']; ?>">
                        <input type="hidden" name="n<?php echo $i; ?>" id="n<?php echo $i; ?>" value="<?php echo  $datar; ?>">
                        <input type="hidden" name="id<?php echo $i; ?>" id="id<?php echo $i; ?>" value="<?php echo  $dataid; ?>">
                    </td>
                 </tr>
    <?php } ?>
            </table>
            <input type="hidden" name="pageno" value="<?php echo $pageno; ?>">
            <input type="hidden" name="lastpage" value="<?php echo $_GET['lastpage']; ?>">
            <input type="hidden" name="nrow" value="<?php echo $i; ?>">
            <input type="hidden" name="nmrows" value="<?php echo $nmrows; ?>">
            <input type="hidden" name="rowsperpage" value="<?php echo $rows_per_page; ?>">
            <input type="hidden" name="submitType">
            <?php if ($pageno>1) { ?><input type="submit" onclick="setType('prevUpdate')" value="Add Above Readings and move to previous page"> <?php } ?>
            <input type="submit" onclick="setType('rcUpdate')" value="Add Above Readings and move to next page">
        </form>
    <?php
            } else echo '<h3>That is the end of the meter file.  Now you should click the check readings button.</h3>';
        }
} else { ?>
    <script>alert('You do not have a high enough access level to post payments\n\ or charges!\n\ Please see your administrator.');</script>
<?php } ?>

