added automatic lead creation for sales email id
diff --git a/home/page/desktop/desktop.js b/home/page/desktop/desktop.js
index 77e0a3d..37011bb 100644
--- a/home/page/desktop/desktop.js
+++ b/home/page/desktop/desktop.js
@@ -82,6 +82,7 @@
 	add_circle('module-icon-calendar', 'todays_events', 'Todays Events');
 	add_circle('module-icon-projects-home', 'open_tasks', 'Open Tasks');
 	add_circle('module-icon-questions', 'unanswered_questions', 'Unanswered Questions');
+	add_circle('module-icon-selling-home', 'open_leads', 'Open Leads');
 
 	erpnext.update_messages();
 
diff --git a/hr/doctype/job_applicant/get_job_applications.py b/hr/doctype/job_applicant/get_job_applications.py
index 7509380..f2b776c 100644
--- a/hr/doctype/job_applicant/get_job_applications.py
+++ b/hr/doctype/job_applicant/get_job_applications.py
@@ -28,16 +28,12 @@
 		return webnotes.conn.sql("select user from tabSessions where \
 			time_to_sec(timediff(now(), lastupdate)) < 1800")
 	
-	def get_existing_application(self, email_id):
-		name = webnotes.conn.sql("""select name from `tabJob Applicant` where
-			email_id = %s""", email_id)
-		return name and name[0][0] or None
-	
 	def process_message(self, mail):
 		if mail.from_email == self.settings.email_id:
 			return
 			
-		name = self.get_existing_application(mail.from_email)
+		name = webnotes.conn.get_value("Job Applicant", {"email_id": mail.from_email}, 
+			"name")
 		if name:
 			applicant = webnotes.model_wrapper("Job Applicant", name)
 			if applicant.doc.status!="Rejected":
diff --git a/public/js/startup.js b/public/js/startup.js
index 817175b..e2413f1 100644
--- a/public/js/startup.js
+++ b/public/js/startup.js
@@ -108,6 +108,7 @@
 				show_in_circle('todays_events', r.message.todays_events);
 				show_in_circle('open_tasks', r.message.open_tasks);
 				show_in_circle('unanswered_questions', r.message.unanswered_questions);
