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