separated communication view
diff --git a/public/js/communication.js b/public/js/communication.js
new file mode 100644
index 0000000..5e7a010
--- /dev/null
+++ b/public/js/communication.js
@@ -0,0 +1,75 @@
+// ERPNext - web based ERP (http://erpnext.com)
+// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
+// 
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+// 
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+erpnext.CommunicationView = Class.extend({
+	init: function(opts) {
+		this.comm_list = [];
+		$.extend(this, opts);
+		
+		this.list.sort(function(a, b) { return new Date(a.modified) > new Date(b.modified) 
+			? -1 : 1 })
+				
+		this.make();
+	},
+	make: function() {
+		var me = this;
+		this.make_body();
+		$.each(this.list, function(i, d) {
+			me.prepare(d);
+			me.make_line(d);
+		});
+		// show first
+		this.comm_list[0].find('.comm-content').toggle(true);
+	},
+	make_body: function() {
+		$(this.parent)
+			.html("")
+			.css({"margin":"10px 0px"});
+			
+		this.wrapper = $("<div><h4>Communication History</h4>\
+			<button class='btn btn-small'>Add Reply</button></p></div>")
+			.appendTo(this.parent);
+			
+		this.body = $("<table class='table table-bordered table-hover table-striped'>")
+			.appendTo(this.wrapper);
+	},
+	prepare: function(doc) {
+		//doc.when = comment_when(this.doc.modified);
+		doc.when = doc.modified;
+		if(doc.content.indexOf("<br>")== -1 && doc.content.indexOf("<p>")== -1) {
+			doc.content = doc.content.replace(/\n/g, "<br>");
+		}
+		doc.email_address = doc.email_address.replace(/</, "&lt;").replace(/>/, "&gt;");
+		doc.content = doc.content.split("=== In response to ===")[0];
+		doc.content = doc.content.split("-----Original Message-----")[0];
+	},
+	make_line: function(doc) {
+		var me = this;
+		var comm = $(repl('<tr><td title="Click to Expand / Collapse">\
+				<p><b>%(email_address)s on %(when)s</b></p>\
+				<div class="comm-content" style="border-top: 1px solid #ddd; padding: 10px; \
+					display: none;"></div>\
+			</td></tr>', doc))
+			.appendTo(this.body)
+			.css({"cursor":"pointer"})
+			.click(function() {
+				$(this).find(".comm-content").toggle();
+			});
+		
+		this.comm_list.push(comm);
+		comm.find(".comm-content").html(doc.content);
+	}
+})
diff --git a/selling/doctype/lead/lead.js b/selling/doctype/lead/lead.js
index 8c940bc..5bb4ce7 100644
--- a/selling/doctype/lead/lead.js
+++ b/selling/doctype/lead/lead.js
@@ -16,6 +16,8 @@
 
 // Module CRM
 
+wn.require("public/app/js/communication.js");
+
 wn.require('app/utilities/doctype/sms_control/sms_control.js');
 wn.require('app/support/doctype/communication/communication.js');
 
@@ -41,8 +43,6 @@
 	if(user=='Guest') doc.naming_series = 'WebLead';
 	
 	cur_frm.add_fetch('customer', 'customer_name', 'company_name');
-
-	cur_frm.cscript.make_communication_body();
 	
 	if(cur_frm.fields_dict.lead_owner.df.options.match(/^Profile/)) {
 		cur_frm.fields_dict.lead_owner.get_query = erpnext.utils.profile_query;
@@ -67,32 +67,22 @@
 }
 
 cur_frm.cscript.refresh = function(doc, cdt, cdn) {
-	// custom buttons
-	//---------------
 	cur_frm.cscript.refresh_custom_buttons(doc);
-	
 	erpnext.hide_naming_series();
-	
-	if (!doc.__islocal) cur_frm.cscript.render_communication_list(doc, cdt, cdn);
+
+	new erpnext.CommunicationView({
+		list: wn.model.get("Communication", {"lead": doc.name}),
+		parent: cur_frm.fields_dict.communication_html.wrapper
+	})
+		
 }
 
 
-// Client Side Triggers
-// ===========================================================
-// ************ Status ******************
+
 cur_frm.cscript.status = function(doc, cdt, cdn){
 	cur_frm.cscript.refresh(doc, cdt, cdn);
 }
 
