Merge branch '1209' of github.com:webnotes/erpnext into 1209
diff --git a/accounts/page/accounts_home/accounts_home.html b/accounts/page/accounts_home/accounts_home.html
index 7fb2c32..5950173 100644
--- a/accounts/page/accounts_home/accounts_home.html
+++ b/accounts/page/accounts_home/accounts_home.html
@@ -69,12 +69,12 @@
 							href="#!Form/Sales and Purchase Return Tool/Sales and Purchase Return Tool">Sales or Purchase Returns</a>
 					</div>
 					<div class="section-item">
-						<a class="section-link" 
+						<a class="section-link" data-role="Analytics"
 							title = "Analyze Sales and Purchase trends and slice them based on item, customer, groups etc"
 							href="#!Report/Profile/Trend Analyzer">Trend Analyzer</a>
 					</div>
 					<div class="section-item">
-						<a class="section-link" 
+						<a class="section-link" data-role="Accounts Manager"
 							title = "Check your Balance Sheet and Profit & Loss Statement"
 							href="#!Financial Statements">Financial Statements</a>
 					</div>
diff --git a/selling/doctype/customer/customer.txt b/selling/doctype/customer/customer.txt
index f95121f..489523f 100644
--- a/selling/doctype/customer/customer.txt
+++ b/selling/doctype/customer/customer.txt
@@ -3,9 +3,9 @@
 
 	# These values are common in all dictionaries
 	{
-		u'creation': '2012-07-18 20:34:41',
+		u'creation': '2012-09-21 09:56:34',
 		u'docstatus': 0,
-		u'modified': '2012-09-17 11:31:55',
+		u'modified': '2012-09-28 12:31:42',
 		u'modified_by': u'Administrator',
 		u'owner': u'Administrator'
 	},
@@ -454,23 +454,13 @@
 	# DocPerm
 	{
 		'amend': 0,
-		'cancel': 1,
-		'create': 1,
+		'cancel': 0,
+		'create': 0,
 		u'doctype': u'DocPerm',
-		'permlevel': 0,
-		'role': u'Sales Master Manager',
+		'permlevel': 1,
+		'role': u'Sales Manager',
 		'submit': 0,
-		'write': 1
-	},
-
-	# DocPerm
-	{
-		'cancel': 1,
-		'create': 1,
-		u'doctype': u'DocPerm',
-		'permlevel': 0,
-		'role': u'Accounts Manager',
-		'write': 1
+		'write': 0
 	},
 
 	# DocPerm
@@ -491,7 +481,7 @@
 		'cancel': 0,
 		'create': 0,
 		u'doctype': u'DocPerm',
-		'permlevel': 0,
+		'permlevel': 1,
 		'role': u'Sales User',
 		'submit': 0,
 		'write': 0
@@ -503,14 +493,40 @@
 		'cancel': 0,
 		'create': 0,
 		u'doctype': u'DocPerm',
-		'permlevel': 1,
-		'role': u'All',
+		'permlevel': 0,
+		'role': u'Sales User',
 		'submit': 0,
 		'write': 0
 	},
 
 	# DocPerm
 	{
+		'amend': 0,
+		'cancel': 1,
+		'create': 1,
+		u'doctype': u'DocPerm',
+		'permlevel': 0,
+		'role': u'Sales Master Manager',
+		'submit': 0,
+		'write': 1
+	},
+
+	# DocPerm
+	{
+		'amend': 0,
+		'cancel': 0,
+		'create': 0,
+		u'doctype': u'DocPerm',
+		'permlevel': 1,
+		'role': u'Sales Master Manager',
+		'submit': 0,
+		'write': 0
+	},
+
+	# DocPerm
+	{
+		'cancel': 0,
+		'create': 0,
 		u'doctype': u'DocPerm',
 		'permlevel': 2,
 		'role': u'Accounts Manager',
@@ -519,6 +535,8 @@
 
 	# DocPerm
 	{
+		'cancel': 0,
+		'create': 0,
 		u'doctype': u'DocPerm',
 		'permlevel': 2,
 		'role': u'System Manager',
@@ -527,8 +545,31 @@
 
 	# DocPerm
 	{
+		'cancel': 0,
+		'create': 0,
 		u'doctype': u'DocPerm',
 		'permlevel': 2,
-		'role': u'All'
+		'role': u'All',
+		'write': 0
+	},
+
+	# DocPerm
+	{
+		'cancel': 1,
+		'create': 1,
+		u'doctype': u'DocPerm',
+		'permlevel': 0,
+		'role': u'Accounts Manager',
+		'write': 1
+	},
+
+	# DocPerm
+	{
+		'cancel': 0,
+		'create': 0,
+		u'doctype': u'DocPerm',
+		'permlevel': 1,
+		'role': u'All',
+		'write': 0
 	}
 ]
\ No newline at end of file
diff --git a/selling/doctype/lead/lead.js b/selling/doctype/lead/lead.js
index 3f45603..523ddd2 100644
--- a/selling/doctype/lead/lead.js
+++ b/selling/doctype/lead/lead.js
@@ -45,7 +45,7 @@
 	cur_frm.cscript.make_communication_body();
 }
 
