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