//var jsonrpc = new JSONRpcClient("/JSON-RPC");

var compLevels;
var langLevelsSpeak;
var langLevelsRead;
var langLevelsWrite;


function addProfile(table, comboId) {
    var combo = document.getElementById(comboId);

    var optional = document.getElementsByName(comboId+'_optional');
    var boolOptional = getChecked(optional);

    var other = document.getElementById(comboId+'_other');

    var level = document.getElementById(comboId+'_level');
    var intLevel = 0;
    if (level) {
        intLevel = level.value;
    }

    if (combo.value>0) {
        jsonrpc.nszForm.addProfile(processProfiles, table, combo.value, intLevel, boolOptional);
    } else if (combo.value == -1) {
        jsonrpc.nszForm.addProfile(processProfiles, table, intLevel, boolOptional, other.value, 0);
    }
}

function addLanguage() {
    var langId = document.getElementById('language').value;
    var readLevelId = document.getElementById('language_read').value;
    var writeLevelId = document.getElementById('language_write').value;
    var speakLevelId = document.getElementById('language_speak').value;
    var langOptional = getChecked(document.getElementsByName('language_optional'));
    var langOther = document.getElementById('language_other').value;

    if (langId > 0) {
        jsonrpc.nszForm.addLanguage(processProfiles, langId, readLevelId, writeLevelId, speakLevelId, langOptional);
    } else if (langId == -1) {
        jsonrpc.nszForm.addLanguage(processProfiles, langOther, readLevelId, writeLevelId, speakLevelId, langOptional);
    }

}

function removeProfile(table, ord) {
    jsonrpc.nszForm.removeProfile(processProfiles, table, ord);
}

function processProfiles(profiles, exception) {
    if(profiles) {
        var optional = true;
        if ('' + profiles.map.optional == 'false') {
            optional = false;
        }
        var table = profiles.map.table;
        
        var profileDiv1 = document.getElementById(table+'_1');
        var profileDiv2 = document.getElementById(table+'_2');
        var workingDiv = profileDiv1.style.display == 'none' ? profileDiv1 : profileDiv2;
        var activeDiv = profileDiv1.style.display == 'none' ? profileDiv2 : profileDiv1;
        workingDiv.innerHTML = '';

        if (profiles.map.values.list.length  > 0) {
            var skillTable = document.createElement("table");
            var skillTbody = document.createElement("tbody");
            skillTable.setAttribute("border", 0);
            skillTable.setAttribute("cellspacing", 1);
            skillTable.setAttribute("cellpading", 0);
            skillTable.className = "lista";

            var heading = document.createElement("tr");
            heading.className = "heading";

            var compLevel = profiles.map.level;
            var county = profiles.map.county;
            var lang = profiles.map.lang;

            var td = document.createElement("td");
            var title;
            if (lang) {
                title = 'Izabrani jezik';
            } else if (county) {
                title = 'Mesto';
            } else {
                title = 'Izabrana veština';
            }
            //            td.innerHTML = (""+table == ""+langStr) ? 'Izabrani jezik' : 'Izabrana veština';
            td.innerHTML = title;
            heading.appendChild(td);

            if (compLevel) {
                compLevels = profiles.map.levels;
                td = document.createElement("td");
                td.innerHTML = 'Nivo znanja';
                heading.appendChild(td);
            } else if (lang) {

                langLevelsRead = profiles.map.langLevelsRead;
                langLevelsWrite = profiles.map.langLevelsWrite;
                langLevelsSpeak = profiles.map.langLevelsSpeak;

                td = document.createElement("td");
                td.innerHTML = 'Čitanje';
                heading.appendChild(td);

                td = document.createElement("td");
                td.innerHTML = 'Pisanje';
                heading.appendChild(td);

                td = document.createElement("td");
                td.innerHTML = 'Govor';
                heading.appendChild(td);
            }

            if (optional) {
                td = document.createElement("td");
                td.innerHTML = '&nbsp;';
                heading.appendChild(td);
            }
            td = document.createElement("td");
            td.innerHTML = '&nbsp;';
            heading.appendChild(td);

            skillTbody.appendChild(heading);
            for (var i = 0; i < profiles.map.values.list.length; i++) {
                var profile = profiles.map.values.list[i];
                var newTr = document.createElement('tr');

                var newTd = document.createElement('td');
                newTd.innerHTML = '<strong>'+profile.name+'</strong>';
                newTr.appendChild(newTd);

                if (compLevel) {
                    newTd = document.createElement('td');
                    newTd.innerHTML = compLevels.map[profile.level];
                    newTr.appendChild(newTd);
                } else if (lang) {
                    var levelStr;
                    for (var j = 0; j < langLevelsRead.length; j++) {
                        //                        alert(langLevelsRead[j].value+' '+profile.readLevelId);
                        if (''+langLevelsRead[j].value == ''+profile.readLevelId) {
                            levelStr = langLevelsRead[j].label;
                        }
                    }

                    newTd = document.createElement('td');
                    newTd.innerHTML = levelStr;
                    newTr.appendChild(newTd);

                    for (j = 0; j < langLevelsWrite.length; j++) {
                        if (''+langLevelsWrite[j].value == ''+profile.writeLevelId) {
                            levelStr = langLevelsWrite[j].label;
                        }
                    }
                    newTd = document.createElement('td');
                    newTd.innerHTML = levelStr;
                    newTr.appendChild(newTd);

                    for (j = 0; j < langLevelsSpeak.length; j++) {
                        if (''+langLevelsSpeak[j].value == ''+profile.speakLevelId) {
                            levelStr = langLevelsSpeak[j].label;
                        }
                    }

                    newTd = document.createElement('td');
                    newTd.innerHTML = levelStr;
                    newTr.appendChild(newTd);

                }
                
                if (optional) {
                    newTd = document.createElement('td');
                    newTd.setAttribute('align', 'center');
                    newTd.innerHTML = profile.optionalString;
                    newTr.appendChild(newTd);
                }
                newTd = document.createElement('td');
                newTd.setAttribute('align', 'center');
                newTd.innerHTML = ' [ <a href="#" onclick="removeProfile(\''+table+'\', '+ profile.ord +'); return false;">izbaci</a> ]';
                newTr.appendChild(newTd);

                skillTbody.appendChild(newTr);
            }
            skillTable.appendChild(skillTbody);
            workingDiv.appendChild(skillTable);
        }
        workingDiv.style.display='block';
        activeDiv.style.display='none';
        activeDiv.innerHTML='';

    }
}

