I'm working with this application:
http://www.grabilla.com/02414-a3219a...fbb0a9c9ae.png
I have the Notes field on the invoice implemented, but I want it to be an optional field. Right now I can't submit anything with the notes field empty. Here is my code, there are 2 seperate .js files for this, the library.js and the main.js:
library:
HTML Code:String.prototype.pad_left = function() { if ( arguments.length < 1 || arguments.length > 2 ) { return this; } var width = parseInt(arguments[0]); var pad = " "; if ( arguments.length == 2 ) pad = arguments[1]; var result = this; while ( result.length < width ) { result = pad + result; } return result; } String.prototype.pad_right = function() { if ( arguments.length < 1 || arguments.length > 2 ) { return this; } var width = parseInt(arguments[0]); var pad = " "; if ( arguments.length == 2 ) pad = arguments[1]; var result = this; while ( result.length < width ) { result = result + pad; } return result; } var Item_Info = function( item_name, item_cost, item_qty, item_notes ) { this.item_name = item_name; this.item_cost = parseFloat(item_cost); this.item_qty = parseInt(item_qty); this.item_notes = item_notes; } var Invoice = function() { this.items = []; this.tax_rate = 0.07; } Invoice.prototype.add_item = function(item_code, item_info) { if ( ! item_info instanceof Item_Info ) return this; if ( isNaN(item_info.item_cost) ) return this; if ( isNaN(item_info.item_qty) ) return this; if ( item_info.item_name == "" ) return this; if ( item_code == "" ) return this; if ( item_info.item_notes == "") return this; item_code = item_code.toUpperCase(); if ( item_code in this.items ) { delete this.items[item_code]; } this.items[item_code] = item_info; this.sort_by_code(); return this; } Invoice.prototype.delete_item = function(item_code) { item_code = item_code.toUpperCase(); if ( item_code in this.items ) { delete this.items[item_code]; } return this; } Invoice.prototype.get_item_list = function() { var item_list, line_cost, item_count, item_notes = 0; item_list = "Item Code".pad_right(10) + " "; item_list += "Item Name".pad_right(25) + " "; item_list += "Qty "; item_list += "Item Cost "; item_list += "Line Cost "; item_list += "Notes\n"; item_list += "".pad_right(10,"-") + " "; item_list += "".pad_right(25,"-") + " "; item_list += "--- "; item_list += "".pad_right(9,"-") + " "; item_list += "".pad_right(9,"-") + " "; item_list += "".pad_right(25,"-") + " "; for ( var code in this.items ) { line_cost = this.items[code].item_qty * this.items[code].item_cost; item_list += code.pad_right(10) + " "; item_list += this.items[code].item_name.pad_right(25) + " "; item_list += this.items[code].item_qty.toString().pad_left(3) + " "; item_list += "$" + this.items[code].item_cost.toFixed(2).pad_left(8) + " "; item_list += "$" + line_cost.toFixed(2).pad_left(8) + " "; item_list += this.items[code].item_notes.pad_right(25) + "\n"; item_count++; } return (item_count == 0) ? "" : item_list; } Invoice.prototype.get_subtotal = function () { var subtotal = 0, line_cost; for ( var code in this.items ) { line_cost = this.items[code].item_qty * this.items[code].item_cost; subtotal += parseFloat( line_cost.toFixed(2) ); } return subtotal; } Invoice.prototype.get_sales_tax = function () { var subtotal = this.get_subtotal(); var sales_tax = subtotal * this.tax_rate; return parseFloat( sales_tax.toFixed(2) ); } Invoice.prototype.get_total = function () { var total = this.get_subtotal() + this.get_sales_tax(); return parseFloat( total.toFixed(2) ); } Invoice.prototype.sort_by_code = function () { var code, codes = [], sorted_list = []; for ( code in this.items ) codes.push(code); codes.sort(); for ( code in codes ) { sorted_list[ codes[code] ] = this.items[ codes[code] ]; } this.items = sorted_list; return this; }
main:
I had to wrap the first block in HTML tags because the regular code tags didn't work for some reason, and, as always, any help is much appreciated.Code:var invoice = new Invoice(); var $ = function(id) { return document.getElementById(id); } var update_invoice = function () { $("item_list").value = invoice.get_item_list(); $("subtotal").value = invoice.get_subtotal().toFixed(2); $("sales_tax").value = invoice.get_sales_tax().toFixed(2); $("total").value = invoice.get_total().toFixed(2); $("item_code").value = ""; $("item_name").value = ""; $("item_cost").value = ""; $("item_qty").value = "1"; $("item_notes").value = ""; $("item_delete_code").value = ""; $("item_code").focus(); } var item_add_click = function() { var item_code = $("item_code").value; var item_name = $("item_name").value; var item_cost = $("item_cost").value; var item_qty = $("item_qty").value; var item_notes = $("item_notes").value; var item_info = new Item_Info(item_name, item_cost, item_qty, item_notes); invoice.add_item(item_code, item_info); update_invoice(); } var item_delete_click = function() { var item_code = $("item_delete_code").value; invoice.delete_item(item_code); update_invoice(); } window.onload = function () { $("item_add").onclick = item_add_click; $("item_delete").onclick = item_delete_click; update_invoice(); $("item_code").focus(); }


Reply With Quote

Bookmarks