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