blob: e714514e972fa8d98925ba7841911266f94f08ba [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
Anand Doshiedbf3e12013-07-02 11:40:16 +053013 });
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053014}
15
16wn.call = function(opts) {
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053017 if(opts.btn) {
18 $(opts.btn).attr("disabled", "disabled");
19 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053020
21 if(opts.msg) {
22 $(opts.msg).toggle(false);
23 }
24
Anand Doshi3dceb842013-06-19 14:57:14 +053025 if(!opts.args) opts.args = {};
26
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053027 // get or post?
28 if(!opts.args._type) {
29 opts.args._type = opts.type || "GET";
30 }
31
32 // method
33 if(opts.method) {
34 opts.args.cmd = opts.method;
35 }
36
37 // stringify
38 $.each(opts.args, function(key, val) {
39 if(typeof val != "string") {
40 opts.args[key] = JSON.stringify(val);
41 }
42 });
43
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053044 $.ajax({
Rushabh Mehtafdc629b2013-01-31 22:05:39 +053045 type: "POST",
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053046 url: "server.py",
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053047 data: opts.args,
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053048 dataType: "json",
49 success: function(data) {
50 if(opts.btn) {
51 $(opts.btn).attr("disabled", false);
52 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053053 if(data.exc) {
Anand Doshi3dceb842013-06-19 14:57:14 +053054 if(opts.btn) {
55 $(opts.btn).addClass("btn-danger");
56 setTimeout(function() { $(opts.btn).removeClass("btn-danger"); }, 1000);
57 }
Anand Doshi99100a42013-07-04 17:13:53 +053058 try {
59 var err = JSON.parse(data.exc);
60 if($.isArray(err)) {
61 err = err.join("\n");
62 }
63 console.error ? console.error(err) : console.log(err);
64 } catch(e) {
65 console.log(data.exc);
66 }
Anand Doshi3dceb842013-06-19 14:57:14 +053067 } else{
68 if(opts.btn) {
69 $(opts.btn).addClass("btn-success");
70 setTimeout(function() { $(opts.btn).removeClass("btn-success"); }, 1000);
71 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053072 }
73 if(opts.msg && data.message) {
74 $(opts.msg).html(data.message).toggle(true);
75 }
76 if(opts.callback)
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053077 opts.callback(data);
Anand Doshi99100a42013-07-04 17:13:53 +053078 },
79 error: function(response) {
80 console.error ? console.error(response) : console.log(response);
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053081 }
82 });
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053083
84 return false;
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053085}
86
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053087// Setup the user tools
88//
89$(document).ready(function() {
90 // update login
91 var full_name = getCookie("full_name");
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053092 if(full_name) {
Anand Doshiab690292013-06-13 11:21:35 +053093 $("#user-tools").addClass("hide");
94 $("#user-tools-post-login").removeClass("hide");
95 $("#user-full-name").text(full_name);
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053096 }
Anand Doshiab690292013-06-13 11:21:35 +053097
Anand Doshiab690292013-06-13 11:21:35 +053098 $("#user-tools a").tooltip({"placement":"bottom"});
99 $("#user-tools-post-login a").tooltip({"placement":"bottom"});
Anand Doshiab690292013-06-13 11:21:35 +0530100});
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530101
102// Utility functions
103
Rushabh Mehta173a0fd2012-12-14 16:39:27 +0530104function valid_email(id) {
105 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)
106 return 0; else return 1; }
107
Rushabh Mehta2e5db352013-01-16 11:34:26 +0530108var validate_email = valid_email;
109
Rushabh Mehta173a0fd2012-12-14 16:39:27 +0530110function get_url_arg(name) {
111 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
112 var regexS = "[\\?&]"+name+"=([^&#]*)";
113 var regex = new RegExp( regexS );
114 var results = regex.exec( window.location.href );
115 if(results == null)
116 return "";
117 else
118 return decodeURIComponent(results[1]);
Rushabh Mehtafd6ad192012-12-17 12:52:43 +0530119}
120
Anand Doshi3dceb842013-06-19 14:57:14 +0530121function make_query_string(obj) {
122 var query_params = [];
123 $.each(obj, function(k, v) { query_params.push(encodeURIComponent(k) + "=" + encodeURIComponent(v)); });
124 return "?" + query_params.join("&");
125}
126
Rushabh Mehtafd6ad192012-12-17 12:52:43 +0530127function repl(s, dict) {
128 if(s==null)return '';
129 for(key in dict) {
130 s = s.split("%("+key+")s").join(dict[key]);
131 }
132 return s;
133}
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530134
Rushabh Mehtada512ba2013-03-22 12:45:44 +0530135function replace_all(s, t1, t2) {
136 return s.split(t1).join(t2);
137}
138
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530139function getCookie(name) {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530140 return getCookies()[name];
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530141}
142
143function getCookies() {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530144 var c = document.cookie, v = 0, cookies = {};
145 if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
146 c = RegExp.$1;
147 v = 1;
148 }
149 if (v === 0) {
150 c.split(/[,;]/).map(function(cookie) {
151 var parts = cookie.split(/=/, 2),
152 name = decodeURIComponent(parts[0].trimLeft()),
153 value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
Nabin Hait6dcee5a2013-03-26 10:45:29 +0530154 if(value && value.charAt(0)==='"') {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530155 value = value.substr(1, value.length-2);
156 }
157 cookies[name] = value;
158 });
159 } else {
160 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) {
161 var name = $0,
162 value = $1.charAt(0) === '"'
163 ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
164 : $1;
165 cookies[name] = value;
166 });
167 }
168 return cookies;
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530169}
170
171if (typeof String.prototype.trimLeft !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530172 String.prototype.trimLeft = function() {
173 return this.replace(/^\s+/, "");
174 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530175}
176if (typeof String.prototype.trimRight !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530177 String.prototype.trimRight = function() {
178 return this.replace(/\s+$/, "");
179 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530180}
181if (typeof Array.prototype.map !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530182 Array.prototype.map = function(callback, thisArg) {
183 for (var i=0, n=this.length, a=[]; i<n; i++) {
184 if (i in this) a[i] = callback.call(thisArg, this[i]);
185 }
186 return a;
187 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530188}
Anand Doshiab690292013-06-13 11:21:35 +0530189
190// shopping cart
191if(!wn.cart) wn.cart = {};
Anand Doshi3dceb842013-06-19 14:57:14 +0530192var full_name = getCookie("full_name");
193
Anand Doshiab690292013-06-13 11:21:35 +0530194$.extend(wn.cart, {
Anand Doshi3dceb842013-06-19 14:57:14 +0530195 update_cart: function(opts) {
196 if(!full_name) {
197 if(localStorage) {
198 localStorage.setItem("last_visited", window.location.pathname.slice(1));
199 localStorage.setItem("pending_add_to_cart", opts.item_code);
200 }
201 window.location.href = "login";
202 } else {
203 wn.call({
204 type: "POST",
205 method: "website.helpers.cart.update_cart",
206 args: {
207 item_code: opts.item_code,
Anand Doshic2a35272013-06-19 17:19:20 +0530208 qty: opts.qty,
209 with_doclist: opts.with_doclist
Anand Doshi3dceb842013-06-19 14:57:14 +0530210 },
211 btn: opts.btn,
212 callback: function(r) {
213 if(opts.callback)
214 opts.callback(r);
215 }
216 });
Anand Doshiab690292013-06-13 11:21:35 +0530217 }
Anand Doshiab690292013-06-13 11:21:35 +0530218 },
Anand Doshiab690292013-06-13 11:21:35 +0530219});