// JavaScript Document
function urlencode(str) {
    str = str.replace('&', '%26');
    return str;
}
function tab(tabid, offtabid, offtabid2) {
    document.getElementById(tabid).className = 'activetab';
    document.getElementById(tabid + 'cont').style.display = 'block';
    document.getElementById(offtabid).className = 'passivetab';
    document.getElementById(offtabid + 'cont').style.display = 'none';
    document.getElementById(offtabid2).className = 'passivetab';
    document.getElementById(offtabid2 + 'cont').style.display = 'none';
}
function create_company_list(value, selected) {
    var url = 'anketa_controller.php?branch_code=' + value + '&selected=' + selected;
    document.getElementById('loading').style.display = 'block';
    new Ajax(url, {
        method: 'get',
        onComplete: function (text, xml) {
            var selectTag = document.getElementById('select_tag');
            selectTag.innerHTML = '';
            var selected_option = xml.getElementsByTagName('selected');
            var optionBlank = document.createElement('option');
            if (selected_option.length > 0) {
                optionBlank.value = selected_option[0].firstChild.nodeValue;
                optionBlank.innerHTML = selected_option[0].firstChild.nodeValue;
            } else {
                optionBlank.value = '';
                optionBlank.innerHTML = '';
            }
            optionBlank.selected = true;
            selectTag.appendChild(optionBlank);
            var optionMU = document.createElement('option');
            optionMU.value = 'QQQZZ120';
            optionMU.innerHTML = 'Mazais uz&#326;&#275;mums';
            selectTag.appendChild(optionMU);
            var optionOther = document.createElement('option');
            optionOther.value = 'other';
            optionOther.innerHTML = 'Cits:_____';
            selectTag.appendChild(optionOther);
            var response_options = xml.getElementsByTagName('option');
            for (i = 0; i < response_options.length; i++) {
                if (response_options[i].hasChildNodes() && response_options[i] != null) {
                    optionTag = document.createElement('option');
                    optionTag.value = response_options[i].childNodes[0].nodeValue;
                    optionTag.innerHTML = response_options[i].childNodes[0].nodeValue;
                    selectTag.appendChild(optionTag);
                }
            }
            document.getElementById('loading').style.display = 'none';
        }
    }).request();
}
function unlock_company_input(value) {
    if (value == 'other' && document.getElementById('branch').value != '24') {
        document.getElementById('add_company').style.visibility = 'visible';
    } else {
        document.getElementById('add_company').style.visibility = 'hidden';
        document.getElementById('new_company').value = '';
    }
}
function unlock_motivator_input() {
    deselect_19();
    if (document.getElementById('ch_7').checked == true) {
        $('motovator_input').setHTML('Cits:<input type="text" class="form_box"  id="ch8_input" name="motovator_input"/>');
    } else if (document.getElementById('ch_7').checked == false) {
        $('motovator_input').setHTML('Cits:<input class="form_grey" type="text" id="ch8_input" name="motovator_input" disabled="disabled"/>');
    }
}
function deselect_other_checkboxes() {
    for (var i = 0; i < document.part3.motivators.length; i++) {
        if (document.part3.motivators[i].value != '19') {
            document.part3.motivators[i].checked = false;
            unlock_motivator_input();
        }
    }
    document.getElementById('ch_6').checked = true;
}
function deselect_19() {
    document.getElementById('ch_6').checked = false;
}
function next_1() {
    var branch = document.getElementById('branch').value;
    var company = document.getElementById('select_tag').value;
    var new_company = document.getElementById('new_company').value;
    if (document.getElementById('add_company').style.visibility == "visible") {
        company = new_company;
    }
    var post_level = '';
    for (var i = 0; i < document.part1.post_level.length; i++) {
        if (document.part1.post_level[i].checked) {
            post_level = document.part1.post_level[i].value;
        }
    }
    if (branch == "" || company == "") {
        document.getElementById('error').innerHTML = 'Laukiem,kas apzimeti ar * ir jabut obligati aizpilditiem!';
    } else {
        var comp = company;
        if (new_company != "") {
            comp = new_company
        }
        var url = "anketa_controller.php";
        var enc_comp = urlencode(comp);
        var params = "branch_save=" + branch + "&company_save=" + enc_comp + "&post_level_save=" + post_level;
        new Ajax(url, {
            method: 'post',
            data: params,
            onComplete: function () {
                window.location.href = '?ide=anketa&part=2';
                return false;
            }
        }).request();
    }
}
function next_2() {
    var salary = document.getElementById('salary').value;
    var colleagues = '';
    colleagues = document.part2.colleagues.value;
    var work_conditions = '';
    work_conditions = document.part2.work_conditions.value;
    var attitude = '';
    attitude = document.part2.attitude.value;
    if (colleagues == 0 || work_conditions == 0 || attitude == 0) {
        document.getElementById('error').innerHTML = 'Laukiem,kas apzimeti ar * ir jabut obligati aizpilditiem!';
    } else {
        var url = "anketa_controller.php?salary_save=" + salary + "&colleagues_save=" + colleagues + "&work_conditions_save=" + work_conditions + "&attitude_save=" + attitude;
        new Ajax(url, {
            method: 'get',
            onComplete: function () {
                window.location = '?ide=anketa&part=3';
            }
        }).request();
    }
}
function next_3() {
    m = new Array(document.part3.motivators.length);
    for (var i = 0; i < document.part3.motivators.length; i++) {
        if (document.part3.motivators[i].checked) {
            if (document.part3.motivators[i].value == 'other') {
                m[i] = document.getElementById('ch8_input').value;
            } else {
                m[i] = document.part3.motivators[i].value;
            }
        } else {
            if (document.part3.motivators[i].value == 'other') {
                m[i] = '';
            } else {
                m[i] = 24;
            }
        }
    }
    var career_code = '';
    for (var i = 0; i < document.part3.career_code.length; i++) {
        if (document.part3.career_code[i].checked) {
            career_code = document.part3.career_code[i].value;
        }
    }
    if (career_code == "") {
        document.getElementById('error').innerHTML = 'Laukiem,kas apzimeti ar * ir jabut obligati aizpilditiem!';
    } else {
        var url = "anketa_controller.php";
        params = "m1=" + m[0] + "&m2=" + m[1] + "&m3=" + m[2] + "&m4=" + m[3] + "&m5=" + m[4] + "&m6=" + m[5] + "&m7=" + urlencode(m[6]) + "&career_code=" + career_code;
        new Ajax(url, {
            method: 'post',
            data: params,
            onComplete: function () {
                window.location = '?ide=anketa&part=sum';
            }
        }).request();
    }
}
function next_4() {
    var comment = document.getElementById('user_comment').value;
    var email = document.getElementById('email').value;
    var newsletter = "";
    if (document.getElementById('newsletter').checked) {
        newsletter = "newsletter";
    }
    params = "user_comment=" + urlencode(comment) + "&email=" + email + "&" + newsletter + "=1";
    var url = "anketa_controller.php";
    new Ajax(url, {
        method: 'post',
        data: params,
        onComplete: function () {
            window.location = '?ide=anketa&part=4';
        }
    }).request();
}
function finish_form() {
    var url = "anketa_controller.php?finish=true";
    new Ajax(url, {
        method: 'get',
        onComplete: function () {
            window.location = '?ide=stats';
        }
    }).request();
}
function validate_required(field) {
    with(field) {
        if (value == null || value == "") {
            return false
        } else {
            return true
        }
    }
}
function validate_email(field) {
    with(field) {
        apos = value.indexOf("@");
        dotpos = value.lastIndexOf(".");
        if (apos < 1 || dotpos - apos < 2) {
            return false;
        } else {
            return true;
        }
    }
}
function validate_topic_form(thisform) {
    with(thisform) {
        if (validate_required(name) == false) {
            name.focus();
            doRed('name');
            return false
        }
        if (validate_required(title) == false) {
            title.focus();
            doRed('title');
            return false
        }
        if (validate_required(text) == false) {
            text.focus();
            doRed('text');
            return false
        }
    }
}
function validate_comment_form(thisform) {
    with(thisform) {
        if (validate_required(name) == false) {
            name.focus();
            doRed('name');
            return false
        }
        if (validate_required(text) == false) {
            text.focus();
            doRed('text');
            return false
        }
    }
}
function validate_contact_form(thisform) {
    with(thisform) {
        if (validate_required(name) == false) {
            name.focus();
            doRed('name');
            return false
        }
        if (validate_required(text) == false) {
            text.focus();
            doRed('text');
            return false
        }
        if ((email.value != '') && (validate_email(email) == false)) {
            email.focus();
            doRed('email');
            return false
        }
    }
}
function doRed(id) {
    document.getElementById(id).style.backgroundColor = '#ff9';
}
function expandSearch() {
    document.getElementById('searchoptions').style.display = 'block';
}
function rate(star) {
    parentOf = star;
    if (star.title == 0) {
        parentOf = star;
        rating = document.getElementById(parentOf.id + 'value').value;
    } else {
        rating = star.title;
        parentOf = star.parentElement;
        parentOf = star.parentNode;
    }
    maxrating = 5;
    if (rating == 0) {
        outrating = 1;
    } else {
        outrating = rating;
    }
    document.getElementById(parentOf.id + 'display').innerHTML = outrating;
    for (var i = 1; i <= rating; i++) {
        astar = parentOf.id + i;
        document.getElementById(astar).src = 'images/star.gif';
    }
    for (var j = i; j <= maxrating; ++j) {
        astar = parentOf.id + j;
        document.getElementById(astar).src = 'images/star-empty.gif';
    }
}
function saveRate(star) {
    rate(star);
    document.getElementById(star.parentNode.id + 'value').value = star.title;
}
function showform(id) {
    document.getElementById(id).style.display = (document.getElementById(id).style.display != 'block') ? 'block' : 'none';
}
function rate2(star, maxrating, barcount) {
    var totalVal = 10;
    for (i = 1; i < 6; i++) {
        valfield = "bar" + i + "value";
        if (document.getElementById(valfield).value) {
            totalVal -= parseInt(document.getElementById(valfield).value);
        }
    }
    parentOf = star;
    if (star.title == 0) {
        parentOf = star;
        rating = document.getElementById(parentOf.id + 'value').value;
    } else {
        parentOf = star.parentElement;
        parentOf = star.parentNode;
        rating = parseInt(star.title);
        var current = parseInt(document.getElementById(parentOf.id + "value").value);
        if (rating - current > totalVal) {
            rating = current + totalVal;
        }
    }
    if (rating < 0) {
        outrating = 0;
    } else {
        outrating = rating;
    }
    document.getElementById(parentOf.id + 'display').innerHTML = outrating;
    for (var i = 1; i <= rating; i++) {
        astar = parentOf.id + i;
        document.getElementById(astar).src = 'images/star.gif';
    }
    for (var j = i; j <= maxrating; ++j) {
        astar = parentOf.id + j;
        document.getElementById(astar).src = 'images/star-empty.gif';
    }
}
function saveRate2(star, totalId) {
    var totalVal = 10;
    for (i = 1; i < 6; i++) {
        valfield = "bar" + i + "value";
        if (document.getElementById(valfield).value) {
            totalVal -= parseInt(document.getElementById(valfield).value);
        }
    }
    var current = parseInt(document.getElementById(star.parentNode.id + 'value').value);
    rate2(star);
    if (parseInt(star.title) == -1) {
        valueGot = 0;
    } else {
        if (parseInt(star.title) > totalVal + current) {
            valueGot = current + totalVal;
            totalVal = 0;
        } else {
            valueGot = star.title;
        }
    }
    document.getElementById(star.parentNode.id + 'value').value = valueGot;
    document.getElementById(totalId).value = totalVal;
    var totalVal = 10;
    for (i = 1; i < 6; i++) {
        valfield = "bar" + i + "value";
        if (document.getElementById(valfield).value) {
            totalVal -= parseInt(document.getElementById(valfield).value);
        }
    }
    if (infof = document.getElementById('info')) {
        infof.innerHTML = totalVal;
    }
    if (submitb = document.getElementById('poll_submit')) {
        if (totalVal == 0) {
            submitb.disabled = false;
        } else {
            submitb.disabled = true;
        }
    }
}
