added client side functions and mode_of_payment defaults
diff --git a/accounts/doctype/mode_of_payment/mode_of_payment.js b/accounts/doctype/mode_of_payment/mode_of_payment.js
index 3cad944..85b2ab1 100644
--- a/accounts/doctype/mode_of_payment/mode_of_payment.js
+++ b/accounts/doctype/mode_of_payment/mode_of_payment.js
@@ -14,13 +14,9 @@
 // You should have received a copy of the GNU General Public License
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
- 
-
-//--------- ONLOAD -------------
-cur_frm.cscript.onload = function(doc, cdt, cdn) {
-   
-}
-
-cur_frm.cscript.refresh = function(doc, cdt, cdn) {
-   
-}
\ No newline at end of file
+cur_frm.set_query("default_account", function(doc) {
+	return erpnext.queries.account({
+		account_type: "Bank or Cash",
+		company: doc.company
+	});
+});
\ No newline at end of file
diff --git a/accounts/doctype/mode_of_payment/mode_of_payment.txt b/accounts/doctype/mode_of_payment/mode_of_payment.txt
index cf7d740..424fdc7 100644
--- a/accounts/doctype/mode_of_payment/mode_of_payment.txt
+++ b/accounts/doctype/mode_of_payment/mode_of_payment.txt
@@ -1,71 +1,71 @@
-# DocType, Mode of Payment
 [
-
-	# These values are common in all dictionaries
-	{
-		'creation': '2012-03-27 14:35:44',
-		'docstatus': 0,
-		'modified': '2012-03-27 14:35:44',
-		'modified_by': u'Administrator',
-		'owner': u'harshada@webnotestech.com'
-	},
-
-	# These values are common for all DocType
-	{
-		'autoname': u'field:mode_of_payment',
-		'colour': u'White:FFF',
-		'doctype': 'DocType',
-		'document_type': u'Master',
-		'module': u'Accounts',
-		'name': '__common__',
-		'section_style': u'Simple',
-		'server_code_error': u' ',
-		'version': 3
-	},
-
-	# These values are common for all DocField
-	{
-		'doctype': u'DocField',
-		'fieldname': u'mode_of_payment',
-		'fieldtype': u'Data',
-		'label': u'Mode of Payment',
-		'name': '__common__',
-		'oldfieldname': u'mode_of_payment',
-		'oldfieldtype': u'Data',
-		'parent': u'Mode of Payment',
-		'parentfield': u'fields',
-		'parenttype': u'DocType',
-		'permlevel': 0,
-		'reqd': 1
-	},
-
-	# These values are common for all DocPerm
-	{
-		'create': 1,
-		'doctype': u'DocPerm',
-		'name': '__common__',
-		'parent': u'Mode of Payment',
-		'parentfield': u'permissions',
-		'parenttype': u'DocType',
-		'permlevel': 0,
-		'read': 1,
-		'role': u'Accounts Manager',
-		'write': 1
-	},
-
-	# DocType, Mode of Payment
-	{
-		'doctype': 'DocType',
-		'name': u'Mode of Payment'
-	},
-
-	# DocPerm
-	{
-		'doctype': u'DocPerm'
-	},
-
-	# DocField
-	{
-		'doctype': u'DocField'
-	}
+ {
+  "owner": "harshada@webnotestech.com", 
+  "docstatus": 0, 
+  "creation": "2012-07-03 13:30:49", 
+  "modified_by": "Administrator", 
+  "modified": "2012-12-04 16:31:20"
+ }, 
+ {
+  "autoname": "field:mode_of_payment", 
+  "name": "__common__", 
+  "doctype": "DocType", 
+  "module": "Accounts", 
+  "document_type": "Master"
+ }, 
+ {
+  "name": "__common__", 
+  "parent": "Mode of Payment", 
+  "doctype": "DocField", 
+  "parenttype": "DocType", 
+  "permlevel": 0, 
+  "parentfield": "fields"
+ }, 
+ {
+  "parent": "Mode of Payment", 
+  "read": 1, 
+  "cancel": 1, 
+  "name": "__common__", 
+  "amend": 0, 
+  "create": 1, 
+  "doctype": "DocPerm", 
+  "submit": 0, 
+  "write": 1, 
+  "parenttype": "DocType", 
+  "role": "Accounts Manager", 
+  "permlevel": 0, 
+  "parentfield": "permissions"
+ }, 
+ {
+  "name": "Mode of Payment", 
+  "doctype": "DocType"
+ }, 
+ {
+  "oldfieldtype": "Data", 
+  "doctype": "DocField", 
+  "label": "Mode of Payment", 
+  "oldfieldname": "mode_of_payment", 
+  "fieldname": "mode_of_payment", 
+  "fieldtype": "Data", 
+  "reqd": 1
+ }, 
+ {
+  "doctype": "DocField", 
+  "label": "Company", 
+  "fieldname": "company", 
+  "fieldtype": "Link", 
+  "options": "Company"
+ }, 
+ {
+  "description": "Will be automatically updated in POS Invoice when this mode is selected.", 
+  "colour": "White:FFF", 
+  "doctype": "DocField", 
+  "label": "Default Account", 
+  "fieldname": "default_account", 
+  "fieldtype": "Link", 
+  "options": "Account"
+ }, 
+ {
+  "doctype": "DocPerm"
+ }
 ]
\ No newline at end of file
diff --git a/accounts/doctype/sales_invoice/sales_invoice.js b/accounts/doctype/sales_invoice/sales_invoice.js
index 4c426f3..48f9e96 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/accounts/doctype/sales_invoice/sales_invoice.js
@@ -148,6 +148,12 @@
 	}
 }
 
+cur_frm.cscript.mode_of_payment = function(doc) {
+	cur_frm.call({
+		method: "get_bank_cash_account",
+		args: { mode_of_payment: doc.mode_of_payment }
+	});
+}
 
 cur_frm.cscript.update_stock = function(doc, dt, dn) {
 	cur_frm.cscript.hide_fields(doc, dt, dn);
@@ -387,9 +393,9 @@
 
 // Income Account in Details Table
 // --------------------------------
-cur_frm.fields_dict.entries.grid.get_field("income_account").get_query = function(doc) {
-	return 'SELECT tabAccount.name FROM tabAccount WHERE (tabAccount.debit_or_credit="Credit" OR tabAccount.account_type = "Income Account") AND tabAccount.group_or_ledger="Ledger" AND tabAccount.docstatus!=2 AND tabAccount.company="'+doc.company+'" AND tabAccount.%(key)s LIKE "%s"';
-}
+cur_frm.set_query("income_account", "entries", function(doc) {
+	return 'SELECT tabAccount.name FROM tabAccount WHERE (tabAccount.debit_or_credit="Credit" OR tabAccount.account_type = "Income Account") AND tabAccount.group_or_ledger="Ledger" AND tabAccount.docstatus!=2 AND tabAccount.company="'+doc.company+'" AND tabAccount.%(key)s LIKE "%s"';	
+})
 
 // warehouse in detail table
 //----------------------------
diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py
index eb7268b..3292be3 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/accounts/doctype/sales_invoice/sales_invoice.py
@@ -322,7 +322,6 @@
 		if res:
 			get_obj('DocType Mapper', 'Project-Sales Invoice').dt_map('Project', 'Sales Invoice', self.doc.project_name, self.doc, self.doclist, "[['Project', 'Sales Invoice']]")
 
-
 	def get_company_abbr(self):
 		return webnotes.conn.sql("select abbr from tabCompany where name=%s", self.doc.company)[0][0]
 		
@@ -899,3 +898,9 @@
 		}
 		assign_to.add(args)
 
+@webnotes.whitelist()
+def get_bank_cash_account(mode_of_payment):
+	return {
+		"cash_bank_account": webnotes.conn.get_value("Mode of Payment", 
+			mode_of_payment, "default_account")
+	}
\ No newline at end of file
diff --git a/public/js/utils.js b/public/js/utils.js
index 4398498..d02fdb3 100644
--- a/public/js/utils.js
+++ b/public/js/utils.js
@@ -82,4 +82,25 @@
 		case when name like \"%s%%\" then 0 else 1 end, \
 		case when supplier_name like \"%s%%\" then 0 else 1 end, \
 		name, supplier_name limit 50";
-};
\ No newline at end of file
+};
+
+wn.provide("erpnext.queries");
+
+erpnext.queries.account = function(opts) {
+	if(!opts) 
+		opts = {};
+	if(!opts.group_or_ledger) 
+		opts.group_or_ledger = "Ledger";
+		
+	conditions = [];
+	$.each(opts, function(key, val) {
+		conditions.push("tabAccount.`" + key + "`='"+esc_quotes(val)+"'");
+	});
+	
+	return 'SELECT tabAccount.name, tabAccount.parent_account, tabAccount.debit_or_credit \
+		FROM tabAccount \
+		WHERE tabAccount.docstatus!=2 \
+		AND tabAccount.%(key)s LIKE "%s" ' + (conditions 
+			? (" AND " + conditions.join(" AND "))
+			: "")
+}
\ No newline at end of file
diff --git a/selling/doctype/opportunity/opportunity.js b/selling/doctype/opportunity/opportunity.js
index 67a6e24..2bb41b2 100644
--- a/selling/doctype/opportunity/opportunity.js
+++ b/selling/doctype/opportunity/opportunity.js
@@ -96,6 +96,7 @@
 cur_frm.cscript.customer = function(doc,dt,dn) {
 	if(doc.customer) {
 		cur_frm.call({
+			doc: cur_frm.doc,
 			method: "get_default_customer_address",
 			args: { customer: doc.customer },
 			callback: function(r) {