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