// $Id: exceptions.js 10170 2010-07-22 08:41:58Z klerik $

function fn_change_options(obj_id, id, option_id)
{
	// Change cart status
	cart_changed = true;
	
	params = [];
	update_ids = [];
	
	parents = $('.cm-reload-' + obj_id);
	jQuery.each(parents, function(id, parent_elm) {
		update_ids.push(parent_elm.getAttribute('id'));
		
		elms = $(':input:not(:radio):not(:checkbox)', parent_elm);
		jQuery.each(elms, function(id, elm) {
			if (elm.type != 'submit' && elm.type != 'file' && !($(this).hasClass('cm-hint') && elm.value == elm.defaultValue) && elm.name.length != 0) {
				params.push({name: elm.name, value: elm.value});
			}
		});
	});
	
	radio = $(':radio:checked, :checkbox', parents);
	jQuery.each(radio, function(id, elm) {
		value = elm.value;
		if ($(elm).is(':checkbox:checked')) {
			if (!$(elm).hasClass('cm-no-change')) {
				value = 'checked';
			}
		} else if ($(elm).is(':checkbox')) {
			if (!$(elm).hasClass('cm-no-change')) {
				value = 'unchecked';
			} else {
				value = '';
			}
		}
		
		params.push({name: elm.name, value: value});
	});
	
	// Disable all input elements until the Ajax query returns the result.
	$(':input', $('[id*=options_update_' + obj_id + ']')).attr('disabled', true);
	
	url = fn_url('products.options?changed_option[' + id + ']=' + option_id);
	
	for (i in params) {
		url += '&' + params[i]['name'] + '=' + escape(params[i]['value']);
	}
	
	jQuery.ajaxRequest(url, {
		result_ids: update_ids.join(',').toString(),
		caching: false,
		force_exec: true,
		method: 'post'
	});
	
}

function fn_set_option_value(id, option_id, value)
{
	elm = $('#option_' + id + '_' + option_id);
	if (elm.attr('type') == 'select-one') {
		elm.val(value).change();
	} else {
		elms = $('#option_' + id + '_' + option_id + '_group');
		$(':radio[value=' + value + ']', elms).click();
	}

	return true;
}