+				show_in_circle('open_leads', r.message.open_leads);
 
 			} else {
 				clearInterval(wn.updates.id);
diff --git a/public/js/website_utils.js b/public/js/website_utils.js
index ff480ed..6f90434 100644
--- a/public/js/website_utils.js
+++ b/public/js/website_utils.js
@@ -11,7 +11,7 @@
 		method: "POST",
 		url: "server.py",
 		data: {
-			cmd: "website.send_message",
+			cmd: "website.helpers.contact.send_message",
 			subject: opts.subject,
 			sender: opts.sender,
 			status: opts.status,
@@ -34,6 +34,8 @@
 	if(id.toLowerCase().search("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")==-1) 
 		return 0; else return 1; }
 
+var validate_email = valid_email;
+
 function get_url_arg(name) {
 	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
 	var regexS = "[\\?&]"+name+"=([^&#]*)";
diff --git a/selling/doctype/lead/get_leads.py b/selling/doctype/lead/get_leads.py
new file mode 100644
index 0000000..6127457
--- /dev/null
+++ b/selling/doctype/lead/get_leads.py
@@ -0,0 +1,57 @@
+# 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/>.
+
+from __future__ import unicode_literals
+import webnotes
+from webnotes.utils import cstr, cint
+from webnotes.utils.email_lib.receive import POP3Mailbox
+from core.doctype.communication.communication import make
+
+class SalesMailbox(POP3Mailbox):	
+	def setup(self):
+		self.settings = webnotes.doc("Sales Email Settings", "Sales Email Settings")
+	
+	def check_mails(self):
+		return webnotes.conn.sql("select user from tabSessions where \
+			time_to_sec(timediff(now(), lastupdate)) < 1800")
+	
+	def process_message(self, mail):
+		if mail.from_email == self.settings.email_id:
+			return
+			
+		name = webnotes.conn.get_value("Lead", {"email_id": mail.from_email}, "name")
+		if name:
+			lead = webnotes.model_wrapper("Lead", name)
+			lead.doc.status = "Open"
+			lead.doc.save()
+		else:
+			lead = webnotes.model_wrapper({
+				"doctype":"Lead",
+				"lead_name": mail.from_real_name or mail.from_email,
+				"email_id": mail.from_email,
+				"status": "Open",
+				"source": "Email"
+			})
+			lead.insert()
+		
+		mail.save_attachments_in_doc(lead.doc)
+				
+		make(content=mail.content, sender=mail.from_email, 
+			doctype="Lead", name=lead.doc.name, lead=lead.doc.name)
+
+def get_leads():
+	if cint(webnotes.conn.get_value('Sales Email Settings', None, 'extract_emails')):
+		SalesMailbox()
\ No newline at end of file
diff --git a/selling/doctype/lead/lead.py b/selling/doctype/lead/lead.py
index 63f9bd0..13d1714 100644
--- a/selling/doctype/lead/lead.py
+++ b/selling/doctype/lead/lead.py
@@ -77,8 +77,11 @@
 		event_user.person = self.doc.contact_by
 		event_user.save()
 
+	def on_communication_sent(self, comm):
+		webnotes.conn.set(self.doc, 'status', 'Replied')
+
 	def on_trash(self):
-		webnotes.conn.sql("""update tabCommunication set lead='' where lead=%s""",
+		webnotes.conn.sql("""delete from tabCommunication where lead=%s""",
 			self.doc.name)
 		webnotes.conn.sql("""update `tabSupport Ticket` set lead='' where lead=%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 6ffe3c4..a4f02060 100644
--- a/selling/doctype/lead/lead.txt
+++ b/selling/doctype/lead/lead.txt
@@ -2,9 +2,9 @@
  {
   "owner": "Administrator", 
   "docstatus": 0, 
-  "creation": "2012-11-02 17:16:46", 
+  "creation": "2013-01-10 16:34:18", 
   "modified_by": "Administrator", 
-  "modified": "2012-11-27 18:27:47"
+  "modified": "2013-01-16 10:51:58"
  }, 
  {
   "autoname": "naming_series:", 
@@ -29,6 +29,7 @@
   "doctype": "DocPerm", 
   "read": 1, 
   "parenttype": "DocType", 
+  "report": 1, 
   "parentfield": "permissions"
  }, 
  {
@@ -39,19 +40,17 @@
   "description": "To manage multiple series please go to Setup > Manage Series", 
   "no_copy": 1, 
   "oldfieldtype": "Select", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Naming Series", 
   "oldfieldname": "naming_series", 
-  "permlevel": 0, 
+  "options": "LEAD\nLEAD/10-11/\nLEAD/MUMBAI/", 
   "fieldname": "naming_series", 
   "fieldtype": "Select", 
   "reqd": 0, 
-  "options": "LEAD\nLEAD/10-11/\nLEAD/MUMBAI/"
+  "permlevel": 0
  }, 
  {
   "oldfieldtype": "Data", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Contact Name", 
   "oldfieldname": "lead_name", 
@@ -64,7 +63,6 @@
  }, 
  {
   "oldfieldtype": "Data", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Email Id", 
   "oldfieldname": "email_id", 
@@ -84,29 +82,25 @@
   "permlevel": 0, 
   "no_copy": 1, 
   "oldfieldtype": "Select", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Status", 
   "oldfieldname": "status", 
   "default": "Open", 
-  "trigger": "Client", 
   "fieldname": "status", 
   "fieldtype": "Select", 
   "search_index": 1, 
   "reqd": 1, 
-  "options": "\nOpen\nAttempted to Contact\nContact in Future\nContacted\nInterested\nNot interested\nLead Lost\nConverted", 
+  "options": "\nOpen\nReplied\nAttempted to Contact\nContact in Future\nContacted\nInterested\nNot interested\nLead Lost\nConverted", 
   "in_filter": 1
  }, 
  {
   "description": "Source of the lead. If via a campaign, select \"Campaign\"", 
   "no_copy": 1, 
   "oldfieldtype": "Select", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Source", 
   "oldfieldname": "source", 
   "permlevel": 0, 
-  "trigger": "Client", 
   "fieldname": "source", 
   "fieldtype": "Select", 
   "search_index": 0, 
@@ -121,9 +115,8 @@
   "permlevel": 0
  }, 
  {
-  "allow_on_submit": 0, 
   "oldfieldtype": "Table", 
-  "colour": "White:FFF", 
+  "allow_on_submit": 0, 
   "doctype": "DocField", 
   "label": "Communication HTML", 
   "oldfieldname": "follow_up", 
@@ -141,45 +134,41 @@
  {
   "description": "Name of organization from where lead has come", 
   "oldfieldtype": "Data", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Company Name", 
   "oldfieldname": "company_name", 
-  "trigger": "Client", 
   "fieldname": "company_name", 
   "fieldtype": "Data", 
   "search_index": 0, 
   "reqd": 0, 
-  "in_filter": 1, 
-  "permlevel": 0
+  "permlevel": 0, 
+  "in_filter": 1
  }, 
  {
   "description": "Source of th", 
   "oldfieldtype": "Link", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "From Customer", 
   "oldfieldname": "customer", 
-  "permlevel": 0, 
+  "options": "Customer", 
   "fieldname": "customer", 
   "fieldtype": "Link", 
   "depends_on": "eval:doc.source == 'Existing Customer'", 
   "hidden": 0, 
-  "options": "Customer"
+  "permlevel": 0
  }, 
  {
   "description": "Enter campaign name if the source of lead is campaign.", 
   "oldfieldtype": "Link", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Campaign Name", 
   "oldfieldname": "campaign_name", 
-  "permlevel": 0, 
+  "options": "Campaign", 
   "fieldname": "campaign_name", 
   "fieldtype": "Link", 
   "depends_on": "eval:doc.source == 'Campaign'", 
   "hidden": 0, 
-  "options": "Campaign"
+  "permlevel": 0
  }, 
  {
   "doctype": "DocField", 
@@ -190,7 +179,6 @@
  }, 
  {
   "oldfieldtype": "Select", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Lead Type", 
   "oldfieldname": "type", 
@@ -202,7 +190,6 @@
  }, 
  {
   "oldfieldtype": "Text", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Remark", 
   "oldfieldname": "remark", 
@@ -220,7 +207,6 @@
  }, 
  {
   "oldfieldtype": "Data", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Phone", 
   "oldfieldname": "contact_no", 
@@ -260,7 +246,6 @@
   "print_hide": 1, 
   "description": "<a href=\"javascript:cur_frm.cscript.TerritoryHelp();\">To manage Territory, click here</a>", 
   "oldfieldtype": "Link", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Territory", 
   "oldfieldname": "territory", 
@@ -313,17 +298,15 @@
   "doctype": "DocField", 
   "label": "Country", 
   "oldfieldname": "country", 
-  "trigger": "Client", 
+  "options": "link:Country", 
   "fieldname": "country", 
   "fieldtype": "Select", 
   "reqd": 0, 
-  "options": "link:Country", 
   "permlevel": 0
  }, 
  {
   "print_hide": 1, 
   "oldfieldtype": "Select", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "State", 
   "oldfieldname": "state", 
@@ -344,7 +327,6 @@
  }, 
  {
   "oldfieldtype": "Section Break", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "More Info", 
   "fieldname": "more_info", 
@@ -436,7 +418,6 @@
   "description": "Your sales person who will contact the lead in future", 
   "permlevel": 0, 
   "oldfieldtype": "Link", 
-  "colour": "White:FFF", 
   "allow_on_submit": 0, 
   "doctype": "DocField", 
   "label": "Next Contact By", 
@@ -453,7 +434,6 @@
   "description": "Your sales person will get a reminder on this date to contact the lead", 
   "no_copy": 1, 
   "oldfieldtype": "Date", 
-  "colour": "White:FFF", 
   "allow_on_submit": 0, 
   "doctype": "DocField", 
   "label": "Next Contact Date", 
@@ -470,7 +450,6 @@
   "description": "Date on which the lead was last contacted", 
   "no_copy": 1, 
   "oldfieldtype": "Date", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Last Contact Date", 
   "oldfieldname": "last_contact_date", 
@@ -481,7 +460,6 @@
  }, 
  {
   "oldfieldtype": "Link", 
-  "colour": "White:FFF", 
   "doctype": "DocField", 
   "label": "Company", 
   "oldfieldname": "company", 
@@ -563,21 +541,5 @@
   "role": "Sales User", 
   "cancel": 0, 
   "permlevel": 1
- }, 
- {
-  "create": 1, 
-  "doctype": "DocPerm", 
-  "write": 1, 
-  "role": "System Manager", 
-  "cancel": 0, 
-  "permlevel": 0
- }, 
- {
-  "create": 1, 
-  "doctype": "DocPerm", 
-  "write": 1, 
-  "role": "Guest", 
-  "cancel": 0, 
-  "permlevel": 0
  }
 ]
\ No newline at end of file
diff --git a/selling/doctype/lead/lead_list.js b/selling/doctype/lead/lead_list.js
index cfbfc36..3b2f53a 100644
--- a/selling/doctype/lead/lead_list.js
+++ b/selling/doctype/lead/lead_list.js
@@ -16,7 +16,10 @@
 		if(data.status=='Interested') {
 			data.label_type = 'success'
 		}
-		else if(['Open', 'Attempted to Contact', 'Contacted', 'Contact in Future'].indexOf(data.status)!=-1) {
+		if(data.status=="Open") {
+			data.label_type = "important"
+		}
+		else if(['Attempted to Contact', 'Contacted', 'Contact in Future'].indexOf(data.status)!=-1) {
 			data.label_type = 'info'
 		}
 		data.status_html = repl('<span class="label label-%(label_type)s">%(status)s</span>', data);
diff --git a/setup/doctype/sales_email_settings/__init__.py b/setup/doctype/sales_email_settings/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/setup/doctype/sales_email_settings/__init__.py
diff --git a/setup/doctype/sales_email_settings/sales_applicant_list.js b/setup/doctype/sales_email_settings/sales_applicant_list.js
new file mode 100644
index 0000000..0a75b89
--- /dev/null
+++ b/setup/doctype/sales_email_settings/sales_applicant_list.js
@@ -0,0 +1,12 @@
+// For license information, please see license.txt
+
+cur_frm.cscript = {
+	refresh: function(doc) {
+		cur_frm.set_intro("");
+		if(doc.extract_emails) {
+			cur_frm.set_intro(wn._("Active: Will extract emails from ") + doc.email_id);
+		} else {
+			cur_frm.set_intro(wn._("Not Active"));
+		}
+	}
+}
\ No newline at end of file
diff --git a/setup/doctype/sales_email_settings/sales_email_settings.py b/setup/doctype/sales_email_settings/sales_email_settings.py
new file mode 100644
index 0000000..b09cefd
--- /dev/null
+++ b/setup/doctype/sales_email_settings/sales_email_settings.py
@@ -0,0 +1,17 @@
+# For license information, please see license.txt
+
+from __future__ import unicode_literals
+import webnotes
+from webnotes import _
+from webnotes.utils import cint
+
+class DocType:
+	def __init__(self, d, dl):
+		self.doc, self.doclist = d, dl
+		
+	def validate(self):
+		if cint(self.doc.extract_emails) and not (self.doc.email_id and self.doc.host and \
+			self.doc.username and self.doc.password):
+			
+			webnotes.msgprint(_("""Host, Email and Password required if emails are to be pulled"""),
+				raise_exception=True)
\ No newline at end of file
diff --git a/setup/doctype/sales_email_settings/sales_email_settings.txt b/setup/doctype/sales_email_settings/sales_email_settings.txt
new file mode 100644
index 0000000..d8042e9
--- /dev/null
+++ b/setup/doctype/sales_email_settings/sales_email_settings.txt
@@ -0,0 +1,89 @@
+[
+ {
+  "owner": "Administrator", 
+  "docstatus": 0, 
+  "creation": "2013-01-16 10:25:26", 
+  "modified_by": "Administrator", 
+  "modified": "2013-01-16 10:25:26"
+ }, 
+ {
+  "issingle": 1, 
+  "description": "Email settings to extract Leads from sales email id e.g. \"sales@example.com\"", 
+  "doctype": "DocType", 
+  "module": "Setup", 
+  "name": "__common__"
+ }, 
+ {
+  "name": "__common__", 
+  "parent": "Sales Email Settings", 
+  "doctype": "DocField", 
+  "parenttype": "DocType", 
+  "permlevel": 0, 
+  "parentfield": "fields"
+ }, 
+ {
+  "parent": "Sales Email Settings", 
+  "read": 1, 
+  "name": "__common__", 
+  "create": 1, 
+  "doctype": "DocPerm", 
+  "write": 1, 
+  "parenttype": "DocType", 
+  "role": "System Manager", 
+  "permlevel": 0, 
+  "parentfield": "permissions"
+ }, 
+ {
+  "name": "Sales Email Settings", 
+  "doctype": "DocType"
+ }, 
+ {
+  "description": "Email settings to extract Leads from sales email id e.g. \"sales@example.com\"", 
+  "doctype": "DocField", 
+  "label": "POP3 Mail Settings", 
+  "fieldname": "pop3_mail_settings", 
+  "fieldtype": "Section Break"
+ }, 
+ {
+  "description": "Check to activate", 
+  "doctype": "DocField", 
+  "label": "Extract Emails", 
+  "fieldname": "extract_emails", 
+  "fieldtype": "Check"
+ }, 
+ {
+  "description": "Email Id where a job applicant will email e.g. \"jobs@example.com\"", 
+  "doctype": "DocField", 
+  "label": "Email Id", 
+  "fieldname": "email_id", 
+  "fieldtype": "Data"
+ }, 
+ {
+  "description": "POP3 server e.g. (pop.gmail.com)", 
+  "doctype": "DocField", 
+  "label": "Host", 
+  "fieldname": "host", 
+  "fieldtype": "Data"
+ }, 
+ {
+  "doctype": "DocField", 
+  "label": "Use SSL", 
+  "fieldname": "use_ssl", 
+  "fieldtype": "Check"
+ }, 
+ {
+  "doctype": "DocField", 
+  "label": "Username", 
+  "fieldname": "username", 
+  "fieldtype": "Data"
+ }, 
+ {
+  "doctype": "DocField", 
+  "label": "Password", 
+  "fieldname": "password", 
+  "fieldtype": "Password"
+ }, 
+ {
+  "doctype": "DocPerm"
+ }
+]
\ No newline at end of file
diff --git a/startup/open_count.py b/startup/open_count.py
index e0767c3..a5fcc77 100644
--- a/startup/open_count.py
+++ b/startup/open_count.py
@@ -17,6 +17,7 @@
 	"Purchase Invoice": {"docstatus":0},
 	"Leave Application": {"status":"Open"},
 	"Expense Claim": {"approval_status":"Draft"},
+	"Job Applicant": {"status":"Open"},
 	"Purchase Receipt": {"docstatus":0},
 	"Delivery Note": {"docstatus":0},
 	"Stock Entry": {"docstatus":0},
diff --git a/startup/schedule_handlers.py b/startup/schedule_handlers.py
index ab53b21..b1ce183 100644
--- a/startup/schedule_handlers.py
+++ b/startup/schedule_handlers.py
@@ -31,8 +31,10 @@
 
 	from hr.doctype.job_applicant.get_job_applications import get_job_applications
 	run_fn(get_job_applications)
-	
-	# bulk email
+
+	from selling.doctype.lead.get_leads import get_leads
+	run_fn(get_job_applications)
+
 	from webnotes.utils.email_lib.bulk import flush
 	run_fn(flush)
 	
diff --git a/startup/startup.py b/startup/startup.py
index 15ea491..c74c596 100644
--- a/startup/startup.py
+++ b/startup/startup.py
@@ -29,38 +29,39 @@
 
 def get_open_support_tickets():
 	"""Returns a count of open support tickets"""
-	from webnotes.utils import cint
 	open_support_tickets = webnotes.conn.sql("""\
 		SELECT COUNT(*) FROM `tabSupport Ticket`
 		WHERE status = 'Open'""")
-	return open_support_tickets and cint(open_support_tickets[0][0]) or 0
+	return open_support_tickets[0][0]
 
 def get_open_tasks():
 	"""Returns a count of open tasks"""
-	from webnotes.utils import cint
 	return webnotes.conn.sql("""\
 		SELECT COUNT(*) FROM `tabTask`
 		WHERE status = 'Open'""")[0][0]
 
 def get_things_todo():
 	"""Returns a count of incomplete todos"""
-	from webnotes.utils import cint
 	incomplete_todos = webnotes.conn.sql("""\
 		SELECT COUNT(*) FROM `tabToDo`
 		WHERE IFNULL(checked, 0) = 0
 		AND (owner = %s or assigned_by=%s)""", (webnotes.session.user, webnotes.session.user))
-	return incomplete_todos and cint(incomplete_todos[0][0]) or 0
+	return incomplete_todos[0][0]
 
 def get_todays_events():
 	"""Returns a count of todays events in calendar"""
-	from webnotes.utils import nowdate, cint
+	from webnotes.utils import nowdate
 	todays_events = webnotes.conn.sql("""\
 		SELECT COUNT(*) FROM `tabEvent`
 		WHERE owner = %s
 		AND event_type != 'Cancel'
 		AND event_date = %s""", (
-		webnotes.session.get('user'), nowdate()))
-	return todays_events and cint(todays_events[0][0]) or 0
+		webnotes.session.user, nowdate()))
+	return todays_events[0][0]
+
+def get_open_leads():
+	return webnotes.conn.sql("""select count(*) from tabLead 
+		where status='Open'""")[0][0]
 
 def get_unanswered_questions():
 	return len(filter(lambda d: d[0]==0,
@@ -75,5 +76,6 @@
 		'things_todo': get_things_todo(),
 		'todays_events': get_todays_events(),
 		'open_tasks': get_open_tasks(),
-		'unanswered_questions': get_unanswered_questions()
+		'unanswered_questions': get_unanswered_questions(),
+		'open_leads': get_open_leads()
 	}
diff --git a/support/doctype/support_ticket/support_ticket.js b/support/doctype/support_ticket/support_ticket.js
index bbaf95b..9abdff0 100644
--- a/support/doctype/support_ticket/support_ticket.js
+++ b/support/doctype/support_ticket/support_ticket.js
@@ -53,7 +53,7 @@
 		var sortfn = function (a, b) { return (b.creation > a.creation) ? 1 : -1; }
 		comm_list = comm_list.sort(sortfn);
 		
-		if(!comm_list.length || (comm_list[0].sender != doc.raised_by)) {
+		if(!comm_list.length || (comm_list[comm_list.length - 1].sender != doc.raised_by)) {
 			comm_list.push({
 				"sender": doc.raised_by,
 				"creation": doc.creation,
diff --git a/website/__init__.py b/website/__init__.py
index 909a936..aace68b 100644
--- a/website/__init__.py
+++ b/website/__init__.py
@@ -6,34 +6,6 @@
 
 import webnotes
 
-max_tickets_per_hour = 200
-
-@webnotes.whitelist(allow_guest=True)
-def send_message():
-	from webnotes.model.doc import Document
-	
-	d = Document('Support Ticket')
-	d.subject = webnotes.form_dict.get('subject', 'Website Query')
-	d.description = webnotes.form_dict.get('message')
-	d.raised_by = webnotes.form_dict.get('sender')
-	d.status = webnotes.form_dict.get("status") or "Open"
-
-	if not d.description:
-		webnotes.response["message"] = 'Please write something'
-		return
-		
-	if not d.raised_by:
-		webnotes.response["message"] = 'Email Id Required'
-		return
-	
-	# guest method, cap max writes per hour
-	if webnotes.conn.sql("""select count(*) from `tabSupport Ticket`
-		where TIMEDIFF(NOW(), modified) < '01:00:00'""")[0][0] > max_tickets_per_hour:
-		webnotes.response["message"] = "Sorry: we believe we have received an unreasonably high number of requests of this kind. Please try later"
-		return
-	
-	d.save()
-	webnotes.response["message"] = 'Thank You'
 	
 def get_site_address():
 	from webnotes.utils import get_request_site_address
diff --git a/website/helpers/contact.py b/website/helpers/contact.py
new file mode 100644
index 0000000..df4510f
--- /dev/null
+++ b/website/helpers/contact.py
@@ -0,0 +1,63 @@
+# 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/>.
+
+from __future__ import unicode_literals
+
+import webnotes
+from core.doctype.communication.communication import make
+
+max_communications_per_hour = 300
+
+@webnotes.whitelist(allow_guest=True)
+def send_message(subject="Website Query", message="", sender="", status="Open"):
+	if not message:
+		webnotes.response["message"] = 'Please write something'
+		return
+		
+	if not sender:
+		webnotes.response["message"] = 'Email Id Required'
+		return
+
+	# make lead / communication
+	
+	name = webnotes.conn.get_value("Lead", {"email_id": sender}, "name")
+	if name:
+		lead = webnotes.model_wrapper("Lead", name)
+		lead.doc.status = "Open"
+		lead.ignore_permissions = True
+		lead.save()
+	else:
+		lead = webnotes.model_wrapper({
+			"doctype":"Lead",
+			"lead_name": sender,
+			"email_id": sender,
+			"status": "Open",
+			"source": "Website"
+		})
+		lead.ignore_permissions = True
+		lead.insert()
+	
+	make(content=message, sender=sender, 
+		doctype="Lead", name=lead.doc.name, lead=lead.doc.name)
+
+	
+	# guest method, cap max writes per hour
+	if webnotes.conn.sql("""select count(*) from `tabCommunication`
+		where TIMEDIFF(NOW(), modified) < '01:00:00'""")[0][0] > max_communications_per_hour:
+		webnotes.response["message"] = "Sorry: we believe we have received an unreasonably high number of requests of this kind. Please try later"
+		return
+	
+	webnotes.response["message"] = 'Thank You'
\ No newline at end of file