1function EncryptDialog(textArea) {
2	this.visible  = true;
3	this.textArea = textArea;
4	this.sack     = new sack(DOKU_BASE + "lib/exe/ajax.php");
5
6	// read cached password
7	if ((JSINFO['crypto_cache_password'] == 1) && (JSINFO['crypto_password'] != undefined)) {
8		this.password = ' value="' + JSINFO['crypto_password'] + '"';
9	} else {
10		this.password = "";
11	}
12
13	// create HTML Structure
14	this.dialog = document.createElement('div');
15	this.dialog.id = 'encrypt_dialog';
16	this.dialog.className  = 'picker cryptodialog';
17	this.dialog.style.top  = (findPosY(textArea)+20)+'px';
18	this.dialog.style.left = (findPosX(textArea)+80)+'px';
19
20	this.textArea.form.parentNode.appendChild(this.dialog);
21
22	this.dialog.innerHTML =
23             '<div id="encrypt_dialog_header" class="cryptoheader">'+
24             ' <img src="' + DOKU_BASE + 'lib/images/close.png" width="16" height="16" align="right" alt="" id="encrypt_dialog_close" class="close"/>' +
25	     LANG.plugins.crypto['encrypt_dialog_title'] +
26	     '</div>' +
27             '<div>' +
28	     ' <table>' +
29     	     '  <tr>' +
30     	     '   <th>' + LANG.plugins.crypto['input_secret'] + ':</th>' +
31	     '   <td><input type="password" class="edit" id="encrypt_dialog_password1"' + this.password + '/></td>' +
32	     '  </tr>' +
33     	     '  <tr>' +
34     	     '   <th>' + LANG.plugins.crypto['repeat_secret'] + ':</th>' +
35	     '   <td><input type="password" class="edit" id="encrypt_dialog_password2"' + this.password + '/></td>' +
36	     '  </tr>' +
37	     '  <tr>' +
38     	     '   <td colspan="2"><button id="encrypt_dialog_button_encrypt" class="toolbutton">' + LANG.plugins.crypto['encrypt_button'] + '</button></td>' +
39	     '  </tr>' +
40     	     ' </table>' +
41	     '</div>';
42
43	// attach event handlers
44	drag.attach(this.dialog, $('encrypt_dialog_header'));
45	var dialog = this;
46
47	$('encrypt_dialog_password1').focus();
48	$('encrypt_dialog_password1').onkeypress = function(event) {
49		if (event.keyCode==9) {
50			$('encrypt_dialog_password2').focus();
51			return false;
52		}
53
54		if (event.keyCode==13) {
55			$('encrypt_dialog_button_encrypt').onclick();
56		}
57
58		return true;
59	};
60
61	$('encrypt_dialog_password2').onkeypress = function(event) {
62		if (event.keyCode==9) {
63			$('encrypt_dialog_button_encrypt').focus();
64			return false;
65		}
66
67		if (event.keyCode==13) {
68			$('encrypt_dialog_button_encrypt').onclick();
69		}
70
71		return true;
72	};
73
74	$('encrypt_dialog_button_encrypt').onkeypress = function(event) {
75		if (event.keyCode==9) {
76			$('encrypt_dialog_password1').focus();
77			return false;
78		}
79
80		return true;
81	};
82
83	// register destructor
84	$('encrypt_dialog_close').onclick = function() {
85		dialog.textArea.focus();
86		dialog.textArea.form.parentNode.removeChild(dialog.dialog);
87		dialog.visible  = false;
88		dialog.dialog   = null;
89		dialog.textArea = null;
90	};
91
92	$('encrypt_dialog_button_encrypt').onclick = function() {
93		var selection = getSelection(dialog.textArea);
94		var text = selection.getText();
95		if ($('encrypt_dialog_password1').value == $('encrypt_dialog_password2').value) {
96			dialog.sack.setVar("call",   "crypto_encrypt");
97			dialog.sack.setVar("secret", escape($('encrypt_dialog_password1').value));
98			dialog.sack.setVar("data",   escape(selection.getText()));
99			dialog.sack.onCompletion = function() {
100				pasteText(selection, dialog.sack.response);
101				$('encrypt_dialog_close').onclick();
102			};
103
104			// cache the password
105			if (JSINFO['crypto_cache_password'] == 1) {
106				JSINFO['crypto_password'] = $('encrypt_dialog_password1').value;
107			} else {
108				JSINFO['crypto_password'] = "";
109			}
110
111			dialog.sack.runAJAX();
112		} else {
113			alert(LANG.plugins.crypto['encrypt_secrets_dont_match']);
114		}
115	};
116};
117