
$(document).ready(function() {

    $("#experienta").change(function() {
        $("#tableFields").toggle();
        $("#tableFields").find("tr.trFields").remove();
    });

    $("#sendForm").bind("submit", function(event) {
        var isok = true;
        $("#sendForm").find(".musai").each(function(index) {
            if ($(this).val() == "") {isok = false}
        });
        if (!isok) {
            event.stopImmediatePropagation();
            alert('Nu ati completat toate campurile obligatorii!');
            return false;
        }
    });
    $('div.clickdiv').bind('click', function(event) {
        var link = $(this).find('a.clickLink').last().attr("href");
        if (link) document.location = link;
    });

    $("form[name='filtruJobs']").bind("submit", function(event) {
        var listalocos = "";
        $('input[type="checkbox"].loco').each(function(index) {
            if ((this.checked == true) && ($(this).val() != "")) {
                listalocos += ((listalocos.length > 0) ? "||" : "") + $(this).val();
            }
        });        
        var listadepartments = "";
        $('input[type="checkbox"].department').each(function(index) {
            if ((this.checked == true) && ($(this).val() != "")) {
                listadepartments += ((listadepartments.length > 0) ? "|" : "") + $(this).val();
            }
        });
        if (listalocos.length > 0) {
            $("#fieldLoco").attr("value", listalocos);
        } else {
            $("#fieldLoco").remove();
        }
        if (listadepartments.length > 0) {
            $("#fieldDep").attr("value", listadepartments);
        } else {
            $("#fieldDep").remove();
        }
    });
});

var nextFieldId = 1;

function getNextFieldId() {
    return nextFieldId++;
}

function removeFields(care) {
    var fields = care.parentNode.parentNode;
    if (fields) {
        var x = fields.parentNode;
        x.removeChild(fields);
    }
}

function addFields() {
    var tfields = document.getElementById('tableFieldsBody');
    var nextId = getNextFieldId();
    
    var newIndustry = document.getElementById('modelIndustry').cloneNode(true);
    newIndustry.setAttribute('name', 'grupFieldsIndustry__' + nextId);
    newIndustry.setAttribute('id', 'grupFieldsIndustry' + nextId);

    var newDepartment = document.getElementById('modelDepartment').cloneNode(true);
    newDepartment.setAttribute('name', 'grupFieldsDepartment' + nextId);
    newDepartment.setAttribute('id', 'grupFieldsDepartment' + nextId);

    var newJob = document.getElementById('modelJob').cloneNode(true);
    newJob.setAttribute('name', 'grupFieldsJob_______' + nextId);
    newJob.setAttribute('id', 'grupFieldsJob' + nextId);

    var tr = document.createElement("tr");    
    tr.setAttribute('id', 'trFields' + nextId);
    tr.setAttribute('class', 'trFields');
    tfields.appendChild(tr);

    var td = document.createElement("td");
    td.setAttribute('class', 'tdIndustry');
    td.appendChild(newIndustry);
    tr.appendChild(td);

    td = document.createElement("td");
    td.setAttribute('class', 'tdDepartment');
    td.appendChild(newDepartment);
    tr.appendChild(td);

    td = document.createElement("td");    
    td.setAttribute('class', 'tdJob');
    td.appendChild(newJob);
    tr.appendChild(td);

    td = document.createElement("td");    
    td.setAttribute('class', 'tdAction');
    td.innerHTML = '<a href="#" onclick="removeFields(this); return false;">sterge</a>';
    tr.appendChild(td);
}

var nextFieldLangId = 1;

function getNextFieldLangId() {
    return nextFieldLangId++;
}

function removeLangField(care) {
    var fields = care.parentNode.parentNode;
    if (fields) {
        var x = fields.parentNode;
        x.removeChild(fields);
    }
}

function addLangFields() {
    var tfields = document.getElementById('tableLangFieldsBody');
    var nextId = getNextFieldLangId();
    
    var newIndustry = document.getElementById('modelLang').cloneNode(true);
    newIndustry.setAttribute('name', 'Fieldlang__' + nextId);
    newIndustry.setAttribute('id', 'Fieldlang' + nextId);

    var tr = document.createElement("tr");    
    tr.setAttribute('id', 'trLangFields' + nextId);
    tfields.appendChild(tr);

    var td = document.createElement("td");
    td.setAttribute('class', 'tdIndustry');
    td.appendChild(newIndustry);
    tr.appendChild(td);

    var span = document.createElement("span");
    span.innerHTML = "*";
    td.appendChild(span);    

    td = document.createElement("td");    
    td.setAttribute('class', 'tdAction');
    td.innerHTML = '<a href="#" onclick="removeLangField(this); return false;">sterge</a>';
    tr.appendChild(td);
}