-//Trigger in Item Table
-//===================================
-cur_frm.cscript.item_code=function(doc,cdt,cdn){
-	var d = locals[cdt][cdn];
-	if (d.item_code) { get_server_fields('get_item_detail',d.item_code,'lead_item_detail',doc,cdt,cdn,1);}
-}
-
-// Create New Customer
-// ===============================================================
 cur_frm.cscript['Create Customer'] = function(){
 	var doc = cur_frm.doc;
 	$c('runserverobj',args={ 'method':'check_status', 'docs':compress_doclist(make_doclist(doc.doctype, doc.name))},
diff --git a/selling/doctype/lead/lead.py b/selling/doctype/lead/lead.py
index 1b51e55..236d337 100644
--- a/selling/doctype/lead/lead.py
+++ b/selling/doctype/lead/lead.py
@@ -18,48 +18,28 @@
 from __future__ import unicode_literals
 import webnotes
 
-from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add
-from webnotes.model import db_exists
-from webnotes.model.doc import Document, addchild, getchildren, make_autoname
-from webnotes.model.wrapper import getlist, copy_doclist
-from webnotes.model.code import get_obj, get_server_obj, run_server_obj, updatedb, check_syntax
-from webnotes import session, form, msgprint, errprint
+from webnotes.utils import cint, cstr, flt, validate_email_add
+from webnotes.model.doc import Document, addchild
+from webnotes import session, msgprint
 
-set = webnotes.conn.set
 sql = webnotes.conn.sql
-get_value = webnotes.conn.get_value
-in_transaction = webnotes.conn.in_transaction
-convert_to_lists = webnotes.conn.convert_to_lists
 	
 # -----------------------------------------------------------------------------------------
 
+from utilities.transaction_base import TransactionBase
 
-class DocType:
+class DocType(TransactionBase):
 	def __init__(self, doc, doclist):
 		self.doc = doc
 		self.doclist = doclist
-	
-	#check status of lead
-	#------------------------
+
+	def onload(self):
+		self.add_communication_list()
+
 	def check_status(self):
 		chk = sql("select status from `tabLead` where name=%s", self.doc.name)
 		chk = chk and chk[0][0] or ''
 		return cstr(chk)
-
-
-	# Get item detail (will be removed later)
-	#=======================================
-	def get_item_detail(self,item_code):
-		it=sql("select item_name,brand,item_group,description,stock_uom from `tabItem` where name='%s'"%item_code)
-		if it:
-			ret = {
-			'item_name'	: it and it[0][0] or '',
-			'brand'			: it and it[0][1] or '',
-			'item_group' : it and it[0][2] or '',
-			'description': it and it[0][3] or '',
-			'uom' : it and it[0][4] or ''
-			}
-			return ret
 	
 	def validate(self):
 		if self.doc.status == 'Lead Lost' and not self.doc.order_lost_reason:
@@ -79,20 +59,6 @@
 		if self.doc.contact_date:
 			self.add_calendar_event()
 		
-		if not self.doc.naming_series:
-			if session['user'] == 'Guest':
-				import webnotes.model.doctype
-				docfield = webnotes.model.doctype.get('Lead')
-				series = [d.options for d in docfield if d.doctype == 'DocField' and d.fieldname == 'naming_series']
-				if series:
-					sr = series[0].split("\n")
-					set(self.doc, 'naming_series', sr[0])
-			else:
-				msgprint("Please specify naming series")
-				raise Exception
-
-	# Add to Calendar
-	# ===========================================================================
 	def add_calendar_event(self):
 		# delete any earlier event by this lead
 		sql("delete from tabEvent where ref_type='Lead' and ref_name=%s", self.doc.name)
@@ -113,3 +79,9 @@
 		event_user = addchild(ev, 'event_individuals', 'Event User')
 		event_user.person = self.doc.contact_by
 		event_user.save()
+
+	def on_trash(self):
+		webnotes.conn.sql("""update tabCommunication set lead='' where lead=%s""",
+			self.doc.name)
+		webnotes.conn.sql("""update `tabSupport Ticket` set contact='' where contact=%s""",
+			self.doc.name)
\ No newline at end of file
diff --git a/selling/doctype/lead/lead.txt b/selling/doctype/lead/lead.txt
index 08218b3..05292f8 100644
--- a/selling/doctype/lead/lead.txt
+++ b/selling/doctype/lead/lead.txt
@@ -4,7 +4,7 @@
   "docstatus": 0, 
   "creation": "2012-11-02 17:16:46", 
   "modified_by": "Administrator", 
-  "modified": "2012-11-26 11:12:27"
+  "modified": "2012-11-26 18:13:22"
  }, 
  {
   "autoname": "naming_series:", 
@@ -200,7 +200,6 @@
  }, 
  {
   "doctype": "DocField", 
-  "label": "Communication History", 
   "fieldname": "communication_history", 
   "fieldtype": "Section Break", 
   "permlevel": 0
diff --git a/support/doctype/support_ticket/support_ticket.js b/support/doctype/support_ticket/support_ticket.js
index 697d894..b740498 100644
--- a/support/doctype/support_ticket/support_ticket.js
+++ b/support/doctype/support_ticket/support_ticket.js
@@ -14,6 +14,10 @@
 // You should have received a copy of the GNU General Public License
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+wn.require("public/app/js/communication.js");
+
+cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
+
 $.extend(cur_frm.cscript, {
 	onload: function(doc, dt, dn) {
 		cur_frm.last_reload = new Date();
@@ -22,8 +26,6 @@
 				<p><a href="#Form/Email Settings/Email Settings">Email Settings</a><br>\
 				<span class="help">Integrate incoming support emails to Support Ticket</span></p>';
 		}
-		
-		if(!doc.customer) hide_field(['customer_name','address_display','contact_display','contact_mobile','contact_email']);		
 	},
 	
 	refresh: function(doc) {
@@ -53,19 +55,13 @@
 	
 	make_listing: function(doc) {
 		var wrapper = cur_frm.fields_dict['thread_html'].wrapper;
-		$(wrapper)
-			.html("")
-			.css({"margin":"10px 0px"});
 		
 		var comm_list = wn.model.get("Communication", {"support_ticket": doc.name})
 		comm_list.push({
 			"email_address": doc.raised_by,
 			"modified": doc.creation,
 			"content": doc.description});
-			
-		comm_list.sort(function(a, b) { return new Date(a.modified) > new Date(b.modified) 
-			? -1 : 1 })
-		
+					
 		new erpnext.CommunicationView({
 			list: comm_list,
 			parent: wrapper
@@ -89,8 +85,8 @@
 				cur_frm.refresh();
 			}
 		}
-		if(doc.customer) $c_obj(make_doclist(doc.doctype, doc.name), 'get_default_customer_address', '', callback);
-		if(doc.customer) unhide_field(['customer_name','address_display','contact_display','contact_mobile','contact_email']);
+		if(doc.customer) $c_obj(make_doclist(doc.doctype, doc.name), 
+			'get_default_customer_address', '', callback);
 	}, 
 	
 	'Close Ticket': function() {
@@ -116,54 +112,3 @@
 	
 })
 
-erpnext.CommunicationView = Class.extend({
-	init: function(opts) {
-		this.comm_list = [];
-		$.extend(this, opts);
-		this.make();
-	},
-	make: function() {
-		var me = this;
-		this.make_body();
-		$.each(this.list, function(i, d) {
-			me.prepare(d);
-			me.make_line(d);
-		});
-		// show first
-		this.comm_list[0].find('.comm-content').toggle(true);
-	},
-	make_body: function() {
-		this.wrapper = $("<div><h4>Communication History</h4></div>").appendTo(this.parent);
-		this.body = $("<table class='table table-bordered table-hover table-striped'>")
-			.appendTo(this.body);
-	},
-	prepare: function(doc) {
-		//doc.when = comment_when(this.doc.modified);
-		doc.when = doc.modified;
-		if(doc.content.indexOf("<br>")== -1 && doc.content.indexOf("<p>")== -1) {
-			doc.content = doc.content.replace(/\n/g, "<br>");
-		}
-		doc.email_address = doc.email_address.replace(/</, "&lt;").replace(/>/, "&gt;");
-		doc.content = doc.content.split("=== In response to ===")[0];
-		doc.content = doc.content.split("-----Original Message-----")[0];
-	},
-	make_line: function(doc) {
-		var me = this;
-		var comm = $(repl('<tr><td title="Click to Expand / Collapse">\
-				<p><b>%(email_address)s on %(when)s</b></p>\
-				<div class="comm-content"></div>\
-			</td></tr>', doc))
-			.appendTo(this.body)
-			.css({"cursor":"pointer"})
-			.click(function() {
-				$(this).find(".comm-content").toggle();
-			});
-		
-		this.comm_list.push(comm);
-		comm.find(".comm-content").html(doc.content);
-	}
-})
-
-
-cur_frm.fields_dict.allocated_to.get_query = erpnext.utils.profile_query;
-cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
\ No newline at end of file
diff --git a/support/doctype/support_ticket/support_ticket.py b/support/doctype/support_ticket/support_ticket.py
index f0ed650..e7f3077 100644
--- a/support/doctype/support_ticket/support_ticket.py
+++ b/support/doctype/support_ticket/support_ticket.py
@@ -28,18 +28,6 @@
 
 	def onload(self):
 		self.add_communication_list()
-		
-	def add_communication_list(self):
-		# remove communications if present
-		self.doclist = webnotes.doclist(self.doclist).get({"doctype": ["!=", "Communcation"]})
-		
-		comm_list = webnotes.conn.sql("""select * from tabCommunication 
-			where support_ticket=%s order by modified desc limit 20""", self.doc.name, as_dict=1)
-		
-		[d.update({"doctype":"Communication"}) for d in comm_list]
-		
-		self.doclist.extend(webnotes.doclist([webnotes.doc(fielddata=d) \
-			for d in comm_list]))
 			
 	def send_response(self):
 		"""
diff --git a/utilities/doctype/contact/contact.py b/utilities/doctype/contact/contact.py
index f384ebb..92adaf4 100644
--- a/utilities/doctype/contact/contact.py
+++ b/utilities/doctype/contact/contact.py
@@ -37,15 +37,9 @@
 		else:
 			self.doc.name = self.doc.first_name + (self.doc.last_name and ' ' + self.doc.last_name or '')
 
-#----------------------
-# Call to Validate
-#----------------------
 	def validate(self):
 		self.validate_primary_contact()
 
-#----------------------
-# Validate that there can only be one primary contact for particular customer, supplier
-#----------------------
 	def validate_primary_contact(self):
 		sql = webnotes.conn.sql
 		if self.doc.is_primary_contact == 1:
@@ -65,3 +59,9 @@
 			elif self.doc.sales_partner:
 				if not sql("select name from tabContact where is_primary_contact=1 and sales_partner = '%s'" % (self.doc.sales_partner)):
 					self.doc.is_primary_contact = 1
+
+	def on_trash(self):
+		webnotes.conn.sql("""update tabCommunication set contact='' where contact=%s""",
+			self.doc.name)
+		webnotes.conn.sql("""update `tabSupport Ticket` set contact='' where contact=%s""",
+			self.doc.name)
\ No newline at end of file
diff --git a/utilities/transaction_base.py b/utilities/transaction_base.py
index 1703b87..398a831 100644
--- a/utilities/transaction_base.py
+++ b/utilities/transaction_base.py
@@ -241,3 +241,18 @@
 	def get_formatted_message(self, args):
 		""" get formatted message for auto notification"""
 		return get_obj('Notification Control').get_formatted_message(args)
+
+	def add_communication_list(self):
+		# remove communications if present
+		self.doclist = webnotes.doclist(self.doclist).get({
+			"doctype": ["!=", "Communcation"]})
+		
+		comm_list = webnotes.conn.sql("""select * from tabCommunication 
+			where %s=%s order by modified desc limit 20""" \
+			% (self.doc.doctype.replace(" ", "_").lower(), "%s"),
+			self.doc.name, as_dict=1)
+		
+		[d.update({"doctype":"Communication"}) for d in comm_list]
+		
+		self.doclist.extend(webnotes.doclist([webnotes.doc(fielddata=d) \
+			for d in comm_list]))
\ No newline at end of file