﻿$(document).ready(function() {
    // Radio button lists are here
    var radioButtons = $('div[rel=RadioButtonListItem]');
    radioButtons.find('input:radio').click(function(eventData) {
        if (eventData) { eventData.stopPropagation(); }
        $(this).parents('div[rel=RadioButtonList]').find('div[rel=RadioButtonListItem]').removeClass('selected');
        if (this.checked) {
            $(this).parents('div[rel=RadioButtonListItem]').addClass('selected');
        }
    });

    radioButtons.click(function(eventData) {
        if (eventData) { eventData.stopPropagation(); }
        var inputs = $(this).find('input:radio');
        if (inputs.length == 1) {
            inputs.attr('checked', 'true').click();
        }
    });

    radioButtons.find('input:checked').click();

    // Check box lists are here
    var checkBoxes = $('div[rel=CheckBoxListItem]');
    checkBoxes.find('input:checkbox').click(function(e) {
        if (e) { e.stopPropagation(); }
        var parent = $(this).parents('div[rel=CheckBoxListItem]').removeClass('selected');
        if (this.checked) {
            parent.addClass('selected');
        }
    }).click(function(e) {
        if (e) { e.stopPropagation(); }
    });

    checkBoxes.click(function(e) {
        if (e) { e.stopPropagation(); }
        var inputs = $(this).find('input:checkbox');
        if (inputs.length == 1) {
            if (inputs.attr('checked') == true) {
                inputs.attr('checked', '').click();
            } else {
                inputs.attr('checked', 'true').click();
            }
        }
    });

    checkBoxes.find(':text').click(function() {
        var inputs = $(this).parents('div[rel=CheckBoxListItem]').find('input:checkbox');
        if (inputs.length == 1) {
            if (inputs.attr('checked') != true) {
                inputs.attr('checked', 'true').click();
            }
        }
    }).click(function(e) {
        if (e) { e.stopPropagation(); }
    });

    checkBoxes.find('input:checked').click();
});
