forked from saagar/hcs-mailman-signup-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
104 lines (92 loc) · 3.35 KB
/
scripts.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
93
94
95
96
97
98
99
100
101
102
103
104
$(document).ready(function(){
$('#submit').click(subscribe);
$('#close').click(resetForm);
$('.mlist').click(
function(event){
if($(event.target).hasClass('icon-ok')){
$(event.target).parent().parent().toggleClass('btn-success');
$(event.target).parent().toggle();
}
else if($(event.target).hasClass('listname')){
$(event.target).parent().toggleClass('btn-success');
$(event.target).parent().children('.icons').toggle();
}
else{
$(event.target).toggleClass('btn-success');
$(this).children('.icons').toggle();
}
}
);
});
function validEmail(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};
function resetForm(){
$('#email').val('');
$('#fullname').val('');
$('#noneSelectedError').html('');
$('#badEmailError').html('');
selectedLists = $('.mlist.active');
selectedLists.each(function(){
$(this).removeClass('btn-success');
$(this).removeClass('active');
$(this).children('.icons').toggle();
});
}
function subscribe() {
var email = $('#email').val();
if (!validEmail(email)){
$('#badEmailError').html('<div id="badEmail" class="alert alert-error"> <a class="close" data-dismiss="alert">×</a><strong>Error! </strong>Invalid email, you noob.</div>');
return;
}
var fullname = $('#fullname').val();
var paramsArray = {
"email" : email,
"fullname":fullname
};
$.ajax({
type: "POST",
url: "backup.php",
data: "params="+JSON.stringify(paramsArray),
dataType: 'json',
success: function(data){
//win with great success
},
//fail silently.
error: function(data){
//alert("/cry");
}
});
var lists = [];
selectedLists = $('.mlist.active');
if (selectedLists.length < 1) {
$('#noneSelectedError').html('<div id="badEmail" class="alert alert-error"> <a class="close" data-dismiss="alert">×</a><strong>Error! </strong>Select at least one list.</div>');
return
}
else
{
selectedLists.each(function(){lists.push($(this).children('.listname').html());});
}
for (var i in lists) {
listName = lists[i].toLowerCase();
var url = "https://lists.hcs.harvard.edu/mailman/subscribe/hcs-"+listName;
var data = "email="+email+"&fullname="+fullname;
$.ajax({
type: "POST",
url: url,
data: data,
dataType: 'json',
success: function(data){
// alert("nomnom");
// alert(data);
},
//this fails by default
error: function(data){
//alert("/cry");
}
});
}
$('#myModal').modal('toggle');
resetForm();
};