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