var product_values_attributes = {};
var product_values_valid_combinations = {};
var product_values_price = {};

function product_values_chooser_update(product_id, name_suffix, invalid_message) {
	attribute_ids = product_values_attributes[product_id + '_' + name_suffix];
	valid_combinations = product_values_valid_combinations[product_id + '_' + name_suffix];
	// wir iterieren ueber alle attribute
	attribute_ids.each( function(attribute_id) {
		// holen den chooser zu diesem attribut
		chooser = $('product_' + product_id + '_' + attribute_id + '_values' + name_suffix);
		// ermitteln alle gesetzten values der *anderen* chooser
    	selected_values = [];
			attribute_ids.each( function(attribute_id) {
    		tmp_chooser = $('product_' + product_id + '_' + attribute_id + '_values' + name_suffix);
				if (tmp_chooser != chooser && typeof(tmp_chooser.value) != 'undefined' && tmp_chooser.value != '') {
	    			selected_values = selected_values.concat(tmp_chooser.value);
				}
    	});
			// jetzt gehen wir ueber alle options des choosers
			options = chooser.options;
		for (i = 0; i < options.length; i++) {
			option = options[i];
				if (typeof(option.value) != 'undefined' && option.value != '') {
					// und pruefen ob eine kombination aus allen in den anderen choosern 
					// selektierten values sowie der aktuellen value gueltig ist
					check_combination = selected_values.slice();
					check_combination = check_combination.concat(option.value);
					valid = false;
					// dazu gehen wird ueber alle validen kombinationen
					valid_combinations.each( function (tmp) {
						valid_combination = tmp[0];
						// und pruefen dann fuer jedes value der zu pruefenden
						// kombination ...
						combination_found = true;
						check_combination.each( function(value) {
							// ob dieses value in der validen kombination enthalten ist.
							value_found = false;
							valid_combination.each( function(value2) {
								if (value == value2) {
									value_found = true;
								}
							});
							if (! value_found) {
								combination_found = false;
							}
						});
						if (combination_found) {
							valid = true;
						}
					});
					if (valid) {
						$(option).removeClassName('greyed');
					} else {
						$(option).addClassName('greyed');
					}
				};
  	};
	});
	price_el = $('product_' + product_id + '_values_price_' + name_suffix);
		if (price_el) {
			price_el.innerHTML = product_values_price[product_id];
		}
	selected_values = [];
		attribute_ids.each( function(attribute_id) {
		tmp_chooser = $('product_' + product_id + '_' + attribute_id + '_values' + name_suffix);
			if (typeof(tmp_chooser.value) != 'undefined' && tmp_chooser.value != '') {
    			selected_values = selected_values.concat(tmp_chooser.value);
			}
	});
	is_valid = false;
		valid_combinations.each( function (tmp) {
			valid_combination = tmp[0];
			product_variant_id = tmp[1];
			price = tmp[2];
			if (selected_values.sort().toString() == valid_combination.sort().toString()) {
				is_valid = true;
   			$('product_' + product_id + '_pv_values' + name_suffix).value = product_variant_id;
				if (price_el) {
					price_el.innerHTML = price;
				}
			};						
		});
		all_selected = true;
		attribute_ids.each( function(attribute_id) {
			chooser = $('product_' + product_id + '_' + attribute_id + '_values' + name_suffix);
			if (typeof(chooser.value) == 'undefined' || chooser.value == '') {
				all_selected = false;
			}
		});
		if (price_el && all_selected && ! is_valid) {
			price_el.innerHTML = invalid_message;
		}
}



function product_extra_param_update() {
	 tmp = []; 
	 $$('.product_extra_param').each( 
	 	function(el) { 
	 		t = false; 
	 		$A(el.options).each( 
	 			function(o) { 
	 				if (o.selected) { 
	 					t = o.text 
	 				};
	 			}
	 		);
	 		if (t) {
	 			tmp = tmp.concat(el.title + ': ' + t) 
	 		}; 
	 	}
	);
	$('extra_param').value = tmp.join('; ');
}

function check_product_extra_param() {
	ok = false;
	if (!product_extra_param_valid || product_extra_param_valid.length < 1) {
		ok = true;
	}
	$A(product_extra_param_valid).each(	function(param_set) {
		found = true;
		i = 0;
		$$('.product_extra_param').each( function(el) { 
			match = false;
	 		$A(param_set[i]).each( function(v) {
				if (el.value == v) {
					match = true;
				};
			});
			i = i+1;
			if (! match) {
				found = false;
			}
 		});
 		if (found) {
 			ok = true;
 		};
 	});
 	if (ok) {
 		return true;
 	} else {
 		window.alert('Es wurden nicht alle Parameter gewählt oder die gewünschte Kombination ist nicht verfügbar!');
 		return false;
	}
}
