blob: 519d6304359df46664ffe7dc4b7263a45ad1bfe7 [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 Doshi2ac0a832013-07-10 20:49:44 +053098 wn.cart.set_cart_count();
99
Anand Doshiab690292013-06-13 11:21:35 +0530100 $("#user-tools a").tooltip({"placement":"bottom"});
101 $("#user-tools-post-login a").tooltip({"placement":"bottom"});
Anand Doshiab690292013-06-13 11:21:35 +0530102});
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530103
104// Utility functions
105
Rushabh Mehta173a0fd2012-12-14 16:39:27 +0530106function valid_email(id) {
107 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)
108 return 0; else return 1; }
109
Rushabh Mehta2e5db352013-01-16 11:34:26 +0530110var validate_email = valid_email;
111
Rushabh Mehta173a0fd2012-12-14 16:39:27 +0530112function get_url_arg(name) {
113 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
114 var regexS = "[\\?&]"+name+"=([^&#]*)";
115 var regex = new RegExp( regexS );
116 var results = regex.exec( window.location.href );
117 if(results == null)
118 return "";
119 else
120 return decodeURIComponent(results[1]);
Rushabh Mehtafd6ad192012-12-17 12:52:43 +0530121}
122
Anand Doshi3dceb842013-06-19 14:57:14 +0530123function make_query_string(obj) {
124 var query_params = [];
125 $.each(obj, function(k, v) { query_params.push(encodeURIComponent(k) + "=" + encodeURIComponent(v)); });
126 return "?" + query_params.join("&");
127}
128
Rushabh Mehtafd6ad192012-12-17 12:52:43 +0530129function repl(s, dict) {
130 if(s==null)return '';
131 for(key in dict) {
132 s = s.split("%("+key+")s").join(dict[key]);
133 }
134 return s;
135}
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530136
Rushabh Mehtada512ba2013-03-22 12:45:44 +0530137function replace_all(s, t1, t2) {
138 return s.split(t1).join(t2);
139}
140
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530141function getCookie(name) {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530142 return getCookies()[name];
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530143}
144
145function getCookies() {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530146 var c = document.cookie, v = 0, cookies = {};
147 if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
148 c = RegExp.$1;
149 v = 1;
150 }
151 if (v === 0) {
152 c.split(/[,;]/).map(function(cookie) {
153 var parts = cookie.split(/=/, 2),
154 name = decodeURIComponent(parts[0].trimLeft()),
155 value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
Nabin Hait6dcee5a2013-03-26 10:45:29 +0530156 if(value && value.charAt(0)==='"') {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530157 value = value.substr(1, value.length-2);
158 }
159 cookies[name] = value;
160 });
161 } else {
162 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) {
163 var name = $0,
164 value = $1.charAt(0) === '"'
165 ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
166 : $1;
167 cookies[name] = value;
168 });
169 }
170 return cookies;
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530171}
172
173if (typeof String.prototype.trimLeft !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530174 String.prototype.trimLeft = function() {
175 return this.replace(/^\s+/, "");
176 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530177}
178if (typeof String.prototype.trimRight !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530179 String.prototype.trimRight = function() {
180 return this.replace(/\s+$/, "");
181 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530182}
183if (typeof Array.prototype.map !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530184 Array.prototype.map = function(callback, thisArg) {
185 for (var i=0, n=this.length, a=[]; i<n; i++) {
186 if (i in this) a[i] = callback.call(thisArg, this[i]);
187 }
188 return a;
189 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530190}
Anand Doshiab690292013-06-13 11:21:35 +0530191
192// shopping cart
193if(!wn.cart) wn.cart = {};
Anand Doshi3dceb842013-06-19 14:57:14 +0530194var full_name = getCookie("full_name");
195
Anand Doshiab690292013-06-13 11:21:35 +0530196$.extend(wn.cart, {
Anand Doshi3dceb842013-06-19 14:57:14 +0530197 update_cart: function(opts) {
198 if(!full_name) {
199 if(localStorage) {
200 localStorage.setItem("last_visited", window.location.pathname.slice(1));
201 localStorage.setItem("pending_add_to_cart", opts.item_code);
202 }
203 window.location.href = "login";
204 } else {
205 wn.call({
206 type: "POST",
207 method: "website.helpers.cart.update_cart",
208 args: {
209 item_code: opts.item_code,
Anand Doshic2a35272013-06-19 17:19:20 +0530210 qty: opts.qty,
211 with_doclist: opts.with_doclist
Anand Doshi3dceb842013-06-19 14:57:14 +0530212 },
213 btn: opts.btn,
214 callback: function(r) {
215 if(opts.callback)
216 opts.callback(r);
Anand Doshi2ac0a832013-07-10 20:49:44 +0530217
218 wn.cart.set_cart_count();
Anand Doshi3dceb842013-06-19 14:57:14 +0530219 }
220 });
Anand Doshiab690292013-06-13 11:21:35 +0530221 }
Anand Doshiab690292013-06-13 11:21:35 +0530222 },
Anand Doshi2ac0a832013-07-10 20:49:44 +0530223
224 set_cart_count: function() {
225 var cart_count = getCookie("cart_count");
226 if(cart_count)
227 $(".cart-count").html("( "+ cart_count +" )")
228 }
Anand Doshiab690292013-06-13 11:21:35 +0530229});