// misc javascript for shopping cart for papetrie

var when_checked_show = {};
// global option of checkbox > Text in the label for the checkbox > input  =  class of the "disappearing item"
when_checked_show[".global-option-42 > .opt-elem:contains('Enclosure Card') > input"] = '.global-option-37';
when_checked_show[".global-option-30 > .opt-elem:contains('Return Address Printing') > input"] = '.global-option-36';
when_checked_show[".global-option-48 > .opt-elem:contains('Return Address Printing') > input"] = '.global-option-36';
when_checked_show[".global-option-53 > .opt-elem:contains('Return Address Printing') > input"] = '.global-option-36';
when_checked_show[".global-option-62 > .opt-elem:contains('Return Address Printing') > input"] = '.global-option-36';
when_checked_show[".global-option-74 > .opt-elem:contains('Return Address Printing') > input"] = '.global-option-36';
when_checked_show[".global-option-111 > .opt-elem:contains('Return Address Printing') > input"] = '.global-option-36';
when_checked_show[".global-option-117 > .opt-elem:contains('Return Address Printing') > input"] = '.global-option-36, .global-option-126';
when_checked_show[".global-option-119 > .opt-elem:contains('Mailing Envelope Lining') > input"] = '.global-option-122, .global-option-128';
when_checked_show[".global-option-124 > .opt-elem:contains('Mailing Envelope Lining') > input"] = '.global-option-122, .global-option-128';
when_checked_show[".global-option-118 > .opt-elem:contains('Personalization') > input"] = '.global-option-120, .global-option-121, .global-option-125';
when_checked_show[".global-option-75 > option:contains('Flat') > select"] = '.global-option-76';


var when_selected_show = {};
// when_selected_show['jQuery path to select'] = { "items" : new Array('1', '2', '3'), "target" : '.global-option-x'}
// this wil show the ".global-option-x" item when the 2nd, 3rd or 4th item is chosen from the select named with the jQuery 
when_selected_show[".global-option-75 > .opt-elem > select"] = {"items" : new Array('1'), "target" : '.global-option-76'};
when_selected_show[".global-option-98 > .opt-elem > select"] = {"items" : new Array('2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'), "target" : '.global-option-97'};

function cartOnReady() {
    hideOptions();
}

function hideOptions() {
    for (var k in when_checked_show) {
		registerClick($(k), $(when_checked_show[k]));
    }

    for (var k in when_selected_show) {
		registerSelect($(k), when_selected_show[k]['items'], $(when_selected_show[k]['target']));
    }
}

function registerSelect(select, items, target) {
    if (optionNumInList(select, items)) showAndEnableOption(target);
    select.change(function () {
    	if (optionNumInList($(this), items)) {
		    showAndEnableOption(target);
		    return;
		}
		hideAndDisableOption(target);
    });
}

function optionNumInList(select, items) {
	for (var i in items) {
		if (optionNumSet(select, items[i])) return true;
	}
	return false;
}
function optionNumSet(select, item_num) {
    var item = select.find('option:eq('+item_num+')');
    if (item == null) return false;
    var selected = select.find('option:selected');
    return item.html() == selected.html();
}

function registerClick(key_element, target_element) {
    if (!key_element.is(':checked')) hideAndDisableOption(target_element);
    key_element.click(function () {
	if ($(this).is(':checked')) {
	    showAndEnableOption(target_element);
	} else {
	    hideAndDisableOption(target_element);
	}
    });
}

function hideAndDisableOption(opt) {
    opt.hide();
    opt.find('select').attr('disabled', true);
    opt.find('input').attr('disabled', true);
    opt.find('textarea').attr('disabled', true);
}
function showAndEnableOption(opt) {
    opt.show();
    opt.find('select').removeAttr('disabled');
    opt.find('input').removeAttr('disabled');
    opt.find('textarea').removeAttr('disabled');
}
