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