blob: 8462c3d98cd60d8a1b9371b8fb49cbcacc566196 [file] [log] [blame]
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05301
2var erpnext = {};
Rushabh Mehtaa75efa72013-03-19 17:59:49 +05303var wn = {};
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05304
Rushabh Mehtaa54cb242013-03-19 11:12:22 +05305// Add / update a new Lead / Communication
Rushabh Mehta173a0fd2012-12-14 16:39:27 +05306// subject, sender, description
7erpnext.send_message = function(opts) {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +05308 wn.call({
9 type: "POST",
10 method: "website.helpers.contact.send_message",
11 args: opts,
12 callback: opts.callback
13 })
14}
15
16wn.call = function(opts) {
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053017 if(opts.btn) {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053018 var $spinner = $('<img src="lib/images/ui/button-load.gif">').appendTo($(opts.btn).parent())
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053019 $(opts.btn).attr("disabled", "disabled");
20 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053021
22 if(opts.msg) {
23 $(opts.msg).toggle(false);
24 }
25
26 // get or post?
27 if(!opts.args._type) {
28 opts.args._type = opts.type || "GET";
29 }
30
31 // method
32 if(opts.method) {
33 opts.args.cmd = opts.method;
34 }
35
36 // stringify
37 $.each(opts.args, function(key, val) {
38 if(typeof val != "string") {
39 opts.args[key] = JSON.stringify(val);
40 }
41 });
42
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053043 $.ajax({
Rushabh Mehtafdc629b2013-01-31 22:05:39 +053044 type: "POST",
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053045 url: "server.py",
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053046 data: opts.args,
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053047 dataType: "json",
48 success: function(data) {
49 if(opts.btn) {
50 $(opts.btn).attr("disabled", false);
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053051 $spinner.remove();
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053052 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053053 if(data.exc) {
54 console.log(data.exc);
55 }
56 if(opts.msg && data.message) {
57 $(opts.msg).html(data.message).toggle(true);
58 }
59 if(opts.callback)
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053060 opts.callback(data);
61 }
62 });
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053063
64 return false;
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053065}
66
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053067// Setup the user tools
68//
69$(document).ready(function() {
70 // update login
71 var full_name = getCookie("full_name");
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053072 if(full_name) {
Rushabh Mehtabed19ac2013-03-21 17:12:25 +053073 $("#user-tools").html(repl('<a href="profile" title="My Profile" id="user-full-name">%(full_name)s</a> | \
74 <a href="account" title="My Account">My Account</a> | \
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053075 <a href="cart" title="Shopping Cart"><i class="icon-shopping-cart"></i> (%(count)s)</a> | \
76 <a href="server.py?cmd=web_logout" title="Sign Out"><i class="icon-signout"></i></a>', {
77 full_name: full_name,
78 count: getCookie("cart_count") || "0"
79 }));
80 $("#user-tools a").tooltip({"placement":"bottom"});
81 }
82})
83
84// Utility functions
85
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053086function valid_email(id) {
87 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)
88 return 0; else return 1; }
89
Rushabh Mehta2e5db352013-01-16 11:34:26 +053090var validate_email = valid_email;
91
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053092function get_url_arg(name) {
93 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
94 var regexS = "[\\?&]"+name+"=([^&#]*)";
95 var regex = new RegExp( regexS );
96 var results = regex.exec( window.location.href );
97 if(results == null)
98 return "";
99 else
100 return decodeURIComponent(results[1]);
Rushabh Mehtafd6ad192012-12-17 12:52:43 +0530101}
102
103function repl(s, dict) {
104 if(s==null)return '';
105 for(key in dict) {
106 s = s.split("%("+key+")s").join(dict[key]);
107 }
108 return s;
109}
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530110
111function getCookie(name) {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530112 return getCookies()[name];
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530113}
114
115function getCookies() {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530116 var c = document.cookie, v = 0, cookies = {};
117 if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
118 c = RegExp.$1;
119 v = 1;
120 }
121 if (v === 0) {
122 c.split(/[,;]/).map(function(cookie) {
123 var parts = cookie.split(/=/, 2),
124 name = decodeURIComponent(parts[0].trimLeft()),
125 value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
126 if(value.charAt(0)==='"') {
127 value = value.substr(1, value.length-2);
128 }
129 cookies[name] = value;
130 });
131 } else {
132 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) {
133 var name = $0,
134 value = $1.charAt(0) === '"'
135 ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
136 : $1;
137 cookies[name] = value;
138 });
139 }
140 return cookies;
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530141}
142
143if (typeof String.prototype.trimLeft !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530144 String.prototype.trimLeft = function() {
145 return this.replace(/^\s+/, "");
146 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530147}
148if (typeof String.prototype.trimRight !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530149 String.prototype.trimRight = function() {
150 return this.replace(/\s+$/, "");
151 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530152}
153if (typeof Array.prototype.map !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530154 Array.prototype.map = function(callback, thisArg) {
155 for (var i=0, n=this.length, a=[]; i<n; i++) {
156 if (i in this) a[i] = callback.call(thisArg, this[i]);
157 }
158 return a;
159 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530160}