﻿function RadioInputClick(item, containerId, otherOption, voteId) {
    var container = prototypeJs(containerId);
    var selected = container.select('.VoteAnswerSelected');
    if (selected.length > 0) {
        selected[0].removeClassName('VoteAnswerSelected');
        selected[0].addClassName('VoteAnswer');
    }

    prototypeJs(item).removeClassName('VoteAnswer');
    prototypeJs(item).addClassName('VoteAnswerSelected');


    var otherOptionWrapper = container.select('.VoteResponseOtherOption');
    if (otherOptionWrapper.length > 0) {
        var optionWrapper = prototypeJs(otherOptionWrapper[0]);
        var validators = prototypeJs(optionWrapper).select('.EnquiryError');
        if (otherOption == true) {
            prototypeJs(optionWrapper).show();
            var optionTextBox = prototypeJs(optionWrapper).select('input');
            if (optionTextBox.length > 0) {
                prototypeJs(optionTextBox[0]).focus();
            }
            if (validators.length > 0) {
                SetValidatorEnabled(validators[0], true);
            }
        }
        else {
            prototypeJs(optionWrapper).hide();
            var optionTextBox = prototypeJs(optionWrapper).select('input');
            if (optionTextBox.length > 0) {
                prototypeJs(optionTextBox[0]).value = '';
            }
            if (validators.length > 0) {
                SetValidatorEnabled(validators[0], false);
            }
        }
    }

    var radioInput = prototypeJs(item).select('[type="radio"]');
    if (radioInput.length > 0) {
        prototypeJs(voteId).value = radioInput[0].value;
    }
}

function CheckBoxInputClick(item, containerId, otherOption, voteId) {
    var container = prototypeJs(containerId);
    var liItem = prototypeJs(item).up('li');

    if (item.checked == true) {
        liItem.removeClassName('VoteAnswer');
        liItem.addClassName('VoteAnswerSelected');
    }
    else {
        liItem.removeClassName('VoteAnswerSelected');
        liItem.addClassName('VoteAnswer');
    }

    var otherOptionWrapper = container.select('.VoteResponseOtherOption');
    if (otherOptionWrapper.length > 0) {
        var optionWrapper = prototypeJs(otherOptionWrapper[0]);
        if (otherOption == true) {
            var validators = prototypeJs(optionWrapper).select('.EnquiryError');
            if (item.checked == true) {
                prototypeJs(optionWrapper).show();
                var optionTextBox = prototypeJs(optionWrapper).select('input');
                if (optionTextBox.length > 0) {
                    prototypeJs(optionTextBox[0]).focus();
                }
                if (validators.length > 0) {
                    SetValidatorEnabled(validators[0], true);
                }
            } else {
                prototypeJs(optionWrapper).hide();
                var optionTextBox = prototypeJs(optionWrapper).select('input');
                if (optionTextBox.length > 0) {
                    prototypeJs(optionTextBox[0]).value = '';
                }
                if (validators.length > 0) {
                    SetValidatorEnabled(validators[0], false);
                }
            }
        }
    }

    prototypeJs(voteId).value = "";
    var checkboxInputs = prototypeJs(liItem).up().select('[type="checkbox"]');
    var i;
    for (i = 0; i < checkboxInputs.length; i++) {
        if (checkboxInputs[i].checked == true) {
            prototypeJs(voteId).value += checkboxInputs[i].value + ",";
        }
    }
    var result = prototypeJs(voteId).value;
    if (result.length > 0) {
        prototypeJs(voteId).value = result.substring(0, result.length - 1);
    }

}

function SetValidatorEnabled(validator, enable) {
    validator.enabled = enable;
    ValidatorUpdateDisplay(validator);
}