-cur_frm.cscript.refresh_custom_buttons = function() {
+cur_frm.cscript.refresh_custom_buttons = function(doc) {
 	cur_frm.clear_custom_buttons();
 	if(!doc.__islocal && !in_list(['Converted', 'Lead Lost'], doc.status)) {
 		if (doc.source != 'Existing Customer') {
@@ -61,7 +61,7 @@
 cur_frm.cscript.refresh = function(doc, cdt, cdn) {
 	// custom buttons
 	//---------------
-	cur_frm.cscript.refresh_custom_buttons();
+	cur_frm.cscript.refresh_custom_buttons(doc);
 	
 	erpnext.hide_naming_series();
 	
diff --git a/selling/search_criteria/customer_address_contact/customer_address_contact.sql b/selling/search_criteria/customer_address_contact/customer_address_contact.sql
index 3752ebc..b30a6f9 100644
--- a/selling/search_criteria/customer_address_contact/customer_address_contact.sql
+++ b/selling/search_criteria/customer_address_contact/customer_address_contact.sql
@@ -1,4 +1,4 @@
-select
+SELECT
     `tabCustomer`.name,
     `tabCustomer`.customer_name,
     `tabAddress`.address_line1,
@@ -12,7 +12,7 @@
     `tabContact`.phone,
     `tabContact`.mobile_no,
     `tabContact`.email_id
-from
+FROM
     `tabCustomer`
     left join `tabAddress` on (
         `tabAddress`.customer=`tabCustomer`.name and
@@ -22,5 +22,7 @@
         `tabContact`.customer=`tabCustomer`.name and 
         ifnull(`tabContact`.is_primary_contact, 0)=1
     )
-order by
+WHERE
+	`tabCustomer`.docstatus<2
+ORDER BY
     `tabCustomer`.customer_name asc
\ No newline at end of file
diff --git a/setup/doctype/global_defaults/global_defaults.js b/setup/doctype/global_defaults/global_defaults.js
index 631fd0b..e8a174b 100644
--- a/setup/doctype/global_defaults/global_defaults.js
+++ b/setup/doctype/global_defaults/global_defaults.js
@@ -16,7 +16,19 @@
 
 // Validate
 cur_frm.cscript.validate = function(doc, cdt, cdn) {
-  $c_obj(make_doclist(cdt, cdn), 'get_defaults', '', function(r, rt){
-    sys_defaults = r.message;
-  });
-}
\ No newline at end of file
+	$c_obj(make_doclist(cdt, cdn), 'get_defaults', '', function(r, rt){
+		sys_defaults = r.message;
+	});
+}
+
+cur_frm.fields_dict['default_territory'].get_query = function(doc,cdt,cdn) {
+	return 'SELECT `tabTerritory`.`name`,`tabTerritory`.`parent_territory` FROM `tabTerritory` WHERE `tabTerritory`.`is_group` = "No" AND `tabTerritory`.`docstatus`!= 2 AND `tabTerritory`.%(key)s LIKE "%s"  ORDER BY  `tabTerritory`.`name` ASC LIMIT 50';
+}
+
+cur_frm.fields_dict['default_customer_group'].get_query = function(doc,cdt,cdn) {
+	return 'SELECT `tabCustomer Group`.`name`, `tabCustomer Group`.`parent_customer_group` FROM `tabCustomer Group` WHERE `tabCustomer Group`.`is_group` = "No" AND `tabCustomer Group`.`docstatus`!= 2 AND `tabCustomer Group`.%(key)s LIKE "%s" ORDER BY	`tabCustomer Group`.`name` ASC LIMIT 50';
+}
+
+cur_frm.fields_dict['default_item_group'].get_query = function(doc,cdt,cdn) {
+	return 'SELECT `tabItem Group`.`name`,`tabItem Group`.`parent_item_group` FROM `tabItem Group` WHERE `tabItem Group`.is_group="No" AND `tabItem Group`.docstatus != 2 AND `tabItem Group`.%(key)s LIKE "%s" LIMIT 50'
+}
diff --git a/startup/js/modules.js b/startup/js/modules.js
index 71d012e..914df81 100644
--- a/startup/js/modules.js
+++ b/startup/js/modules.js
@@ -28,13 +28,19 @@
 // not have read permissions
 
 erpnext.module_page.hide_links = function(wrapper) {
+	function replace_link(link) {
+		var txt = $(link).text();
+		$(link).parent().css('color', '#999');
+		$(link).replaceWith('<span title="No read permission">'
+			+txt+'</span>');
+	}
+	
 	// lists
 	$(wrapper).find('[href*="List/"]').each(function() {
 		var href = $(this).attr('href');
 		var dt = href.split('/')[1];
 		if(wn.boot.profile.all_read.indexOf(get_label_doctype(dt))==-1) {
-			var txt = $(this).text();
-			$(this).parent().css('color', '#999').html(txt);
+			replace_link(this);
 		}
 	});
 	
@@ -42,8 +48,7 @@
 	$(wrapper).find('[data-doctype]').each(function() {
 		var dt = $(this).attr('data-doctype');
 		if(wn.boot.profile.all_read.indexOf(dt)==-1) {
-			var txt = $(this).text();
-			$(this).parent().css('color', '#999').html(txt);
+			replace_link(this);
 		}
 	});
 	
@@ -52,8 +57,7 @@
 		var href = $(this).attr('href');
 		var dt = href.split('/')[1];
 		if(wn.boot.profile.all_read.indexOf(get_label_doctype(dt))==-1) {
-			var txt = $(this).text();
-			$(this).parent().css('color', '#999').html(txt);
+			replace_link(this);
 		}
 	});
 	
@@ -62,7 +66,9 @@
 		if(!has_common(user_roles, [$(this).attr("data-role"), "System Manager"])) {
 			var html = $(this).html();
 			$(this).parent().css('color', '#999');
-			$(this).replaceWith(html);
+			$(this).replaceWith('<span title="Only accessible by Roles: '+
+				$(this).attr("data-role") 
+				+' and System Manager">'+html+'</span>');
 		}
 	});
 }
