ca1321acc049e84faf823e6a18d3c3fe4b9be85f
[squirrelmail.git] / templates / default / js / default.js
1 /**
2 * This array is used to remember mark status of rows in browse mode
3 *
4 * @copyright © 2005-2006 The SquirrelMail Project Team
5 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
6 * @version $Id$
7 */
8 var marked_row = new Array;
9 var orig_row_colors = new Array();
10
11 /*
12 * (un)Checks checkbox for the row that the current table cell is in
13 * when it gets clicked.
14 *
15 * @param string The (internal) name of the checkbox
16 * that should be (un)checked
17 * @param JavaScript event object The JavaScript event associated
18 * with this mouse click
19 * @param string The name of the encapsulating form
20 * @param string The (real) name of the checkbox
21 * that should be (un)checked
22 * @param string Any extra JavaScript (that will
23 * be executed herein if non-empty);
24 * must be valid JavaScript expression(s)
25 */
26 function row_click(chkboxName, event, formName, checkboxRealName, extra) {
27 var chkbox = document.getElementById(chkboxName);
28 if (chkbox) {
29 // initialize orig_row_color if not defined already
30 if (!orig_row_colors[chkboxName]) {
31 orig_row_colors[chkboxName] = chkbox.parentNode.getAttribute('bgcolor');
32 if (orig_row_colors[chkboxName].indexOf("clicked_") == 0)
33 orig_row_colors[chkboxName] = orig_row_colors[chkboxName].substring(8, orig_row_colors[chkboxName].length);
34 }
35 chkbox.checked = (chkbox.checked ? false : true);
36
37 if (extra != '') eval(extra);
38 }
39 }
40
41 /*
42 * Gets the current class of the requested row. This is a browser specific function.
43 * Code shamelessly ripped from setPointer() below.
44 */
45 function getCSSClass (theRow)
46 {
47 var rowClass;
48 // 3.1 ... with DOM compatible browsers except Opera that does not return
49 // valid values with "getAttribute"
50 if (typeof(window.opera) == 'undefined'
51 && typeof(theRow.getAttribute) != 'undefined'
52 && theRow.getAttribute('className') ) {
53 rowClass = theRow.getAttribute('className');
54 }
55 // 3.2 ... with other browsers
56 else {
57 rowClass = theRow.className;
58 }
59
60 return rowClass;
61 }
62
63 /*
64 * Sets a new CSS class for the given row. Browser-specific.
65 */
66 function setCSSClass (obj, newClass) {
67 if (typeof(window.opera) == 'undefined' && typeof(obj.getAttribute) != 'undefined' && obj.getAttribute('className') ) {
68 obj.setAttribute('className', newClass, 0);
69 }
70 else {
71 obj.className = newClass;
72 }
73 }
74
75 /*
76 * This function is used to initialize the orig_row_color array so we do not
77 * need to predefine the entire array
78 */
79 function rowOver(chkboxName) {
80 var chkbox = document.getElementById(chkboxName);
81 var rowClass, rowNum, overClass, clickedClass;
82 if (chkbox) {
83 if (!orig_row_colors[chkboxName]) {
84 rowClass = getCSSClass(chkbox.parentNode.parentNode);
85 if (rowClass.indexOf("clicked_") == 0)
86 rowClass = rowClass.substring(8, rowClass.length);
87 orig_row_colors[chkboxName] = rowClass;
88 } else {
89 rowClass = orig_row_colors[chkboxName];
90 }
91 rowNum = chkboxName.substring(chkboxName.length - 1, chkboxName.length);
92
93 /*
94 * The mouseover and clicked CSS classes are always the same name!
95 */
96 overClass = 'mouse_over';
97 clickedClass = 'clicked';
98 setPointer(chkbox.parentNode.parentNode, rowNum, 'over' , rowClass, overClass, clickedClass);
99 }
100 }
101
102 /*
103 * (un)Checks all checkboxes for the message list from a specific form
104 * when it gets clicked.
105 *
106 * @param string the id of the form where all checkboxes should be (un)checked
107 * @param boolean use fancy row coloring when a checkbox is checked
108 * @param string new color of the checked rows
109 */
110 function toggle_all(formname, fancy) {
111 var TargetForm = document.getElementById(formname);
112 var j = 0;
113 for (var i = 0; i < TargetForm.elements.length; i++) {
114 if (TargetForm.elements[i].type == 'checkbox' && TargetForm.elements[i].name.substring(0,3) == 'msg') {
115 if (fancy) {
116 array_key = TargetForm.elements[i].getAttribute('id');
117 // initialize orig_row_color if not defined already
118 if (!orig_row_colors[array_key]) {
119 rowClass = getCSSClass(TargetForm.elements[i].parentNode.parentNode);
120 if (rowClass.indexOf("clicked_") == 0)
121 rowClass = rowClass.substring(8, rowClass.length);
122 orig_row_colors[array_key] = rowClass;
123 }
124 origClass = orig_row_colors[array_key];
125 clickedClass = 'clicked';
126 setPointer(TargetForm.elements[i].parentNode.parentNode, j,'click' , origClass, origClass, clickedClass);
127 j++
128 }
129 TargetForm.elements[i].checked = !(TargetForm.elements[i].checked);
130 }
131 }
132 }
133
134 /*
135 * Sets/unsets the pointer and marker in browse mode
136 *
137 * @param object theRow the table row
138 * @param integer theRowNum the row number
139 * @param string theAction the action calling this script (over, out or click)
140 * @param string defaultClass the default background CSS class
141 * @param string mouseoverClass the CSS class to use for mouseover
142 * @param string clickedClass the CSS class to use for marking a row
143 *
144 * @return boolean whether pointer is set or not
145 */
146 function setPointer(theRow, theRowNum, theAction, defaultClass, mouseoverClass, clickedClass)
147 {
148 // 1. Pointer and mark feature are disabled or the browser can't get the
149 // row -> exits
150 if ((mouseoverClass == '' && clickedClass == '')
151 || typeof(theRow.className) == 'undefined') {
152 return false;
153 }
154
155 // 2. Verify we can get the current row or exit
156 if (typeof(document.getElementsByTagName) != 'undefined') {
157 // We are ok
158 }
159 else if (typeof(theRow) != 'undefined') {
160 // We are ok
161 }
162 else {
163 return false;
164 }
165
166 // 3. Gets the current CSS class...
167 var newClass = null;
168 var currentClass = getCSSClass(theRow);
169 if (currentClass.indexOf("clicked_") == 0)
170 currentClass = 'clicked';
171
172 // 4. Defines the new class
173 // 4.1 Current class is the default one
174 if (currentClass == ''
175 || currentClass.toLowerCase() == defaultClass.toLowerCase()) {
176 if (theAction == 'over' && mouseoverClass != '') {
177 newClass = mouseoverClass;
178 }
179 else if (theAction == 'click' && clickedClass != '') {
180 newClass = clickedClass;
181 marked_row[theRowNum] = true;
182 // deactivated onclick marking of the checkbox because it's also executed
183 // when an action (clicking on the checkbox itself) on a single item is
184 // performed. Then the checkbox would get deactived, even though we need
185 // it activated. Maybe there is a way to detect if the row was clicked,
186 // and not an item therein...
187 //document.getElementById('msg[' + theRowNum + ']').checked = true;
188 }
189 }
190 // 4.1.2 Current class is the mouseover one
191 else if (currentClass.toLowerCase() == mouseoverClass.toLowerCase()
192 && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
193 if (theAction == 'out') {
194 newClass = defaultClass;
195 }
196 else if (theAction == 'click' && clickedClass != '') {
197 newClass = clickedClass;
198 marked_row[theRowNum] = true;
199 //document.getElementById('msg[' + theRowNum + ']').checked = true;
200 }
201 }
202 // 4.1.3 Current color is the clicked one
203 else if (currentClass.toLowerCase() == clickedClass.toLowerCase()) {
204 if (theAction == 'click') {
205 newClass = (mouseoverClass != '')
206 ? mouseoverClass
207 : defaultClass;
208 marked_row[theRowNum] = false;
209 //document.getElementById('msg[' + theRowNum + ']').checked = false;
210 }
211 } // end 4
212
213 // 5. Sets the new color...
214 if (newClass) {
215 setCSSClass(theRow, newClass);
216 }
217
218 return true;
219 } // end of the 'setPointer()' function
220
221 function comp_in_new_form(comp_uri, button, myform, iWidth, iHeight) {
222 comp_uri += "&" + button.name + "=1";
223 for ( var i=0; i < myform.elements.length; i++ ) {
224 if ( myform.elements[i].type == "checkbox" && myform.elements[i].checked )
225 comp_uri += "&" + myform.elements[i].name + "=1";
226 }
227 if (!iWidth) iWidth = 640;
228 if (!iHeight) iHeight = 550;
229 var sArg = "width=" + iWidth + ",height=" + iHeight + ",scrollbars=yes,resizable=yes,status=yes";
230 var newwin = window.open(comp_uri, "_blank", sArg);
231 }
232
233 function comp_in_new(comp_uri, iWidth, iHeight) {
234 if (!iWidth) iWidth = 640;
235 if (!iHeight) iHeight = 550;
236 sArg = "width=" + iWidth + ",height=" + iHeight + ",scrollbars=yes,resizable=yes,status=yes";
237 var newwin = window.open(comp_uri , "_blank", sArg);
238 }
239
240 /*
241 * Reload the read_body screen on sending an mdn receipt
242 */
243 function sendMDN() {
244 var mdnuri=window.location+'&sendreceipt=1';
245 window.location = mdnuri;
246 }
247
248 var alreadyFocused = false;
249 function checkForm(smaction) {
250
251 if (alreadyFocused) return;
252
253 /*
254 * this part is used for setting the focus in the compose screen
255 */
256 if (smaction) {
257 if (smaction == "select") {
258 document.forms['compose'].body.select();
259 } else if (smaction == "focus") {
260 document.forms['compose'].body.focus();
261 }
262 } else {
263 /*
264 * All other forms that need to set the focus
265 */
266 var f = document.forms.length;
267 var i = 0;
268 var pos = -1;
269 while( pos == -1 && i < f ) {
270 var e = document.forms[i].elements.length;
271 var j = 0;
272 while( pos == -1 && j < e ) {
273 if ( document.forms[i].elements[j].type == 'text' || document.forms[i].elements[j].type == 'password' ) {
274 pos = j;
275 }
276 j++;
277 }
278 i++;
279 }
280 if( pos >= 0 ) {
281 document.forms[i-1].elements[pos].focus();
282 }
283 }
284 }