-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcforms.js
More file actions
533 lines (408 loc) · 18.7 KB
/
cforms.js
File metadata and controls
533 lines (408 loc) · 18.7 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
* @licstart The following is the entire license notice for the
* JavaScript code in this page.
*
* Copyright (c) 2007-2012 Oliver Seidel (email : oliver.seidel @ deliciousdays.com)
* Copyright (c) 2014-2017 Bastian Germann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @licend The above is the entire license notice
* for the JavaScript code in this page.
*/
function clearField(thefield) {
if (thefield.defaultValue == thefield.value)
thefield.value = '';
}
function setField(thefield) {
if (thefield.value == '')
thefield.value = thefield.defaultValue;
}
function cforms_validate(no, directFormSubmission) {
var doInnerXHTML = function (elementId, stringXHTML) {
try {
if (document.getElementById(elementId + 'a'))
document.getElementById(elementId + 'a').innerHTML = stringXHTML;
if (document.getElementById(elementId + 'b'))
document.getElementById(elementId + 'b').innerHTML = stringXHTML;
return true;
} catch (ee) {
return false;
}
};
var stripslashes = function (str) {
str = str.replace(/\\'/g, '\'');
str = str.replace(/\\"/g, '"');
str = str.replace(/\\\\/g, '\\');
str = str.replace(/\\0/g, '\0');
return str;
};
var set_customerr = function (id, parent_el) {
var gotone = all_custom_error[id];
if (all_custom_error[id] && gotone != '') {
if (show_err_ins === 'y') {
insert_err_p[insert_err_count] = parent_el.id;
var ul = document.createElement('UL');
var li = document.createElement('LI');
li.innerHTML = stripslashes(gotone);
ul.appendChild(li);
ul.setAttribute('class', 'cf_li_text_err');
insert_err[insert_err_count++] = ul;
}
if (parent_el.id != '')
return custom_error + '<li><a href="#' + parent_el.id + '">' + gotone + ' »</li></a>';
else
return custom_error + '<li>' + gotone + '</li>';
} else
return custom_error;
};
// track and store all errors until end
var check_for_customerr = function (id) {
var parent_el = document.getElementById(id).parentNode;
parent_el.className = "cf_li_err";
return set_customerr(id, parent_el);
};
var check_for_customerr_radio = function (id, cerr) {
var parent_el = document.getElementById(id.substr(0, id.length - 5));
parent_el.className = "cf-box-title cf_li_err";
return set_customerr(cerr, parent_el);
};
var isParentChkBoxGroup = function (el) {
while (el.parentNode) {
if (el.parentNode.className === 'cf-box-group')
return true;
else
el = el.parentNode;
}
return false;
};
var cforms_submitform = function (no) {
var regexp = new RegExp('[$][#][$]', ['g']);
var prefix = '\n';
var params = '';
var objColl = document.getElementById('cforms' + no + 'form').getElementsByTagName('*');
for (var i = 0, j = objColl.length; i < j; i++) {
var fld = objColl[i].nodeName.toLowerCase();
var typ = objColl[i].type;
if (fld == "input" || fld == "textarea" || fld == "select") {
if (typ == "checkbox") {
if (objColl[i].name.match(/\[\]/)) {
var group = '';
while (i < j && isParentChkBoxGroup(objColl[i])) {
if (objColl[i].type == 'checkbox' && objColl[i].name.match(/\[\]/) && objColl[i].checked) {
group = group + objColl[i].value + ',';
}
i++;
}
if (group.length > 1)
params = params + prefix + group.substring(0, group.length - 1);
else
params = params + prefix + "";
} else
params = params + prefix + (objColl[i].checked ? ((objColl[i].value != "") ? objColl[i].value : "X") : "");
} else if (typ == "radio") {
var group = objColl[i].checked ? ((objColl[i].value != "") ? objColl[i].value : "X") : '';
while (i < j && isParentChkBoxGroup(objColl[i + 1])) {
if (objColl[i + 1].type == 'radio' && objColl[i + 1].checked) {
group = group + ',' + objColl[i + 1].value;
}
i++;
}
if (group.charAt(0) == ',')
params = params + prefix + group.substring(1, group.length);
else
params = params + prefix + group;
} else if (typ == "select-multiple") {
var all_child_obj = '';
for (var z = 0; z < objColl[i].childNodes.length; z++) {
if (objColl[i].childNodes[z].nodeName.toLowerCase() == 'option' && objColl[i].childNodes[z].selected) {
all_child_obj = all_child_obj + objColl[i].childNodes[z].value.replace(regexp, '$') + ',';
}
}
params = params + prefix + all_child_obj.substring(0, all_child_obj.length - 1);
} else if (typ == "hidden" && objColl[i].className.match(/cfhidden/)) {
params = params + prefix + objColl[i].value;
} else if (typ != "hidden" && typ != "submit") {
params = params + prefix + objColl[i].value.replace(regexp, '$');
}
}
}
// Overwrite params with generic form
params = jQuery('#cforms' + no + 'form').serialize();
var post_data = 'action=submitcform&_wpnonce='
+ cforms2_ajax.nonces['submitcform']
+ '&cforms_id=' + no + '&' + params;
jQuery.post(
cforms2_ajax.url,
post_data,
function (data) {
cforms_setsuccessmessage(data);
}
);
};
var cforms_setsuccessmessage = function (message) {
var no = message.no;
if (!message.result.match(/success/)) {
call_err(no, message.html, '');
return;
}
if (!document.getElementById('cforms' + no + 'form').className.match(/cfnoreset/))
document.getElementById('cforms' + no + 'form').reset();
document.getElementById('sendbutton' + no).style.cursor = "auto";
document.getElementById('sendbutton' + no).disabled = false;
if (document.createEvent) {
var event = document.createEvent("HTMLEvents");
event.initEvent("cforms2FormSent", true, true);
event.eventName = "cforms2FormSent";
event.formNumber = no;
document.body.dispatchEvent(event);
}
var stringXHTML = message.html;
// for both message boxes
var isA = false;
var ucm = (parseInt(no) > 1) ? ' ' + message.result + no : '';
if (document.getElementById('usermessage' + no + 'a')) {
document.getElementById('usermessage' + no + 'a').className = "cf_info " + message.result + ucm;
isA = true;
}
if (document.getElementById('usermessage' + no + 'b') && !(message.hide && isA)) {
document.getElementById('usermessage' + no + 'b').className = "cf_info " + message.result + ucm;
}
doInnerXHTML('usermessage' + no, stringXHTML);
if (message.hide) {
document.getElementById('cforms' + no + 'form').style.display = 'none';
if (!message.redirection)
location.hash = '#usermessage' + no + 'a';
}
if (message.redirection) {
location.href = message.redirection;
}
};
var write_customerr = function () {
for (var n = 0; n < insert_err_p.length; n++) {
if (document.getElementById(insert_err_p[n])) {
document.getElementById(insert_err_p[n]).insertBefore(
insert_err[n],
document.getElementById(insert_err_p[n]).firstChild
);
}
}
};
if (!no)
no = '';
var msgbox = 'usermessage' + no;
if (document.getElementById(msgbox + 'a')) {
document.getElementById(msgbox + 'a').className = "cf_info waiting";
}
if (document.getElementById(msgbox + 'b')) {
document.getElementById(msgbox + 'b').className = "cf_info waiting";
}
var waiting = decodeURI(document.getElementById('cf_working' + no).value);
waiting = waiting.replace(/\\/g, "");
var insert_err = [];
var insert_err_p = [];
var insert_err_count = 0;
var all_custom_error = [];
var customerr_concatenated = document.getElementById('cf_customerr' + no).value;
var show_err_ins = customerr_concatenated.substr(0, 1);
var error_container = decodeURIComponent(customerr_concatenated.substr(1)).split('|');
for (var i = 0; i < error_container.length; i++) {
var keyvalue = error_container[i].split('$#$');
all_custom_error[keyvalue[0]] = keyvalue[1];
}
var custom_error = '';
if (!doInnerXHTML(msgbox, waiting)) {
return true;
}
var all_valid = true;
var regexp_e = new RegExp('^[_a-z0-9+-]+(\\.[_a-z0-9+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,63})$', 'i'); // email regexp
// clean enhanced error if present
var objColl = document.getElementById('cforms' + no + 'form').getElementsByTagName('li');
for (var i = 0; i < objColl.length; i++) {
if (objColl[i].className.match(/cf_li_err/)) {
if (objColl[i].className.match(/cf-box-title/))
objColl[i].className = 'cf-box-title';
else
objColl[i].className = '';
}
}
objColl = document.getElementById('cforms' + no + 'form').getElementsByTagName('ul');
while (objColl.length > 0) {
objColl[0].parentNode.removeChild(objColl[0]);
}
objColl = document.getElementById('cforms' + no + 'form').getElementsByTagName('*');
var last_one = false;
for (var i = 0, j = objColl.length; i < j; i++) {
var temp = objColl[i].className;
var newclass = '';
if (temp.match(/secinput/))
newclass = 'secinput';
else if (temp.match(/cf-box-./))
newclass = temp.match(/cf-box-./);
else if (temp.match(/cformselect/))
newclass = 'cformselect';
else if (temp.match(/upload/))
newclass = 'cf_upload';
else if (temp.match(/single/))
newclass = 'single';
else if (temp.match(/area/))
newclass = 'area';
else if (temp.match(/cfselectmulti/))
newclass = 'cfselectmulti';
var fld = objColl[i].nodeName.toLowerCase();
var typ = objColl[i].type;
if ((fld == "input" || fld == "textarea" || fld == "select") && !(typ == "hidden" || typ == "submit")) {
if (temp.match(/required/) && !temp.match(/email/) && typ != "radio") {
newclass = newclass + ' fldrequired';
var n = objColl[i].nextSibling;
var p = objColl[i].previousSibling;
if (temp.match(/cf-box-./)) {
if (!objColl[i].checked) {
custom_error = check_for_customerr(objColl[i].id);
newclass = newclass + ' cf_error';
// we can't change the checkbox much but the text on the side!
if (n && n.nodeName.toLowerCase() == "label" && !n.className.match(/errortxt/))
n.className = n.className + " cf_errortxt";
else if (p && p.nodeName.toLowerCase() == "label" && !p.className.match(/errortxt/))
p.className = p.className + " cf_errortxt";
all_valid = false;
if (!last_one && objColl[i].id != '')
last_one = objColl[i].id;
} else {
// we can't change the checkbox much but the text on the side!
if (n && n.nodeName.toLowerCase() == "label" && n.className.match(/cf_errortxt/))
n.className = n.className.substr(0, n.className.search(/ cf_errortxt/));
else if (p && p.nodeName.toLowerCase() == "label" && p.className.match(/cf_errortxt/))
p.className = p.className.substr(0, p.className.search(/ cf_errortxt/));
}
} else if (temp.match(/cformselect/)) {
if (objColl[i].value == '' || objColl[i].value == '-') {
newclass = newclass + ' cf_error';
all_valid = false;
if (!last_one && objColl[i].id != '')
last_one = objColl[i].id;
custom_error = check_for_customerr(objColl[i].id);
}
} else if (objColl[i].value == '') {
newclass = newclass + ' cf_error';
all_valid = false;
if (!last_one && objColl[i].id != '')
last_one = objColl[i].id;
custom_error = check_for_customerr(objColl[i].id);
}
}
if (temp.match(/email/)) {
newclass = newclass + ' fldemail';
if (!(objColl[i].value == '' && !temp.match(/required/))) {
if (!regexp_e.test(objColl[i].value)) {
newclass = newclass + ' fldrequired cf_error';
all_valid = false;
if (!last_one)
last_one = objColl[i].name;
custom_error = check_for_customerr(objColl[i].id);
} else
newclass = newclass + ' fldrequired';
}
}
if (temp.match(/required/) && temp.match(/cf-box-b/) && typ.match(/radio/)) {
var temp_i = i;
var radio_valid = false;
while (objColl[i].parentNode.className.match(/cf-box-group/)
|| objColl[i].parentNode.parentNode.className.match(/cf-box-group/)) {
temp = objColl[i].className;
if (temp.match(/cf-box-b/) && objColl[i].checked) {
radio_valid = true;
}
i++;
}
if (!radio_valid) {
all_valid = false;
if (!last_one)
last_one = objColl[temp_i].parentNode.id;
custom_error = check_for_customerr_radio(
objColl[temp_i].parentNode.id,
objColl[temp_i].id.substr(0, objColl[temp_i].id.length - 2)
);
}
} else
objColl[i].className = newclass;
}
// if regexp provided use it!
var regexp = 1;
if (objColl[i] && document.getElementById(objColl[i].id + '_regexp')) {
var obj_regexp = document.getElementById(objColl[i].id + '_regexp');
var inpVal = objColl[i].value;
if (typ == 'textarea')
inpVal = inpVal.replace(/\n\r?/g, ' ');
if (obj_regexp && obj_regexp.value != '') {
if (document.getElementById(obj_regexp.value)) {
if (inpVal != document.getElementById(obj_regexp.value).value)
regexp = null;
} else {
if (inpVal != '') { // overrule: normal field, normal regexp, left empty
regexp = new RegExp(obj_regexp.value, ['g']);
regexp = inpVal.match(regexp);
}
}
if (regexp == null) {
newclass = newclass + ' cf_error';
all_valid = false;
if (!last_one && objColl[i].id != '')
last_one = objColl[i].id;
custom_error = check_for_customerr(objColl[i].id);
}
}
}
}
// write out all custom errors
if (show_err_ins === 'y')
write_customerr();
if (all_valid) {
document.getElementById('sendbutton' + no).style.cursor = "progress";
if (directFormSubmission) {
return true;
} else {
document.getElementById('sendbutton' + no).disabled = true;
cforms_submitform(no);
}
}
var call_err = function (no, err, custom_error) {
// temp. turn send button back on
document.getElementById('sendbutton' + no).style.cursor = "auto";
document.getElementById('sendbutton' + no).disabled = false;
if (custom_error != '')
custom_error = '<ol>' + custom_error + '</ol>';
err = decodeURI(err) + custom_error;
var stringXHTML = err.replace(/(\r\n)/g, '<br />');
var msgbox = 'usermessage' + no;
var ucm = (parseInt(no) > 1) ? ' failure' + no : '';
if (document.getElementById(msgbox + 'a'))
document.getElementById(msgbox + 'a').className = "cf_info failure" + ucm;
if (document.getElementById(msgbox + 'b'))
document.getElementById(msgbox + 'b').className = "cf_info failure" + ucm;
doInnerXHTML(msgbox, stringXHTML.replace(/\\/g, ""));
};
if (!all_valid) {
call_err(no, document.getElementById('cf_failure' + no).value, custom_error);
}
return false;
}
jQuery(function () {
jQuery('form.cform[id^="cforms"][id$="form"]').submit(function (ev) {
var id = jQuery(ev.target).attr('id');
var no = /^cforms(\d*)form$/.exec(id)[1];
var direct = jQuery(ev.target).hasClass('cformsdirect');
return cforms_validate(no, direct);
});
});