Replace sectors by job terms in profile and search (job and mentoring).
[platal.git] / htdocs / javascript / profile.js
index 8d1544a..6145692 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *  Copyright (C) 2003-2009 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-// Page initialization
+// Page initialization {{{1
 
 function wizPage_onLoad(id)
 {
     switch (id) {
       case 'general':
-        var i = 0;
-        var prefix  = 'edu_';
-        while ($('.' + prefix + i).length != 0) {
-            i++;
-        }
-        i--;
-        for (var j = 0; j < i; j++) {
-            prepareType(j);
+        var i = 1;
+        while ($('.edu_' + i).length != 0) {
+            prepareType(i - 1);
+            ++i;
         }
         break;
+      case 'adresses':
+        checkCurrentAddress();
+        break;
       case 'poly':
-        updateGroupSubLink(document.forms.prof_annu.groupesx_sub);
+        updateGroupSubLink();
         break;
       case 'deco':
         for (var i in names) {
-            if (typeof names[i] != 'function') {
-                if (document.getElementById("medal_" + i) != null) {
-                    getMedalName(i);
-                    buildGrade(i, document.forms.prof_annu["medal_" + i + "_grade"].value);
-                }
+            if ($('#medal_' + i).length != 0) {
+                getMedalName(i);
+                buildGrade(i, $('#medal_' + i).find('[name*=medal_' + i + '_grade]').val());
             }
         }
         break;
       case 'emploi':
-        for (var i = 0 ; document.getElementById('job_' + i) != null ; ++i) {
-            updateJobSecteur(i, 'job_' + i, 'jobs[' + i + ']',
-                             document.forms.prof_annu["jobs[" + i + "][ss_secteur]"].value);
-            updateJobSousSecteur(i, 'job_' + i, 'jobs[' + i + ']',
-                             document.forms.prof_annu["jobs[" + i + "][sss_secteur]"].value);
+        for (var i = 0 ; $('#job_' + i).length != 0; ++i) {
+            updateJobSector(i, $('#job_' + i).find("[name='jobs[" + i + "][subSector]']").val());
+            updateJobSubSector(i, $('#job_' + i).find("[name='jobs[" + i + "][subSubSector]']").val());
+            updateJobAlternates(i);
         }
-        setTimeout('registerEnterpriseAutocomplete(-1)', 100);
         break;
     }
 }
@@ -62,72 +57,32 @@ function wizPage_onLoad(id)
 var educationDegree;
 var educationDegreeAll;
 var educationDegreeName;
-
-// General
-
 var subgrades;
 var names;
-function fillType(selectCtrl, edu, fill)
-{
-    var i;
-    var i0 = 0;
-
-    for (i = selectCtrl.options.length; i >= 0; i--) {
-        selectCtrl.options[i] = null;
-    }
-
-    if (fill || edu < 0) {
-        selectCtrl.options[0] = new Option(' ');
-        i0 = 1;
-    }
-    if (edu >= 0) {
-        for (i = 0; i < educationDegree[edu].length; i++) {
-            selectCtrl.options[i0 + i] = new Option(educationDegreeName[educationDegree[edu][i] - 1], educationDegree[edu][i]);
-        }
-    } else if (fill) {
-        for (i = 0; i < educationDegreeAll.length; i++) {
-            selectCtrl.options[i0 + i] = new Option(educationDegreeName[educationDegreeAll[i] - 1], educationDegreeAll[i]);
-        }
-    }
-}
 
+// Names {{{1
 
-function selectType(selectCtrl, type)
+function toggleNamesAdvanced()
 {
-    for (i = 0; i < selectCtrl.options.length; i++) {
-        if (selectCtrl.options[i].value == type) {
-            selectCtrl.selectedIndex = i;
-        }
-    }
-}
-
-function prepareType(i)
-{
-    fillType(document.forms.prof_annu["edus[" + i + "][degreeid]"], document.forms.prof_annu["edus[" + i + "][eduid]"].selectedIndex - 1);
-    selectType(document.forms.prof_annu["edus[" + i + "][degreeid]"], document.forms.prof_annu["edu_" + i + "_tmp"].value);
-}
-
-function displayNamesAdvanced()
-{
-    $('.names_advanced').show();
+    $('.names_advanced').toggle();
 }
 
-function addSearchName()
+function addSearchName(isFemale)
 {
     var i = 0;
     while ($('#search_name_' + i).length != 0) {
         i++;
     }
-    Ajax.update_html('search_name_' + i, 'profile/ajax/searchname/' + i, function(data){
+    Ajax.update_html('search_name_' + i, 'profile/ajax/searchname/' + i + '/' + isFemale, function(data){
         $('#searchname').before(data);
         changeNameFlag(i);
     });
 }
 
-function removeSearchName(i)
+function removeSearchName(i, isFemale)
 {
     $('#search_name_' + i).remove();
-    updateNameDisplay();
+    updateNameDisplay(isFemale);
 }
 
 function changeNameFlag(i)
@@ -135,7 +90,7 @@ function changeNameFlag(i)
     $('#flag_' + i).remove();
     var typeid = $('#search_name_' + i).find('select').val();
     var type   = $('#search_name_' + i).find('select :selected').text();
-    if ($('[@name=sn_type_' + typeid + '_' + i + ']').val() > 0) {
+    if ($('[name=sn_type_' + typeid + '_' + i + ']').val() > 0) {
         $('#flag_cb_' + i).after('<span id="flag_' + i + '">&nbsp;' +
             '<img src="images/icons/flag_green.gif" alt="site public" title="site public" />' +
             '<input type="hidden" name="search_names[' + i + '][pub]" value="1"/>' +
@@ -149,46 +104,122 @@ function changeNameFlag(i)
     }
 }
 
-function updateNameDisplay()
+function updateNameDisplay(isFemale)
 {
     var searchnames = '';
     for (var i = 0; i < 10; i++) {
         if ($('#search_name_' + i).find(':text').val()) {
             searchnames += $('#search_name_' + i).find('[name*=typeid]').val() + ';';
-            searchnames += $('#search_name_' + i).find(':checked').length-1 + ';';
             searchnames += $('#search_name_' + i).find(':text').val() + ';;';
         }
     }
-    Ajax.update_html(null, 'profile/ajax/buildnames/' + searchnames, function(data){
+    Ajax.update_html(null, 'profile/ajax/buildnames/' + searchnames + '/' + isFemale, function(data){
         var name = data.split(';');
         $('#public_name').html(name[0]);
         $('#private_name').html(name[0] + name[1]);
     });
 }
 
+function toggleParticle(id)
+{
+    if ($('#search_name_' + id).find("[name*='[particle]']").val() == '') {
+        $('#search_name_' + id).find("[name*='[particle]']").val(1);
+    } else {
+        $('#search_name_' + id).find("[name*='[particle]']").val('');
+    }
+}
+
+// Promotions {{{1
+
+function togglePromotionEdition()
+{
+    $(".promotion_edition").toggle();
+}
+
+// Nationalities {{{1
+
 function delNationality(i)
 {
-    $('#nationalite' + i).hide().find('select').val('');
+    $('#nationality' + i).hide().find('select').val('');
 }
 
 function addNationality()
 {
     var i = 0;
-    if ($('#nationalite2').find('select').val() == "") {
+    if ($('#nationality2').find('select').val() == "") {
         i = 2;
-    } else if ($('#nationalite3').find('select').val() == "") {
+    } else if ($('#nationality3').find('select').val() == "") {
         i = 3;
     }
     if ((i == 2) || (i == 3)) {
-        $('#nationalite' + i).show();
+        $('#nationality' + i).show();
+    }
+}
+
+// Education {{{1
+
+function prepareType(id)
+{
+    var edu    = $('.edu_' + id).find("[name='edus[" + id + "][eduid]']").val() - 1;
+    var sel    = $('.edu_' + id).find('[name=edu_' + id + '_tmp]').val();
+    var html   = '';
+    var length = educationDegree[edu].length;
+    for (i = 0; i < length; ++i) {
+        html += '<option value="' + educationDegree[edu][i] + '"';
+        if (sel == educationDegree[edu][i]) {
+            html += ' selected="selected"';
+        }
+        html += '>' + educationDegreeName[educationDegree[edu][i] - 1] + '</option>';
     }
+    $('.edu_' + id).find("[name='edus[" + id + "][degreeid]']").html(html);
 }
 
+function addEdu()
+{
+    var i = 0;
+    var j = 0;
+    var prefix  = 'edu_';
+    var class_parity;
+
+    while (!$('#edu_add').hasClass(prefix + i)) {
+        if ($('.' + prefix + i).length != 0) {
+            j++;
+        }
+        i++;
+    }
+    if (j % 2) {
+        class_parity = 'pair';
+    } else {
+        class_parity = 'impair';
+    }
+    $('#edu_add').removeClass(prefix + i);
+    i++;
+    $('#edu_add').addClass(prefix + i);
+    i--;
+    $.get(platal_baseurl + 'profile/ajax/edu/' + i + '/' + class_parity,
+          function(data) {
+              $('#edu_add').before(data);
+              prepareType(i);
+          });
+}
+
+function removeEdu(i)
+{
+    var prefix  = 'edu_';
+    $('.' + prefix + i).remove();
+    while (!$('#edu_add').hasClass(prefix + i)) {
+        $('.' + prefix + i).toggleClass('pair');
+        $('.' + prefix + i).toggleClass('impair');
+        i++;
+    }
+}
+
+// Networking {{{1
+
 function addNetworking()
 {
     var i = 0;
-    var nws = 'networking_';
-    while (document.getElementById(nws + i) != null) {
+    while ($('#networking_' + i).length != 0) {
         i++;
     }
     var namefirst = '';
@@ -199,7 +230,7 @@ function addNetworking()
         + '        <input type="checkbox" name="networking[' + i + '][pub]"/>'
         + '        <img src="images/icons/flag_green.gif" alt="site public" title="site public">'
         + '      </span>&nbsp;'
-        + '      <select id="networking_type_' + i + '" name="networking[' + i + '][type]" onchange="javascript:updateNetworking(' + i + ');">';
+        + '      <select name="networking[' + i + '][type]" onchange="javascript:updateNetworking(' + i + ');">';
     for (nw in nw_list) {
         if (namefirst == '') {
             namefirst = nw;
@@ -207,7 +238,7 @@ function addNetworking()
         html += '  <option value="' + nw_list[nw] + '">' + nw + '</option>';
     }
     html += '</select>'
-        + '      <input type="hidden" id="networking_name_' + i + '" name="networking[' + i + '][name]" value="' + namefirst + '"/>'
+        + '      <input type="hidden" name="networking[' + i + '][name]" value="' + namefirst + '"/>'
         + '    </div>'
         + '    <div style="float: left">'
         + '      <input type="text" name="networking[' + i + '][address]" value="" size="30"/>'
@@ -228,245 +259,181 @@ function removeNetworking(id)
 
 function updateNetworking(i)
 {
-    var name = document.getElementById('networking_name_' + i);
-    var type = document.getElementById('networking_type_' + i);
-    if (type != null && name != null) {
-        name.value = type.options[type.selectedIndex].text;
-    }
-
+    $('#networking_' + i).find("[name='networking[" + i + "][name]']").val($('#networking_' + i).find('select option:selected').text());
 }
 
-// Addresses
+// Addresses {{{1
 
-function removeObject(id, pref)
+function toggleAddress(id, val)
 {
-    document.getElementById(id).style.display = "none";
-    document.forms.prof_annu[pref + "[removed]"].value = "1";
+    $('#addresses_' + id + '_grayed').toggle();
+    $('#addresses_' + id).toggle();
+    $('#addresses_' + id + '_cont').find('[name*=removed]').val(val);
+    checkCurrentAddress();
 }
 
-function restoreObject(id, pref)
+function checkCurrentAddress(id)
 {
-    document.getElementById(id).style.display = '';
-    document.forms.prof_annu[pref + "[removed]"].value = "0";
-}
-
-function getAddressElement(adrid, adelement)
-{
-    return document.forms.prof_annu["addresses[" + adrid + "][" + adelement + "]"];
-}
-
-function checkCurrentAddress(newCurrent)
-{
-    var hasCurrent = false;
+    var hasCurrentAddress = id ? true : false;
     var i = 0;
-    while (getAddressElement(i, 'pub') != null) {
-        var radio = getAddressElement(i, 'current');
-        var removed = getAddressElement(i, 'removed');
-        if (removed.value == "1" && radio.checked) {
-            radio.checked = false;
-        } else if (radio.checked && radio != newCurrent) {
-            radio.checked = false;
-        } else if (radio.checked) {
-            hasCurrent = true;
+    while ($('#addresses_' + i + '_cont').length != 0) {
+        if ($('#addresses_' + i + '_cont').find('[name*=removed]').val() == 1) {
+            $('#addresses_' + i + '_cont').find('[name*=current]').attr('checked', false);
+        }
+        if (!hasCurrentAddress && $('#addresses_' + i + '_cont').find('[name*=current]:checked').length != 0) {
+            hasCurrentAddress = true;
+        } else {
+            $('#addresses_' + i + '_cont').find('[name*=current]').attr('checked', false);
         }
         i++;
     }
-    if (!hasCurrent) {
+    if (!hasCurrentAddress) {
         i = 0;
-        while (getAddressElement(i, 'pub') != null) {
-            var radio = getAddressElement(i, 'current');
-            var removed = getAddressElement(i, 'removed');
-            if (removed.value != "1") {
-                radio.checked= true;
-                return;
-            }
-            i++;
+        while ($('#addresses_' + i + '_cont').length != 0) {
+               if ($('#addresses_' + i + '_cont').find('[name*=removed]').val() == 0) {
+                   $('#addresses_' + i + '_cont').find('[name*=current]').attr('checked', 'checked');
+                   break;
+               }
+               i++;
         }
     }
-}
-
-function removeAddress(id, pref)
-{
-    removeObject(id, pref);
-    checkCurrentAddress(null);
-    if (document.forms.prof_annu[pref + '[datemaj]'].value != '') {
-        document.getElementById(id + '_grayed').style.display = '';
+    if (id) {
+        $('#addresses_' + id + '_cont').find('[name*=current]').attr('checked', 'checked');
     }
 }
 
-function restoreAddress(id, pref)
-{
-    document.getElementById(id +  '_grayed').style.display = 'none';
-    checkCurrentAddress(null);
-    restoreObject(id, pref);
-}
-
 function addAddress()
 {
     var i = 0;
-    while (getAddressElement(i, 'pub') != null) {
+    while ($('#addresses_' + i + '_cont').length != 0) {
         i++;
     }
-    $("#add_adr").before('<div id="addresses_' + i + '_cont"></div>');
-    Ajax.update_html('addresses_' + i + '_cont', 'profile/ajax/address/' + i, checkCurrentAddress);
+    $('#add_address').before('<div id="addresses_' + i + '_cont"></div>');
+    Ajax.update_html('addresses_' + i + '_cont', 'profile/ajax/address/' + i, checkCurrentAddress());
 }
 
-function addEdu()
+function addressChanged(prefid)
 {
-    var i = 0;
-    var j = 0;
-    var prefix  = 'edu_';
-    var class_parity;
-
-    while (!$('#edu_add').hasClass(prefix + i)) {
-        if ($('.' + prefix + i).length != 0) {
-            j++;
-        }
-        i++;
-    }
-    if (j % 2) {
-        class_parity = 'pair';
-    } else {
-        class_parity = 'impair';
-    }
-    $('#edu_add').removeClass(prefix + i);
-    i++;
-    $('#edu_add').addClass(prefix + i);
-    i--;
-    $.get(platal_baseurl + 'profile/ajax/edu/' + i + '/' + class_parity,
-          function(data) {
-              $('#edu_add').before(data);
-              prepareType(i);
-          });
+    $('#' + prefid + '_cont').find('[name*=changed]').val("1");
 }
 
-function removeEdu(i)
+function validGeoloc(prefid, id, geoloc)
 {
-    var prefix  = 'edu_';
-    $('.' + prefix + i).remove();
-    while (!$('#edu_add').hasClass(prefix + i)) {
-        $('.' + prefix + i).toggleClass('pair');
-        $('.' + prefix + i).toggleClass('impair');
-        i++;
+    if (geoloc == 1) {
+        $('#' + prefid + '_cont').find('[name*=text]').val($('#' + prefid + '_cont').find('[name*=geoloc]').val());
+        $('#' + prefid + '_cont').find('[name*=postalText]').val($('#' + prefid + '_cont').find('[name*=geocodedPostalText]').val());
+    }
+    if (geoloc > 0) {
+        $('#' + prefid + '_cont').find("[name*='[geoloc]']").remove();
     }
+    $('#' + prefid + '_cont').find('[name*=text]').removeClass('error');
+    $('#' + prefid + '_cont').find('[name*=geoloc_choice]').val(geoloc);
+    $('.' + prefid + '_geoloc').remove();
 }
 
+// {{{1 Phones
+
 function addTel(prefid, prefname)
 {
     var i = 0;
     var prefix  = prefid + '_';
-    while (document.getElementById(prefix + i) != null) {
+    while ($('#' + prefix + i).length != 0) {
         i++;
     }
     $('#' + prefix + 'add').before('<div id="' + prefix + i + '" style="clear: both; padding-top: 4px; padding-bottom: 4px"></div>');
     Ajax.update_html(prefix + i, 'profile/ajax/tel/' + prefid + '/' + prefname + '/' + i);
 }
 
-function removeTel(id)
+function removeTel(prefname, prefid, id)
 {
-    $('#' + id).remove();
+    var total = 0;
+    while ($('#' + prefid + '_' + total).length != 0) {
+        ++total;
+    }
+    $('#' + prefid + '_' + id).remove();
+    for (var i = parseInt(id) + 1; i < total; ++i) {
+        renumberPhone(prefname, prefid, i);
+    }
 }
 
-function addPhoneComment(id, pref)
+function addPhoneComment(id)
 {
-    document.getElementById(id+'_comment').style.display = '';
-    document.getElementById(id+'_addComment').style.display = 'none';
+    $('#' + id + '_comment').show();
+    $('#' + id + '_addComment').hide();
 }
 
 function removePhoneComment(id, pref)
 {
-    document.getElementById(id+'_comment').style.display = 'none';
-    document.forms.prof_annu[pref+ '[comment]'].value = '';
-    document.getElementById(id+'_addComment').style.display = '';
+    $('#' + id + '_comment').hide();
+    $('#' + id + '_comment').find("[name='" + pref + "[comment]']").val('');
+    $('#' + id + '_addComment').show();
 }
 
-// Geoloc
-
-function validGeoloc(id, pref)
+function renumberPhone(prefname, prefid, i)
 {
-    document.getElementById(id + '_geoloc').style.display = 'none';
-    document.getElementById(id + '_geoloc_error').style.display = 'none';
-    document.getElementById(id + '_geoloc_valid').style.display = 'none';
-    document.forms.prof_annu[pref + "[parsevalid]"].value = "1";
-    document.forms.prof_annu[pref + "[text]"].value = document.forms.prof_annu[pref + "[geoloc]"].value;
-    document.forms.prof_annu[pref + "[cityid]"].value = document.forms.prof_annu[pref + "[geoloc_cityid]"].value;
-    $(document.forms.prof_annu[pref + "[text]"]).click(function() { document.forms.prof_annu[pref + "[text]"].blur(); });
-    document.forms.prof_annu[pref + "[text]"].className = '';
-}
+    var telid = i - 1;
+    var telprefOld = prefname + '[' + i + ']';
+    var telpref = prefname + '[' + telid + ']';
+    var idOld = prefid + '_' + i;
+    var id = prefid + '_' + telid;
 
-function validAddress(id, pref)
-{
-    document.getElementById(id + '_geoloc').style.display = 'none';
-    document.getElementById(id + '_geoloc_error').style.display = 'none';
-    document.getElementById(id + '_geoloc_valid').style.display = 'none';
-    document.forms.prof_annu[pref + "[parsevalid]"].value = "1";
-    $(document.forms.prof_annu[pref + "[text]"]).click(function() { document.forms.prof_annu[pref + "[text]"].blur(); });
-    document.forms.prof_annu[pref + "[text]"].className = '';
+    $('#' + idOld).attr('id', id);
+    $('#' + id).find('div.titre').html('N°' + i);
+    $('#' + id).find('a.removeTel').attr('href', 'javascript:removeTel(\'' + prefname + '\',\'' + prefid + '\',' + telid + ')');
+    $('#' + id).find('select').attr('name', telpref + '[type]');
+    $('#' + id).find("[name='" + telprefOld + "[display]']").attr('name', telpref + '[display]');
+    $('#' + id).find("[name='" + telprefOld + "[comment]']").attr('name', telpref + '[comment]');
+    $('#' + id).find('a.removePhoneComment').attr('href', 'javascript:removePhoneComment(' + id + ',' + telpref + ')');
+    $('#' + id).find('#' + idOld + '_addComment').attr('id', id + '_addComment');
+    $('#' + id).find('#' + id + '_addComment').attr('href', 'javascript:addPhoneComment(' + id + ')');
+    $('#' + id).find('#' + idOld + '_comment').attr('id', id + '_comment');
+    $('#' + id).find("[name='" + telprefOld + "[pub]']").attr('name', telpref + '[pub]');
 }
 
+// {{{1 Groups
 
-// Groups
-
-function updateGroup(type)
+function addBinet()
 {
-    var val = document.forms.prof_annu[type + '_sel'].value;
-    if (val == '0' || document.getElementById(type + '_' + val) != null) {
-        document.getElementById(type + '_add').style.display = 'none';
-    } else {
-        document.getElementById(type + '_add').style.display = '';
-    }
-}
-
-function removeGroup(cat, id)
-{
-    $('#' + cat + '_' + id).remove();
-    updateGroup(cat);
-}
-
-function addGroup(cat)
-{
-    var cb   = document.forms.prof_annu[cat + '_sel'];
-    var id   = cb.value;
-    var text = cb.options[cb.selectedIndex].text;
-    var html = '<tr id="' + cat + '_' + id + '">'
-        + '  <td>'
-        + '    <input type="hidden" name="' + cat + '[' + id + ']" value="' + text + '" />'
-        + '  </td>'
-        + '  <td>'
-        + '    <div style="float: left; width: 70%">'
-        +        text
-        + '    </div>'
-        + '    <a href="javascript:removeGroup(\'' + cat + '\', ' + id + ')">'
-        + '      <img src="images/icons/cross.gif" alt="cross" title="Supprimer ce groupe" />'
-        + '    </a>'
-        + '  </td>'
-        + '</tr>';
-    $('#' + cat).after(html);
-    updateGroup(cat);
+    var id   = $('#binets_table').find('[name=binets_sel]').val();
+    var text = $('#binets_table').find('select option:selected').text();
+    var html = '<tr id="binets_' + id + '">'
+             + '  <td>'
+             + '    <input type="hidden" name="binets[' + id + ']" value="' + text + '" />'
+             + '  </td>'
+             + '  <td>'
+             + '    <div style="float: left; width: 70%">'
+             +        text
+             + '    </div>'
+             + '    <a href="javascript:removeElement(\'binets\',' + id + ')">'
+             + '      <img src="images/icons/cross.gif" alt="cross" title="Supprimer ce groupe" />'
+             + '    </a>'
+             + '  </td>'
+             + '</tr>';
+    $('#binets_table').after(html);
+    updateElement('binets');
 }
 
-function updateGroupSubLink(cb)
+function updateGroupSubLink()
 {
-    var href = cb.value ? cb.value : "http://www.polytechnique.net";
-    document.getElementById("groupesx_sub").href = href;
+    var href = $('[name*=groupesx_sub]').val() ? $('[name*=groupesx_sub]').val() : 'http://www.polytechnique.net';
+    $('#groupesx_sub').attr('href', href);
 }
 
-
-// Medals
+// {{{1 Medals
 
 function updateMedal()
 {
-    var val = document.forms.prof_annu['medal_sel'].value;
-    if (val == '' || document.getElementById('medal_' + val) != null) {
-        document.getElementById('medal_add').style.display = 'none';
+    var val = $('#medals').find('[name*=medal_sel]').val();
+    if (val && ($('#medal_' + val).length == 0)) {
+        $('#medal_add').show();
     } else {
-        document.getElementById('medal_add').style.display = '';
+        $('#medal_add').hide();
     }
 }
 
 function getMedalName(id)
 {
-    document.getElementById('medal_name_' + id).innerHTML = names[id];
+    $('#medal_name_' + id).html(names[id]);
 }
 
 function buildGrade(id, current)
@@ -505,7 +472,7 @@ function makeAddProcess(id)
 
 function addMedal()
 {
-    var id = document.forms.prof_annu['medal_sel'].value;
+    var id = $('#medals').find('[name=medal_sel]').val();
     $.get(platal_baseurl + 'profile/ajax/medal/' + id, makeAddProcess(id));
 }
 
@@ -515,43 +482,59 @@ function removeMedal(id)
     updateMedal();
 }
 
-
-// Jobs
+// Jobs {{{1
 
 function removeJob(id, pref)
 {
-    document.getElementById(id + '_cont').style.display = 'none';
-    if (document.forms.prof_annu[pref + '[new]'].value == '0') {
-        document.getElementById(id + '_grayed').style.display = '';
-        document.getElementById(id + '_grayed_name').innerHTML =
-            document.forms.prof_annu[pref + "[name]"].value.replace('<', '&lt;');
+    $('#' + id + '_cont').hide();
+    if ($('#' + id).find("[name='" + pref + "[new]']").val() == '0') {
+        $('#' + id + '_grayed').show();
+        $('#' + id + '_grayed_name').html($('#' + id).find("[name='" + pref + "[name]']").val());
     }
-    document.forms.prof_annu[pref + "[removed]"].value = "1";
+    $('#' + id).find("[name='" + pref + "[removed]']").val('1');
 }
 
 function restoreJob(id, pref)
 {
-    document.getElementById(id + '_cont').style.display = '';
-    document.getElementById(id + '_grayed').style.display = 'none';
-    document.forms.prof_annu[pref + "[removed]"].value = "0";
+    $('#' + id + '_cont').show();
+    $('#' + id + '_grayed').hide();
+    $('#' + id).find("[name='" + pref + "[removed]']").val('0');
+}
+
+function updateJobSector(id, sel)
+{
+    var sector = $('#job_' + id).find("[name='jobs[" + id + "][sector]']").val();
+    if (sector == '') {
+        sector = '-1';
+    }
+    Ajax.update_html('job_' + id + '_subSector', 'profile/ajax/sector/' + id + '/job_' + id + '/jobs[' + id + ']/' + sector + '/' + sel);
 }
 
-function updateJobSecteur(nb, id, pref, sel)
+function updateJobSubSector(id, sel)
 {
-    var secteur = document.forms.prof_annu[pref + '[secteur]'].value;
-    if (secteur == '') {
-        secteur = '-1';
+    var subSector = $('#job_' + id).find("[name='jobs[" + id + "][subSector]']").val();
+    if (subSector == '') {
+        subSector = '-1';
     }
-    Ajax.update_html(id + '_ss_secteur', 'profile/ajax/secteur/' + nb + '/' + id + '/' + pref + '/' + secteur + '/' + sel);
+    Ajax.update_html('job_' + id + '_subSubSector', 'profile/ajax/sub_sector/' + id + '/' + subSector + '/' + sel);
 }
 
-function updateJobSousSecteur(nb, id, pref, sel)
+function updateJobAlternates(id)
 {
-    var ssecteur = document.forms.prof_annu[pref + '[ss_secteur]'].value;
-    if (ssecteur == '') {
-        ssecteur = '-1';
+    var subSubSector = $('#job_' + id).find("[name='jobs[" + id + "][subSubSector]']").val();
+    if (subSubSector != '') {
+        Ajax.update_html('job_' + id + '_alternates', 'profile/ajax/alternates/' + id + '/' + subSubSector);
     }
-    Ajax.update_html(id + '_sss_secteur', 'profile/ajax/ssecteur/' + nb + '/' + ssecteur + '/' + sel);
+}
+
+function emptyJobSubSector(id)
+{
+    Ajax.update_html('job_' + id + '_subSubSector', 'profile/ajax/sub_sector/' + id + '/-1/-1');
+}
+
+function emptyJobAlternates(id)
+{
+    Ajax.update_html('job_' + id + '_alternates', 'profile/ajax/alternates/' + id + '/-1');
 }
 
 function displayAllSector(id)
@@ -566,14 +549,13 @@ function makeAddJob(id)
     {
         $('#add_job').before(data);
         registerEnterpriseAutocomplete(id);
-        updateSecteur('job_' + id, 'jobs[' + id + ']', '');
     };
 }
 
 function addJob()
 {
     var i = 0;
-    while (document.getElementById('job_' + i) != null) {
+    while ($('#job_' + i).length != 0) {
         ++i;
     }
     $.get(platal_baseurl + 'profile/ajax/job/' + i, makeAddJob(i));
@@ -584,165 +566,253 @@ function addEntreprise(id)
     $('.entreprise_' + id).toggle();
 }
 
-// Skills
-
-function updateSkill(cat)
-{
-    var val  = document.forms.prof_annu[cat + '_sel'].value;
-    var show = true;
-    if (val == '') {
-        show = false;
+/**
+ * Adds a job term in job profile page
+ * @param jobid id of profile's job among his different jobs
+ * @param jtid id of job term to add
+ * @param full_name full text of job term
+ * @return false if the term already exist for this job, true otherwise
+ */
+function addJobTerm(jobid, jtid, full_name)
+{
+    var termid = 0;
+    var parentpath;
+    var formvarname;
+    if (jobid < 0) {
+        parentpath = '';
+        jobid = '';
+        formvarname = 'terms';
+    } else {
+        parentpath = '#job_'+jobid+' ';
+        formvarname = 'jobs['+jobid+'][terms]';
+    }
+    var lastJobTerm = $(parentpath + '.job_term:last');
+    if (lastJobTerm.length != 0) {
+        termid = parseInt(lastJobTerm.children('input').attr('name').replace(/^(jobs\[[0-9]+\]\[terms\]|terms)\[([0-9]+)\]\[jtid\]/, '$2')) + 1;
+        if ($('#job'+jobid+'_term'+jtid).length > 0) {
+            return false;
+        }
     }
-    if (document.getElementById(cat + '_' + val) != null) {
-        show = false;
+    var newdiv = '<div class="job_term" id="job'+jobid+'_term'+jtid+'">'+
+        '<span>'+full_name+'</span>'+
+        '<input type="hidden" name="'+formvarname+'['+termid+'][jtid]" value="'+jtid+'" />'+
+        '<img title="Retirer ce mot-clef" alt="retirer" src="images/icons/cross.gif" />'+
+        '</div>';
+    if (lastJobTerm.length == 0) {
+        $(parentpath + '.job_terms').prepend(newdiv);
+    } else {
+        lastJobTerm.after(newdiv);
     }
-    document.getElementById(cat + '_add').style.display = show ? '' : 'none';
+    $('#job'+jobid+'_term'+jtid+' img').css('cursor','pointer').click(removeJobTerm);
+    return true;
 }
 
-function addSkill(cat)
+/**
+ * Remove a job term in job profile page.
+ * Must be called from a button in a div containing the term
+ */
+function removeJobTerm()
 {
-    var sel  = document.forms.prof_annu[cat + '_sel'];
-    var val  = sel.value;
-    var text = sel.options[sel.selectedIndex].text;
-    $.get(platal_baseurl + 'profile/ajax/skill/' + cat + '/' + val,
-          function(data) {
-          $('#' + cat).append(data);
-          document.getElementById(cat + '_' + val + '_title').innerHTML = text;
-          updateSkill(cat);
-          });
+    $(this).parent().remove();
 }
 
-function removeSkill(cat, id)
+/**
+ * Prepare display for autocomplete suggestions in job terms
+ * @param row an array of (title of term, id of term)
+ * @return text to display
+ * If id is negative, it is because there are too much terms to
+ * be displayed.
+ */
+function displayJobTerm(row)
 {
-    $('#' + cat + '_' + id).remove();
-    updateSkill(cat);
+    if (row[1] < 0) {
+        return '... <em>prĂ©cise ta recherche</em> ...';
+    }
+    return row[0];
 }
 
+/**
+ * Function called when a job term has been selected from autocompletion
+ * in search
+ * @param li is the list item (<li>) that has been clicked
+ * The context is the jsquery autocomplete object.
+ */
+function selectJobTerm(li)
+{
+    if (li.extra[0] < 0) {
+        return;
+    }
+    var jobid = this.extraParams.jobid;
+    addJobTerm(jobid,li.extra[0],$(li).text());
+    var search_input;
+    if (jobid < 0) {
+        search_input = $('.term_search')[0];
+    } else {
+        search_input = $('#job_'+jobid+' .term_search')[0];
+    }
+    search_input.value = '';
+    search_input.focus();
+}
 
-// Mentor
-
-function updateCountry()
+/**
+ * Function to show or hide a terms tree in job edition
+ * @param jobid is the id of the job currently edited
+ */
+function toggleJobTermsTree(jobid)
 {
-    var val = document.forms.prof_annu.countries_sel.value;
-    var show = true;
-    if (val == '' || val == '00') {
-        show = false;
+    var treepath;
+    if (jobid < 0) {
+        treepath = '';
+    } else {
+        treepath = '#job_'+jobid+' ';
     }
-    if (document.getElementById('countries_' + val) != null) {
-        show = false;
+    treepath += '.term_tree';
+    if ($(treepath + ' ul').length > 0) {
+        $(treepath).empty().removeClass().addClass('term_tree');
+        return;
     }
-    document.getElementById('countries_add').style.display = show ? '' : 'none';
+    createJobTermsTree(treepath, 'profile/ajax/tree/jobterms/all', 'job' + jobid, 'chooseJobTerm');
 }
 
+/**
+ * Function called when a job term is chosen from terms tree
+ * @param treeid is the full id of the tree (must look like job3)
+ * @param jtid is the id of the job term chosen
+ * @param fullname is the complete name (understandable without context) of the term
+ */
+function chooseJobTerm(treeid, jtid, fullname)
+{
+    addJobTerm(treeid.replace(/^job(.*)$/, '$1'), jtid, fullname);
+}
+
+// {{{1 Skills
+
+function addSkill(cat)
+{
+    var val  = $('#' + cat + '_table').find('[name=' + cat + '_sel]').val();
+    var text = $('#' + cat + '_table').find('[name=' + cat + '_sel] :selected').text();
+    $.get(platal_baseurl + 'profile/ajax/skill/' + cat + '/' + val,
+          function(data) {
+              $('#' + cat).append(data);
+              $('#' + cat + '_' + val + '_title').text(text);
+              updateElement(cat);
+          });
+}
+
+// {{{1 Mentor
+
 function addCountry()
 {
-    var cb   = document.forms.prof_annu.countries_sel;
-    var val  = cb.value;
-    var text = cb.options[cb.selectedIndex].text;
+    var val  = $('#countries_table').find('[name=countries_sel] :selected').val();
+    var text = $('#countries_table').find('[name=countries_sel] :selected').text();
     var html = '<div id="countries_' + val + '" style="clear: both; margin-bottom: 0.7em">'
-        + '  <a href="javascript:removeCountry(\'' + val + '\')" style="display: block; float:right">'
+        + '  <a href="javascript:removeElement(\'countries\',\'' + val + '\')" style="display: block; float:right">'
         + '    <img src="images/icons/cross.gif" alt="" title="Supprimer ce pays" />'
         + '  </a>'
         + '  <div style="float: left; width: 50%">' + text + '</div>'
         + '  <input type="hidden" name="countries[' + val + ']" value="' + text + '" />'
         + '</div>';
     $('#countries').append(html);
-    updateCountry();
+    updateElement('countries');
 }
 
-function removeCountry(id)
+function updateSubSector()
 {
-    $('#countries_' + id).remove();
-    updateCountry();
+    var s  = $('#sectorSelection').find('[name=sectorSelection]').val();
+    var ss = $('#subSectorSelection').find("[name='jobs[-1][subSector]']").val();
+    if ((s == '' || ss == '') || $('#sectors_' + s + '_' + ss).length != 0) {
+        $('#addSector').hide();
+    } else {
+        $('#addSector').show();
+    }
 }
-function updateSSecteur()
+
+function removeSector(s, ss)
 {
-    var s  = document.forms.prof_annu.secteur_sel.value;
-    var ss = document.forms.prof_annu['jobs[-1][ss_secteur]'].value;
-    var show = true;
-    if (s == '' || ss == '') {
-        show = false;
-    }
-    if (document.getElementById('secteurs_' + s + '_' + ss) != null) {
-        show = false;
-    }
-    document.getElementById('secteurs_add').style.display = show ? 'block' : 'none';
+    $('#sectors_' + s + '_' + ss).remove();
+    updateSubSector();
 }
 
-function updateSecteur()
+function updateSector()
 {
-    var secteur = document.forms.prof_annu.secteur_sel.value;
-    if (secteur == '') {
-        secteur = '-1';
-        document.getElementById('ss_secteur_sel').innerHTML = '';
+    var sector = $('#sectorSelection').find('[name=sectorSelection]').val();
+    if (sector == '') {
+        sector = '-1';
+        $('#subSectorSelection').html('');
         return;
     }
-    $.get(platal_baseurl + 'profile/ajax/secteur/-1/0/0/' + secteur,
+    $.get(platal_baseurl + 'profile/ajax/sector/-1/0/0/' + sector,
           function(data) {
-              data = '<a href="javascript:addSecteur()" style="display: none; float: right" id="secteurs_add">'
-                     +  '  <img src="images/icons/add.gif" alt="" title="Ajouter ce secteur" />'
-                     +  '</a>' + data;
-              document.getElementById('ss_secteur_sel').innerHTML = data;
-              $(document.forms.prof_annu['jobs[-1][ss_secteur]']).change(updateSSecteur);
+              data = '<a href="javascript:addSector()" style="display: none; float: right" id="addSector">'
+                   + '  <img src="images/icons/add.gif" alt="Ajouter ce secteur" title="Ajouter ce secteur" />'
+                   + '</a>' + data;
+              $('#subSectorSelection').html(data);
+              $('#subSectorSelection').find("[name='jobs[-1][subSector]']").change(updateSubSector);
           });
 }
 
-function addSecteur()
+function addSector()
 {
-    var scb = document.forms.prof_annu.secteur_sel;
-    var s  = scb.value;
-    var st = scb.options[scb.selectedIndex].text;
-
-    var sscb = document.forms.prof_annu['jobs[-1][ss_secteur]'];
-    var ss = sscb.value;
-    var sst = sscb.options[sscb.selectedIndex].text;
-
-    var html = '<div id="secteurs_' + s + '_' + ss + '" style="clear: both; margin-top: 0.5em" class="titre">'
-        + '  <a href="javascript:removeSecteur(\'' + s + '\', \'' + ss + '\')" style="display: block; float: right">'
-        + '    <img src="images/icons/cross.gif" alt="" title="Supprimer ce secteur" />'
-        + '  </a>'
-        + '  <input type="hidden" name="secteurs[' + s + '][' + ss + ']" value="' + sst + '" />'
-        + '  ' + sst
-        + '</div>';
-    $('#secteurs').append(html);
-    updateSSecteur();
-}
+    var s   = $('#sectorSelection').find('[name=sectorSelection]').val();
+    var ss  = $('#subSectorSelection').find("[name='jobs[-1][subSector]']").val();
+    var sst = $('#subSectorSelection').find("[name='jobs[-1][subSector]'] :selected").text();
 
-function removeSecteur(s, ss)
-{
-    $('#secteurs_' + s + '_' + ss).remove();
-    updateSSecteur();
+    var html = '<div id="sectors_' + s + '_' + ss + '" style="clear: both; margin-top: 0.5em" class="titre">'
+             + '  <a href="javascript:removeSector(\'' + s + '\',\'' + ss + '\')" style="display: block; float: right">'
+             + '    <img src="images/icons/cross.gif" alt="" title="Supprimer ce secteur" />'
+             + '  </a>'
+             + '  <input type="hidden" name="sectors[' + s + '][' + ss + ']" value="' + sst + '" />'
+             + '  ' + sst
+             + '</div>';
+    $('#sectors').append(html);
+    updateSubSector();
 }
 
 function registerEnterpriseAutocomplete(id)
 {
-    $(".enterprise_name").each(
+    $(".enterpriseName").each(
       function() {
         if (id == -1 || this.name == "jobs[" + id + "][name]") {
             $(this).autocomplete(platal_baseurl + "search/autocomplete/entreprise",
                                  {
-                                   selectOnly:1,
-                                   field:this.name,
-                                   matchSubset:0,
-                                   width:$(this).width()
+                                     selectOnly:1,
+                                     field:this.name,
+                                     matchSubset:0,
+                                     width:$(this).width()
                                  });
         }
-      }
-    );
+      });
 
-    $(".sector_name").each(
+    $(".sectorName").each(
       function() {
-        if (id == -1 || this.name == "jobs[" + id + "][sss_secteur_name]") {
-            $(this).autocomplete(platal_baseurl + "search/autocomplete/sss_secteur",
+        if (id == -1 || this.name == "jobs[" + id + "][subSubSectorName]") {
+            $(this).autocomplete(platal_baseurl + "search/autocomplete/subSubSector",
                                  {
-                                   selectOnly:1,
-                                   field:this.name,
-                                   matchSubset:0,
-                                   width:$(this).width()
+                                     selectOnly:1,
+                                     field:this.name,
+                                     matchSubset:0,
+                                     width:$(this).width()
                                  });
         }
-      }
-    );
+      });
+}
+
+// {{{1 Multiusage functions
+
+function updateElement(cat)
+{
+    var val = $('#' + cat + '_table').find('[name=' + cat + '_sel]').val();
+    if (val == '' || $('#' + cat + '_' + val).length != 0) {
+        $('#' + cat + '_add').hide();
+    } else {
+        $('#' + cat + '_add').show();
+    }
+}
+
+function removeElement(cat, id)
+{
+    $('#' + cat + '_' + id).remove();
+    updateElement(cat);
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: