blob: 65f7a691537e0f8c01c9ed49288ddd94b2dead0f [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 Mehtaa75efa72013-03-19 17:59:49 +053073 $("#user-tools").html(repl('<a href="account" title="My Account" id="user-full-name">%(full_name)s</a> | \
Rushabh Mehtaa54cb242013-03-19 11:12:22 +053074 <a href="cart" title="Shopping Cart"><i class="icon-shopping-cart"></i> (%(count)s)</a> | \
75 <a href="server.py?cmd=web_logout" title="Sign Out"><i class="icon-signout"></i></a>', {
76 full_name: full_name,
77 count: getCookie("cart_count") || "0"
78 }));
79 $("#user-tools a").tooltip({"placement":"bottom"});
80 }
81})
82
83// Utility functions
84
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053085function valid_email(id) {
86 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)
87 return 0; else return 1; }
88
Rushabh Mehta2e5db352013-01-16 11:34:26 +053089var validate_email = valid_email;
90
Rushabh Mehta173a0fd2012-12-14 16:39:27 +053091function get_url_arg(name) {
92 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
93 var regexS = "[\\?&]"+name+"=([^&#]*)";
94 var regex = new RegExp( regexS );
95 var results = regex.exec( window.location.href );
96 if(results == null)
97 return "";
98 else
99 return decodeURIComponent(results[1]);
Rushabh Mehtafd6ad192012-12-17 12:52:43 +0530100}
101
102function repl(s, dict) {
103 if(s==null)return '';
104 for(key in dict) {
105 s = s.split("%("+key+")s").join(dict[key]);
106 }
107 return s;
108}
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530109
110function getCookie(name) {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530111 return getCookies()[name];
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530112}
113
114function getCookies() {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530115 var c = document.cookie, v = 0, cookies = {};
116 if (document.cookie.match(/^\s*\$Version=(?:"1"|1);\s*(.*)/)) {
117 c = RegExp.$1;
118 v = 1;
119 }
120 if (v === 0) {
121 c.split(/[,;]/).map(function(cookie) {
122 var parts = cookie.split(/=/, 2),
123 name = decodeURIComponent(parts[0].trimLeft()),
124 value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
125 if(value.charAt(0)==='"') {
126 value = value.substr(1, value.length-2);
127 }
128 cookies[name] = value;
129 });
130 } else {
131 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) {
132 var name = $0,
133 value = $1.charAt(0) === '"'
134 ? $1.substr(1, -1).replace(/\\(.)/g, "$1")
135 : $1;
136 cookies[name] = value;
137 });
138 }
139 return cookies;
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530140}
141
142if (typeof String.prototype.trimLeft !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530143 String.prototype.trimLeft = function() {
144 return this.replace(/^\s+/, "");
145 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530146}
147if (typeof String.prototype.trimRight !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530148 String.prototype.trimRight = function() {
149 return this.replace(/\s+$/, "");
150 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530151}
152if (typeof Array.prototype.map !== "function") {
Rushabh Mehtaa75efa72013-03-19 17:59:49 +0530153 Array.prototype.map = function(callback, thisArg) {
154 for (var i=0, n=this.length, a=[]; i<n; i++) {
155 if (i in this) a[i] = callback.call(thisArg, this[i]);
156 }
157 return a;
158 };
Rushabh Mehtaa54cb242013-03-19 11:12:22 +0530159}