Adapts new autocomplete to jobterms.
[platal.git] / htdocs / javascript / profile.js
CommitLineData
46ae38a9 1/***************************************************************************
5e1513f6 2 * Copyright (C) 2003-2011 Polytechnique.org *
46ae38a9
FB
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
8c7ac79e 21// Page initialization {{{1
46ae38a9
FB
22
23function wizPage_onLoad(id)
24{
25 switch (id) {
26 case 'general':
6505acf7
SJ
27 var i = 1;
28 while ($('.edu_' + i).length != 0) {
29 prepareType(i - 1);
30 ++i;
043bbacf 31 }
46ae38a9 32 break;
041a5cec
SJ
33 case 'adresses':
34 checkCurrentAddress();
35 break;
46ae38a9 36 case 'poly':
6505acf7 37 updateGroupSubLink();
46ae38a9
FB
38 break;
39 case 'deco':
e18807a8
SJ
40 var i = 0;
41 while ($('#medal_' + i).length != 0) {
42 prepareMedal(i);
43 ++i;
46ae38a9
FB
44 }
45 break;
46 case 'emploi':
6fc390c7 47 if ($('#jobs_0').find("[name='jobs[0][name]']").val() == '') {
98b19917 48 registerEnterpriseAutocomplete(0);
948a6de9 49 }
46ae38a9
FB
50 break;
51 }
52}
53
f711b03f
SJ
54var educationDegree;
55var educationDegreeAll;
56var educationDegreeName;
46ae38a9
FB
57var subgrades;
58var names;
e18807a8 59var multiples;
8c7ac79e 60
6fc390c7
SJ
61// Publicity follows the following ordering: private < ax < public.
62var publicity = [];
63publicity['private'] = 0;
64publicity['ax'] = 1;
65publicity['public'] = 2;
66
8c7ac79e
SJ
67// Names {{{1
68
e1082e20 69function toggleNamesAdvanced()
6e32823c 70{
e1082e20 71 $('.names_advanced').toggle();
6e32823c
SJ
72}
73
04e200e0 74function addSearchName(isFemale)
b04882ff 75{
6e32823c
SJ
76 var i = 0;
77 while ($('#search_name_' + i).length != 0) {
78 i++;
79 }
0e1dfbad
SJ
80 $('#search_name_' + i).updateHtml('profile/ajax/searchname/' + i + '/' + isFemale,
81 function(data) {
82 $('#searchname').before(data);
83 });
b04882ff 84}
46ae38a9 85
e8a7cf31 86function removeSearchName(i, isFemale)
b04882ff 87{
6e32823c 88 $('#search_name_' + i).remove();
e8a7cf31 89 updateNameDisplay(isFemale);
6e32823c
SJ
90}
91
e8a7cf31 92function updateNameDisplay(isFemale)
6e32823c 93{
0e1dfbad
SJ
94 var lastnames = new Array('lastname_main', 'lastname_ordinary', 'lastname_marital', 'pseudonym');
95 var firstnames = new Array('firstname_main', 'firstname_ordinary');
6e32823c 96 var searchnames = '';
0e1dfbad
SJ
97
98 for (var i = 0; i < 4; ++i) {
99 searchnames += $('.names_advanced').find('[name*=' + lastnames[i] + ']').val() + ';';
100 }
101 searchnames += '-;'
102 for (var i = 0; i < 2; ++i) {
103 searchnames += $('.names_advanced').find('[name*=' + firstnames[i] + ']').val() + ';';
104 }
105 searchnames += '-';
106
107 var has_private = false;
108 for (var i = 0; i < 10; ++i) {
6e32823c 109 if ($('#search_name_' + i).find(':text').val()) {
0e1dfbad
SJ
110 searchnames += ';' + $('#search_name_' + i).find('[name*=type]').val() + ';' + $('#search_name_' + i).find(':text').val();
111 has_private = true;
6e32823c
SJ
112 }
113 }
0e1dfbad 114 searchnames += (has_private ? '' : ';');
ce38195f 115 $.xget('profile/ajax/buildnames/' + searchnames + '/' + isFemale,
b0e935d6
FB
116 function(data){
117 var name = data.split(';');
118 $('#public_name').html(name[0]);
119 $('#private_name').html(name[0] + name[1]);
120 });
b04882ff 121}
46ae38a9 122
7e233317
SJ
123// Promotions {{{1
124
125function togglePromotionEdition()
126{
127 $(".promotion_edition").toggle();
128}
129
8c7ac79e
SJ
130// Nationalities {{{1
131
8450c2aa
SJ
132function delNationality(i)
133{
7996ae8c 134 $('#nationality' + i).hide().find('select').val('');
8450c2aa
SJ
135}
136
137function addNationality()
138{
139 var i = 0;
323b972d 140 if ($('#nationality2').find('select').val() == "") {
8450c2aa 141 i = 2;
323b972d 142 } else if ($('#nationality3').find('select').val() == "") {
8450c2aa
SJ
143 i = 3;
144 }
145 if ((i == 2) || (i == 3)) {
323b972d 146 $('#nationality' + i).show();
8450c2aa
SJ
147 }
148}
149
6505acf7
SJ
150// Education {{{1
151
152function prepareType(id)
153{
154 var edu = $('.edu_' + id).find("[name='edus[" + id + "][eduid]']").val() - 1;
155 var sel = $('.edu_' + id).find('[name=edu_' + id + '_tmp]').val();
156 var html = '';
ed405a22
SJ
157 if (educationDegree[edu]) {
158 var length = educationDegree[edu].length;
159 } else {
160 var length = 0;
161 }
6505acf7
SJ
162 for (i = 0; i < length; ++i) {
163 html += '<option value="' + educationDegree[edu][i] + '"';
164 if (sel == educationDegree[edu][i]) {
165 html += ' selected="selected"';
166 }
167 html += '>' + educationDegreeName[educationDegree[edu][i] - 1] + '</option>';
168 }
ed405a22
SJ
169 // XXX: to be removed once SQL table profile_merge_issues is.
170 if (sel != '' && html == '') {
171 html += '<option value="' + sel + '" selected="selected">' + educationDegreeName[sel - 1] + '</option>';
172 }
6505acf7
SJ
173 $('.edu_' + id).find("[name='edus[" + id + "][degreeid]']").html(html);
174}
175
176function addEdu()
177{
178 var i = 0;
179 var j = 0;
180 var prefix = 'edu_';
181 var class_parity;
182
183 while (!$('#edu_add').hasClass(prefix + i)) {
184 if ($('.' + prefix + i).length != 0) {
185 j++;
186 }
187 i++;
188 }
189 if (j % 2) {
190 class_parity = 'pair';
191 } else {
192 class_parity = 'impair';
193 }
194 $('#edu_add').removeClass(prefix + i);
195 i++;
196 $('#edu_add').addClass(prefix + i);
197 i--;
63c89179 198 $.xget('profile/ajax/edu/' + i + '/' + class_parity,
6505acf7
SJ
199 function(data) {
200 $('#edu_add').before(data);
201 prepareType(i);
202 });
203}
204
205function removeEdu(i)
206{
207 var prefix = 'edu_';
208 $('.' + prefix + i).remove();
209 while (!$('#edu_add').hasClass(prefix + i)) {
210 $('.' + prefix + i).toggleClass('pair');
211 $('.' + prefix + i).toggleClass('impair');
212 i++;
213 }
214}
215
8c7ac79e
SJ
216// Networking {{{1
217
d1a2252a
GB
218function addNetworking()
219{
220 var i = 0;
6505acf7 221 while ($('#networking_' + i).length != 0) {
d1a2252a
GB
222 i++;
223 }
92c3f9e5 224 var namefirst = '';
d1a2252a 225 var html = '<tr id="networking_' + i + '">'
92c3f9e5
GB
226 + ' <td colspan="2">'
227 + ' <div style="float: left; width: 200px;">'
228 + ' <span class="flags">'
229 + ' <input type="checkbox" name="networking[' + i + '][pub]"/>'
230 + ' <img src="images/icons/flag_green.gif" alt="site public" title="site public">'
231 + ' </span>&nbsp;'
6505acf7 232 + ' <select name="networking[' + i + '][type]" onchange="javascript:updateNetworking(' + i + ');">';
92c3f9e5
GB
233 for (nw in nw_list) {
234 if (namefirst == '') {
235 namefirst = nw;
236 }
237 html += ' <option value="' + nw_list[nw] + '">' + nw + '</option>';
238 }
239 html += '</select>'
6505acf7 240 + ' <input type="hidden" name="networking[' + i + '][name]" value="' + namefirst + '"/>'
92c3f9e5
GB
241 + ' </div>'
242 + ' <div style="float: left">'
243 + ' <input type="text" name="networking[' + i + '][address]" value="" size="30"/>'
244 + ' <a href="javascript:removeNetworking(' + i + ')">'
245 + ' <img src="images/icons/cross.gif" alt="cross" title="Supprimer cet élément"/>'
246 + ' </a>'
247 + ' </div>'
d1a2252a
GB
248 + ' </td>'
249 + '</tr>';
250
92c3f9e5 251 $('#networking').before(html);
d1a2252a
GB
252}
253
254function removeNetworking(id)
255{
256 $('#networking_' + id).remove();
257}
258
92c3f9e5 259function updateNetworking(i)
d1a2252a 260{
6505acf7 261 $('#networking_' + i).find("[name='networking[" + i + "][name]']").val($('#networking_' + i).find('select option:selected').text());
d1a2252a
GB
262}
263
8fcfc71f
SJ
264// Hobby {{{1
265
266function addHobby()
267{
268 var i = 0;
269 while ($('#hobby_' + i).length != 0) {
270 ++i;
271 }
272 var html = '<tr id="hobby_' + i + '">'
273 + ' <td colspan="2">'
274 + ' <div style="float: left; width: 200px;">'
275 + ' <span class="flags">'
276 + ' <input type="checkbox" name="hobbies[' + i + '][pub]"/>'
277 + ' <img src="images/icons/flag_green.gif" alt="site public" title="site public">'
278 + ' </span>&nbsp;'
279 + ' <select name="hobbies[' + i + '][type]">'
280 + ' <option value="Sport">Sport</option>'
281 + ' <option value="Loisir">Loisir</option>'
282 + ' <option value="Hobby">Hobby</option>'
283 + ' </select>'
284 + ' </div>'
285 + ' <div style="float: left">'
286 + ' <input type="text" name="hobbies[' + i + '][text]" value="" size="30"/>'
287 + ' <a href="javascript:removeHobby(' + i + ')">'
288 + ' <img src="images/icons/cross.gif" alt="cross" title="Supprimer cet élément"/>'
289 + ' </a>'
290 + ' </div>'
291 + ' </td>'
292 + '</tr>';
293
294 $('#hobby').before(html);
295}
296
297function removeHobby(id)
298{
299 $('#hobby_' + id).remove();
300}
301
8c7ac79e 302// Addresses {{{1
46ae38a9 303
041a5cec 304function toggleAddress(id, val)
46ae38a9 305{
041a5cec
SJ
306 $('#addresses_' + id + '_grayed').toggle();
307 $('#addresses_' + id).toggle();
308 $('#addresses_' + id + '_cont').find('[name*=removed]').val(val);
309 checkCurrentAddress();
46ae38a9
FB
310}
311
041a5cec 312function checkCurrentAddress(id)
46ae38a9 313{
041a5cec 314 var hasCurrentAddress = id ? true : false;
46ae38a9 315 var i = 0;
041a5cec
SJ
316 while ($('#addresses_' + i + '_cont').length != 0) {
317 if ($('#addresses_' + i + '_cont').find('[name*=removed]').val() == 1) {
318 $('#addresses_' + i + '_cont').find('[name*=current]').attr('checked', false);
319 }
320 if (!hasCurrentAddress && $('#addresses_' + i + '_cont').find('[name*=current]:checked').length != 0) {
321 hasCurrentAddress = true;
322 } else {
323 $('#addresses_' + i + '_cont').find('[name*=current]').attr('checked', false);
46ae38a9
FB
324 }
325 i++;
326 }
041a5cec 327 if (!hasCurrentAddress) {
46ae38a9 328 i = 0;
041a5cec
SJ
329 while ($('#addresses_' + i + '_cont').length != 0) {
330 if ($('#addresses_' + i + '_cont').find('[name*=removed]').val() == 0) {
331 $('#addresses_' + i + '_cont').find('[name*=current]').attr('checked', 'checked');
332 break;
333 }
334 i++;
46ae38a9
FB
335 }
336 }
041a5cec
SJ
337 if (id) {
338 $('#addresses_' + id + '_cont').find('[name*=current]').attr('checked', 'checked');
46ae38a9
FB
339 }
340}
341
a3328b1e 342function addAddress(pid)
46ae38a9
FB
343{
344 var i = 0;
041a5cec 345 while ($('#addresses_' + i + '_cont').length != 0) {
46ae38a9
FB
346 i++;
347 }
041a5cec 348 $('#add_address').before('<div id="addresses_' + i + '_cont"></div>');
a3328b1e 349 $('#addresses_' + i + '_cont').updateHtml('profile/ajax/address/' + i + '/' + pid,
b0e935d6 350 checkCurrentAddress());
46ae38a9
FB
351}
352
b1f39e8b 353function addressChanged(prefid, color)
043bbacf 354{
b1f39e8b 355 var text = $('#' + prefid + '_cont').find("[name*='[text]']").val();
541e8d03 356 $('#' + prefid + '_cont').find('[name*=changed]').val("1");
b1f39e8b 357 $.xpost('map_url/', { text:text, color:color }, function(data) {
a2b84de0
SJ
358 if (data) {
359 $('#' + prefid + '_static_map_url').show();
360 $('#' + prefid + '_static_map_url').find('img').attr('src', data);
361 } else {
362 $('#' + prefid + '_static_map_url').hide();
363 $('#' + prefid + '_geocoding_removal').find('[name*=request]:checkbox').removeAttr('checked');
364 }
b1f39e8b 365 });
043bbacf
SJ
366}
367
a3328b1e 368function deleteGeocoding(prefid)
043bbacf 369{
a3328b1e
SJ
370 if($('#' + prefid + '_geocoding_removal').find('[name*=request]:checkbox:checked').length == 0) {
371 return true;
5112531d 372 }
a3328b1e
SJ
373
374 return confirm(
006158ff
SJ
375 "La localisation de l'adresse sert à deux choses : te placer dans "
376 + "le planisphère et te faire apparaître dans la recherche avancée par "
57fa97b3 377 + "pays, région, département, ville... La supprimer t'en fera disparaître. "
006158ff
SJ
378 + "\nIl ne faut le faire que si cette localisation "
379 + "est réellement erronée. Avant de supprimer cette localisation, l'équipe de "
380 + "Polytechnique.org tentera de la réparer.\n\nConfirmes-tu ta "
381 + "demande de suppression de cette localisation ?");
043bbacf
SJ
382}
383
8c7ac79e
SJ
384// {{{1 Phones
385
6fc390c7 386function addTel(prefid, prefname, subField, mainField, mainId)
46ae38a9
FB
387{
388 var i = 0;
bde2be3b 389 var prefix = prefid + '_';
6505acf7 390 while ($('#' + prefix + i).length != 0) {
46ae38a9
FB
391 i++;
392 }
bde2be3b 393 $('#' + prefix + 'add').before('<div id="' + prefix + i + '" style="clear: both; padding-top: 4px; padding-bottom: 4px"></div>');
f8a1899e 394 $('#' + prefix + i).updateHtml('profile/ajax/tel/' + prefid + '/' + prefname + '/' + i + '/' + subField + '/' + mainField + '/' + mainId);
46ae38a9
FB
395}
396
c8023e61 397function removeTel(prefname, prefid, id)
bde2be3b 398{
ed01acac 399 var total = 0;
c8023e61 400 while ($('#' + prefid + '_' + total).length != 0) {
ed01acac
SJ
401 ++total;
402 }
c8023e61 403 $('#' + prefid + '_' + id).remove();
ed01acac 404 for (var i = parseInt(id) + 1; i < total; ++i) {
c8023e61 405 renumberPhone(prefname, prefid, i);
ed01acac 406 }
bde2be3b
GB
407}
408
6505acf7 409function addPhoneComment(id)
bde2be3b 410{
3cd815ca
SJ
411 $('#' + id + '_comment').show();
412 $('#' + id + '_addComment').hide();
bde2be3b
GB
413}
414
415function removePhoneComment(id, pref)
416{
3cd815ca
SJ
417 $('#' + id + '_comment').hide();
418 $('#' + id + '_comment').find("[name='" + pref + "[comment]']").val('');
419 $('#' + id + '_addComment').show();
bde2be3b 420}
46ae38a9 421
c8023e61 422function renumberPhone(prefname, prefid, i)
ed01acac
SJ
423{
424 var telid = i - 1;
c8023e61
SJ
425 var telprefOld = prefname + '[' + i + ']';
426 var telpref = prefname + '[' + telid + ']';
427 var idOld = prefid + '_' + i;
428 var id = prefid + '_' + telid;
429
430 $('#' + idOld).attr('id', id);
431 $('#' + id).find('div.titre').html('N°' + i);
432 $('#' + id).find('a.removeTel').attr('href', 'javascript:removeTel(\'' + prefname + '\',\'' + prefid + '\',' + telid + ')');
433 $('#' + id).find('select').attr('name', telpref + '[type]');
0b6c8b36 434 $('#' + id).find("[name='" + telprefOld + "[display]']").attr('name', telpref + '[display]');
c8023e61
SJ
435 $('#' + id).find("[name='" + telprefOld + "[comment]']").attr('name', telpref + '[comment]');
436 $('#' + id).find('a.removePhoneComment').attr('href', 'javascript:removePhoneComment(' + id + ',' + telpref + ')');
437 $('#' + id).find('#' + idOld + '_addComment').attr('id', id + '_addComment');
438 $('#' + id).find('#' + id + '_addComment').attr('href', 'javascript:addPhoneComment(' + id + ')');
439 $('#' + id).find('#' + idOld + '_comment').attr('id', id + '_comment');
440 $('#' + id).find("[name='" + telprefOld + "[pub]']").attr('name', telpref + '[pub]');
ed01acac
SJ
441}
442
6505acf7 443// {{{1 Groups
46ae38a9 444
6505acf7 445function addBinet()
46ae38a9 446{
6505acf7
SJ
447 var id = $('#binets_table').find('[name=binets_sel]').val();
448 var text = $('#binets_table').find('select option:selected').text();
449 var html = '<tr id="binets_' + id + '">'
450 + ' <td>'
451 + ' <input type="hidden" name="binets[' + id + ']" value="' + text + '" />'
452 + ' </td>'
453 + ' <td>'
454 + ' <div style="float: left; width: 70%">'
455 + text
456 + ' </div>'
9fce7016 457 + ' <a href="javascript:removeElement(\'binets\',' + id + ')">'
6505acf7
SJ
458 + ' <img src="images/icons/cross.gif" alt="cross" title="Supprimer ce groupe" />'
459 + ' </a>'
460 + ' </td>'
461 + '</tr>';
462 $('#binets_table').after(html);
463 updateElement('binets');
46ae38a9
FB
464}
465
6505acf7 466function updateGroupSubLink()
46ae38a9 467{
6505acf7
SJ
468 var href = $('[name*=groupesx_sub]').val() ? $('[name*=groupesx_sub]').val() : 'http://www.polytechnique.net';
469 $('#groupesx_sub').attr('href', href);
46ae38a9
FB
470}
471
6505acf7 472// {{{1 Medals
46ae38a9 473
e18807a8
SJ
474function prepareMedal(i)
475{
476 getMedalName($('#medal_' + i).find('[name="medals[' + i + '][id]"]').val());
477 buildGrade(i);
478}
479
46ae38a9
FB
480function updateMedal()
481{
6505acf7 482 var val = $('#medals').find('[name*=medal_sel]').val();
e18807a8
SJ
483
484 if ((multiple[val] && subgrades[val]) || $('.medal_name_' + val).length == 0) {
6505acf7 485 $('#medal_add').show();
46ae38a9 486 } else {
6505acf7 487 $('#medal_add').hide();
46ae38a9
FB
488 }
489}
490
491function getMedalName(id)
492{
e18807a8 493 $('.medal_name_' + id).html(names[id]);
46ae38a9
FB
494}
495
e18807a8 496function buildGrade(i)
46ae38a9 497{
e18807a8
SJ
498 var id = $('#medal_' + i).find('[name="medals[' + i + '][id]"]').val();
499 var current = $('#medal_' + i).find('[name="medals_' + i + '_grade"]').val();
500 var subg = subgrades[id];
501 var obj = $('#medal_grade_' + i);
46ae38a9 502 if (!subg) {
e18807a8 503 obj.prepend('<input type="hidden" name="medals[' + i + '][grade]" value="0" />');
46ae38a9 504 } else {
e18807a8 505 var html = 'Agrafe : <select name="medals[' + i + '][grade]">';
46ae38a9 506 html += '<option value="0">Non précisée</option>';
e18807a8 507 for (var grade = 0; grade < subg.length; ++grade) {
46ae38a9
FB
508 html += '<option value="' + subg[grade][0] + '"';
509 if (subg[grade][0] == current) {
510 html += ' selected="selected"';
511 }
512 html += '>' + subg[grade][1] + '</option>';
513 }
46ae38a9
FB
514 html += '</select>';
515 obj.prepend(html);
516 }
517}
518
e18807a8 519function makeAddProcess(i, id)
46ae38a9
FB
520{
521 return function(data)
522 {
523 $('#medals').after(data);
524 updateMedal();
525 getMedalName(id);
e18807a8 526 buildGrade(i, 0);
46ae38a9
FB
527 };
528}
529
530function addMedal()
531{
e18807a8
SJ
532 var i = 0;
533 while ($('#medal_' + i).length != 0) {
534 ++i;
535 }
536
6505acf7 537 var id = $('#medals').find('[name=medal_sel]').val();
e18807a8 538 $.xget('profile/ajax/medal/' + i + '/' + id, makeAddProcess(i, id));
46ae38a9
FB
539}
540
541function removeMedal(id)
542{
e18807a8
SJ
543 var total = 0;
544 while ($('#medal_' + total).length != 0) {
545 ++total;
546 }
547 $('#medal_' + id).remove();
548 for (var i = parseInt(id) + 1; i < total; ++i) {
549 renumberMedal(i);
550 }
46ae38a9
FB
551 updateMedal();
552}
553
e18807a8
SJ
554function renumberMedal(i)
555{
556 var new_i = i - 1;
557
558 $('#medal_' + i).attr('id', 'medal_' + new_i);
559 $('#medal_grade_' + i).attr('id', 'medal_grade_' + new_i);
560 $('#medal_grade_' + new_i).find("[name='medals_" + i + "_grade']").attr('name', 'medals_' + new_i + '_grade');
561 $('#medal_grade_' + new_i).find("[name='medals[" + i + "][id]']").attr('name', 'medals[' + new_i + '][id]');
562 $('#medal_grade_' + new_i).find("[name='medals[" + i + "][valid]']").attr('name', 'medals[' + new_i + '][valid]');
563 $('#medal_grade_' + new_i).find("[name='medals[" + i + "][grade]']").attr('name', 'medals[' + new_i + '][grade]');
564 $('#medal_' + new_i).find('a.removeMedal').attr('href', 'javascript:removeMedal(' + new_i + ')');
565}
566
8c7ac79e 567// Jobs {{{1
46ae38a9
FB
568
569function removeJob(id, pref)
570{
6505acf7 571 $('#' + id + '_cont').hide();
00ca8c09 572 if ($('#' + id).find("[name='" + pref + "[new]']").val() == '0') {
6505acf7 573 $('#' + id + '_grayed').show();
00ca8c09 574 $('#' + id + '_grayed_name').html($('#' + id).find("[name='" + pref + "[name]']").val());
46ae38a9 575 }
00ca8c09 576 $('#' + id).find("[name='" + pref + "[removed]']").val('1');
46ae38a9
FB
577}
578
579function restoreJob(id, pref)
580{
6505acf7
SJ
581 $('#' + id + '_cont').show();
582 $('#' + id + '_grayed').hide();
00ca8c09 583 $('#' + id).find("[name='" + pref + "[removed]']").val('0');
46ae38a9
FB
584}
585
46ae38a9
FB
586function makeAddJob(id)
587{
588 return function(data)
589 {
590 $('#add_job').before(data);
16594a1a 591 registerEnterpriseAutocomplete(id);
46ae38a9
FB
592 };
593}
594
3c746422 595function addJob(pid)
46ae38a9
FB
596{
597 var i = 0;
6fc390c7 598 while ($('#jobs_' + i).length != 0) {
46ae38a9
FB
599 ++i;
600 }
3c746422 601 $.xget('profile/ajax/job/' + i + '/' + pid, makeAddJob(i));
46ae38a9
FB
602}
603
b814a8b8
SJ
604function addEntreprise(id)
605{
606 $('.entreprise_' + id).toggle();
607}
46ae38a9 608
3ac45f10
PC
609/**
610 * Adds a job term in job profile page
611 * @param jobid id of profile's job among his different jobs
612 * @param jtid id of job term to add
613 * @param full_name full text of job term
614 * @return false if the term already exist for this job, true otherwise
615 */
616function addJobTerm(jobid, jtid, full_name)
617{
618 var termid = 0;
619 var parentpath;
620 var formvarname;
621 if (jobid < 0) {
622 parentpath = '';
623 jobid = '';
624 formvarname = 'terms';
625 } else {
6fc390c7 626 parentpath = '#jobs_'+jobid+' ';
3ac45f10
PC
627 formvarname = 'jobs['+jobid+'][terms]';
628 }
6fc390c7 629 var lastJobTerm = $(parentpath + '.jobs_term:last');
3ac45f10
PC
630 if (lastJobTerm.length != 0) {
631 termid = parseInt(lastJobTerm.children('input').attr('name').replace(/^(jobs\[[0-9]+\]\[terms\]|terms)\[([0-9]+)\]\[jtid\]/, '$2')) + 1;
632 if ($('#job'+jobid+'_term'+jtid).length > 0) {
633 return false;
634 }
635 }
6fc390c7 636 var newdiv = '<div class="jobs_term" id="job'+jobid+'_term'+jtid+'">'+
3ac45f10
PC
637 '<span>'+full_name+'</span>'+
638 '<input type="hidden" name="'+formvarname+'['+termid+'][jtid]" value="'+jtid+'" />'+
639 '<img title="Retirer ce mot-clef" alt="retirer" src="images/icons/cross.gif" />'+
640 '</div>';
641 if (lastJobTerm.length == 0) {
6fc390c7 642 $(parentpath + '.jobs_terms').prepend(newdiv);
3ac45f10
PC
643 } else {
644 lastJobTerm.after(newdiv);
645 }
646 $('#job'+jobid+'_term'+jtid+' img').css('cursor','pointer').click(removeJobTerm);
647 return true;
648}
649
650/**
651 * Remove a job term in job profile page.
652 * Must be called from a button in a div containing the term
653 */
654function removeJobTerm()
655{
656 $(this).parent().remove();
657}
658
659/**
3ac45f10
PC
660 * Function called when a job term has been selected from autocompletion
661 * in search
662 * @param li is the list item (<li>) that has been clicked
663 * The context is the jsquery autocomplete object.
664 */
658cffd4 665function selectJobTerm(id, value, jobid)
3ac45f10 666{
658cffd4
SJ
667 if (id >= 0) {
668 addJobTerm(jobid, id, value);
f31c2a24 669 }
3ac45f10
PC
670 var search_input;
671 if (jobid < 0) {
672 search_input = $('.term_search')[0];
673 } else {
658cffd4 674 search_input = $('#jobs_' + jobid + ' .term_search')[0];
3ac45f10 675 }
658cffd4 676 if (id >= 0) {
f31c2a24
PC
677 search_input.value = '';
678 search_input.focus();
679 } else {
658cffd4
SJ
680 search_input.value = value.replace(/%$/, '');
681 toggleJobTermsTree(jobid, ''); // Use given value instead
f31c2a24 682 }
3ac45f10
PC
683}
684
685/**
686 * Function to show or hide a terms tree in job edition
687 * @param jobid is the id of the job currently edited
688 */
f31c2a24 689function toggleJobTermsTree(jobid, textfilter)
3ac45f10 690{
8e79f7c7
SJ
691 $('#term_tree_comment').toggle();
692
3ac45f10
PC
693 var treepath;
694 if (jobid < 0) {
695 treepath = '';
696 } else {
6fc390c7 697 treepath = '#jobs_'+jobid+' ';
3ac45f10
PC
698 }
699 treepath += '.term_tree';
700 if ($(treepath + ' ul').length > 0) {
701 $(treepath).empty().removeClass().addClass('term_tree');
f31c2a24
PC
702 if (!textfilter) {
703 return;
704 }
3ac45f10 705 }
f31c2a24 706 createJobTermsTree(treepath, 'profile/ajax/tree/jobterms/all', 'job' + jobid, 'chooseJobTerm', textfilter);
3ac45f10
PC
707}
708
709/**
710 * Function called when a job term is chosen from terms tree
711 * @param treeid is the full id of the tree (must look like job3)
712 * @param jtid is the id of the job term chosen
713 * @param fullname is the complete name (understandable without context) of the term
714 */
715function chooseJobTerm(treeid, jtid, fullname)
716{
717 addJobTerm(treeid.replace(/^job(.*)$/, '$1'), jtid, fullname);
718}
719
8c7ac79e 720// {{{1 Skills
46ae38a9 721
46ae38a9
FB
722function addSkill(cat)
723{
6505acf7
SJ
724 var val = $('#' + cat + '_table').find('[name=' + cat + '_sel]').val();
725 var text = $('#' + cat + '_table').find('[name=' + cat + '_sel] :selected').text();
63c89179 726 $.xget('profile/ajax/skill/' + cat + '/' + val,
46ae38a9 727 function(data) {
6505acf7
SJ
728 $('#' + cat).append(data);
729 $('#' + cat + '_' + val + '_title').text(text);
730 updateElement(cat);
46ae38a9
FB
731 });
732}
733
6505acf7 734// {{{1 Mentor
46ae38a9
FB
735
736function addCountry()
737{
6505acf7
SJ
738 var val = $('#countries_table').find('[name=countries_sel] :selected').val();
739 var text = $('#countries_table').find('[name=countries_sel] :selected').text();
46ae38a9 740 var html = '<div id="countries_' + val + '" style="clear: both; margin-bottom: 0.7em">'
9fce7016 741 + ' <a href="javascript:removeElement(\'countries\',\'' + val + '\')" style="display: block; float:right">'
46ae38a9
FB
742 + ' <img src="images/icons/cross.gif" alt="" title="Supprimer ce pays" />'
743 + ' </a>'
1d414e12
PC
744 + ' <div style="float: left; width: 50%">' + text + '</div>'
745 + ' <input type="hidden" name="countries[' + val + ']" value="' + text + '" />'
46ae38a9
FB
746 + '</div>';
747 $('#countries').append(html);
6505acf7 748 updateElement('countries');
46ae38a9
FB
749}
750
16594a1a
FB
751function registerEnterpriseAutocomplete(id)
752{
d4cb8f1f 753 $('.enterprise_name').each(function() {
16594a1a 754 if (id == -1 || this.name == "jobs[" + id + "][name]") {
d4cb8f1f
SJ
755 $(this).autocomplete({
756 source: $.plURL('search/autocomplete/entreprise/') + this.name,
757 change: function(event, ui) {
758 if (ui.item != null && ui.item.field != null) {
759 $(this).val(ui.item.field);
760 }
761 }
762 });
16594a1a 763 }
d4cb8f1f 764 });
6505acf7
SJ
765}
766
767// {{{1 Multiusage functions
768
769function updateElement(cat)
770{
771 var val = $('#' + cat + '_table').find('[name=' + cat + '_sel]').val();
772 if (val == '' || $('#' + cat + '_' + val).length != 0) {
773 $('#' + cat + '_add').hide();
774 } else {
775 $('#' + cat + '_add').show();
776 }
777}
778
779function removeElement(cat, id)
780{
781 $('#' + cat + '_' + id).remove();
782 updateElement(cat);
16594a1a
FB
783}
784
6fc390c7
SJ
785function updateSubPublicity(subFieldId, name, mainPub)
786{
787 var subPub = $(subFieldId).find("[name='" + name + "']:checked").val();
788 if (publicity[subPub] > publicity[mainPub]) {
789 $(subFieldId).find("[name='" + name + "']:checked").removeAttr('checked');
790 $(subFieldId).find('[value=' + mainPub + ']').attr('checked', 'checked');
791 }
792}
793
794function updatePublicity(mainField, mainId, subField, subId)
795{
796 var mainFieldId = '#' + mainField + '_' + mainId;
797 var mainPub = $(mainFieldId).find("[name='" + mainField + "[" + mainId + "][pub]']:checked").val();
798 if (subId == -1) {
799 var subFields = subField.split(',');
800 for (var i =0; i < subFields.length; ++i) {
801 var subFieldBaseId = mainFieldId + '_' + subFields[i];
802 var name = mainField + '[' + mainId + '][' + subFields[i] + ']';
803 if ($(subFieldBaseId).length != 0) {
804 updateSubPublicity(subFieldBaseId, name + '[pub]', mainPub);
805 updateSubPublicity(subFieldBaseId, mainField + '[' + mainId + '][' + subFields[i] + '_pub]', mainPub);
806 }
807 subId = 0;
808 while ($(subFieldBaseId + '_' + subId).length != 0) {
809 updateSubPublicity(subFieldBaseId + '_' + subId, name + '[' + subId + '][pub]', mainPub);
810 ++subId;
811 }
812 }
813 } else {
814 if (subId == '') {
815 updateSubPublicity(mainFieldId + '_' + subField, mainField + '[' + mainId + '][' + subField + '_pub]', mainPub);
816 updateSubPublicity(mainFieldId + '_' + subField, mainField + '[' + mainId + '][' + subField + '][pub]', mainPub);
817 } else {
818 updateSubPublicity(mainFieldId + '_' + subField + '_' + subId, mainField + '[' + mainId + '][' + subField + '][' + subId + '][pub]', mainPub);
819 }
820 }
821}
822
46ae38a9 823// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: