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