function update_form(div)
{
    var $div = $(div);
    var $element = $div.find('input[type=checkbox], input[type=radio]');
    var name = new String($element.attr('name'));
    
    if ($element.attr('type')=='checkbox' && !$element.attr('disabled')) {
        if ($element.attr('checked')) {
            $element.attr('checked','');
            $div.attr('class','checkbox_unchecked');
            jQuery('label[for='+ $element.attr('id') +']').removeClass('label_selected');
        } else {
            $element.attr('checked','checked');
            $div.attr('class','checkbox_checked');
            jQuery('label[for='+ $element.attr('id') +']').addClass('label_selected');

            //deselect "no_preference"-Option if other option is selected
            if (!$element.hasClass('no_preference')) {
                jQuery('input[type=checkbox][name="'+name+'"]').each(function() {
                    $this = $(this);
                    if ($this.hasClass('no_preference')) {
                        $this.attr('checked', '');
                        $this.parent().attr('class', 'checkbox_unchecked');
                        jQuery('label[for='+ $this.attr('id') +']').removeClass('label_selected');
                    }
                });
            } else {
                jQuery('input[type=checkbox][name="'+name+'"]').each(function() {
                    $this = $(this);
                    if (!$this.hasClass('no_preference')) {
                        $this.attr('checked', '');
                        $this.parent().attr('class', 'checkbox_unchecked');
                        jQuery('label[for='+ $this.attr('id') +']').removeClass('label_selected');
                    }
                });
            }
        }

    } else if ($element.attr('type')=='radio' && !$element.attr('disabled')) {
        if (!$element.attr('checked')) {
            jQuery('input[type=radio][name="'+name+'"]').each(function() {
                $this = $(this);
                $this.attr('checked', '');
                $this.parent().attr('class', 'radio_unselected');
                jQuery('label[for='+ $this.attr('id') +']').removeClass('label_selected');
            });
            $element.attr('checked','checked');
            $div.attr('class','radio_selected');
            jQuery('label[for='+ $element.attr('id') +']').addClass('label_selected');
        }
    }
    
    // save that changes are made in form
    update_changes_made();

    var onchange = $element.attr('onchange');
    if (onchange) {
        onchange();
    }
}

function style_form()
{
	//$('select').selectbox();
        
	$('input[type=checkbox], input[type=radio]').each( function() {
		$this = $(this);
		var checkbox_class = '';

                if ($this.attr('type')=='checkbox') {
                    if ($this.attr('checked')) {
                        checkbox_class = 'checkbox_checked';
                    } else {
                        checkbox_class = 'checkbox_unchecked';
                    }

                } else if ($this.attr('type')=='radio') {
                    if ($this.attr('checked')) {
                        checkbox_class = 'radio_selected';
                    } else {
                        checkbox_class = 'radio_unselected';
                    }
                }
                checkbox_class += ' ' + $this.attr('class');

		$this.wrap('<div onclick="update_form(this)" class="'+checkbox_class+'" id="'+$this.attr('id')+'_div"></div>');
                $this.css('display','none');

		// add onclick to label and style
                $label = $('label[for='+ $this.attr('id') +']'); 
                $label.addClass('checkbox_label');
                if ($this.attr('checked')) {
                    $label.addClass('label_selected');
                }
                $label.click(function(){
                    update_form('#'+$(this).attr('for')+'_div');
                });
                if ($this.attr('disabled')) {
                    if (!$this.attr('checked')) {
                        $label.addClass('label_disabled');
                    }
                } else {
                    $label.css('cursor','pointer');
                }
	});
}


function style_payment_method()
{

    $('li[class^=list_payment_method]').each( function() {

            $this = $(this);

            var $input = $this.find('input[type=radio]');
            var $input_value = $input.attr('value');

            $this.addClass('method_' + $input_value);
    });

}

function add_nested_checkbox($list) {
    $list.find('li.parent').each( function() {
        var $checkbox = $(this).find('>div');
        var $label = $(this).find('>label');
        $checkbox.attr('onclick','');
        $label.unbind('click');

        $sub_list = $(this).find('ul.level2 li').each( function() {
            $checkbox_sub = $(this).find('>div');
            $label_sub = $(this).find('>label');
            $checkbox_sub.click(function(){
                update_nested_form('#'+$checkbox.attr('id'));
            });
            $label_sub.click(function(){
                update_nested_form('#'+$checkbox.attr('id'));
            });
        });
    });
}

function update_nested_form(parent_checkbox_div) {
    var $parent_checkbox_div = $(parent_checkbox_div);
    var $parent_checkbox = $parent_checkbox_div.find('input[type=checkbox]');

    $sub_list = $parent_checkbox_div.parent().find('ul.level2 input[type=checkbox]:checked');
    if($sub_list.length > 0) {
        if(!$parent_checkbox.attr('checked')) {
            update_form(parent_checkbox_div);
        }
    } else {
        if($parent_checkbox.attr('checked')) {
            update_form(parent_checkbox_div);
        }
    }
}
