toggle display of contact info based on customer and conatct, address query based on customer
diff --git a/accounts/doctype/sales_invoice/sales_invoice.js b/accounts/doctype/sales_invoice/sales_invoice.js
index 86dc3a5..eab1d47 100644
--- a/accounts/doctype/sales_invoice/sales_invoice.js
+++ b/accounts/doctype/sales_invoice/sales_invoice.js
@@ -94,8 +94,7 @@
 		for(f in item_flds_pos) cur_frm.fields_dict['entries'].grid.set_column_disp(item_flds_pos[f], false);
 	}
 
-	if(doc.customer) unhide_field('contact_section');
-	else hide_field('contact_section');
+	cur_frm.toggle_display("contact_section", doc.customer);
 
 	// India related fields
 	var cp = wn.control_panel;
@@ -173,6 +172,8 @@
 
 //Customer
 cur_frm.cscript.customer = function(doc,dt,dn,onload) {
+	cur_frm.toggle_display("contact_section", doc.customer);
+	
 	var pl = doc.price_list_name;
 	var callback = function(r,rt) {
 			var callback2 = function(doc, dt, dn) {
@@ -191,8 +192,6 @@
 	}
 	var args = onload ? 'onload':''
 	if(doc.customer) $c_obj(make_doclist(doc.doctype, doc.name), 'get_default_customer_address', args, callback);
-
-	if(doc.customer) unhide_field('contact_section');
 	
 }
 
@@ -202,25 +201,6 @@
 	if(doc.customer) get_server_fields('get_customer_address', JSON.stringify({customer: doc.customer, address: doc.customer_address, contact: doc.contact_person}),'', doc, dt, dn, 1);
 }
 
-cur_frm.fields_dict.customer_address.on_new = function(dn) {
-	locals['Address'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
-	locals['Address'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
-}
-
-cur_frm.fields_dict.contact_person.on_new = function(dn) {
-	locals['Contact'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
-	locals['Contact'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
-}
-
-cur_frm.fields_dict['customer_address'].get_query = function(doc, cdt, cdn) {
-	return 'SELECT name,address_line1,city FROM tabAddress WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
-}
-
-cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) {
-	return 'SELECT name,CONCAT(first_name," ",ifnull(last_name,"")) As FullName,department,designation FROM tabContact WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
-}
-
-
 // Set Due Date = posting date + credit days
 cur_frm.cscript.debit_to = function(doc,dt,dn) {
 
@@ -232,7 +212,8 @@
 	var callback = function(r,rt) {
 			var doc = locals[cur_frm.doctype][cur_frm.docname];
 			if(doc.customer) $c_obj(make_doclist(dt,dn), 'get_default_customer_address', '', callback2);
-			if(doc.customer) unhide_field('contact_section');
+			cur_frm.toggle_display("contact_section", doc.customer);
+			
 			cur_frm.refresh();
 	}
 
diff --git a/selling/doctype/opportunity/opportunity.js b/selling/doctype/opportunity/opportunity.js
index d5c665a..664b88d 100644
--- a/selling/doctype/opportunity/opportunity.js
+++ b/selling/doctype/opportunity/opportunity.js
@@ -25,6 +25,9 @@
 		cur_frm.add_custom_button('Opportunity Lost', cur_frm.cscript['Declare Opportunity Lost']);
 		cur_frm.add_custom_button('Send SMS', cur_frm.cscript.send_sms);
 	}
+	
+	cur_frm.toggle_display("contact_info", doc.customer || doc.lead);
+	
 }
 
 // ONLOAD
@@ -79,7 +82,7 @@
 }
 
 // hide - unhide fields based on lead or customer
-cur_frm.cscript.lead_cust_show = function(doc,cdt,cdn){
+cur_frm.cscript.lead_cust_show = function(doc,cdt,cdn){	
 	if(doc.enquiry_from == 'Lead'){
 		unhide_field(['lead']);
 		hide_field(['lead_name','customer','customer_address','contact_person','customer_name','address_display','contact_display','contact_mobile','contact_email','territory','customer_group']);
@@ -87,13 +90,15 @@
 	}
 	else if(doc.enquiry_from == 'Customer'){		
 		unhide_field(['customer']);
-		hide_field(['lead','lead_name','address_display','contact_display','contact_mobile','contact_email','territory']);		
+		hide_field(['lead','lead_name','address_display','contact_display','contact_mobile','contact_email','territory', 'customer_group']);		
 		doc.lead = doc.lead_name = doc.customer = doc.customer_address = doc.contact_person = doc.address_display = doc.contact_display = doc.contact_mobile = doc.contact_email = doc.territory = doc.customer_group = "";
 	}
 }
 
 // customer
 cur_frm.cscript.customer = function(doc,dt,dn) {
+	cur_frm.toggle_display("contact_info", doc.customer || doc.lead);
+	
 	if(doc.customer) {
 		cur_frm.call({
 			doc: cur_frm.doc,
@@ -127,11 +132,18 @@
 }
 
 cur_frm.fields_dict['customer_address'].get_query = function(doc, cdt, cdn) {
-	return 'SELECT name,address_line1,city FROM tabAddress WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
+	return 'SELECT name, address_line1, city FROM tabAddress \
+		WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND \
+		%(key)s LIKE "%s" ORDER BY name ASC LIMIT 50';
 }
 
 cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) {
-	return 'SELECT name,CONCAT(first_name," ",ifnull(last_name,"")) As FullName,department,designation FROM tabContact WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
+	if (!doc.customer) msgprint("Please select customer first");
+	else {
+		return 'SELECT name, CONCAT(first_name," ",ifnull(last_name,"")) As FullName, \
+		department, designation FROM tabContact WHERE customer = "'+ doc.customer + 
+		'" AND docstatus != 2 AND %(key)s LIKE "%s" ORDER BY name ASC LIMIT 50';
+	}
 }
 
 // lead
@@ -140,6 +152,8 @@
 }
 
 cur_frm.cscript.lead = function(doc, cdt, cdn) {
+	cur_frm.toggle_display("contact_info", doc.customer || doc.lead);
+	
 	if(doc.lead) get_server_fields('get_lead_details', doc.lead,'', doc, cdt, cdn, 1);
 	if(doc.lead) unhide_field(['lead_name','address_display','contact_mobile','contact_email','territory']);	
 }
diff --git a/selling/doctype/quotation/quotation.js b/selling/doctype/quotation/quotation.js
index 3673b07..532cecc 100644
--- a/selling/doctype/quotation/quotation.js
+++ b/selling/doctype/quotation/quotation.js
@@ -94,8 +94,7 @@
 
 	erpnext.hide_naming_series();
 	
-	if(doc.customer || doc.lead) $(cur_frm.fields_dict.contact_section.row.wrapper).toggle(true);
-	else $(cur_frm.fields_dict.contact_section.row.wrapper).toggle(false);
+	cur_frm.toggle_display("contact_section", doc.customer || doc.lead);
 	
 	if (!doc.__islocal) {
 		cur_frm.communication_view = new wn.views.CommunicationList({
@@ -119,7 +118,8 @@
 
 	if(doc.customer) $c_obj(make_doclist(doc.doctype, doc.name), 
 		'get_default_customer_address', '', callback);
-	if(doc.customer) unhide_field(['customer_address','contact_person','territory', 'customer_group']);
+		
+	cur_frm.toggle_display("contact_section", doc.customer);
 }
 
 cur_frm.cscript.customer_address = cur_frm.cscript.contact_person = function(doc,dt,dn) {
@@ -130,24 +130,6 @@
 	}),'', doc, dt, dn, 1);
 }
 
-cur_frm.fields_dict.customer_address.on_new = function(dn) {
-	locals['Address'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
-	locals['Address'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
-}
-
-cur_frm.fields_dict.contact_person.on_new = function(dn) {
-	locals['Contact'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
-	locals['Contact'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
-}
-
-cur_frm.fields_dict['customer_address'].get_query = function(doc, cdt, cdn) {
-	return 'SELECT name,address_line1,city FROM tabAddress WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
-}
-
-cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) {
-	return 'SELECT name,CONCAT(first_name," ",ifnull(last_name,"")) As FullName,department,designation FROM tabContact WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
-}
-
 cur_frm.fields_dict.lead.get_query = erpnext.utils.lead_query;
 
 cur_frm.cscript.lead = function(doc, cdt, cdn) {
diff --git a/selling/doctype/sales_common/sales_common.js b/selling/doctype/sales_common/sales_common.js
index c1435bd..4ceac9e 100644
--- a/selling/doctype/sales_common/sales_common.js
+++ b/selling/doctype/sales_common/sales_common.js
@@ -320,7 +320,27 @@
 		}
 	}
 
+cur_frm.fields_dict.customer_address.on_new = function(dn) {
+	locals['Address'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
+	locals['Address'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
+}
 
+cur_frm.fields_dict.contact_person.on_new = function(dn) {
+	locals['Contact'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
+	locals['Contact'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
+}
+
+cur_frm.fields_dict['customer_address'].get_query = function(doc, cdt, cdn) {
+	return 'SELECT name, address_line1, city FROM tabAddress \
+		WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND \
+		%(key)s LIKE "%s" ORDER BY name ASC LIMIT 50';
+}
+
+cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) {
+	return 'SELECT name, CONCAT(first_name," ",ifnull(last_name,"")) As FullName, \
+		department, designation FROM tabContact WHERE customer = "'+ doc.customer + 
+		'" AND docstatus != 2 AND %(key)s LIKE "%s" ORDER BY name ASC LIMIT 50';
+}
 
 // *********************** QUANTITY ***************************
 cur_frm.cscript.qty = function(doc, cdt, cdn) { cur_frm.cscript.recalc(doc, 1); }
diff --git a/selling/doctype/sales_order/sales_order.js b/selling/doctype/sales_order/sales_order.js
index a3b71b4..f8d9fde 100644
--- a/selling/doctype/sales_order/sales_order.js
+++ b/selling/doctype/sales_order/sales_order.js
@@ -36,7 +36,9 @@
 	// load default charges
 	
 	if(doc.__islocal && !doc.customer){
-		hide_field(['customer_address','contact_person','customer_name','address_display','contact_display','contact_mobile','contact_email','territory','customer_group']);
+			hide_field(['customer_address', 'contact_person', 'customer_name', 
+				'address_display', 'contact_display', 'contact_mobile', 
+				'contact_email', 'territory', 'customer_group']);
 	}
 }
 
@@ -59,9 +61,8 @@
 
 	if (!cur_frm.cscript.is_onload) cur_frm.cscript.hide_price_list_currency(doc, cdt, cdn); 
 	
-	if(doc.customer) $(cur_frm.fields_dict.contact_info.row.wrapper).toggle(true);
-	else $(cur_frm.fields_dict.contact_info.row.wrapper).toggle(false);
-
+	cur_frm.toggle_display("contact_info", doc.customer);
+	
 	if(doc.docstatus==1) {
 		if(doc.status != 'Stopped') {
 			cur_frm.add_custom_button('Send SMS', cur_frm.cscript.send_sms);
@@ -105,11 +106,11 @@
 
 //customer
 cur_frm.cscript.customer = function(doc,dt,dn) {
+	cur_frm.toggle_display("contact_info", doc.customer);
+	
 	var pl = doc.price_list_name;
 	var callback = function(r,rt) {
 		var callback2  = function(r, rt) {
-
-			if(doc.customer) unhide_field(['customer_address', 'contact_person', 'territory','customer_group']);
 			cur_frm.refresh();
 			
 			if(!onload && (pl != doc.price_list_name)) cur_frm.cscript.price_list_name(doc, dt, dn);
@@ -126,24 +127,6 @@
 	if(doc.customer) get_server_fields('get_customer_address', JSON.stringify({customer: doc.customer, address: doc.customer_address, contact: doc.contact_person}),'', doc, dt, dn, 1);
 }
 
-cur_frm.fields_dict.customer_address.on_new = function(dn) {
-	locals['Address'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
-	locals['Address'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
-}
-
-cur_frm.fields_dict.contact_person.on_new = function(dn) {
-	locals['Contact'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
-	locals['Contact'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
-}
-
-cur_frm.fields_dict['customer_address'].get_query = function(doc, cdt, cdn) {
-	return 'SELECT name,address_line1,city FROM tabAddress WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
-}
-
-cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) {
-	return 'SELECT name,CONCAT(first_name," ",ifnull(last_name,"")) As FullName,department,designation FROM tabContact WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
-}
-
 cur_frm.cscript.pull_quotation_details = function(doc,dt,dn) {
 	var callback = function(r,rt){
 		var doc = locals[cur_frm.doctype][cur_frm.docname];					
diff --git a/stock/doctype/delivery_note/delivery_note.js b/stock/doctype/delivery_note/delivery_note.js
index a80966a..21e6dc0 100644
--- a/stock/doctype/delivery_note/delivery_note.js
+++ b/stock/doctype/delivery_note/delivery_note.js
@@ -69,8 +69,7 @@
 		cur_frm.add_custom_button('Make Packing Slip', cur_frm.cscript['Make Packing Slip']);
 	}
 	
-	if(doc.customer) $(cur_frm.fields_dict.contact_info.row.wrapper).toggle(true);
-	else $(cur_frm.fields_dict.contact_info.row.wrapper).toggle(false);
+	cur_frm.toggle_display("contact_info", doc.customer);
 	
 	set_print_hide(doc, cdt, cdn);
 }
@@ -78,38 +77,25 @@
 
 //customer
 cur_frm.cscript.customer = function(doc,dt,dn,onload) {	
+	cur_frm.toggle_display("contact_info", doc.customer);
+	
 	var pl = doc.price_list_name;
 	var callback = function(r,rt) {
 			var doc = locals[cur_frm.doctype][cur_frm.docname];
-			if(doc.customer) unhide_field(['customer_address','contact_person','territory','customer_group']);
+			if(doc.customer)
+			 	unhide_field(['customer_address','contact_person','territory','customer_group']);
 			cur_frm.refresh();
 			if(!onload && (pl != doc.price_list_name)) cur_frm.cscript.price_list_name(doc, dt, dn); 
 	} 
 	var args = onload ? 'onload':''
-	if(doc.customer) $c_obj(make_doclist(doc.doctype, doc.name), 'get_default_customer_shipping_address', args, callback);
+	if(doc.customer) $c_obj(make_doclist(doc.doctype, doc.name),
+	 	'get_default_customer_shipping_address', args, callback);
 }
 
 cur_frm.cscript.customer_address = cur_frm.cscript.contact_person = function(doc,dt,dn) {		
 	if(doc.customer) get_server_fields('get_customer_address', JSON.stringify({customer: doc.customer, address: doc.customer_address, contact: doc.contact_person}),'', doc, dt, dn, 1);
 }
 
-cur_frm.fields_dict.customer_address.on_new = function(dn) {
-	locals['Address'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
-	locals['Address'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
-}
-
-cur_frm.fields_dict.contact_person.on_new = function(dn) {
-	locals['Contact'][dn].customer = locals[cur_frm.doctype][cur_frm.docname].customer;
-	locals['Contact'][dn].customer_name = locals[cur_frm.doctype][cur_frm.docname].customer_name;
-}
-
-cur_frm.fields_dict['customer_address'].get_query = function(doc, cdt, cdn) {
-	return 'SELECT name,address_line1,city FROM tabAddress WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
-}
-
-cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) {
-	return 'SELECT name,CONCAT(first_name," ",ifnull(last_name,"")) As FullName,department,designation FROM tabContact WHERE customer = "'+ doc.customer +'" AND docstatus != 2 AND name LIKE "%s" ORDER BY name ASC LIMIT 50';
-}
 
 cur_frm.cscript.get_items = function(doc,dt,dn) {
 	var callback = function(r,rt){
@@ -117,9 +103,12 @@
 		if(r.message){							
 			doc.sales_order_no = r.message;			
 			if(doc.sales_order_no) {					
-					unhide_field(['customer_address','contact_person','territory','customer_group']);														
+				unhide_field(['customer_address','contact_person','territory','customer_group']);														
 			}			
-			refresh_many(['delivery_note_details','customer','customer_address','contact_person','customer_name','address_display','contact_display','contact_mobile','contact_email','territory','customer_group']);
+			
+			refresh_many(['delivery_note_details', 'customer', 'customer_address',
+			 	'contact_person', 'customer_name', 'address_display', 'contact_display',
+			 	'contact_mobile', 'contact_email', 'territory', 'customer_group']);
 		}
 	} 
  $c_obj(make_doclist(doc.doctype, doc.name),'pull_sales_order_details','',callback);