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