--- transaction.scm	2014-04-02 08:41:36.000000000 +0530
+++ transaction.scm	2014-04-04 22:06:58.690115518 +0530
@@ -9,6 +9,8 @@
 ;; Michael T. Garrison Stuber
 ;; Modified account names display by Tomas Pospisek
 ;; <tpo_deb@sourcepole.ch> with a lot of help from "warlord"
+;; Modified by AMM for pagewise reports with headers on each page
+;;   and other minor improvements
 ;;
 ;; This program is free software; you can redistribute it and/or    
 ;; modify it under the terms of the GNU General Public License as   
@@ -44,6 +46,7 @@
 
 ;; Define the strings here to avoid typos and make changes easier.
 
+(define heading-list (list))
 (define reportname (N_ "Transaction Report"))
 (define pagename-sorting (N_ "Sorting"))
 (define optname-prime-sortkey (N_ "Primary Key"))
@@ -54,6 +57,9 @@
 (define optname-sec-date-subtotal (N_ "Secondary Subtotal for Date Key"))
 (define optname-void-transactions (N_ "Void Transactions"))
 (define optname-table-export (N_ "Table for Exporting"))
+(define optname-table-pagebreak (N_ "Insert Pagebreak before Tables"))
+(define optname-table-onetable (N_ "Use Single Table for Accounts"))
+(define optname-table-style (N_ "Additional Style Data for Document"))
 (define optname-common-currency (N_ "Common Currency"))
 (define optname-currency (N_ "Report's currency"))
 (define def:grand-total-style "grand-total")
@@ -127,12 +133,22 @@
     (apply gnc:html-table-set-row-style! arg-list)))
 
 (define (add-subheading-row data table width subheading-style)
-  (let ((heading-cell (gnc:make-html-table-cell data)))
+  (let ((heading-cell (gnc:make-html-table-cell/markup "total-label-cell" data))
+        (actheadstart-cell (gnc:make-html-table-cell))
+        (actheadend-cell (gnc:make-html-table-cell)))
+    (gnc:html-table-append-row! table (list actheadstart-cell))
+    (set-last-row-style! table "tr" 'attribute (list "class" "actheadstart"))
     (gnc:html-table-cell-set-colspan! heading-cell width)
     (gnc:html-table-append-row/markup!
      table
      subheading-style
-     (list heading-cell))))
+     (list heading-cell))
+    (gnc:html-table-append-row/markup!
+     table
+     subheading-style
+     (reverse heading-list))
+    (gnc:html-table-append-row! table (list actheadend-cell))
+    (set-last-row-style! table "tr" 'attribute (list "class" "actheadend"))))
 
 ;; display an account name depending on the options the user has set
 (define (account-namestring account show-account-code show-account-name show-account-full-name)
@@ -355,7 +371,12 @@
   (do ((i 0 (+ i 1)) 
        (col-req 0 col-req)) 
       ((>= i columns-used-size) col-req)
-    (if (vector-ref columns-used i) (set! col-req (+ col-req 1)))))
+    (if (and (not (= i 12)) (not (= i 16)) (not (= i 18)) (not (= i 19)) (vector-ref columns-used i))
+      (set! col-req (+ col-req 1)))
+    (if (or (and (= i 14) (vector-ref columns-used 14) (vector-ref columns-used 4))
+            (and (= i 15) (vector-ref columns-used 15) (vector-ref columns-used 5))
+            (and (= i 18) (vector-ref columns-used 18) (vector-ref columns-used 17)))
+      (set! col-req (- col-req 1)))))
 
 (define (build-column-used options)   
   (define (opt-val section name)
@@ -407,7 +428,7 @@
     column-list))
 
 (define (make-heading-list column-vector options)
-  (let ((heading-list '()))
+  (let ((lclheading-list '()))
     (if (used-date column-vector)
         (addto! heading-list (_ "Date")))
     (if (used-reconciled-date column-vector)
@@ -443,9 +464,9 @@
         (addto! heading-list (_ "Amount")))
     ;; FIXME: Proper labels: what?
     (if (used-amount-double-positive column-vector)
-        (addto! heading-list (_ "Debit")))
-    (if (used-amount-double-negative column-vector)
         (addto! heading-list (_ "Credit")))
+    (if (used-amount-double-negative column-vector)
+        (addto! heading-list (_ "Debit")))
     (if (used-running-balance column-vector)
         (addto! heading-list (_ "Balance")))
     (reverse heading-list)))
@@ -629,6 +650,21 @@
     gnc:pagename-general optname-table-export
     "g" (N_ "Formats the table suitable for cut & paste exporting with extra cells.") #f))  
   
+  (gnc:register-trep-option
+   (gnc:make-simple-boolean-option
+    gnc:pagename-general optname-table-pagebreak
+    "h" (N_ "Formats the table for printing each table on separate page.") #f))  
+  
+  (gnc:register-trep-option
+   (gnc:make-simple-boolean-option
+    gnc:pagename-general optname-table-onetable
+    "i" (N_ "Use only one table for all accounts, instead separate tables for each Account.") #f))  
+  
+  (gnc:register-trep-option
+   (gnc:make-text-option
+    gnc:pagename-general optname-table-style
+    "j" (N_ "Additional Style to apply to document.") ".subac { margin-bottom:20px; }\n@page { margin:5mm; }\ntable { width:100%; }\n"))  
+  
   ;; Accounts options
   
   ;; account to do report on
@@ -1213,9 +1249,11 @@
          (account-types-to-reverse
           (get-account-types-to-reverse options)))
 
-    (gnc:html-table-set-col-headers!
-     table
-     (make-heading-list used-columns options))
+;;    (gnc:html-table-set-col-headers!
+;;     table
+;;     (make-heading-list used-columns options))
+    (set! heading-list (list))
+    (make-heading-list used-columns options)
     ;;     (gnc:warn "Splits:" splits)
     (if (not (null? splits))
         (begin
@@ -1398,7 +1436,6 @@
 	  (member fullname (get-other-account-names account-list))
 	  )))
 
-
   (gnc:report-starting reportname)
   (let ((document (gnc:make-html-document))
 	(c_account_1 (opt-val gnc:pagename-accounts "Accounts"))
@@ -1418,6 +1455,8 @@
         (secondary-key (opt-val pagename-sorting optname-sec-sortkey))
         (secondary-order (opt-val pagename-sorting "Secondary Sort Order"))
 	(void-status (opt-val gnc:pagename-accounts optname-void-transactions))
+	(pagebreak? (opt-val gnc:pagename-general optname-table-pagebreak))
+	(onetable? (opt-val gnc:pagename-general optname-table-onetable))
         (splits '())
         (query (qof-query-create-for-splits)))
 
@@ -1509,7 +1548,32 @@
                    (display-date-interval begindate enddate))))
                 (gnc:html-document-add-object!
                  document 
+                 (gnc:make-html-text
+                  "<div>\n"))
+                (gnc:html-document-add-object!
+                 document 
                  table)
+                (gnc:html-document-add-object!
+                 document 
+                 (gnc:make-html-text
+                  "</div>\n"))
+                (gnc:html-document-add-object!
+                 document 
+                 (gnc:make-html-text
+                  "<style>.subac{page-break-before:"
+                  (if pagebreak? "always" "auto")
+                  "}\n"
+                  (opt-val gnc:pagename-general optname-table-style)
+                  "</style>\n"
+                  "<script>var onetable="
+                  (if onetable? "true" "false")
+                  ";if (!onetable)window.onload=function(){\n"
+                  "var tblre=new RegExp('<table\\\\b.+>','i');"
+                  "var tbl=tblre.exec(document.body.innerHTML);\n"
+                  "if (tbl==null) tbl='<table cellspacing=\"3.0\" border=\"0\" cellpadding=\"3.0\">';\n"
+                  "document.body.innerHTML=document.body.innerHTML.replace(/<tr\\b.+class=\"actheadstart\"[\\S\\s]+?<\\/tr>/g,'</table></div><div class=\"subac\">'+tbl+'<thead>');\n"
+                  "document.body.innerHTML=document.body.innerHTML.replace(/<tr\\b.+class=\"actheadend\"[\\S\\s]+?<\\/tr>/g,'</thead>');\n"
+                  "}</script>\n"))
                 (qof-query-destroy query))
               ;; error condition: no splits found
               (let ((p (gnc:make-html-text)))
