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