blob: 7897cd548b39c1013ac188f4769d67aed2283b94 [file] [log] [blame]
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05301
2var erpnext = {};
3
Rushabh Mehtaa54cb242013-03-19 11:12:22 +05304// Add / update a new Lead / Communication
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05305// subject, sender, description
6erpnext.send_message = function(opts) {
7 if(opts.btn) {
8 $(opts.btn).attr("disabled", "disabled");
9 }
10
11 $.ajax({
Rushabh Mehtafdc629b2013-01-31 22:05:39 +053012 type: "POST",
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053013 url: "server.py",
14 data: {
Rushabh Mehta2e5db352013-01-16 11:34:26 +053015 cmd: "website.helpers.contact.send_message",
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053016 subject: opts.subject,
17 sender: opts.sender,
Anand Doshie47ceae2012-12-24 19:50:15 +053018 status: opts.status,
Anand Doshi5acd0822013-02-21 20:06:57 +053019 _type: "POST",
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053020 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 Mehtaa54cb242013-03-19 11:12:22 +053035// 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 Mehta173a0fd2012-12-14 16:39:27 +053056function 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 Mehta2e5db352013-01-16 11:34:26 +053060var validate_email = valid_email;
61
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053062function 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 Mehtafd6ad192012-12-17 12:52:43 +053071}
72
73function 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 Mehtaa54cb242013-03-19 11:12:22 +053080
81function getCookie(name) {
82 return getCookies()[name];
83}
84
85function 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
110if (typeof String.prototype.trimLeft !== "function") {
111 String.prototype.trimLeft = function() {
112 return this.replace(/^\s+/, "");
113 };
114}
115if (typeof String.prototype.trimRight !== "function") {
116 String.prototype.trimRight = function() {
117 return this.replace(/\s+$/, "");
118 };
119}
120if (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}