js bug
[old-projects.git] / philter / philter / philter.js
1 /********************************************************************************
2 * include/philter.js : philter javascript functions
3 * ------------------
4 *
5 * This file is part of the philter distribution
6 * Copyright: See COPYING files that comes with this distribution
7 ********************************************************************************/
8
9 function text_onfocus(object,val) {
10 if(object.value == val)
11 object.value = '';
12 }
13
14 function text_onblur(object,val) {
15 if(object.value == '')
16 object.value = val;
17 }
18
19 function cleanChilds(Node, nbChilds) {
20 while(Node.childNodes.length>nbChilds)
21 Node.removeChild(Node.childNodes[nbChilds]);
22 }
23
24 function getElement(obj) {
25 if(document.getElementById)
26 return document.getElementById(obj);
27 if(document.all)
28 return document.all[obj];
29 return false;
30 }
31
32 function del_onclick(val) {
33 if(window.confirm("Do you really want to delete this email address ?")) {
34 getElement('emailsDel').value = val;
35 document.forms['emails'].submit();
36 }
37 }
38
39 /********** ORDER FORM FUNCTIONS **********/
40
41 function order_up() {
42 form = document.forms['order'];
43 select = getElement('orderSelect');
44
45 if(select.selectedIndex>1) {
46 i = select.selectedIndex;
47 value1 = select.options[i].value;
48 text1 = select.options[i].text;
49 value2 = select.options[i-1].value;
50 text2 = select.options[i-1].text;
51
52 select.options[i] = new Option(text2,value2,false,false);
53 select.options[i-1] = new Option(text1,value1,false,false);
54 select.selectedIndex = i-1;
55
56 form.elements['order'+value1].value = i-1;
57 form.elements['order'+value2].value = i;
58 }
59 }
60
61 function order_dn() {
62 form = document.forms['order'];
63 select = getElement('orderSelect');
64
65 if(select.selectedIndex>0 && select.selectedIndex<select.length-1) {
66 i = select.selectedIndex;
67 value1 = select.options[i].value;
68 text1 = select.options[i].text;
69 value2 = select.options[i+1].value;
70 text2 = select.options[i+1].text;
71
72 select.options[i] = new Option(text2,value2,false,false);
73 select.options[i+1] = new Option(text1,value1,false,false);
74 select.selectedIndex = i+1;
75
76 form.elements['order'+value1].value = i+1;
77 form.elements['order'+value2].value = i;
78 }
79 }
80
81 function order_submit(obj) {
82 if(obj.name == 'order[submit]')
83 obj.form.elements['order[action]'].value = 'submit';
84 if(obj.name == 'order[delete]')
85 obj.form.elements['order[action]'].value = 'delete';
86 obj.form.submit();
87 }
88
89 /********** RULE FORM GLOBALS **********/
90
91 var matches_list = new Array();
92 var matches_func = new Array();
93
94 var actions_list = new Array()
95 var actions_func = new Array();
96
97 var mail_pool = new Array();
98 var filter = new Array();
99
100 var current_rule;
101
102 var actions_i = 0;
103 var matches_i = 0;
104
105 /********** RULE.ACTION FORM FUNCTIONS **********/
106
107 function createActionSelect(div,data) {
108 var i,j;
109 var select = document.createElement("select");
110 var sel = 0;
111 select.name = div.name+'[0]';
112
113 // OG: must insert select node before creating options because of IE5 bug
114 div.appendChild(select);
115 for(i=0, j=0; i<actions_list.length; i++) {
116 if(data==i) sel = j;
117 if(actions_list[i]) select.options[j++] = new Option(actions_list[i],i,false,(data==i));
118 }
119
120 select.selectedIndex = sel;
121 select.onchange = function () { changeRow(this,actions_func); };
122
123 return select;
124 }
125
126 function createActionRow(Node,data) {
127 var div = document.createElement("div");
128 div.name = 'rule[actions]['+actions_i+']';
129 actions_i++;
130 Node.appendChild(div);
131
132 var del = document.createElement("input");
133 del.setAttribute("type", "submit");
134 del.setAttribute("value", "Del");
135 del.onclick = function () { del.parentNode.parentNode.removeChild(del.parentNode); };
136 div.appendChild(del);
137
138 div.appendChild(document.createTextNode(" "));
139
140 var select = createActionSelect(div,data[0]);
141
142 actions_func[select.options[select.selectedIndex].value](div,data);
143 }
144
145 function newAction() {
146 createActionRow(getElement('actionsRow'), [-1]);
147 }
148
149 /********** RULE.MATCH FORM FUNCTIONS **********/
150
151 function createMatchSelect(div,data) {
152 var i,j;
153 var select = document.createElement("select");
154 var sel = 0;
155 select.name = div.name+'[0]';
156
157 // OG: must insert select node before creating options because of IE5 bug
158 div.appendChild(select);
159 for(i=0, j=0; i<matches_list.length; i++) {
160 if(data==i) sel = j;
161 if(matches_list[i]) select.options[j++] = new Option(matches_list[i],i,false,(data==i));
162 }
163
164 select.selectedIndex = sel;
165 select.onchange = function () { changeRow(this,matches_func); };
166
167 return select;
168 }
169
170 function createMatchRow(Node,data) {
171 var div = document.createElement("div");
172 div.name = 'rule[matches]['+actions_i+']';
173 actions_i++;
174 Node.appendChild(div);
175
176 var del = document.createElement("input");
177 del.setAttribute("type", "submit");
178 del.setAttribute("value", "Del");
179 del.onclick = function () { del.parentNode.parentNode.removeChild(del.parentNode); };
180 div.appendChild(del);
181
182 div.appendChild(document.createTextNode(" "));
183
184 var select = createMatchSelect(div,data[0]);
185
186 matches_func[select.options[select.selectedIndex].value](div,data);
187 }
188
189 function newMatch() {
190 createMatchRow(getElement('matchesRow'), [-1]);
191 }
192
193 /********** RULE FORM FUNCTIONS **********/
194
195 function changeRow(sel, funcs) {
196 var row = sel.parentNode;
197 cleanChilds(row,3);
198 funcs[sel.options[sel.selectedIndex].value](row,0);
199 }
200
201 function createRuleForm() {
202 var i;
203 var sel = getElement('orderSelect');
204 var index = sel.options[sel.selectedIndex].value;
205
206 actions_i = matches_i = 0;
207 current_rule = filter[index];
208
209 getElement('ruleId').value = index;
210 getElement('ruleName').value = current_rule.name;
211 var c = (current_rule.all ? '1' : '0');
212 getElement('ruleAll'+c).checked = true;
213 getElement('ruleBlock').checked = current_rule.block;
214
215 var mr = getElement('matchesRow');
216 cleanChilds(mr,0);
217 for(i=0; i<current_rule.matches.length; i++)
218 createMatchRow(mr, current_rule.matches[i]);
219
220 var ar = getElement('actionsRow');
221 cleanChilds(ar,0);
222 for(i=0; i<current_rule.actions.length; i++)
223 createActionRow(ar, current_rule.actions[i]);
224 }
225
226 /********************************************************************************
227 * $Id$
228 * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
229 ********************************************************************************/