blob: 59614c54a61289076811cf9ab7e8d4da5f8cdd0e [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 }
58 } else{
59 if(opts.btn) {
60 $(opts.btn).addClass("btn-success");
61 setTimeout(function() { $(opts.btn).removeClass("btn-success"); }, 1000);
62 }
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053063 }
64 if(opts.msg && data.message) {
65 $(opts.msg).html(data.message).toggle(true);
66 }
67 if(opts.callback)
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053068 opts.callback(data);
69 }
70 });
Rushabh Mehtaa75efa72013-03-19 17:59:49 +053071
72 return false;
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053073}
74
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053075// Setup the user tools
76//
77$(document).ready(function() {
78 // update login
79 var full_name = getCookie("full_name");
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053080 if(full_name) {
Anand Doshiab690292013-06-13 11:21:35 +053081 $("#user-tools").addClass("hide");
82 $("#user-tools-post-login").removeClass("hide");
83 $("#user-full-name").text(full_name);
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053084 }
Anand Doshiab690292013-06-13 11:21:35 +053085
Anand Doshiab690292013-06-13 11:21:35 +053086 $("#user-tools a").tooltip({"placement":"bottom"});
87 $("#user-tools-post-login a").tooltip({"placement":"bottom"});
Anand Doshiab690292013-06-13 11:21:35 +053088});
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053089
90// Utility functions
91
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053092function valid_email(id) {
93 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)
94 return 0; else return 1; }
95
Rushabh Mehta2e5db352013-01-16 11:34:26 +053096var validate_email = valid_email;
97
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053098function get_url_arg(name) {
99 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
100 var regexS = "[\\?&]"+name+"=([^&#]*)";
101 var regex = new RegExp( regexS );
102 var results = regex.exec( window.location.href );
103 if(results == null)
104 return "";
105 else
106 return decodeURIComponent(results[1]);
Rushabh Mehtafd6ad192012-12-17 12:52:43 +0530107}
108
Anand Doshi3dceb842013-06-19 14:57:14 +0530109function make_query_string(obj) {
110 var query_params = [];
111 $.each(obj, function(k, v) { query_params.push(encodeURIComponent(k) + "=" + encodeURIComponent(v)); });
112 return "?" + query_params.join("&");
113}
114
Rushabh Mehtafd6ad192012-12-17 12:52:43 +0530115function repl(s, dict) {
116 if(s==null)return '';
117 for(key in dict) {
118 s = s.split("%("+key+")s").join(dict[key]);
119 }
120 return s;
121}
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530122
Rushabh Mehtada512ba2013-03-22 12:45:44 +0530123function replace_all(s, t1, t2) {
124 return s.split(t1).join(t2);
125}
126
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530127function getCookie(name) {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530128 return getCookies()[name];
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530129}
130
131function getCookies() {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530132 var c = document.cookie, v = 0, cookies = {};
133 if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
134 c = RegExp.$1;
135 v = 1;
136 }
137 if (v === 0) {
138 c.split(/[,;]/).map(function(cookie) {
139 var parts = cookie.split(/=/, 2),
140 name = decodeURIComponent(parts[0].trimLeft()),
141 value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
Nabin Hait6dcee5a2013-03-26 10:45:29 +0530142 if(value && value.charAt(0)==='"') {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530143 value = value.substr(1, value.length-2);
144 }
145 cookies[name] = value;
146 });
147 } else {
148 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) {
149 var name = $0,
150 value = $1.charAt(0) === '"'
151 ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
152 : $1;
153 cookies[name] = value;
154 });
155 }
156 return cookies;
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530157}
158
159if (typeof String.prototype.trimLeft !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530160 String.prototype.trimLeft = function() {
161 return this.replace(/^\s+/, "");
162 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530163}
164if (typeof String.prototype.trimRight !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530165 String.prototype.trimRight = function() {
166 return this.replace(/\s+$/, "");
167 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530168}
169if (typeof Array.prototype.map !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530170 Array.prototype.map = function(callback, thisArg) {
171 for (var i=0, n=this.length, a=[]; i<n; i++) {
172 if (i in this) a[i] = callback.call(thisArg, this[i]);
173 }
174 return a;
175 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530176}
Anand Doshiab690292013-06-13 11:21:35 +0530177
178// shopping cart
179if(!wn.cart) wn.cart = {};
Anand Doshi3dceb842013-06-19 14:57:14 +0530180var full_name = getCookie("full_name");
181
Anand Doshiab690292013-06-13 11:21:35 +0530182$.extend(wn.cart, {
Anand Doshi3dceb842013-06-19 14:57:14 +0530183 update_cart: function(opts) {
184 if(!full_name) {
185 if(localStorage) {
186 localStorage.setItem("last_visited", window.location.pathname.slice(1));
187 localStorage.setItem("pending_add_to_cart", opts.item_code);
188 }
189 window.location.href = "login";
190 } else {
191 wn.call({
192 type: "POST",
193 method: "website.helpers.cart.update_cart",
194 args: {
195 item_code: opts.item_code,
Anand Doshic2a35272013-06-19 17:19:20 +0530196 qty: opts.qty,
197 with_doclist: opts.with_doclist
Anand Doshi3dceb842013-06-19 14:57:14 +0530198 },
199 btn: opts.btn,
200 callback: function(r) {
201 if(opts.callback)
202 opts.callback(r);
203 }
204 });
Anand Doshiab690292013-06-13 11:21:35 +0530205 }
Anand Doshiab690292013-06-13 11:21:35 +0530206 },
Anand Doshiab690292013-06-13 11:21:35 +0530207});