-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
253 lines (223 loc) · 8.02 KB
/
upload.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
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
function init()
{
// The Other OSI-Approved form fields will show up by default, in case
// someone has JavaScript disabled. So, instead, we hide them with JavaScript
// initially.
for (var i = 1; i <= config_upload_maxFiles; i++)
handleLicenseSelector(i);
}
// Run our init() function on page load
window.onload=init;
function handleLicenseSelector(uploadIndex)
{
var selected = $('licenseselector'+uploadIndex).value;
// If they did not choose "Other OSI-Approved", just show them the license
// selector
if (selected != 255)
{
$('otherlicense'+uploadIndex).className = 'hide';
$('licensename'+uploadIndex).disabled = true;
$('licenseurl'+uploadIndex).disabled = true;
$('licensetext'+uploadIndex).disabled = true;
}
else
{
$('licensename'+uploadIndex).disabled = false;
$('licenseurl'+uploadIndex).disabled = false;
$('licensetext'+uploadIndex).disabled = false;
$('otherlicense'+uploadIndex).className = '';
}
}
// Used to pop up a link to the currently selected license
function showLicense(uploadIndex)
{
var selected = $('licenseselector'+uploadIndex).value;
// Make sure we have the current license
if (!config_licenses || !config_licenses[selected]) return;
// Open the popup
doPopup(config_licenses[selected][1], 'license');
}
function validateForm()
{
// Error output
var output = Array();
// If there was an error, this will be set to true. We won't bail out of the
// script until everything was validated so that we can highlight every error
var isError = false;
// Keep track of how many images the user wants to upload
var imageUploads = 0;
// Check each upload
for (var i = 1; i <= config_upload_maxFiles; i++)
{
var file = $('file'+i);
if (file.value.length > 0)
{
// They filled in the file upload box, so increment our counter
imageUploads++;
// TODO: Look for a .png extension
// Verify they filled in an author field
var authorname = $('authorname'+i);
// Length is 0, they didn't fill in a value
if (authorname.value.length == 0) {
isError = true;
output.push(lang['errorAuthornameInvalid'].replace("%ID%", i));
authorname.className = 'invalid';
}
else authorname.className = '';
// Do some checks on the license selector.
// A value of 0 == "I stole this"
// A value of 255 == "Other OSI-Approved"
var licenseselector = $('licenseselector'+i);
if (licenseselector.value == 0)
{
isError = true;
output.push(lang['errorLicenseselectorInvalid'].replace("%ID%", i));
licenseselector.className = 'invalid';
} // if (licenseselector.value == 0)
else if (licenseselector.value == 255)
{
var licensename = $('licensename'+i);
var licenseurl = $('licenseurl'+i);
var licensetext = $('licensetext'+i);
// Assume they provide a license name, and either a license url or
// license text
licenseselector.className = '';
// If there is no license name, mark the selector and the name invalid
if (licensename.value.length == 0)
{
isError = true;
output.push(lang['errorOtherlicensenameInvalid'].replace("%ID%", i));
licenseselector.className = 'invalid';
licensename.className = 'invalid';
}
else licensename.className = '';
// If there is no license url or text, mark the selector and the url and
// text fields invalid
if (licenseurl.value.length == 0 && licensetext.value.length == 0)
{
isError = true;
output.push(lang['errorOtherlicenseurlandtextInvalid'].replace("%ID%", i));
licenseselector.className = 'invalid';
licenseurl.className = 'invalid';
licensetext.className = 'invalid';
}
else
{
licenseurl.className = '';
licensetext.className = '';
}
} // else if (licenseselector.value == 255)
else licenseselector.className = '';
// They must also a confirm their image follows the TOS
var confirm = $('confirm'+i);
if (!confirm.checked)
{
isError = true;
output.push(lang['errorConfirmInvalid'].replace("%ID%", i));
$('confirmlabel'+i).className = 'invalid';
}
else $('confirmlabel'+i).className = '';
} // if (file.value.length > 0)
}
// If the user didn't fill in any of the upload boxes, we have nothing to do
if (imageUploads == 0)
{
output.push(lang['errorNoFilesSpecified']);
isError = true;
}
// Check uploader portion
var uploaderfirstname = $('uploaderfirstname');
var uploaderlastname = $('uploaderlastname');
var uploaderemail = $('uploaderemail');
var confirmtos = $('confirmtos');
var confirmaccurate = $('confirmaccurate');
// Check uploader first name
if (uploaderfirstname.value.length == 0)
{
isError = true;
output.push(lang['errorUploaderfirstnameInvalid']);
uploaderfirstname.className = 'invalid';
}
else uploaderfirstname.className = '';
// Check uploader last name
if (uploaderlastname.value.length == 0)
{
isError = true;
output.push(lang['errorUploaderlastnameInvalid']);
uploaderlastname.className = 'invalid';
}
else uploaderlastname.className = '';
// Verify the first name doesn't start with 's' and that the last name isn't
// 'ubmitimages'
if (uploaderfirstname.value.substr(0, 1).toLowerCase() == "s" && uploaderlastname.value.toLowerCase() == "ubmitimages")
{
isError = true;
output.push(lang['errorUploaderfirstnameInvalid']);
output.push(lang['errorUploaderlastnameInvalid']);
uploaderfirstname.className = 'invalid';
uploaderlastname.className = 'invalid';
}
// Check uploader email address
var emailreg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9\-\.])+\.([A-Za-z]{2,4})$/;
if (uploaderemail.value.length == 0 || emailreg.test(uploaderemail.value) == false)
{
isError = true;
output.push(lang['errorUploaderemailInvalid']);
uploaderemail.className = 'invalid';
}
else uploaderemail.className = '';
// Check overall TOS confirmation
if (!confirmtos.checked)
{
isError = true;
output.push(lang['errorConfirmtosInvalid']);
$('confirmtoslabel').className = 'invalid';
}
else $('confirmtoslabel').className = '';
// Check confirmation to accuracy
if (!confirmaccurate.checked)
{
isError = true;
output.push(lang['errorConfirmaccurateInvalid']);
$('confirmaccuratelabel').className = 'invalid';
}
else $('confirmaccuratelabel').className = '';
var erroroutput = $('erroroutput');
for (var a = 0; a < erroroutput.childNodes.length; a++)
{
if (erroroutput.childNodes[a].nodeName == "LEGEND")
erroroutput.childNodes[a].textContent = (isError)?lang['errors']:lang['pleasewait'];
else if (erroroutput.childNodes[a].nodeName == "UL")
{
// We'll be using this item a few times here, so store it in a short name
var list = erroroutput.childNodes[a];
// First step, remove all the child elements from our UL.
for (var b = 0; b < list.childNodes.length; x++)
list.removeChild(list.childNodes[b]);
if (isError)
{
// Add the error output to the unordered list
for (var x = 0; x < output.length; x++)
{
var item = document.createElement("LI");
// Since we may include HTML in our messages, use innerHTML instead of
// using textContent.
item.innerHTML = output[x];
list.appendChild(item);
}
}
else
{
var item = document.createElement("LI");
// Since we may include HTML in our messages, use innerHTML instead of
// using textContent.
item.innerHTML = lang['pleasewaituploading'];
list.appendChild(item);
}
}
}
// Show the error output fieldset and the horizontal-rule above it
erroroutput.style.display = '';
$('errorhr').style.display = '';
return !isError;
}