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