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