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