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