-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact.js
92 lines (82 loc) · 2.42 KB
/
contact.js
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
form = document.getElementById('contact_form');
form.addEventListener('submit', validate);
error_div = document.getElementById('error_div');
error_list = [];
name_error = 'Name field is required.';
message_error = 'Message field is required.';
reason_for_contact_error = 'Please select at least one reason for contact.';
month_error = 'Please select month of purchase.'
submitted_div = document.getElementById('submitted_div');
name = '';
message = '';
reasons = [];
function validate(event) {
error_div.innerHTML = '';
submitted_div.innerHTML = '';
reasons = [];
if ($('#name').val() == '') {
error_list.push(name_error);
}
else {
name = $('#name').val();
}
if ($('#message').val() == '') {
error_list.push(message_error);
}
else {
message = $('#message').val();
}
checkboxes = $('input[type=checkbox]');
for (i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
reasons.push(checkboxes[i].value);
}
}
if (reasons.length <= 0) {
error_list.push(reason_for_contact_error);
}
if ($('#past_order').is(':checked') && $('#how_long').val() == 'Select Month') {
error_list.push(month_error);
}
event.preventDefault();
if (error_list.length > 0) {
print_errors();
}
else {
submitted();
}
}
function print_errors() {
for (i = 0; i < error_list.length; i++) {
error_div.innerHTML += '** ' + error_list[i] + '<br>';
}
error_list = [];
window.scrollTo(0, 0);
}
function submitted() {
submit_txt = name + ', we appreciate your message regarding:<br>';
for (i = 0; i < reasons.length; i++) {
submit_txt += ' ' + (i + 1) + ' - ' + reasons[i] + '<br>';
}
submit_txt += 'We will get back to you shortly!'
submitted_div.innerHTML = submit_txt;
document.getElementById('submit').scrollIntoView();
}
$('#past_order').click(check_if_past);
br_list = $('.if_past');
function check_if_past(event) {
if (event.target.checked) {
$('#how_long').show();
$('label[for="how_long"]').show();
for (let i = 0; i < br_list.length; i++) {
$(br_list[i]).show();
}
}
else {
$('#how_long').hide();
$('label[for="how_long"]').hide();
for (let i = 0; i < br_list.length; i++) {
$(br_list[i]).hide();
}
}
}