If a country is chosen, only proposes its cities (Closes #1284).
[platal.git] / htdocs / javascript / search.js
CommitLineData
e16215c4
SJ
1/***************************************************************************
2 * Copyright (C) 2003-2011 Polytechnique.org *
3 * http://opensource.polytechnique.org/ *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************/
20
21// {{{ Page initialization
22
23var baseurl = $.plURL('search/');
e76e94fb 24var address_types = new Array('country', 'administrative_area_level_1', 'administrative_area_level_2', 'locality', 'postal_code');
4aab5061 25var address_types_count = address_types.length;
38f35d89 26var autocomplete_sub = {'country': 'locality_text'};
e16215c4
SJ
27
28function load_advanced_search(request)
29{
82f06260 30 $('.autocomplete_target').hide();
e16215c4 31 $('.autocomplete').show().each(function() {
23ccdf38
SJ
32 $(this).autocomplete({
33 source: baseurl + 'autocomplete/' + this.name,
34 select: function(event, ui) {
35 select_autocomplete(this.name, ui.item.id);
36 },
37 change: function(event, ui) {
38 if (ui.item != null && ui.item.field != null) {
39 $(this).val(ui.item.field);
40 }
41 }
82f06260 42 });
e16215c4
SJ
43 });
44
45 $('.autocomplete').change(function() { $(this).removeClass('hidden_valid'); });
46
47 if (request['country']) {
e16215c4
SJ
48 setAddress(0, 1, new Array(request['country'],
49 request['administrative_area_level_1'],
50 request['administrative_area_level_2'],
e16215c4 51 request['locality'],
e76e94fb 52 request['postal_code'])
e16215c4
SJ
53 );
54 } else {
4aab5061
SJ
55 for (var i = 1; i < address_types_count; ++i) {
56 $('tr#' + address_types[i]).hide();
e16215c4
SJ
57 }
58 }
59
82f06260 60 $(".autocomplete[name='school_text']").change(function() { changeSchool('', ''); });
e16215c4
SJ
61 changeSchool(request['school'], request['diploma']);
62
82f06260
SJ
63 $(".autocomplete_to_select").each(function() {
64 var field_name = $(this).attr('href');
e16215c4 65
82f06260
SJ
66 if ($(".autocomplete_target[name='" + field_name + "']").val()) {
67 display_list(field_name);
68 }
e16215c4 69
82f06260
SJ
70 $(this).attr('href', baseurl + 'list/' + field_name).click(function() {
71 if ($(this).attr('title') == 'display') {
72 display_list(field_name);
73 } else {
74 var value = $("select[name='" + field_name + "']").val();
75 var text_value = $("select[name='" + field_name + "'] option:selected").text();
76 $('#' + field_name + '_list').html('');
77 $(".autocomplete[name='" + field_name + "_text']").show();
78 $('#' + field_name + '_table').attr('title', 'display');
79 if (value) {
80 $(".autocomplete_target[name='" + field_name + "']").val(value);
81 $(".autocomplete[name='" + field_name + "_text']").val(text_value).addClass('hidden_valid');
82 }
83 }
e16215c4
SJ
84
85 return false;
86 });
e16215c4
SJ
87 });
88
89 $('#only_referent').change(function() { changeOnlyReferent(); });
4aab5061
SJ
90
91 $('.delete_address_component').click(function() {
92 var field_name = $(this).attr('href');
93 var hide = false;
249510f5 94 var remove = false;
4aab5061
SJ
95
96 for (var i = 1; i < address_types_count; ++i) {
97 if (field_name == address_types[i]) {
98 hide = true;
99 }
100 if (hide) {
249510f5
SJ
101 if (field_name != address_types[i]) {
102 remove = true;
103 }
104 delete_address_component(address_types[i], remove);
4aab5061
SJ
105 }
106 }
107
108 return false;
109 });
e16215c4
SJ
110}
111
82f06260
SJ
112function display_list(field_name)
113{
114 var value = $("input.autocomplete_target[name='" + field_name + "']").val();
115
116 $('#' + field_name + '_list').load(baseurl + 'list/' + field_name, {}, function(selectBox) {
117 $(".autocomplete_target[name='" + field_name + "']").val('');
118 $(".autocomplete[name='" + field_name + "_text']").hide().val('').removeClass('hidden_valid');
119 $("select[name='" + field_name + "']").val(value);
120 $('#' + field_name + '_table').attr('title', 'hide');
121 });
122}
123
e16215c4
SJ
124// }}}
125// {{{ Regexps to wipe out from search queries
126
127var default_form_values = [ /&woman=0(&|$)/, /&subscriber=0(&|$)/, /&alive=0(&|$)/, /&egal[12]=[^&]*&promo[12]=(&|$)/g, /&networking_type=0(&|$)/, /&[^&=]+=(&|$)/g ];
128
129/** Uses javascript to clean form from all empty fields */
222195aa 130function cleanForm(f, targeturl)
e16215c4
SJ
131{
132 var query = $(f).formSerialize();
133 var old_query;
134 for (var i in default_form_values) {
135 var reg = default_form_values[i];
136 if (typeof(reg) != 'undefined') {
137 do {
138 old_query = query;
139 query = query.replace(reg, '$1');
140 } while (old_query != query);
141 }
142 }
143 query = query.replace(/^&*(.*)&*$/, '$1');
144 if (query == 'rechercher=Chercher') {
145 alert("Aucun critère n'a été spécifié.");
146 return false;
147 }
ec35cec0 148 document.location = $.plURL(targeturl + '?' + query);
e16215c4
SJ
149 return false;
150}
151
152// }}}
153// {{{ Autocomplete related functions.
154
e16215c4
SJ
155function cancel_autocomplete(field, realfield)
156{
157 $(".autocomplete[name='" + field + "']").removeClass('hidden_valid').val('').focus();
158 if (typeof(realfield) != 'undefined') {
82f06260 159 $(".autocomplete_target[name='" + realfield + "']").val('');
e16215c4
SJ
160 }
161 return;
162}
163
164// when choosing autocomplete from list, must validate
23ccdf38 165function select_autocomplete(name, id)
e16215c4 166{
5c89683f 167 var field_name = name.replace(/_text$/, '');
38f35d89
SJ
168 if (autocomplete_sub[field_name] != null) {
169 $(".autocomplete[name='" + autocomplete_sub[field_name] + "']").autocomplete('option', 'source', baseurl + 'autocomplete/' + autocomplete_sub[field_name] + '/' + id);
170 }
e16215c4 171
f6ce54a7 172 // just display field as valid if field is not a text field for a list
5c89683f 173 if (field_name == name) {
23ccdf38
SJ
174 $(".autocomplete[name='" + name + "']").addClass('hidden_valid');
175 return;
e16215c4
SJ
176 }
177
4dea11cc
SJ
178 // When changing country, locality or school, open next address component.
179 if (field_name == 'country' || field_name == 'locality' || field_name == 'school') {
23ccdf38
SJ
180 if (id < 0) {
181 cancel_autocomplete(name, field_name);
182 id = '';
e16215c4
SJ
183 }
184
23ccdf38
SJ
185 if (field_name == 'school') {
186 changeSchool(id, '');
187 } else {
188 changeAddressComponents(field_name, id);
e16215c4
SJ
189 }
190
23ccdf38 191 $(".autocomplete_target[name='" + field_name + "']").attr('value', id);
f6ce54a7 192 $(".autocomplete[name='" + name + "']").addClass('hidden_valid');
23ccdf38 193 return;
e16215c4 194 }
23ccdf38
SJ
195
196 // change field in list and display text field as valid
197 if (id < 0) {
198 cancel_autocomplete(this.field, field_name);
199 return;
200 }
201
202 $(".autocomplete_target[name='" + field_name + "']").attr('value', id);
203 $(".autocomplete[name='" + name + "']").addClass('hidden_valid');
e16215c4
SJ
204}
205
206// }}}
207// {{{ Various search functions.
208
209function setAddress(i, j, values)
210{
211 var prev_type = address_types[i];
212 var next_type = address_types[j];
213 var next_list = next_type + '_list';
214
4aab5061 215 $('#' + next_list).load(baseurl + 'list/' + next_type, { previous:prev_type, value:values[i] }, function() {
e16215c4 216 if ($("select[name='" + next_type + "']").children("option").size() > 1) {
4aab5061 217 $("tr#" + next_type).show();
e16215c4 218 $("select[name='" + next_type + "']").attr('value', values[j]);
e0088da2
SJ
219 if (next_type == 'locality') {
220 $('tr#locality_text').hide();
221 $("select[name='locality_text']").attr('value', '');
222 }
4aab5061 223 if (j < address_types_count) {
e16215c4
SJ
224 setAddress(j, j + 1, values);
225 }
226 } else {
4aab5061 227 $("tr#" + next_type).hide();
e16215c4 228 $("select[name='" + next_type + "']").attr('value', '');
4aab5061 229 if (j < address_types_count) {
e16215c4
SJ
230 setAddress(i, j + 1, values);
231 }
232 }
233 });
234}
235
236function displayNextAddressComponent(i, j, value)
237{
238 var prev_type = address_types[i];
239 var next_type = address_types[j];
240 var next_list = next_type + '_list';
241
4aab5061 242 if (next_type == 'locality') {
e16215c4 243 $('tr#locality_text').hide();
82f06260 244 $("select[name='locality_text']").attr('value', '');
e16215c4
SJ
245 }
246
38f35d89
SJ
247 if (autocomplete_sub[prev_type] != null) {
248 $(".autocomplete[name='" + autocomplete_sub[prev_type] + "']").autocomplete('option', 'source', baseurl + 'autocomplete/' + autocomplete_sub[prev_type] + '/' + value);
249 }
250
4aab5061 251 $('#' + next_list).load(baseurl + 'list/' + next_type, { previous:prev_type, value:value }, function() {
e16215c4
SJ
252 $("select[name='" + next_type + "']").attr('value', '');
253 if ($("select[name='" + next_type + "']").children('option').size() > 1) {
4aab5061 254 $('tr#' + next_type).show();
e16215c4 255 } else {
4aab5061
SJ
256 $('tr#' + next_type).hide();
257 if (j < address_types_count) {
e16215c4
SJ
258 displayNextAddressComponent(i, j + 1, value);
259 }
260 }
261 });
262}
263
264function changeAddressComponents(type, value)
265{
4aab5061 266 var i = 0;
e16215c4 267
4aab5061 268 while (address_types[i] != type && i < address_types_count) {
e16215c4
SJ
269 ++i;
270 }
271
4aab5061 272 for (var j = i + 1; j < address_types_count; ++j) {
249510f5 273 delete_address_component(address_types[j], true);
e16215c4
SJ
274 }
275
4aab5061 276 if (value != '' && i < address_types_count) {
e16215c4
SJ
277 $("select[name='" + type + "']").attr('value', value);
278 displayNextAddressComponent(i, i + 1, value);
279 }
280}
281
249510f5 282function delete_address_component(field_name, remove)
4aab5061 283{
249510f5
SJ
284 if (remove || field_name == 'locality') {
285 $('tr#' + field_name).hide();
286 $('#' + field_name + '_list').html('');
4aab5061 287
249510f5
SJ
288 if (field_name == 'locality') {
289 $("input[name='locality_text']").val('');
290 $('tr#locality_text').show();
291 }
292 } else {
293 $("select[name='" + field_name + "']").val('');
294 $("input[name='" + field_name + "']").val('');
4aab5061
SJ
295 }
296}
297
e16215c4
SJ
298// when changing school, open diploma choice
299function changeSchool(schoolId, diploma)
300{
301 $(".autocompleteTarget[name='school']").attr('value', schoolId);
302
303 if (schoolId) {
82f06260 304 $(".autocomplete[name='school_text']").addClass('hidden_valid');
e16215c4 305 } else {
82f06260 306 $(".autocomplete[name='school_text']").removeClass('hidden_valid');
e16215c4
SJ
307 }
308
309 $("[name='diploma']").parent().load(baseurl + 'list/diploma/', { school:schoolId }, function() {
310 $("select[name='diploma']").attr('value', diploma);
311 });
312}
313
e16215c4
SJ
314// when choosing a job term in tree, hide tree and set job term field
315function searchForJobTerm(treeid, jtid, full_name)
316{
317 $(".term_tree").remove();
82f06260 318 $("input[name='jobterm_text']").val(full_name).addClass("hidden_valid").show();
e16215c4
SJ
319 $("input[name='jobterm']").val(jtid);
320}
321
3ccb060d
SJ
322function addressesDump()
323{
324 if ($('#addresses_dump:checked').length > 0) {
325 $('#recherche').attr('action', 'search/adv/addresses').attr('method', 'post').removeAttr('onsubmit');
326 } else {
327 $('#recherche').attr('action', 'search/adv').attr('method', 'get');
328 }
329}
330
e16215c4
SJ
331// }}}
332// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: