summaryrefslogtreecommitdiffhomepage
path: root/plugins/webbox/html/webbox.js
blob: a85c0c642b132c4c8899add8ece70318896d6d28 (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
var currentDir = "/";
var listElements;
var numberOfSelectedRows = 0;
var username = "notaname";
var password = "password";

function relativePath(path) {
 if (path.startsWith("/"))
  return path.substr(1);
 else
  return path;
}

function clearContents() {
	var result = "<table class=\"list\">";
	// empty list
	result += "</table>"
	
	var listElement = document.getElementById("list");

	listElement.innerHTML = result;
}

function loadContents(dir) {
	numberOfSelectedRows = 0;

	var xhr = new XMLHttpRequest();

	xhr.onreadystatechange = function() {
		if (this.readyState != 4 || this.status != 200) {
			return;
		}

		var list = xhr.responseXML;
		listElements = list.getElementsByTagName("listentry");
		
		var result = "<div class=\"fullwidth tooltip\"><table class=\"list\">";

		if (listElements.length == 0) {
			result += "<tr><td class=\"type\"></td><td class=\"name\">(empty)</td></tr>";
		} else {
			for (var i = 0; i < listElements.length; i++) {
				var type = listElements[i].getAttribute("type");
				if (type == "file") {
					type = "<img src=\"webbox-html/file.png\"/>";
				} else if (type == "dir") {
					type = "<img src=\"webbox-html/directory.png\"/>";
				} else {
					type = "";
				}

                                var selector = "<td class=\"selector\"></td>";
				if (listElements[i].textContent != "..") {
					selector = "<td class=\"selector\" onclick=\"toggleSelection('" + listElements[i].textContent + "')\">&#9744;</td>";
				}

				var mouseupdown =
					"onmousedown=\"entryMouseDown('" + listElements[i].textContent + "')\" " +
					"onmouseup=\"entryMouseUp('" + listElements[i].textContent + "')\"";
				result += "<tr>" +
					selector +
					"<td class=\"type\" " + mouseupdown + ">" + type + "</td>" +
					"<td class=\"name\" " + mouseupdown + ">" + listElements[i].textContent + "</td></tr>";
			}
		}

		result += "</table><div class=\"tooltiptext\">To select entry,<br/>click and hold</div></div>"

		var listElement = document.getElementById("list");

		listElement.innerHTML = result;

	}

	xhr.open("GET", relativePath(dir) + "?command=list", true);
	xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
	xhr.send();
}

// return list of file names
function getFileList() {
	var result = [];

	for (var i = 0; i < listElements.length; i++) {
		result.push(listElements[i].childNodes[0].nodeValue);
	}

	return result;
}

function getFileType(filename) {
	for (var i = 0; i < listElements.length; i++) {
		if (listElements[i].childNodes[0].nodeValue == filename) {
			return listElements[i].getAttribute("type");
		}
	}
	return "";
}

function getRow(filename) {
	var list = document.getElementById("list");
	var rows = list.getElementsByTagName("tr");

	for (var i = 0; i < rows.length; i++) {
		var nameElement = rows[i].getElementsByClassName("name")[0];
		if (nameElement.childNodes[0].nodeValue == filename) {
			return rows[i];
		}
	}
	return "";
}

// return list of files
function getSelectedFiles() {
	var result = [];
	var list = document.getElementById("list");
	var rows = list.getElementsByTagName("tr");

	for (var i = 0; i < rows.length; i++) {
		if (rows[i].classList.contains("selectedrow")) {
			result.push(rows[i].getElementsByClassName("name")[0].childNodes[0].nodeValue);
		}
	}

	return result;
}

// As long as this is 1, the mouse was pressed less than 1 second ago
var mouseShortFlag = 0;
var mouseTimeout = undefined;

function getSelection(filename) {
	var row = getRow(filename);

	if (row.classList.contains("selectedrow")) {
		return true;
	}
	return false;
}

function clearSelection(filename) {
	var row = getRow(filename);

	if (row.classList.contains("selectedrow")) {
		row.classList.remove("selectedrow");
		numberOfSelectedRows--;
	}
	
	var selectorElement = row.getElementsByClassName("selector")[0];
	selectorElement.innerHTML = "&#9744;";
}

function setSelection(filename) {
	if (filename == "..") { // don't select ".."
		return;
	}

	var row = getRow(filename);

	if (!row.classList.contains("selectedrow")) {
		row.classList.add("selectedrow");
		numberOfSelectedRows++;
	}
	
	var selectorElement = row.getElementsByClassName("selector")[0];
	selectorElement.innerHTML = "&#9745;";
}

function toggleSelection(filename) {
	if (filename == "..") { // don't select ".."
		return;
	}

	var row = getRow(filename);

	var selector_sign;

	if (row.classList.contains("selectedrow")) {
		row.classList.remove("selectedrow");
		numberOfSelectedRows--;
		selector_sign = "&#9744;"
	} else {
		row.classList.add("selectedrow");
		numberOfSelectedRows++;
		selector_sign = "&#9745;"
	}

	var selectorElement = row.getElementsByClassName("selector")[0];
	selectorElement.innerHTML = selector_sign;
}

function mouseTimeoutFunction(filename) {
	mouseShortFlag = 0;
	toggleSelection(filename);
}

function entryMouseDown(filename) {
	if (mouseTimeout !== undefined) {
		clearTimeout(mouseTimeout);
	}

	if (numberOfSelectedRows > 0) {
		toggleSelection(filename);
	} else {
		mouseShortFlag = 1;
		mouseTimeout = setTimeout(function(){ mouseTimeoutFunction(filename); }, 1000);
	}
}

function entryMouseUp(filename) {
	if (mouseTimeout !== undefined) {
		clearTimeout(mouseTimeout);
	}

	// short click: download / change dir
	if (mouseShortFlag) {
		var type = getFileType(filename);
		if (type == "file") {
			download(filename);
		} else if (type == "dir") {
			if (filename == "..") {
				if (!currentDir.includes("/")) {
					// error: this shouldn't happen
					alert("Bad path " + currentDir + " for " + filename);
					return;
				}
				currentDir = currentDir.substr(0, currentDir.lastIndexOf("/"));

				if (currentDir == "") {
					currentDir = "/";
				}

				setCurrentDir(currentDir);
				return;
			}

			if (!currentDir.endsWith("/")) {
				currentDir += "/";
			}
			setCurrentDir(currentDir + filename);
		}
	}

	mouseShortFlag = 0;
}

function showDialog() {
	document.getElementById("greyout").style.display = 'block';
	document.getElementById("dialogwindow").style.display = 'block';
	document.getElementById("okbutton").focus();
}

function hideDialog() {
	document.getElementById("greyout").style.display = 'none';
	document.getElementById("dialogwindow").style.display = 'none';
}

function showMenu() {
	document.getElementById("greyout").style.display = 'block';
	document.getElementById("menuwindow").style.display = 'block';
}

function hideMenu() {
	document.getElementById("greyout").style.display = 'none';
	document.getElementById("menuwindow").style.display = 'none';
}

// if readOnly = "1", disable respective controls since this setting is global and constant
function prepareReadOnly(readOnly) {
	if (readOnly == "1") {
		var writecommands = document.getElementsByClassName("writecommand");
		for (var i = 0; i < writecommands.length; i++) {
			writecommands[i].style.display = "none";
		}
	}
}

function login(title) {
	showDialog();

	document.getElementById("okbutton").onclick = function() {
		// restore dialog buttons
		document.getElementById("cancelbutton").style.display = "block";
		document.getElementById("okbutton").childNodes[0].nodeValue = "OK";
		hideDialog();

		username = document.getElementById("loginusername").value;
		password = document.getElementById("loginpassword").value;
		
		initMainpage();
	}

	// rearrange dialog buttons
	document.getElementById("cancelbutton").style.display = "none";
	document.getElementById("okbutton").childNodes[0].nodeValue = "Login";
	
	document.getElementById("dialog").innerHTML = document.getElementById("login-dialog").innerHTML;
	
	document.getElementById("logintitle").childNodes[0].nodeValue = title;
	
	document.getElementById("loginusername").focus();

	document.getElementById("loginusername").onkeydown = function(evt) {
		if (evt.key == "Enter") {
			document.getElementById("okbutton").click();
		}
	}
	
	document.getElementById("loginpassword").onkeydown = function(evt) {
		if (evt.key == "Enter") {
			document.getElementById("okbutton").click();
		}
	}
}

function initMainpage() {
	// load title
	var xhrTitle = new XMLHttpRequest();

	xhrTitle.onreadystatechange = function() {
		if (this.readyState != 4) {
			return;
		}
		if (this.status == 401) { // login error: goto login page
			var authheader = this.getResponseHeader("WWW-Authenticate");
			var title = "Webbox";
			// For web servers with standard AUTH BASIC, triggering problems in
			// client browsers, popping up the browser's "Authenticate" window
			// but we want our own
			if (authheader.startsWith("Basic realm=\"") && authheader.endsWith("\"")) {
				title = authheader.substr(13, authheader.length - 14);
			} else
			// Fixed up Apache server
			if (authheader.startsWith("SR_Basic realm=\"") && authheader.endsWith("\"")) {
				title = authheader.substr(16, authheader.length - 17);
			}

			// enable logout function if logging in
			document.getElementById("logoutcommand").style.display = "table-row";

			login(title);
			return;
		} else
		if (this.status != 200) {
			document.getElementsByClassName("title")[0].innerHTML = "HTTP error";
			return;
		}

		var serverInfo = xhrTitle.responseXML;
		var title = serverInfo.getElementsByTagName("title")[0].textContent;
		document.getElementsByClassName("title")[0].innerHTML = title;

		var readOnly = serverInfo.getElementsByTagName("readonly")[0].textContent;
		prepareReadOnly(readOnly);

		// if successful: continue loading
		initMainpage2();
	}

	xhrTitle.open("GET", "?command=server-info", true);
	xhrTitle.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
	xhrTitle.send();

}

// deferred initialization after successful login
function initMainpage2() {
	// fill file list initially
	setCurrentDir("/");

	// load footer
	var xhrFooter = new XMLHttpRequest();

	xhrFooter.onreadystatechange = function() {
		if (this.readyState != 4 || this.status != 200) {
			return;
		}
		document.getElementsByClassName("footer")[0].innerHTML = xhrFooter.responseText;
	}

	xhrFooter.open("GET", "?command=version", true);
	xhrFooter.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
	xhrFooter.send();
	
	// default action for "Cancel" button: hide dialog window
	document.getElementById("cancelbutton").onclick = hideDialog;

	// on click outside of menu, close menu
	document.getElementById("greyout").onclick = function() {
		hideDialog();
		hideMenu();
	}
	
	// on Escape, globally hide dialog and menu window
	document.onkeydown = function(evt) {
		if (evt.key == "Escape") {
			hideDialog();
			hideMenu();
		}
	}
}

function setCurrentDir(newDir) {
	currentDir = newDir;
	loadContents(newDir);

	var menu = document.getElementsByClassName("menu")[0];
	var firsttd = menu.getElementsByClassName("firsttd")[0];
	firsttd.innerHTML = newDir;
}

function download(filename) {
	var files = getSelectedFiles();

	// if activated via menu, download directly (not as zip)
	if (files.length == 1 && filename === undefined) {
		filename = files[0];
	}

	if (filename === undefined) { // download selection as ZIP
		showDialog();

		if (files.length == 0) {
			document.getElementById("dialog").innerHTML = "No files selected.";
			document.getElementById("okbutton").onclick = hideDialog;
			return;
		}

		document.getElementById("dialog").innerHTML = document.getElementById("download-zip-dialog").innerHTML;

		document.getElementById("okbutton").onclick = function() {
			hideDialog();

			// send info request for files
			var xhr = new XMLHttpRequest();

			xhr.onreadystatechange = function() {
				if (this.readyState != 4 || this.status != 200) {
					return;
				}

				var a = document.getElementById("download-a");
				a.setAttribute("download", "webbox-download.zip");
				var file = new Blob([this.response]);
				a.href = window.URL.createObjectURL(file);
				a.click();
				progressOff();
			}

			progressOn();
			var files = getSelectedFiles();
			var parser = new DOMParser();
			var xmlDocument = parser.parseFromString("<files></files>", "text/xml");
			var filesElement = xmlDocument.getElementsByTagName("files")[0];

			for (var i = 0; i < files.length; i++) {
				var fileElement = xmlDocument.createElement("file");
				fileElement.appendChild(document.createTextNode(files[i]));
				filesElement.appendChild(fileElement);
			}

			xhr.open("POST", relativePath(currentDir) + "?command=download-zip", true);
			xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
			xhr.setRequestHeader("Content-type", "text/xml");
			xhr.responseType = 'blob';
			xhr.send(xmlDocument);
		}
	} else { // single file download
		var xhr = new XMLHttpRequest();

		xhr.onreadystatechange = function() {
			if (this.readyState != 4 || this.status != 200) {
				return;
			}

			var a = document.getElementById("download-a");
			a.setAttribute("download", filename);
			var file = new Blob([this.response]);
			a.href = window.URL.createObjectURL(file);
			a.click();
			progressOff();
		}

		var dir = currentDir;
		if (dir != "/") {
			dir += "/"
		}
		progressOn();
		xhr.open("GET", relativePath(dir) + filename, true);
		xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
		xhr.responseType = 'blob';
		xhr.send();
	}
}

function createDir() {
	showDialog();

	// hide dialog when done
	document.getElementById("okbutton").onclick = function() {
		// send new folder request for directory
		var xhr = new XMLHttpRequest();

		xhr.onreadystatechange = function() {
			if (this.readyState != 4) {
				return;
			}
			if (this.status != 200) {
				document.getElementById("dialog").innerHTML = "HTTP error";
			} else {
				document.getElementById("dialog").innerHTML = xhr.responseText;
			}
			document.getElementById("okbutton").onclick = hideDialog;
			document.getElementById("okbutton").focus();
			loadContents(currentDir); // load new file list with new dir
		}

		var parser = new DOMParser();
		var xmlDocument = parser.parseFromString("<dirname></dirname>", "text/xml");
		var dirElement = xmlDocument.getElementsByTagName("dirname")[0];

		dirElement.appendChild(document.createTextNode(document.getElementById("newdir").value));

		xhr.open("POST", relativePath(currentDir) + "?command=newdir", true);
		xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
		xhr.setRequestHeader("Content-type", "text/xml");
		xhr.send(xmlDocument);
	}
	
	document.getElementById("dialog").innerHTML = document.getElementById("create-dir-dialog").innerHTML;
	document.getElementById("newdir").focus();

	document.getElementById("newdir").onkeydown = function(evt) {
		if (evt.key == "Enter") {
			document.getElementById("okbutton").click();
		}
	}
}

function upload() {
	var uploadfile = document.getElementById("uploadfile");

	uploadfile.click();
}

// Callback on selected and uploaded file
function onUploadFile() {
	var xhr = new XMLHttpRequest();

	xhr.onreadystatechange = function() {
		if (this.readyState != 4) {
			return;
		}

		showDialog();
		var message = "";

		if (this.status != 200) {
			message = "HTTP error";
		} else {
			if (xhr.responseText == "") {
				message = "Upload successful.";
				loadContents(currentDir); // load new file list with uploaded file
			} else {
				message = xhr.responseText;
			}
		}

		document.getElementById("dialog").innerHTML = message;
		document.getElementById("cancelbutton").style.display = "none";
		document.getElementById("okbutton").onclick = function() {
			hideDialog();
			document.getElementById("cancelbutton").style.display = "block";
		}
		document.getElementById("okbutton").focus();
		progressOff();
	}

	progressOn();

        var uploadfile = document.getElementById("uploadfile");
        var formData = new FormData();

	for (var i = 0; i < uploadfile.files.length; i++) {
        	formData.append("uploadfile", uploadfile.files[i]);
	}

        xhr.open("POST", relativePath(currentDir) + "?command=upload", true);
	xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
        xhr.send(formData);
}

function deleteItems() {
	showDialog();
	
	var files = getSelectedFiles();
	if (files.length == 0) {
		document.getElementById("dialog").innerHTML = "No files selected.";
		document.getElementById("okbutton").onclick = hideDialog;
		return;
	}

	var message = "Are sure to delete the following files and directories?<br/><br/>";

	for (var i = 0; i < files.length; i++) {
		message += files[i] + "<br/>";
	}

	document.getElementById("dialog").innerHTML = message;
	
	document.getElementById("okbutton").onclick = function() {

		var xhr = new XMLHttpRequest();

		xhr.onreadystatechange = function() {
			if (this.readyState != 4) {
				return;
			}
			if (this.status != 200) {
				document.getElementById("dialog").innerHTML = "HTTP error";
			} else {
				document.getElementById("dialog").innerHTML = xhr.responseText;
			}
			document.getElementById("okbutton").onclick = hideDialog;
			loadContents(currentDir); // load new file list with deleted items
		}

		var parser = new DOMParser();
		var xmlDocument = parser.parseFromString("<files></files>", "text/xml");
		var filesElement = xmlDocument.getElementsByTagName("files")[0];

		for (var i = 0; i < files.length; i++) {
			var fileElement = xmlDocument.createElement("file");
			fileElement.appendChild(document.createTextNode(files[i]));
			filesElement.appendChild(fileElement);
		}

		xhr.open("POST", relativePath(currentDir) + "?command=delete", true);
		xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
		xhr.setRequestHeader("Content-type", "text/xml");
		xhr.send(xmlDocument);
	}
}

function move() {
	showDialog();
	
	var files = getSelectedFiles();
	if (files.length == 0) {
		document.getElementById("dialog").innerHTML = "No files selected.";
		document.getElementById("okbutton").onclick = hideDialog;
		return;
	}

	var message = "";
	for (var i = 0; i < files.length; i++) {
		message += files[i] + "<br/>";
	}

	document.getElementById("dialog").innerHTML = document.getElementById("move-dialog").innerHTML;
	document.getElementById("movedir").focus();
	document.getElementById("movedir").onkeydown = function(evt) {
		if (evt.key == "Enter") {
			document.getElementById("okbutton").click();
		}
	}

	document.getElementById("movefiles").innerHTML = message;
	
	document.getElementById("okbutton").onclick = function() {

		var xhr = new XMLHttpRequest();

		xhr.onreadystatechange = function() {
			if (this.readyState != 4) {
				return;
			}
			if (this.status != 200) {
				document.getElementById("dialog").innerHTML = "HTTP error";
			} else {
				document.getElementById("dialog").innerHTML = xhr.responseText;
			}

			document.getElementById("okbutton").onclick = hideDialog;
			document.getElementById("okbutton").focus();
			loadContents(currentDir); // load new file list with deleted items
		}

		var parser = new DOMParser();
		var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
		var filesElement = xmlDocument.getElementsByTagName("request")[0];
			
		var targetElement = xmlDocument.createElement("target");
		targetElement.appendChild(document.createTextNode(document.getElementById("movedir").value));
		filesElement.appendChild(targetElement);

		for (var i = 0; i < files.length; i++) {
			var fileElement = xmlDocument.createElement("file");
			fileElement.appendChild(document.createTextNode(files[i]));
			filesElement.appendChild(fileElement);
		}

		xhr.open("POST", relativePath(currentDir) + "?command=move", true);
		xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
		xhr.setRequestHeader("Content-type", "text/xml");
		xhr.send(xmlDocument);
	}
}

function rename() {
	showDialog();
	
	var files = getSelectedFiles();
	if (files.length == 0) {
		document.getElementById("dialog").innerHTML = "No files selected.";
		document.getElementById("okbutton").onclick = hideDialog;
		return;
	}
	if (files.length > 1) {
		document.getElementById("dialog").innerHTML = "Only one file can be renamed at once.";
		document.getElementById("okbutton").onclick = hideDialog;
		return;
	}

	document.getElementById("renameold").innerHTML = files[0];

	document.getElementById("dialog").innerHTML = document.getElementById("rename-dialog").innerHTML;
	document.getElementById("renamenew").value = files[0];
	document.getElementById("renamenew").focus();
	document.getElementById("renamenew").onkeydown = function(evt) {
		if (evt.key == "Enter") {
			document.getElementById("okbutton").click();
		}
	}

	document.getElementById("okbutton").onclick = function() {

		var xhr = new XMLHttpRequest();

		xhr.onreadystatechange = function() {
			if (this.readyState != 4) {
				return;
			}
			if (this.status != 200) {
				document.getElementById("dialog").innerHTML = "HTTP error";
			} else {
				document.getElementById("dialog").innerHTML = xhr.responseText;
			}

			document.getElementById("okbutton").onclick = hideDialog;
			document.getElementById("okbutton").focus();
			loadContents(currentDir); // load new file list with deleted items
		}

		var parser = new DOMParser();
		var xmlDocument = parser.parseFromString("<request></request>", "text/xml");
		var filesElement = xmlDocument.getElementsByTagName("request")[0];
			
		var oldnameElement = xmlDocument.createElement("oldname");
		oldnameElement.appendChild(document.createTextNode(document.getElementById("renameold").childNodes[0].nodeValue));
		filesElement.appendChild(oldnameElement);
		
		var newnameElement = xmlDocument.createElement("newname");
		newnameElement.appendChild(document.createTextNode(document.getElementById("renamenew").value));
		filesElement.appendChild(newnameElement);

		xhr.open("POST", relativePath(currentDir) + "?command=rename", true);
		xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
		xhr.setRequestHeader("Content-type", "text/xml");
		xhr.send(xmlDocument);
	}
}

// File info: date, size, type
function info() {
	showDialog();
	document.getElementById("cancelbutton").style.display = "none"; // hide "cancel" button in info dialog, only provide "OK"

	// hide dialog when done
	document.getElementById("okbutton").onclick = function() {
		hideDialog();
		document.getElementById("cancelbutton").style.display = "block";
	}
	
	var files = getSelectedFiles();
	if (files.length == 0) {
		document.getElementById("dialog").innerHTML = "No files selected.";
		return;
	}

	// send info request for files
	var xhr = new XMLHttpRequest();

	xhr.onreadystatechange = function() {
		if (this.readyState != 4) {
			return;
		}
		if (this.status != 200) {
			document.getElementById("dialog").innerHTML = "HTTP error";
		} else {
			document.getElementById("dialog").innerHTML = xhr.responseText;
		}
	}

	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString("<files></files>", "text/xml");
	var filesElement = xmlDocument.getElementsByTagName("files")[0];

	for (var i = 0; i < files.length; i++) {
		var fileElement = xmlDocument.createElement("file");
		fileElement.appendChild(document.createTextNode(files[i]));
		filesElement.appendChild(fileElement);
	}

	xhr.open("POST", relativePath(currentDir) + "?command=info", true);
	xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
	xhr.setRequestHeader("Content-type", "text/xml");
	xhr.send(xmlDocument);
}

// select all files, except if all are selected, unselect all
function selectAll() {
	var files = getFileList();
	
	var allSelected = true;

	for (var i = 0; i < files.length; i++) {
		if (getSelection(files[i]) === false && files[i] != "..") {
			allSelected = false;
		}
	}

	for (var i = 0; i < files.length; i++) {
		if (allSelected) {
			clearSelection(files[i]);
		} else {
			setSelection(files[i]);
		}
	}
}

function refresh() {
	loadContents(currentDir); // load new file list
}

function logout() {
	showDialog();

	document.getElementById("okbutton").onclick = function() {
		hideDialog();

		var menu = document.getElementsByClassName("menu")[0];
		var firsttd = menu.getElementsByClassName("firsttd")[0];
		firsttd.innerHTML = "/";

		clearContents();

		username = "notaname";
		password = "password";
		initMainpage();
	}

	document.getElementById("dialog").innerHTML = document.getElementById("logout-dialog").innerHTML;
}

// Progress indication

function drawDot(c, offsetx, offsety, radius, distance, phase) {
        c.beginPath();
        c.arc(distance * Math.sin(phase) + offsetx,
                - distance * Math.cos(phase) + offsety, radius, 0, 2 * Math.PI);
        c.fill();
}

function drawLogo(canvasid, rotation) {
        var canvas = document.getElementById(canvasid);

        var height = canvas.height;
        var width = canvas.width;
        var radius = height * 0.1;
        var distance = height * 0.1 * 4 / 3;
        
        var c = canvas.getContext("2d");

        c.clearRect(0, 0, width, height);
        c.fillStyle = "#1132A7";
        drawDot(c, width / 2, height / 2, radius, distance, rotation);
        drawDot(c, width / 2, height / 2, radius, distance, rotation + 2 * Math.PI / 3);
        drawDot(c, width / 2, height / 2, radius, distance, rotation + 2 * Math.PI / 3 * 2);
        drawDot(c, width / 2, height / 2, radius, distance * 2, rotation + Math.PI);
        drawDot(c, width / 2, height / 2, radius, distance * 2, rotation + Math.PI + 2 * Math.PI / 3);
        drawDot(c, width / 2, height / 2, radius, distance * 2, rotation + Math.PI + 2 * Math.PI / 3 * 2);
}

var progressState = 0;

function drawAni(timeStamp) {
        var rotation = timeStamp / 1000 * 6;

        drawLogo("progresscanvas", rotation);

	if (progressState == 1) {
	        requestAnimationFrame(drawAni);
	}
}

function progressOn() {
	progressState = 1;
	document.getElementById("progresswindow").style.display = "block";
        requestAnimationFrame(drawAni);
}

function progressOff() {
	progressState = 0;
	document.getElementById("progresswindow").style.display = "none";
}