-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpLink.js
75 lines (75 loc) · 2.84 KB
/
mpLink.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
var emailSubject = ""; // Enter your desired subject line (optional)
var emailBody = ""; // Enter the default email body (optional)
var emailCCs = ""; // Enter comma-separated emails you want to be CC'd (optional)
function FindMP() {
var pc = document.getElementById("pcInput").value;
if (!IsValidCanadianPostalCode(pc)) {
Message("Please enter a valid Canadian postal code.");
}
else {
pc = pc.replace(/\s/g, '').replace('-', '');
Message("Finding MP for: " + pc + "...");
document.getElementById("pcForm").hidden = true;
GetMP(pc.toUpperCase());
}
return true;
}
function ResetForm() {
Message("");
document.getElementById("pcForm").hidden = false;
document.getElementById("pcInput").value = "";
document.getElementById("MPInfo").innerHTML = "";
document.getElementById("EmailLink").innerHTML = "";
}
function GetMP(pc) {
$.getJSON('https://represent.opennorth.ca/postcodes/' + pc + '?callback=?', function (data) {
var reps = data["representatives_centroid"];
var repName;
var repEmail;
var repParty;
var repDistrict;
for (var x in reps) {
if (reps[x]["elected_office"] == "MP") {
repName = reps[x]["name"];
repEmail = reps[x]["email"];
repParty = reps[x]["party_name"];
repDistrict = reps[x]["district_name"];
}
}
if (repName != "" && repEmail != "") {
document.getElementById("MPInfo").innerHTML =
"<b>Your District: </b>" + repDistrict + "<br>" +
"<b>MP Name: </b>" + repName + "<br>" +
"<b>Party: </b>" + repParty + "<br>" +
"<b>MP Email: </b>" + repEmail;
document.getElementById("EmailLink").innerHTML =
"<a href='mailto:" + repEmail +
"?subject=" + encodeURIComponent(emailSubject) +
"&body=" + encodeURIComponent(emailBody) +
"&cc=" + encodeURIComponent(emailCCs) +
"'>Click here to email your MP!</a>";
Message("Success! We found your MP: ");
}
else {
Message("Couldn't find the name and email address of your MP. Please try a different postal code.");
}
});
}
function Message(msg) {
document.getElementById("out").innerHTML = msg;
}
function IsValidCanadianPostalCode(pc) {
var regex = /^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][ -]?\d[ABCEGHJ-NPRSTV-Z]\d$/i;
var match = regex.exec(pc);
if (match) {
if ((pc.indexOf("-") !== -1 || pc.indexOf(" ") !== -1) && pc.length == 7) {
return true;
}
else if ((pc.indexOf("-") == -1 || pc.indexOf(" ") == -1) && pc.length == 6) {
return true;
}
}
else {
return false;
}
}