-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
154 lines (134 loc) · 5.1 KB
/
script.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
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
// JavaScript 6th Edition
// Chapter 8
// Individual Case Project
// Author: Madeline Jones
// Date: 03/18/2018
// Filename: script.js
"use strict";
var newSurveyArray = []; // creates an unpopulated array to store survey results
var newSurveySubmission; // creates a variable to store the converted string
var waitForUser; // creates a variable to later store a timeout timer in
function createSurvey(event) {
if (event === undefined) {
event = window.event;
} // for ie
var surveyElement = event.target || event.srcElement;
var surveyName = surveyElement.value;
var fields = document.querySelectorAll("input"); // finds all values for all input types
if (surveyElement.checked) {
newSurveyArray.push(surveyName); // inserts the value of the checked box into the array
} else {
for (var i = 0; i < fields.length; i++) {
if (newSurveyArray[i] === surveyName) {
newSurveyArray.splice(i, 1); // removes the value of the unchecked box from the array
}
}
}
}
function validateName() {
var nameInput = document.getElementById("name");
var error = document.getElementById("nameError");
try {
if (nameInput.value.search(/^[a-zA-Z]+( [a-zA-Z]+)*$/)) {
throw "Name should consist of letters only.";
}
nameInput.style.background = ""; // removes red background
error.style.display = "none"; // removes error message
}
catch(msg) {
// display error message
error.style.display = "block";
error.innerHTML = msg;
// change input style
nameInput.style.background = "rgb(255,233,233)";
}
}
function validateMail() {
// var mailInput = document.getElementById("email");
// var error = document.getElementById("mailError");
var mailInput = $(this).val();
var pattern = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
try {
if
// (mailInput.value.search(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/))
(!pattern.test(mailInput)) {
throw "E-mail is not in a valid format.";
}
// mailInput.style.background = ""; // removes red background
// error.style.display = "none"; // removes error message
$("#mailError").hide();
$("#email").css("background-color", "");
}
catch(msg) {
// display error message
// error.style.display = "block";
// error.innerHTML = msg;
$("#mailError").css("display", "block");
$("#mailError").html(msg);
// change input style
// mailInput.style.background = "rgb(255,233,233)";
$("#email").css("background-color", "rgb(255,233,233)");
}
}
function convertToString() {
newSurveySubmission = newSurveyArray.toString();
} // converts the array into a string
function geoTest() {
waitForUser = setTimeout(fail, 10000);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(createMap, fail,{timeout:10000});
} else {
fail();
}
}
function createMap(position) {
clearTimeout(waitForUser);
if (position.coords) {
var Lat = position.coords.latitude;
var Lng = position.coords.longitude;
var Alt = position.coords.altitude;
document.getElementById("latitude").innerHTML = "<strong>Latitude</strong>: " + Lat;
document.getElementById("longitude").innerHTML = "<strong>Longitude</strong>: " + Lng;
document.getElementById("altitude").innerHTML = "<strong>Altitude</strong>: " + Alt;
}
var mapOptions = {
center: new google.maps.LatLng(Lat, Lng),
zoom: 10
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
}
function createEventListeners() {
var nameInput = document.getElementById("name");
var mailInput = document.getElementById("email");
if (nameInput.addEventListener) {
nameInput.addEventListener("change", validateName, false);
mailInput.addEventListener("change", validateMail, false);
} else if (nameInput.attachEvent) {
nameInput.attachEvent("onchange", validateName);
mailInput.attachEvent("onchange", validateMail);
}
var menu = document.getElementsByName("menu");
if (menu[0].addEventListener) {
for (var i = 0; i < menu.length; i++) {
menu[i].addEventListener("change", createSurvey, false);
}
} else if (menu[0].attachEvent) {
for (var i = 0; i < menu.length; i++) {
menu[i].attachEvent("onchange", createSurvey);
} // runs the createSurvey function when a checkbox is checked or unchecked
}
var button = document.getElementById("submitBtn");
if (button.addEventListener) {
button.addEventListener("click", convertToString, false);
} else if (button.attachEvent) {
button.attachEvent("onclick", convertToString);
} // runs the convertToString function when the submit button is clicked
}
function fail() {
document.getElementById("map").innerHTML = "Unable to access your location.";
} // tells the user that the location check has failed
if (window.addEventListener) {
window.addEventListener("load", createEventListeners, false);
} else if (window.attachEvent) {
window.attachEvent("onload", createEventListeners);
} // loads all event listeners when page is loaded