function pushEducation() {
    jsonrpc.nszForm.pushEducation(processEducations);
}

function removeEducation(ord) {
    jsonrpc.nszForm.removeEducation(processEducations,ord);
}

function processEducations(educations, exception) {
    if(educations) {
        var table = 'code_education';
        
        var educationDiv1 = document.getElementById(table+'_table1');
        var educationDiv2 = document.getElementById(table+'_table2');
        var workingDiv = educationDiv1.style.display == 'none' ? educationDiv1 : educationDiv2;
        var activeDiv = educationDiv1.style.display == 'none' ? educationDiv2 : educationDiv1;
        workingDiv.innerHTML = '';
        
        if (educations.map.values.list.length  > 0) {
            var eduTable = document.createElement("table");
            var eduTbody = document.createElement("tbody");
            eduTable.setAttribute("border", 0);
            eduTable.setAttribute("cellspacing", 1);
            eduTable.setAttribute("cellpading", 0);
            eduTable.className = "lista";

            var heading = document.createElement("tr");
            heading.className = "heading";
            
            td = document.createElement("td");
            td.innerHTML = 'Nivo obrazovanja';
            heading.appendChild(td);

            td = document.createElement("td");
            td.innerHTML = 'Zvanje';
            heading.appendChild(td);

            td = document.createElement("td");
            td.innerHTML = 'Ocena';
            heading.appendChild(td);

            td = document.createElement("td");
            td.innerHTML = '&nbsp;';
            heading.appendChild(td);
            
            eduTbody.appendChild(heading);
            for (var i = 0; i < educations.map.values.list.length; i++) {
                var education = educations.map.values.list[i];
                var newTr = document.createElement('tr');

                var newTd = document.createElement('td');
                newTd.innerHTML = '<strong>'+education.educationType.name+'</strong>';
                newTr.appendChild(newTd);
                
                newTd = document.createElement('td');
                if(education.educationField.name != null)
                newTd.innerHTML = '<strong>Oblast: </strong> ' + education.educationField.name;
                if(education.education.name != null)
                newTd.innerHTML += '<br/><strong>Zvanje: </strong> ' + education.education.name
                if(education.educationMajor.name != null)
                newTd.innerHTML += '<br/><strong>Smer: </strong> ' + education.educationMajor.name;
                newTr.appendChild(newTd);
                
                newTd = document.createElement('td');
                newTd.innerHTML = education.averageGrade.name;
                newTr.appendChild(newTd);
                
                newTd = document.createElement('td');
                newTd.innerHTML = '[&nbsp;<a href="#"  onclick="removeEducation('+i+'); return false;">izbaci</a>&nbsp;]';
                newTr.appendChild(newTd);

                eduTbody.appendChild(newTr);
            }
            
            eduTable.appendChild(eduTbody);
            workingDiv.appendChild(eduTable);
        }
        
        workingDiv.style.display='block';
        activeDiv.style.display='none';
        activeDiv.innerHTML='';
    }
}
