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