5 // Specifically for skipping importable transactions, we have an "X" type here.
6 apms
.allowedTypes
= new RegExp(/[CTLPJFX]/i);
8 function getDocumentTotal(doc
) {
9 docTotal
= new Number(0);
12 moneyField
= document
.getElementById("txn["+doc
+"]["+i
+"][amount]");
14 amount
= new Number(moneyField
.value
);
15 if ( amount
) docTotal
+= amount
;
18 } while( moneyField
);
23 function checkDocumentTotal(doc
) {
26 totalField
= $("#wanttotal-"+doc
).get(0);
27 totalField
.setAttribute("class","money");
28 wantTotal
= new Number(totalField
.value
);
29 gotTotalField
= $("#gottotal-"+doc
).get(0);
30 gotTotalField
.setAttribute("class","money");
31 docTotal
= getDocumentTotal(doc
);
32 matching
= (wantTotal
== docTotal
);
33 $(totalField
).css("background-color",(matching
? "#40f040" : "#ffc040" ));
34 gotTotalField
.value
= docTotal
;
35 // alert("Total is now "+docTotal+" vs. "+wantTotal+ " they are " + (wantTotal == docTotal ? "" : "not " )+"equal.");
37 visibility
= ( !matching
|| (+i
> 2) ? "text" : "hidden");
38 totalField
.setAttribute("type",visibility
);
39 gotTotalField
.setAttribute("type",visibility
);
40 if ( matching
) $("#txn-"+doc
).removeClass("error");
48 function splitDocument(doc
){
52 idSelector
= "#row-"+doc
+"-"+txn
;
53 row
= $(idSelector
).html();
54 if ( row
) rows
[txn
++] = row
;
56 row
= document
.getElementById("row-"+doc
+"-1");
58 new_row
= row
.innerHTML
;
59 replaceTxn
= new RegExp("txn\\["+doc
+"\\]\\[1\\]","g")
60 new_row
= new_row
.replace(replaceTxn
,"txn["+doc
+"]["+txn
+"]");
61 // alert("New Row "+txn+" is " +new_row);
62 new_row
= '<tr class="'+row
.getAttribute("class")+'" id="row-'+doc
+'-'+txn
+'">'+new_row
+'</tr>';
63 new_row_id
= new_row
.id
;
65 $("#txn-"+doc
).append(new_row
);
67 rowButton
= document
.getElementById("split-txn["+doc
+"]["+txn
+"]");
68 rowButton
.innerHTML
= "Join";
69 rowButton
.setAttribute("onClick","joinDocument("+doc
+","+txn
+")");
70 $("#split-txn["+doc
+"]["+txn
+"]").text("Join");
71 moneyField
= document
.getElementById("txn["+doc
+"]["+txn
+"][amount]");
72 moneyField
.setAttribute("onChange","checkDocumentTotal("+doc
+")");
74 $("#row-"+doc
+"-"+txn
+" "+"input").each(function(){
75 $(this).on("focusin",function(e
){e
.target
.select();});
76 $(this).on("keypress",nextFieldOnEnter
);
79 newMoney
= document
.getElementById("txn["+doc
+"]["+txn
+"][amount]");
81 totalField
= $("#wanttotal-"+doc
).get(0);
82 wantTotal
= new Number(totalField
.value
);
84 gotTotal
= getDocumentTotal(doc
);
85 // alert("Want: "+wantTotal+", Got: "+gotTotal+", Diff: "+(wantTotal - gotTotal));
86 newMoney
.value
= (wantTotal
- gotTotal
).toFixed(2);
88 checkDocumentTotal(doc
);
92 function joinDocument(doc
,deleteTxn
) {
96 idSelector
= "#row-"+doc
+"-"+txn
;
97 row
= $(idSelector
).html();
98 if ( row
) rows
[txn
++] = row
;
100 row
= document
.getElementById("row-"+doc
+"-"+deleteTxn
);
101 row
.parentNode
.removeChild(row
);
103 for( i
=deleteTxn
+1; i
< txn
; i
++ ) {
104 idSelector
= "#row-"+doc
+"-"+i
;
105 row
= $(idSelector
).get(0);
108 row
.id
="row-"+doc
+"-"+j
;
109 inputs
= $("#row-"+doc
+"-"+j
+" :input").get();
110 replaceTxn
= new RegExp("txn\\["+doc
+"\\]\\[\\d+\\]","g");
111 for( k
=0; k
<inputs
.length
; k
++ ) {
112 replacement
= "txn["+doc
+"]["+j
+"]";
113 inputs
[k
].name
= inputs
[k
].name
.replace(replaceTxn
,replacement
);
114 if ( inputs
[k
].id
) inputs
[k
].id
= inputs
[k
].id
.replace(replaceTxn
,replacement
);
116 rowButtonId
= "split-txn["+doc
+"]["+i
+"]";
117 rowButton
= document
.getElementById(rowButtonId
);
119 rowButton
= document
.getElementById("split-txn["+doc
+"]["+j
+"]");
122 rowButton
.id
= "split-txn["+doc
+"]["+j
+"]";
125 alert("Row button not found for "+rowButtonId
);
128 rowButton
.innerHTML
= "Join";
129 rowButton
.setAttribute("onClick","joinDocument("+doc
+","+j
+")");
131 checkDocumentTotal(doc
);
135 function validateAll() {
139 et
= document
.getElementById("txn["+doc
+"][1][entitytype]");
143 entityTypeField
= document
.getElementById("txn["+doc
+"]["+txn
+"][entitytype]");
144 if ( entityTypeField
&& !onEntityTypeChange(entityTypeField
) && firstBad
== 0 ) firstBad
= doc
;
146 } while( entityTypeField
);
147 if ( !checkDocumentTotal(doc
) ) {
148 if ( firstBad
== 0 ) firstBad
= doc
;
149 $("#txn-"+doc
).addClass("error");
154 if ( firstBad
== 0 ) return true;
156 alert("Errors in the form have been highlighted.");
158 // Otherwise try and move the focus there.
159 entityTypeField
= document
.getElementById("txn["+firstBad
+"][1][entitytype]");
160 entityTypeField
.focus();