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