Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 1 | |
| 2 | var erpnext = {}; |
| 3 | |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 4 | // Add / update a new Lead / Communication |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 5 | // subject, sender, description |
| 6 | erpnext.send_message = function(opts) { |
| 7 | if(opts.btn) { |
| 8 | $(opts.btn).attr("disabled", "disabled"); |
| 9 | } |
| 10 | |
| 11 | $.ajax({ |
Rushabh Mehta | fdc629b | 2013-01-31 22:05:39 +0530 | [diff] [blame] | 12 | type: "POST", |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 13 | url: "server.py", |
| 14 | data: { |
Rushabh Mehta | 2e5db35 | 2013-01-16 11:34:26 +0530 | [diff] [blame] | 15 | cmd: "website.helpers.contact.send_message", |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 16 | subject: opts.subject, |
| 17 | sender: opts.sender, |
Anand Doshi | e47ceae | 2012-12-24 19:50:15 +0530 | [diff] [blame] | 18 | status: opts.status, |
Anand Doshi | 5acd082 | 2013-02-21 20:06:57 +0530 | [diff] [blame] | 19 | _type: "POST", |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 20 | message: typeof opts.message == "string" |
| 21 | ? opts.message |
| 22 | : JSON.stringify(opts.message) |
| 23 | }, |
| 24 | dataType: "json", |
| 25 | success: function(data) { |
| 26 | if(opts.btn) { |
| 27 | $(opts.btn).attr("disabled", false); |
| 28 | } |
| 29 | if(opts.callback) |
| 30 | opts.callback(data); |
| 31 | } |
| 32 | }); |
| 33 | } |
| 34 | |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 35 | // Setup the user tools |
| 36 | // |
| 37 | $(document).ready(function() { |
| 38 | // update login |
| 39 | var full_name = getCookie("full_name"); |
| 40 | if(full_name.substr(0,1)=='"') { |
| 41 | full_name = full_name.substr(1, full_name.length-2); |
| 42 | } |
| 43 | if(full_name) { |
| 44 | $("#user-tools").html(repl('<a href="account" title="My Account">%(full_name)s</a> | \ |
| 45 | <a href="cart" title="Shopping Cart"><i class="icon-shopping-cart"></i> (%(count)s)</a> | \ |
| 46 | <a href="server.py?cmd=web_logout" title="Sign Out"><i class="icon-signout"></i></a>', { |
| 47 | full_name: full_name, |
| 48 | count: getCookie("cart_count") || "0" |
| 49 | })); |
| 50 | $("#user-tools a").tooltip({"placement":"bottom"}); |
| 51 | } |
| 52 | }) |
| 53 | |
| 54 | // Utility functions |
| 55 | |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 56 | function valid_email(id) { |
| 57 | 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) |
| 58 | return 0; else return 1; } |
| 59 | |
Rushabh Mehta | 2e5db35 | 2013-01-16 11:34:26 +0530 | [diff] [blame] | 60 | var validate_email = valid_email; |
| 61 | |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 62 | function get_url_arg(name) { |
| 63 | name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); |
| 64 | var regexS = "[\\?&]"+name+"=([^&#]*)"; |
| 65 | var regex = new RegExp( regexS ); |
| 66 | var results = regex.exec( window.location.href ); |
| 67 | if(results == null) |
| 68 | return ""; |
| 69 | else |
| 70 | return decodeURIComponent(results[1]); |
Rushabh Mehta | fd6ad19 | 2012-12-17 12:52:43 +0530 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | function repl(s, dict) { |
| 74 | if(s==null)return ''; |
| 75 | for(key in dict) { |
| 76 | s = s.split("%("+key+")s").join(dict[key]); |
| 77 | } |
| 78 | return s; |
| 79 | } |
Rushabh Mehta | a54cb24 | 2013-03-19 11:12:22 +0530 | [diff] [blame] | 80 | |
| 81 | function getCookie(name) { |
| 82 | return getCookies()[name]; |
| 83 | } |
| 84 | |
| 85 | function getCookies() { |
| 86 | var c = document.cookie, v = 0, cookies = {}; |
| 87 | if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) { |
| 88 | c = RegExp.$1; |
| 89 | v = 1; |
| 90 | } |
| 91 | if (v === 0) { |
| 92 | c.split(/[,;]/).map(function(cookie) { |
| 93 | var parts = cookie.split(/=/, 2), |
| 94 | name = decodeURIComponent(parts[0].trimLeft()), |
| 95 | value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null; |
| 96 | cookies[name] = value; |
| 97 | }); |
| 98 | } else { |
| 99 | c.match(/(?:^|\s+)([!#$%&'*+\-.0-9A-Z^`a-z|~]+)=([!#$%&'*+\-.0-9A-Z^`a-z|~]*|"(?:[\x20-\x7E\x80\xFF]|\\[\x00-\x7F])*")(?=\s*[,;]|$)/g).map(function($0, $1) { |
| 100 | var name = $0, |
| 101 | value = $1.charAt(0) === '"' |
| 102 | ? $1.substr(1, -1).replace(/\\(.)/g, "$1") |
| 103 | : $1; |
| 104 | cookies[name] = value; |
| 105 | }); |
| 106 | } |
| 107 | return cookies; |
| 108 | } |
| 109 | |
| 110 | if (typeof String.prototype.trimLeft !== "function") { |
| 111 | String.prototype.trimLeft = function() { |
| 112 | return this.replace(/^\s+/, ""); |
| 113 | }; |
| 114 | } |
| 115 | if (typeof String.prototype.trimRight !== "function") { |
| 116 | String.prototype.trimRight = function() { |
| 117 | return this.replace(/\s+$/, ""); |
| 118 | }; |
| 119 | } |
| 120 | if (typeof Array.prototype.map !== "function") { |
| 121 | Array.prototype.map = function(callback, thisArg) { |
| 122 | for (var i=0, n=this.length, a=[]; i<n; i++) { |
| 123 | if (i in this) a[i] = callback.call(thisArg, this[i]); |
| 124 | } |
| 125 | return a; |
| 126 | }; |
| 127 | } |