-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js.php
276 lines (246 loc) · 9.09 KB
/
common.js.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
@header("Content-Type: text/javascript; charset=UTF-8");
$currDir=dirname(__FILE__);
include("$currDir/defaultLang.php");
include("$currDir/language.php");
?>
document.observe("dom:loaded", function() {
});
/* initials and fixes */
jQuery(function(){
jQuery(window).resize(function(){ fix_table_responsive_width(); });
fix_table_responsive_width();
jQuery('img.img-responsive').each(function(){
jQuery(this).css({ maxWidth: jQuery(this)[0].naturalWidth });
});
});
/* fix table-responsive behavior on Chrome */
function fix_table_responsive_width(){
var resp_width = jQuery('div.table-responsive').width();
var table_width;
if(resp_width){
jQuery('div.table-responsive table').width('100%');
table_width = jQuery('div.table-responsive table').width();
resp_width = jQuery('div.table-responsive').width();
if(resp_width == table_width){
jQuery('div.table-responsive table').width(resp_width - 1);
}
}
}
function colorize(){ }
function clients_validateData(){
if($('name').value == ''){ modal_window({ message: '<div class="alert alert-danger"><?php echo addslashes($Translation['field not null']); ?></div>', title: "<?php echo addslashes($Translation['error:']); ?> Name", close: function(){ jQuery('#name').focus(); } }); return false; };
return true;
}
function companies_validateData(){
return true;
}
function sic_validateData(){
return true;
}
function reports_validateData(){
return true;
}
function entries_validateData(){
if($('report').value == ''){ modal_window({ message: '<div class="alert alert-danger"><?php echo addslashes($Translation['field not null']); ?></div>', title: "<?php echo addslashes($Translation['error:']); ?> Report", close: function(){ jQuery('#report').focus(); } }); return false; };
return true;
}
function outcome_areas_validateData(){
return true;
}
function outcomes_validateData(){
return true;
}
function beneficiary_groups_validateData(){
return true;
}
function indicators_validateData(){
return true;
}
function tax_entry_validateData(){
if($('report').value == ''){ modal_window({ message: '<div class="alert alert-danger"><?php echo addslashes($Translation['field not null']); ?></div>', title: "<?php echo addslashes($Translation['error:']); ?> Report", close: function(){ jQuery('#report').focus(); } }); return false; };
return true;
}
function post(url, params, update, disable, loading){
new Ajax.Request(
url, {
method: 'post',
parameters: params,
onCreate: function() {
if($(disable) != undefined) $(disable).disabled=true;
if($(loading) != undefined && update != loading) $(loading).update('<div style="direction: ltr;"><img src="loading.gif"> <?php echo $Translation['Loading ...']; ?></div>');
},
onSuccess: function(resp) {
if($(update) != undefined) $(update).update(resp.responseText);
},
onComplete: function() {
if($(disable) != undefined) $(disable).disabled=false;
if($(loading) != undefined && loading != update) $(loading).update('');
}
}
);
}
function post2(url, params, notify, disable, loading, redirectOnSuccess){
new Ajax.Request(
url, {
method: 'post',
parameters: params,
onCreate: function() {
if($(disable) != undefined) $(disable).disabled=true;
if($(loading) != undefined) $(loading).show();
},
onSuccess: function(resp) {
/* show notification containing returned text */
if($(notify) != undefined) $(notify).removeClassName('Error').appear().update(resp.responseText);
/* in case no errors returned, */
if(!resp.responseText.match(/<?php echo $Translation['error:']; ?>/)){
/* redirect to provided url */
if(redirectOnSuccess != undefined){
window.location=redirectOnSuccess;
/* or hide notification after a few seconds if no url is provided */
}else{
if($(notify) != undefined) window.setTimeout(function(){ $(notify).fade(); }, 15000);
}
/* in case of error, apply error class */
}else{
$(notify).addClassName('Error');
}
},
onComplete: function() {
if($(disable) != undefined) $(disable).disabled=false;
if($(loading) != undefined) $(loading).hide();
}
}
);
}
function passwordStrength(password, username){
// score calculation (out of 10)
var score = 0;
re = new RegExp(username, 'i');
if(username.length && password.match(re)) score -= 5;
if(password.length < 6) score -= 3;
else if(password.length > 8) score += 5;
else score += 3;
if(password.match(/(.*[0-9].*[0-9].*[0-9])/)) score += 3;
if(password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5;
if(password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) score += 2;
if(score >= 9)
return 'strong';
else if(score >= 5)
return 'good';
else
return 'weak';
}
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
function loadScript(jsUrl, cssUrl, callback){
// adding the script tag to the head
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = jsUrl;
if(cssUrl != ''){
var css = document.createElement('link');
css.href = cssUrl;
css.rel = "stylesheet";
css.type = "text/css";
head.appendChild(css);
}
// then bind the event to the callback function
// there are several events for cross browser compatibility
if(script.onreadystatechange != undefined){ script.onreadystatechange = callback; }
if(script.onload != undefined){ script.onload = callback; }
// fire the loading
head.appendChild(script);
}
/**
* options object. The following members can be provided:
* url: iframe url to load
* message: instead of a url to open, you could pass a message. HTML tags allowed.
* id: id attribute of modal window
* title: optional modal window title
* size: 'default', 'full'
* close: optional function to execute on closing the modal
* footer: optional array of objects describing the buttons to display in the footer.
* Each button object can have the following members:
* label: string, label of button
* bs_class: string, button bootstrap class. Can be 'primary', 'default', 'success', 'warning' or 'danger'
* click: function to execute on clicking the button. If the button closes the modal, this
* function is executed before the close handler
* causes_closing: boolean, default is true.
*/
function modal_window(options){
var id = options.id;
var url = options.url;
var title = options.title;
var footer = options.footer;
var message = options.message;
if(typeof(id) == 'undefined') id = random_string(20);
if(typeof(footer) == 'undefined') footer = [];
if(jQuery('#' + id).length){
/* modal exists -- remove it first */
jQuery('#' + id).remove();
}
/* prepare footer buttons, if any */
var footer_buttons = '';
for(i = 0; i < footer.length; i++){
if(typeof(footer[i].causes_closing) == 'undefined'){ footer[i].causes_closing = true; }
if(typeof(footer[i].bs_class) == 'undefined'){ footer[i].bs_class = 'default'; }
footer[i].id = id + '_footer_button_' + random_string(10);
footer_buttons += '<button type="button" class="btn btn-' + footer[i].bs_class + '" ' +
(footer[i].causes_closing ? 'data-dismiss="modal" ' : '') +
'id="' + footer[i].id + '" ' +
'>' + footer[i].label + '</button>';
}
jQuery('body').append(
'<div class="modal fade" id="' + id + '" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">' +
'<div class="modal-dialog">' +
'<div class="modal-content">' +
( title != undefined ?
'<div class="modal-header">' +
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' +
'<h3 class="modal-title" id="myModalLabel">' + title + '</h3>' +
'</div>'
: ''
) +
'<div class="modal-body" style="overflow-y: auto;">' +
( url != undefined ?
'<iframe width="100%" height="100%" sandbox="allow-forms allow-scripts allow-same-origin" src="' + url + '"></iframe>'
: message
) +
'</div>' +
( footer != undefined ?
'<div class="modal-footer">' + footer_buttons + '</div>'
: ''
) +
'</div>' +
'</div>' +
'</div>'
);
for(i = 0; i < footer.length; i++){
if(typeof(footer[i].click) == 'function'){
jQuery('#' + footer[i].id).click(footer[i].click);
}
}
jQuery('#' + id).modal();
if(typeof(options.close) == 'function'){
jQuery('#' + id).on('hidden.bs.modal', options.close);
}
if(typeof(options.size) == 'undefined') options.size = 'default';
if(options.size == 'full'){
jQuery(window).resize(function(){
jQuery('#' + id + ' .modal-dialog').width(jQuery(window).width() * 0.95);
jQuery('#' + id + ' .modal-body').height(jQuery(window).height() * 0.7);
}).trigger('resize');
}
return id;
}
function random_string(string_length){
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i = 0; i < string_length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}