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