excludeOptions = new Array();
excludeHidden = new Array();

function updateMainConfigImage(ele, clear)
{
    parts = ele.id.split('-');
    imgele = $('bundle-optimg-'+parts[2]+'-'+parts[3]);
    mainpic = $('mainpic-opt-'+parts[2]);
	maintitle = $('maintitle-opt-'+parts[2]);
	titleele = $('bundle-opttitle-'+parts[2]+'-'+parts[3]);
	if (mainpic != null) {
		if (!clear) {
		    mainpic.setStyle({
		        background:'url("'+imgele.src+'")'
		    });
			maintitle.update(titleele.innerHTML);
		} else {
			mainpic.setStyle({
				background:'none'
			});
		}
	}
}

function update_selection_dropdown(dropdown, img_id, radio_id, update_bundle)
{
	id_parts = dropdown[dropdown.selectedIndex].value.split('-');
	$(img_id).src = $("bundle-optimg-"+id_parts[2]+"-"+id_parts[3]).longDesc;
	$(radio_id).value = id_parts[3];
	if (update_bundle) {
		bundle.changeSelection($(dropdown[dropdown.selectedIndex].value));
	}
	// updateConfigDisplay();
}

function all_options_picked()
{
	for (var i=1;i<=2;i++) {
		if (!$RF('product_addtocart_form', 'bundle_option['+i+']')) {
			return false;
		}
	}
	
	return true;
}

function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }

    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}

function updateConfigDisplay()
{
    // Show all options
    var showme = excludeHidden.pop();
    while(showme != null) {
        showme.show();
        showme = excludeHidden.pop();
    }

    // Un-show those that should be hidden
    all_opts = $$('.bundle-option-radio');
    for (var ao=0; ao<all_opts.length; ao++) {
        if (all_opts[ao].checked) {
            updateMainConfigImage(all_opts[ao]);
            ao_parts = all_opts[ao].id.split('-');
            barr = ao_parts[2] + '-' + ao_parts[3];
            var bad_arr = excludeOptions[barr];
            if (bad_arr != null) {
                for (var i=0; i<bad_arr.length; i++) {
                    ex_arr = $$('#product-id-'+bad_arr[i]);
                    for (var j=0; j<ex_arr.length; j++) {
                        excludeHidden.push(ex_arr[j]);
                        ex_arr[j].hide();
						if (ex_arr[j].firstChild.nextSibling.checked==true) {
							ex_arr[j].firstChild.nextSibling.checked=false;
							updateMainConfigImage(ex_arr[j].firstChild.nextSibling, true);
						}
                    }
                }
            }
        }
    }
}

function addConfigExcludeProd(index, id)
{
    if (excludeOptions[index] == null) {
        excludeOptions[index] = new Array();
    }

    excludeOptions[index].push(id);
}

