19589e455749fb5145b8090fae8aebf059480646
[platal.git] / htdocs / javascript / profile.js
1 /***************************************************************************
2 * Copyright (C) 2003-2009 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 = 0;
28 var prefix = 'edu_';
29 while ($('.' + prefix + i).length != 0) {
30 i++;
31 }
32 i--;
33 for (var j = 0; j < i; j++) {
34 prepareType(j);
35 }
36 break;
37 case 'adresses':
38 checkCurrentAddress();
39 break;
40 case 'poly':
41 updateGroupSubLink(document.forms.prof_annu.groupesx_sub);
42 break;
43 case 'deco':
44 for (var i in names) {
45 if (typeof names[i] != 'function') {
46 if (document.getElementById("medal_" + i) != null) {
47 getMedalName(i);
48 buildGrade(i, document.forms.prof_annu["medal_" + i + "_grade"].value);
49 }
50 }
51 }
52 break;
53 case 'emploi':
54 for (var i = 0 ; document.getElementById('job_' + i) != null ; ++i) {
55 updateJobSecteur(i, 'job_' + i, 'jobs[' + i + ']',
56 document.forms.prof_annu["jobs[" + i + "][ss_secteur]"].value);
57 updateJobSousSecteur(i, 'job_' + i, 'jobs[' + i + ']',
58 document.forms.prof_annu["jobs[" + i + "][sss_secteur]"].value);
59 }
60 setTimeout('registerEnterpriseAutocomplete(-1)', 100);
61 break;
62 }
63 }
64
65 var educationDegree;
66 var educationDegreeAll;
67 var educationDegreeName;
68 var subgrades;
69 var names;
70
71 // Education {{{1
72
73 function fillType(selectCtrl, edu, fill)
74 {
75 var i;
76 var i0 = 0;
77
78 for (i = selectCtrl.options.length; i >= 0; i--) {
79 selectCtrl.options[i] = null;
80 }
81
82 if (fill || edu < 0) {
83 selectCtrl.options[0] = new Option(' ');
84 i0 = 1;
85 }
86 if (edu >= 0) {
87 for (i = 0; i < educationDegree[edu].length; i++) {
88 selectCtrl.options[i0 + i] = new Option(educationDegreeName[educationDegree[edu][i] - 1], educationDegree[edu][i]);
89 }
90 } else if (fill) {
91 for (i = 0; i < educationDegreeAll.length; i++) {
92 selectCtrl.options[i0 + i] = new Option(educationDegreeName[educationDegreeAll[i] - 1], educationDegreeAll[i]);
93 }
94 }
95 }
96
97
98 function selectType(selectCtrl, type)
99 {
100 for (i = 0; i < selectCtrl.options.length; i++) {
101 if (selectCtrl.options[i].value == type) {
102 selectCtrl.selectedIndex = i;
103 }
104 }
105 }
106
107 function prepareType(i)
108 {
109 fillType(document.forms.prof_annu["edus[" + i + "][degreeid]"], document.forms.prof_annu["edus[" + i + "][eduid]"].selectedIndex - 1);
110 selectType(document.forms.prof_annu["edus[" + i + "][degreeid]"], document.forms.prof_annu["edu_" + i + "_tmp"].value);
111 }
112
113 function addEdu()
114 {
115 var i = 0;
116 var j = 0;
117 var prefix = 'edu_';
118 var class_parity;
119
120 while (!$('#edu_add').hasClass(prefix + i)) {
121 if ($('.' + prefix + i).length != 0) {
122 j++;
123 }
124 i++;
125 }
126 if (j % 2) {
127 class_parity = 'pair';
128 } else {
129 class_parity = 'impair';
130 }
131 $('#edu_add').removeClass(prefix + i);
132 i++;
133 $('#edu_add').addClass(prefix + i);
134 i--;
135 $.get(platal_baseurl + 'profile/ajax/edu/' + i + '/' + class_parity,
136 function(data) {
137 $('#edu_add').before(data);
138 prepareType(i);
139 });
140 }
141
142 function removeEdu(i)
143 {
144 var prefix = 'edu_';
145 $('.' + prefix + i).remove();
146 while (!$('#edu_add').hasClass(prefix + i)) {
147 $('.' + prefix + i).toggleClass('pair');
148 $('.' + prefix + i).toggleClass('impair');
149 i++;
150 }
151 }
152
153 // Names {{{1
154
155 function toggleNamesAdvanced()
156 {
157 $('.names_advanced').toggle();
158 }
159
160 function addSearchName()
161 {
162 var i = 0;
163 while ($('#search_name_' + i).length != 0) {
164 i++;
165 }
166 Ajax.update_html('search_name_' + i, 'profile/ajax/searchname/' + i, function(data){
167 $('#searchname').before(data);
168 changeNameFlag(i);
169 });
170 }
171
172 function removeSearchName(i)
173 {
174 $('#search_name_' + i).remove();
175 updateNameDisplay();
176 }
177
178 function changeNameFlag(i)
179 {
180 $('#flag_' + i).remove();
181 var typeid = $('#search_name_' + i).find('select').val();
182 var type = $('#search_name_' + i).find('select :selected').text();
183 if ($('[@name=sn_type_' + typeid + '_' + i + ']').val() > 0) {
184 $('#flag_cb_' + i).after('<span id="flag_' + i + '">&nbsp;' +
185 '<img src="images/icons/flag_green.gif" alt="site public" title="site public" />' +
186 '<input type="hidden" name="search_names[' + i + '][pub]" value="1"/>' +
187 '<input type="hidden" name="search_names[' + i + '][typeid]" value="' + typeid + '"/>' +
188 '<input type="hidden" name="search_names[' + i + '][type]" value="' + type + '"/></span>');
189 } else {
190 $('#flag_cb_' + i).after('<span id="flag_' + i + '">&nbsp;' +
191 '<img src="images/icons/flag_red.gif" alt="site privé" title="site privé" />' +
192 '<input type="hidden" name="search_names[' + i + '][typeid]" value="' + typeid + '"/>' +
193 '<input type="hidden" name="search_names[' + i + '][type]" value="' + type + '"/></span>');
194 }
195 }
196
197 function updateNameDisplay()
198 {
199 var searchnames = '';
200 for (var i = 0; i < 10; i++) {
201 if ($('#search_name_' + i).find(':text').val()) {
202 searchnames += $('#search_name_' + i).find('[name*=typeid]').val() + ';';
203 searchnames += $('#search_name_' + i).find(':text').val() + ';;';
204 }
205 }
206 Ajax.update_html(null, 'profile/ajax/buildnames/' + searchnames, function(data){
207 var name = data.split(';');
208 $('#public_name').html(name[0]);
209 $('#private_name').html(name[0] + name[1]);
210 });
211 }
212
213 // Nationalities {{{1
214
215 function delNationality(i)
216 {
217 $('#nationalite' + i).hide().find('select').val('');
218 }
219
220 function addNationality()
221 {
222 var i = 0;
223 if ($('#nationalite2').find('select').val() == "") {
224 i = 2;
225 } else if ($('#nationalite3').find('select').val() == "") {
226 i = 3;
227 }
228 if ((i == 2) || (i == 3)) {
229 $('#nationalite' + i).show();
230 }
231 }
232
233 // Networking {{{1
234
235 function addNetworking()
236 {
237 var i = 0;
238 var nws = 'networking_';
239 while (document.getElementById(nws + i) != null) {
240 i++;
241 }
242 var namefirst = '';
243 var html = '<tr id="networking_' + i + '">'
244 + ' <td colspan="2">'
245 + ' <div style="float: left; width: 200px;">'
246 + ' <span class="flags">'
247 + ' <input type="checkbox" name="networking[' + i + '][pub]"/>'
248 + ' <img src="images/icons/flag_green.gif" alt="site public" title="site public">'
249 + ' </span>&nbsp;'
250 + ' <select id="networking_type_' + i + '" name="networking[' + i + '][type]" onchange="javascript:updateNetworking(' + i + ');">';
251 for (nw in nw_list) {
252 if (namefirst == '') {
253 namefirst = nw;
254 }
255 html += ' <option value="' + nw_list[nw] + '">' + nw + '</option>';
256 }
257 html += '</select>'
258 + ' <input type="hidden" id="networking_name_' + i + '" name="networking[' + i + '][name]" value="' + namefirst + '"/>'
259 + ' </div>'
260 + ' <div style="float: left">'
261 + ' <input type="text" name="networking[' + i + '][address]" value="" size="30"/>'
262 + ' <a href="javascript:removeNetworking(' + i + ')">'
263 + ' <img src="images/icons/cross.gif" alt="cross" title="Supprimer cet élément"/>'
264 + ' </a>'
265 + ' </div>'
266 + ' </td>'
267 + '</tr>';
268
269 $('#networking').before(html);
270 }
271
272 function removeNetworking(id)
273 {
274 $('#networking_' + id).remove();
275 }
276
277 function updateNetworking(i)
278 {
279 var name = document.getElementById('networking_name_' + i);
280 var type = document.getElementById('networking_type_' + i);
281 if (type != null && name != null) {
282 name.value = type.options[type.selectedIndex].text;
283 }
284
285 }
286
287 // Addresses {{{1
288
289 function toggleAddress(id, val)
290 {
291 $('#addresses_' + id + '_grayed').toggle();
292 $('#addresses_' + id).toggle();
293 $('#addresses_' + id + '_cont').find('[name*=removed]').val(val);
294 checkCurrentAddress();
295 }
296
297 function checkCurrentAddress(id)
298 {
299 var hasCurrentAddress = id ? true : false;
300 var i = 0;
301 while ($('#addresses_' + i + '_cont').length != 0) {
302 if ($('#addresses_' + i + '_cont').find('[name*=removed]').val() == 1) {
303 $('#addresses_' + i + '_cont').find('[name*=current]').attr('checked', false);
304 }
305 if (!hasCurrentAddress && $('#addresses_' + i + '_cont').find('[name*=current]:checked').length != 0) {
306 hasCurrentAddress = true;
307 } else {
308 $('#addresses_' + i + '_cont').find('[name*=current]').attr('checked', false);
309 }
310 i++;
311 }
312 if (!hasCurrentAddress) {
313 i = 0;
314 while ($('#addresses_' + i + '_cont').length != 0) {
315 if ($('#addresses_' + i + '_cont').find('[name*=removed]').val() == 0) {
316 $('#addresses_' + i + '_cont').find('[name*=current]').attr('checked', 'checked');
317 break;
318 }
319 i++;
320 }
321 }
322 if (id) {
323 $('#addresses_' + id + '_cont').find('[name*=current]').attr('checked', 'checked');
324 }
325 }
326
327 function addAddress()
328 {
329 var i = 0;
330 while ($('#addresses_' + i + '_cont').length != 0) {
331 i++;
332 }
333 $('#add_address').before('<div id="addresses_' + i + '_cont"></div>');
334 Ajax.update_html('addresses_' + i + '_cont', 'profile/ajax/address/' + i, checkCurrentAddress());
335 }
336
337 function addressChanged(id)
338 {
339 $('#addresses_' + id + '_cont').find('[name*=changed]').val("1");
340 }
341
342 function validGeoloc(id, geoloc)
343 {
344 if (geoloc == 1) {
345 $('#addresses_' + id + '_cont').find('[name*=text]').val($('#addresses_' + id + '_cont').find('[name*=geoloc]').val());
346 }
347 $('#addresses_' + id + '_cont').find('[name*=text]').removeClass('error');
348 $('#addresses_' + id + '_cont').find('[name*=geoloc_choice]').val(geoloc);
349 $('.addresses_' + id + '_geoloc').remove();
350 }
351
352 // {{{1 Phones
353
354 function addTel(prefid, prefname)
355 {
356 var i = 0;
357 var prefix = prefid + '_';
358 while (document.getElementById(prefix + i) != null) {
359 i++;
360 }
361 $('#' + prefix + 'add').before('<div id="' + prefix + i + '" style="clear: both; padding-top: 4px; padding-bottom: 4px"></div>');
362 Ajax.update_html(prefix + i, 'profile/ajax/tel/' + prefid + '/' + prefname + '/' + i);
363 }
364
365 function removeTel(id)
366 {
367 $('#' + id).remove();
368 }
369
370 function addPhoneComment(id, pref)
371 {
372 document.getElementById(id+'_comment').style.display = '';
373 document.getElementById(id+'_addComment').style.display = 'none';
374 }
375
376 function removePhoneComment(id, pref)
377 {
378 document.getElementById(id+'_comment').style.display = 'none';
379 document.forms.prof_annu[pref+ '[comment]'].value = '';
380 document.getElementById(id+'_addComment').style.display = '';
381 }
382
383 // Groups {{{1
384
385 function updateGroup(type)
386 {
387 var val = document.forms.prof_annu[type + '_sel'].value;
388 if (val == '0' || document.getElementById(type + '_' + val) != null) {
389 document.getElementById(type + '_add').style.display = 'none';
390 } else {
391 document.getElementById(type + '_add').style.display = '';
392 }
393 }
394
395 function removeGroup(cat, id)
396 {
397 $('#' + cat + '_' + id).remove();
398 updateGroup(cat);
399 }
400
401 function addGroup(cat)
402 {
403 var cb = document.forms.prof_annu[cat + '_sel'];
404 var id = cb.value;
405 var text = cb.options[cb.selectedIndex].text;
406 var html = '<tr id="' + cat + '_' + id + '">'
407 + ' <td>'
408 + ' <input type="hidden" name="' + cat + '[' + id + ']" value="' + text + '" />'
409 + ' </td>'
410 + ' <td>'
411 + ' <div style="float: left; width: 70%">'
412 + text
413 + ' </div>'
414 + ' <a href="javascript:removeGroup(\'' + cat + '\', ' + id + ')">'
415 + ' <img src="images/icons/cross.gif" alt="cross" title="Supprimer ce groupe" />'
416 + ' </a>'
417 + ' </td>'
418 + '</tr>';
419 $('#' + cat).after(html);
420 updateGroup(cat);
421 }
422
423 function updateGroupSubLink(cb)
424 {
425 var href = cb.value ? cb.value : "http://www.polytechnique.net";
426 document.getElementById("groupesx_sub").href = href;
427 }
428
429 // Medals {{{1
430
431 function updateMedal()
432 {
433 var val = document.forms.prof_annu['medal_sel'].value;
434 if (val == '' || document.getElementById('medal_' + val) != null) {
435 document.getElementById('medal_add').style.display = 'none';
436 } else {
437 document.getElementById('medal_add').style.display = '';
438 }
439 }
440
441 function getMedalName(id)
442 {
443 document.getElementById('medal_name_' + id).innerHTML = names[id];
444 }
445
446 function buildGrade(id, current)
447 {
448 var grade;
449 var subg = subgrades[id];
450 var obj = $('#medal_grade_' + id);
451 if (!subg) {
452 obj.prepend('<input type="hidden" name="medals[' + id + '][grade]" value="0" />');
453 } else {
454 var html = 'Agrafe : <select name="medals[' + id + '][grade]">';
455 html += '<option value="0">Non précisée</option>';
456 for (grade = 0 ; grade < subg.length ; grade++) {
457 html += '<option value="' + subg[grade][0] + '"';
458 if (subg[grade][0] == current) {
459 html += ' selected="selected"';
460 }
461 html += '>' + subg[grade][1] + '</option>';
462 }
463
464 html += '</select>';
465 obj.prepend(html);
466 }
467 }
468
469 function makeAddProcess(id)
470 {
471 return function(data)
472 {
473 $('#medals').after(data);
474 updateMedal();
475 getMedalName(id);
476 buildGrade(id, 0);
477 };
478 }
479
480 function addMedal()
481 {
482 var id = document.forms.prof_annu['medal_sel'].value;
483 $.get(platal_baseurl + 'profile/ajax/medal/' + id, makeAddProcess(id));
484 }
485
486 function removeMedal(id)
487 {
488 $("#medal_" + id).remove();
489 updateMedal();
490 }
491
492 // Jobs {{{1
493
494 function removeJob(id, pref)
495 {
496 document.getElementById(id + '_cont').style.display = 'none';
497 if (document.forms.prof_annu[pref + '[new]'].value == '0') {
498 document.getElementById(id + '_grayed').style.display = '';
499 document.getElementById(id + '_grayed_name').innerHTML =
500 document.forms.prof_annu[pref + "[name]"].value.replace('<', '&lt;');
501 }
502 document.forms.prof_annu[pref + "[removed]"].value = "1";
503 }
504
505 function restoreJob(id, pref)
506 {
507 document.getElementById(id + '_cont').style.display = '';
508 document.getElementById(id + '_grayed').style.display = 'none';
509 document.forms.prof_annu[pref + "[removed]"].value = "0";
510 }
511
512 function updateJobSecteur(nb, id, pref, sel)
513 {
514 var secteur = document.forms.prof_annu[pref + '[secteur]'].value;
515 if (secteur == '') {
516 secteur = '-1';
517 }
518 Ajax.update_html(id + '_ss_secteur', 'profile/ajax/secteur/' + nb + '/' + id + '/' + pref + '/' + secteur + '/' + sel);
519 }
520
521 function updateJobSousSecteur(nb, id, pref, sel)
522 {
523 var ssecteur = document.forms.prof_annu[pref + '[ss_secteur]'].value;
524 if (ssecteur == '') {
525 ssecteur = '-1';
526 }
527 Ajax.update_html(id + '_sss_secteur', 'profile/ajax/ssecteur/' + nb + '/' + ssecteur + '/' + sel);
528 }
529
530 function displayAllSector(id)
531 {
532 $('.sector_text_' + id).remove();
533 $('.sector_' + id).show();
534 }
535
536 function makeAddJob(id)
537 {
538 return function(data)
539 {
540 $('#add_job').before(data);
541 registerEnterpriseAutocomplete(id);
542 updateSecteur('job_' + id, 'jobs[' + id + ']', '');
543 };
544 }
545
546 function addJob()
547 {
548 var i = 0;
549 while (document.getElementById('job_' + i) != null) {
550 ++i;
551 }
552 $.get(platal_baseurl + 'profile/ajax/job/' + i, makeAddJob(i));
553 }
554
555 function addEntreprise(id)
556 {
557 $('.entreprise_' + id).toggle();
558 }
559
560 // {{{1 Skills
561
562 function updateSkill(cat)
563 {
564 var val = document.forms.prof_annu[cat + '_sel'].value;
565 var show = true;
566 if (val == '') {
567 show = false;
568 }
569 if (document.getElementById(cat + '_' + val) != null) {
570 show = false;
571 }
572 document.getElementById(cat + '_add').style.display = show ? '' : 'none';
573 }
574
575 function addSkill(cat)
576 {
577 var sel = document.forms.prof_annu[cat + '_sel'];
578 var val = sel.value;
579 var text = sel.options[sel.selectedIndex].text;
580 $.get(platal_baseurl + 'profile/ajax/skill/' + cat + '/' + val,
581 function(data) {
582 $('#' + cat).append(data);
583 document.getElementById(cat + '_' + val + '_title').innerHTML = text;
584 updateSkill(cat);
585 });
586 }
587
588 function removeSkill(cat, id)
589 {
590 $('#' + cat + '_' + id).remove();
591 updateSkill(cat);
592 }
593
594 // Mentor {{{1
595
596 function updateCountry()
597 {
598 var val = document.forms.prof_annu.countries_sel.value;
599 var show = true;
600 if (val == '' || val == '00') {
601 show = false;
602 }
603 if (document.getElementById('countries_' + val) != null) {
604 show = false;
605 }
606 document.getElementById('countries_add').style.display = show ? '' : 'none';
607 }
608
609 function addCountry()
610 {
611 var cb = document.forms.prof_annu.countries_sel;
612 var val = cb.value;
613 var text = cb.options[cb.selectedIndex].text;
614 var html = '<div id="countries_' + val + '" style="clear: both; margin-bottom: 0.7em">'
615 + ' <a href="javascript:removeCountry(\'' + val + '\')" style="display: block; float:right">'
616 + ' <img src="images/icons/cross.gif" alt="" title="Supprimer ce pays" />'
617 + ' </a>'
618 + ' <div style="float: left; width: 50%">' + text + '</div>'
619 + ' <input type="hidden" name="countries[' + val + ']" value="' + text + '" />'
620 + '</div>';
621 $('#countries').append(html);
622 updateCountry();
623 }
624
625 function removeCountry(id)
626 {
627 $('#countries_' + id).remove();
628 updateCountry();
629 }
630 function updateSSecteur()
631 {
632 var s = document.forms.prof_annu.secteur_sel.value;
633 var ss = document.forms.prof_annu['jobs[-1][ss_secteur]'].value;
634 var show = true;
635 if (s == '' || ss == '') {
636 show = false;
637 }
638 if (document.getElementById('secteurs_' + s + '_' + ss) != null) {
639 show = false;
640 }
641 document.getElementById('secteurs_add').style.display = show ? 'block' : 'none';
642 }
643
644 function updateSecteur()
645 {
646 var secteur = document.forms.prof_annu.secteur_sel.value;
647 if (secteur == '') {
648 secteur = '-1';
649 document.getElementById('ss_secteur_sel').innerHTML = '';
650 return;
651 }
652 $.get(platal_baseurl + 'profile/ajax/secteur/-1/0/0/' + secteur,
653 function(data) {
654 data = '<a href="javascript:addSecteur()" style="display: none; float: right" id="secteurs_add">'
655 + ' <img src="images/icons/add.gif" alt="" title="Ajouter ce secteur" />'
656 + '</a>' + data;
657 document.getElementById('ss_secteur_sel').innerHTML = data;
658 $(document.forms.prof_annu['jobs[-1][ss_secteur]']).change(updateSSecteur);
659 });
660 }
661
662 function addSecteur()
663 {
664 var scb = document.forms.prof_annu.secteur_sel;
665 var s = scb.value;
666 var st = scb.options[scb.selectedIndex].text;
667
668 var sscb = document.forms.prof_annu['jobs[-1][ss_secteur]'];
669 var ss = sscb.value;
670 var sst = sscb.options[sscb.selectedIndex].text;
671
672 var html = '<div id="secteurs_' + s + '_' + ss + '" style="clear: both; margin-top: 0.5em" class="titre">'
673 + ' <a href="javascript:removeSecteur(\'' + s + '\', \'' + ss + '\')" style="display: block; float: right">'
674 + ' <img src="images/icons/cross.gif" alt="" title="Supprimer ce secteur" />'
675 + ' </a>'
676 + ' <input type="hidden" name="secteurs[' + s + '][' + ss + ']" value="' + sst + '" />'
677 + ' ' + sst
678 + '</div>';
679 $('#secteurs').append(html);
680 updateSSecteur();
681 }
682
683 function removeSecteur(s, ss)
684 {
685 $('#secteurs_' + s + '_' + ss).remove();
686 updateSSecteur();
687 }
688
689 function registerEnterpriseAutocomplete(id)
690 {
691 $(".enterprise_name").each(
692 function() {
693 if (id == -1 || this.name == "jobs[" + id + "][name]") {
694 $(this).autocomplete(platal_baseurl + "search/autocomplete/entreprise",
695 {
696 selectOnly:1,
697 field:this.name,
698 matchSubset:0,
699 width:$(this).width()
700 });
701 }
702 }
703 );
704
705 $(".sector_name").each(
706 function() {
707 if (id == -1 || this.name == "jobs[" + id + "][sss_secteur_name]") {
708 $(this).autocomplete(platal_baseurl + "search/autocomplete/sss_secteur",
709 {
710 selectOnly:1,
711 field:this.name,
712 matchSubset:0,
713 width:$(this).width()
714 });
715 }
716 }
717 );
718 }
719
720 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: