1 var contactSelectorAction = 'mailer-contacts';
2 var signatureLength = 0;
4 function onContactAdd() {
6 var selectorURL = '?popup=YES&selectorId=mailer-contacts';
8 urlstr = ApplicationBaseURL
10 + contactSelectorAction + selectorURL;
11 var w = window.open(urlstr, "Addressbook",
12 "width=640,height=400,resizable=1,scrollbars=0");
13 w.selector = selector;
20 function addContact(tag, fullContactName, contactId, contactName, contactEmail) {
21 if (!mailIsRecipient(contactEmail)) {
22 var neededOptionValue = 0;
24 neededOptionValue = 1;
25 else if (tag == "bcc")
26 neededOptionValue = 2;
30 var currentRow = $('row_' + counter);
31 while (currentRow && !stop) {
32 var currentValue = $(currentRow.childNodesWithTag("td")[1]).childNodesWithTag("input")[0].value;
33 if (currentValue == neededOptionValue) {
35 insertContact($("addr_" + counter), contactName, contactEmail);
38 currentRow = $('row_' + counter);
42 fancyAddRow(false, "");
43 $($("row_" + counter).childNodesWithTag("td")[0]).childNodesWithTag("select")[0].value
45 insertContact($("addr_" + counter), contactName, contactEmail);
51 function mailIsRecipient(mailto) {
52 var isRecipient = false;
55 var currentRow = $('row_' + counter);
57 var email = extractEmailAddress(mailto).toUpperCase();
59 while (currentRow && !isRecipient) {
60 var currentValue = $("addr_"+counter).value.toUpperCase();
61 if (currentValue.indexOf(email) > -1)
66 currentRow = $('row_' + counter);
73 function insertContact(inputNode, contactName, contactEmail) {
74 var value = '' + inputNode.value;
76 var newContact = contactName;
77 if (newContact.length > 0)
78 newContact += ' <' + contactEmail + '>';
80 newContact = contactEmail;
86 inputNode.value = value;
89 function toggleAttachments() {
90 var div = $("attachmentsArea");
91 if (div.style.display)
92 div.style.display = "";
94 div.style.display = "block;";
99 function updateInlineAttachmentList(sender, attachments) {
102 var div = $("attachmentsArea");
104 count = attachments.length;
108 for (var i = 0; i < count; i++) {
109 text = text + attachments[i];
110 text = text + '<br />';
113 var e = $('compose_attachments_list');
115 if (!div.style.display)
116 div.style.display = "block;";
119 div.style.display = "";
123 function validateEditorInput(sender) {
127 field = document.pageform.subject;
128 if (field.value == "")
129 errortext = errortext + labels["error_missingsubject"] + "\n";
131 if (!hasRecipients())
132 errortext = errortext + labels["error_missingrecipients"] + "\n";
134 if (errortext.length > 0) {
135 alert(labels["error_validationfailed"] + ":\n" + errortext);
142 function clickedEditorSend(sender) {
143 if (!validateEditorInput(sender))
146 var input = currentAttachmentInput();
148 input.parentNode.removeChild(input);
150 var toolbar = document.getElementById("toolbar");
151 if (!document.busyAnim)
152 document.busyAnim = startAnimation(toolbar);
154 window.shouldPreserve = true;
155 document.pageform.action = "send";
156 document.pageform.submit();
161 function currentAttachmentInput() {
164 var inputs = $("attachmentsArea").getElementsByTagName("input");
166 while (!input && i < inputs.length)
167 if ($(inputs[i]).hasClassName("currentAttachment"))
175 function clickedEditorAttach(sender) {
176 var input = currentAttachmentInput();
178 var area = $("attachmentsArea");
180 if (!area.style.display) {
181 area.setStyle({ display: "block" });
182 onWindowResize(null);
184 var inputs = area.getElementsByTagName("input");
185 var attachmentName = "attachment" + inputs.length;
186 var newAttachment = createElement("input", attachmentName,
187 "currentAttachment", null,
189 name: attachmentName },
191 Event.observe(newAttachment, "change",
192 onAttachmentChange.bindAsEventListener(newAttachment));
198 function onAddAttachment() {
199 return clickedEditorAttach(null);
202 function onAttachmentChange(event) {
203 if (this.value == "")
204 this.parentNode.removeChild(this);
206 this.addClassName("attachment");
207 this.removeClassName("currentAttachment");
208 var list = $("attachments");
209 createAttachment(this, list);
213 function createAttachment(node, list) {
214 var attachment = createElement("li", null, null, { node: node }, null, list);
215 createElement("img", null, null, { src: ResourcesURL + "/attachment.gif" },
217 Event.observe(attachment, "click", onRowClick);
219 var filename = node.value;
221 if (navigator.appVersion.indexOf("Windows") > -1)
225 var fileArray = filename.split(separator);
226 var attachmentName = document.createTextNode(fileArray[fileArray.length-1]);
227 attachment.appendChild(attachmentName);
230 function clickedEditorSave(sender) {
231 var input = currentAttachmentInput();
233 input.parentNode.removeChild(input);
235 window.shouldPreserve = true;
236 document.pageform.action = "save";
237 document.pageform.submit();
239 if (window.opener && window.open && !window.closed)
240 window.opener.refreshFolderByType('draft');
244 function onTextFocus() {
245 this.insertBefore(document.createTextNode("\r\n"),
247 if (signatureLength > 0) {
248 var length = this.getValue().length - signatureLength - 1;
249 this.setSelectionRange(length, length);
251 Event.stopObserving(this, "focus", onTextFocus);
254 function initMailEditor() {
255 var list = $("attachments");
256 $(list).attachMenu("attachmentsMenu");
257 var elements = list.childNodesWithTag("li");
258 for (var i = 0; i < elements.length; i++) {
259 Event.observe(elements[i], "click",
260 onRowClick.bindAsEventListener(elements[i]));
263 var listContent = $("attachments").childNodesWithTag("li");
264 if (listContent.length > 0)
265 $("attachmentsArea").setStyle({ display: "block" });
267 var list = $("addressList");
268 TableKit.Resizable.init(list, {'trueResize' : true, 'keepWidth' : true});
270 var textarea = $("text");
271 var textContent = textarea.getValue();
272 var sigLimit = textContent.lastIndexOf("--");
274 signatureLength = (textContent.length - sigLimit);
275 Event.observe(textarea, "focus",
276 onTextFocus.bindAsEventListener(textarea));
278 onWindowResize(null);
279 Event.observe(window, "resize", onWindowResize);
280 Event.observe(window, "beforeunload", onMailEditorClose);
283 function getMenus() {
284 return { "attachmentsMenu": new Array(null, onRemoveAttachments,
285 onSelectAllAttachments,
287 onAddAttachment, null) };
290 function onRemoveAttachments() {
291 var list = $("attachments");
292 var nodes = list.getSelectedNodes();
293 for (var i = nodes.length-1; i > -1; i--) {
294 var input = $(nodes[i]).node;
296 input.parentNode.removeChild(input);
297 list.removeChild(nodes[i]);
301 var childNodes = nodes[i].childNodes;
302 for (var j = 0; j < childNodes.length; j++) {
303 if (childNodes[j].nodeType == 3)
304 filename += childNodes[j].nodeValue;
306 var url = "" + window.location;
307 var parts = url.split("/");
308 parts[parts.length-1] = "deleteAttachment?filename=" + encodeURIComponent(filename);
309 url = parts.join("/");
310 triggerAjaxRequest(url, attachmentDeleteCallback,
316 function attachmentDeleteCallback(http) {
317 if (http.readyState == 4) {
318 if (isHttpStatus204(http.status)) {
319 var node = http.callbackData;
320 node.parentNode.removeChild(node);
323 log("attachmentDeleteCallback: an error occured: " + http.responseText);
327 function onSelectAllAttachments() {
328 var list = $("attachments");
329 var nodes = list.childNodesWithTag("li");
330 for (var i = 0; i < nodes.length; i++)
334 function onWindowResize(event) {
335 var textarea = document.pageform.text;
336 var rowheight = (Element.getHeight(textarea) / textarea.rows);
337 var headerarea = $("headerArea");
339 var attachmentsarea = $("attachmentsArea");
340 var attachmentswidth = 0;
341 if (attachmentsarea.style.display) {
342 attachmentswidth = attachmentsarea.getWidth();
343 // Resize of attachment list is b0rken under IE7
344 // fromfield = $(document).getElementsByClassName('headerField',
346 // $("attachments").setStyle({ height: (headerarea.getHeight() - fromfield.getHeight() - 10) + 'px' });
348 // var subjectfield = $(document).getElementsByClassName('headerField',
349 // $('subjectRow'))[0];
350 // var subjectinput = $(document).getElementsByClassName('textField',
351 // $('subjectRow'))[0];
353 // Resize subject field
354 // subjectinput.setStyle({ width: (window.width()
355 // - $(subjectfield).getWidth()
356 // - attachmentswidth
357 // - 4 - 30) + 'px' });
359 // Resize address fields
360 var addresslist = $('addressList');
361 addresslist.setStyle({ width: ($(this).width() - attachmentswidth - 10) + 'px' });
363 // Set textarea position
364 textarea.setStyle({ 'top': (headerarea.getHeight() + headerarea.offsetTop) + 'px' });
366 var textareaoffset = textarea.offsetTop;
368 // Resize the textarea (message content)
369 textarea.rows = Math.round((window.height() - textareaoffset) / rowheight);
372 function onMailEditorClose(event) {
373 if (window.shouldPreserve)
374 window.shouldPreserve = false;
376 if (window.opener && window.opener.open && !window.opener.closed) {
377 var url = "" + window.location;
378 var parts = url.split("/");
379 parts[parts.length-1] = "delete";
380 url = parts.join("/");
381 window.opener.deleteDraft(url);
385 Event.stopObserving(window, "beforeunload", onMailEditorClose);
388 FastInit.addOnLoad(initMailEditor);