diff --git a/startup/startup.js b/startup/startup.js
index 9a6a526..73938db 100644
--- a/startup/startup.js
+++ b/startup/startup.js
@@ -195,3 +195,14 @@
 		}
 	}
 }
+
+Mousetrap.bind(["command+g", "ctrl+g"], function() {
+	wn.ui.toolbar.search.show();
+	return false;
+});
+
+Mousetrap.bind(["command+s", "ctrl+s"], function() {
+	if(cur_frm)
+		cur_frm.save();
+	return false;
+});
\ No newline at end of file
diff --git a/utilities/page/messages/messages.css b/utilities/page/messages/messages.css
index 49b3771..2a32de0 100644
--- a/utilities/page/messages/messages.css
+++ b/utilities/page/messages/messages.css
@@ -1,17 +1,12 @@
 #message-post-text {
-	width: 50%;
 }
 
-#message-list {
-	
+#message-list {	
 }
 
 .message {
-	border-radius: 5px;
-	max-width: 60%;
-	min-width: 40%;
 	padding: 7px;
-	margin-bottom: 7px;
+	border-bottom: 1px solid #ccc;
 }
 
 .message .help {
@@ -22,14 +17,8 @@
 }
 
 .message-other {
-	background-color: #EBFF9C;
-	border: 1px solid #C3CF78;
-	float: right;
-	text-align: right;
 }
 
 .message-self {
 	background-color: #eee;
-	border: 1px solid #ccc;
-	float: left;	
 }
\ No newline at end of file
diff --git a/utilities/page/messages/messages.html b/utilities/page/messages/messages.html
index 7538785..e69de29 100644
--- a/utilities/page/messages/messages.html
+++ b/utilities/page/messages/messages.html
@@ -1,28 +0,0 @@
-<div class="layout-wrapper layout-wrapper-background">
-	<div class="layout-main-section">
-		<a class="close" onclick="window.history.back();">&times;</a>		
-		<h1>Messages</h1>
-		<div class="well">
-			<input id="message-post-text"></input>
-			<button disabled="disabled" id="message-post" class="btn btn-small"><i class="icon-play"></i> Post</button>
-		</div>
-		<div id="message-list">
-		</div>
-	</div>
-	<div class="layout-side-section">
-		<div class="psidebar">
-			<div class="section">
-				<div class="section-head">
-					Messages By
-				</div>
-				<div class="section-body">
-					<div class="section-item">
-						<a href="#!messages"><b>All messages</b></a>
-					</div>
-				</div>
-			</div>
-		</div>
-	</div>
-	<div style="clear: both;">
-	</div>	
-</div>
\ No newline at end of file
diff --git a/utilities/page/messages/messages.js b/utilities/page/messages/messages.js
index 9283f94..5ad540f 100644
--- a/utilities/page/messages/messages.js
+++ b/utilities/page/messages/messages.js
@@ -17,71 +17,87 @@
 wn.provide('erpnext.messages');
 
 wn.pages.messages.onload = function(wrapper) {
-	erpnext.messages.show_active_users();
-	erpnext.messages.make_list();
-	erpnext.update_messages('reset'); //Resets notification icons
-	
-	// post message
-	$('#message-post').click(function() {
-		var txt = $('#message-post-text').val();
-		if(txt) {
-			wn.call({
-				module:'utilities',
-				page:'messages',
-				method:'post',
-				args: {
-					txt: txt,
-					contact: erpnext.messages.contact
-				},
-				callback:function(r,rt) {
-					$('#message-post-text').val('')
-					erpnext.messages.list.run();
-				},
-				btn: this
-			});
-		}
+	wn.ui.make_app_page({
+		parent: wrapper,
+		title: "Messages"
 	});
 	
-	// enable, disable button
-	$('#message-post-text').keyup(function(e) {
-		if($(this).val()) {
-			$('#message-post').attr('disabled', false);
-		} else {
-			$('#message-post').attr('disabled', true);
-		}
-		
-		if(e.which==13) {
-			$('#message-post').click();
-		}
-	})
+	$('<h3 id="message-title">Everyone</h3>\
+	<div id="show-everyone" style="display: none;">\
+		<a href="#messages" style="font-size: 80%;">\
+			Show messages from everyone</a></div><hr>\
+	<div id="post-message" style="display: none">\
+	<textarea style="width: 100%; height: 24px;"></textarea>\
+	<div><button class="btn btn-small">Post</button></div><hr>\
+	</div>\
+	<div class="all-messages"></div>').appendTo($(wrapper).find('.layout-main-section'));
+	
+	erpnext.messages = new erpnext.Messages(wrapper);
 }
 
 $(wn.pages.messages).bind('show', function() {
 	erpnext.messages.show();
-	setTimeout(erpnext.messages.refresh, 7000);
-	$('#message-post-text').focus();
+	setTimeout("erpnext.messages.refresh()", 7000);
 })
 
-erpnext.messages = {
+erpnext.Messages = Class.extend({
+	init: function(wrapper) {
+		this.wrapper = wrapper;
+		this.show_active_users();
+		this.make_post_message();
+		this.make_list();
+		//this.update_messages('reset'); //Resets notification icons		
+	},
+	make_post_message: function() {
+		var me = this;
+		$('#post-message textarea').keydown(function(e) {
+			if(e.which==13) {
+				$('#post-message .btn').click();
+				return false;
+			}
+		});
+		
+		$('#post-message .btn').click(function() {
+			var txt = $('#post-message textarea').val();
+			if(txt) {
+				wn.call({
+					module:'utilities',
+					page:'messages',
+					method:'post',
+					args: {
+						txt: txt,
+						contact: me.contact
+					},
+					callback:function(r,rt) {
+						$('#post-message textarea').val('')
+						me.list.run();
+					},
+					btn: this
+				});
+			}			
+		});
+	},
 	show: function() {
-		var contact = erpnext.messages.get_contact();
+		var contact = this.get_contact();
 
+		$('#message-title').text(contact==user ? "Everyone" :
+			wn.boot.user_info[contact].fullname)
+
+		$("#show-everyone").toggle(contact!=user);
+		
 		// can't send message to self
-		$(wn.pages.messages).find('.well').toggle(contact==user ? false : true);
+		$('#post-message').toggle(contact!=user);
 
-		$(wn.pages.messages).find('h1:first').html('Messages: ' 
-			+ (user==contact ? 'From everyone' : wn.user_info(contact).fullname));
-
-		erpnext.messages.contact = contact;
-		erpnext.messages.list.opts.args.contact = contact;
-		erpnext.messages.list.run();
+		this.contact = contact;
+		this.list.opts.args.contact = contact;
+		this.list.run();
 		
 	},
 	// check for updates every 5 seconds if page is active
 	refresh: function() {
-		setTimeout(erpnext.messages.refresh, 7000);
+		setTimeout("erpnext.messages.refresh()", 7000);
 		if(wn.container.page.label != 'Messages') return;
-		erpnext.messages.show();
+		this.show();
 	},
 	get_contact: function() {
 		var route = location.hash;
@@ -95,12 +111,14 @@
 		return user;	
 	},
 	make_list: function() {
-		erpnext.messages.list = new wn.ui.Listing({
-			parent: $('#message-list').get(0),
+		this.list = new wn.ui.Listing({
+			parent: $(this.wrapper).find('.all-messages'),
 			method: 'utilities.page.messages.messages.get_list',
 			args: {
 				contact: null
 			},
+			hide_refresh: true,
+			no_loading: true,
 			render_row: function(wrapper, data) {
 				$(wrapper).removeClass('list-row');
 				
@@ -111,18 +129,22 @@
 				if(data.owner==user) {
 					data.cls = 'message-self';
 					data.comment_by_fullname = 'You';	
-					data.delete_html = repl('<a class="close" \
-						onclick="erpnext.messages.delete(this)"\
-						data-name="%(name)s">&times;</a>', data);
 				} else {
 					data.cls = 'message-other';
-					data.delete_html = '';
-					if(erpnext.messages.contact==user) {
+					if(this.contact==user) {
 						data.reply_html = repl('<a href="#!messages/%(owner)s">\
 							<i class="icon-share-alt"></i> Reply</a>', data)
 					}
 				}
 
+				// delete
+				data.delete_html = "";
+				if(data.owner==user || data.comment.indexOf("assigned to")!=-1) {
+					data.delete_html = repl('<a class="close" \
+						onclick="erpnext.messages.delete(this)"\
+						data-name="%(name)s">&times;</a>', data);
+				}
+
 				wrapper.innerHTML = repl('<div class="message %(cls)s">%(delete_html)s\
 						<b>%(comment)s</b>\
 						<div class="help">by %(comment_by_fullname)s, %(creation)s</div>\
@@ -142,22 +164,30 @@
 		});
 	},
 	show_active_users: function() {
+		var me = this;
 		wn.call({
 			module:'utilities',
 			page:'messages',
 			method:'get_active_users',
 			callback: function(r,rt) {
-				var $body = $(wn.pages.messages).find('.section-body');
+				var $body = $(me.wrapper).find('.layout-side-section');
+				$("<h4>Users</h4><hr>").appendTo($body);
 				for(var i in r.message) {
 					var p = r.message[i];
-					p.fullname = wn.user_info(p.name).fullname;
-					p.name = p.name.replace('@', '__at__');
-					$body.append(repl('<div class="section-item">\
-						<a href="#!messages/%(name)s">%(fullname)s</a></div>', p))
+					if(p.name != user) {
+						p.fullname = wn.user_info(p.name).fullname;
+						p.name = p.name.replace('@', '__at__');
+						p.label_status = p.has_session ? "label-success" : "";
+						p.status = p.has_session ? "Online" : "Offline";
+						$(repl('<p><span class="label %(label_status)s">%(status)s</span>\
+							<a href="#!messages/%(name)s">%(fullname)s</a>\
+							</p>', p))
+							.appendTo($body);						
+					}
 				}
 			}
 		});
 	}
-}
+});
 
 
diff --git a/utilities/page/messages/messages.py b/utilities/page/messages/messages.py
index 2528413..f2381ab 100644
--- a/utilities/page/messages/messages.py
+++ b/utilities/page/messages/messages.py
@@ -48,7 +48,10 @@
 
 @webnotes.whitelist()
 def get_active_users(arg=None):
-	return webnotes.conn.sql("""select name from tabProfile 
+	return webnotes.conn.sql("""select name,
+		(select count(*) from tabSessions where user=tabProfile.name
+			and timediff(now(), lastupdate) < time("01:00:00")) as has_session
+	 	from tabProfile 
 		where ifnull(enabled,0)=1 and
 		docstatus < 2 and 
 		name not in ('Administrator', 'Guest') 
diff --git a/website/doctype/style_settings/style_settings.js b/website/doctype/style_settings/style_settings.js
index 0d0bf9e..54091a3 100644
--- a/website/doctype/style_settings/style_settings.js
+++ b/website/doctype/style_settings/style_settings.js
@@ -14,9 +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/>.
 
-wn.require('lib/js/lib/jscolor/jscolor.js');
 
 cur_frm.cscript.onload_post_render = function() {
+	wn.require('lib/public/js/lib/jscolor/jscolor.js');
 	cur_frm.fields_dict.background_color.input.className = 'color';
 	jscolor.bind();
 }
\ No newline at end of file