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