// $Id: exceptions.js 1161 2006-01-25 15:08:17Z imac $

var except = new Array();
var exceptnone = new Array();
var exceptions = new Array();
var pr_i = new Array(); // option images
var pr_a = new Array(); // option amount
var pr_o = new Array(); // option exceptions
var pr_d = new Array(); // discount
var hide = new Array();
var price = new Array();
var price_orig = new Array();
var list_price = new Array();
var dni = 0;
var ind = 0;
var i = 0; 
var j = 0; 
// images
var new_ref = new Array();
var image_changed = new Array();
var detailed_changed = new Array();
var default_image = new Array();
var default_href = new Array();

//
// Check all products for exceptions
//
function fn_check_all_exceptions(one)
{
	for (var iii in pr_o) {
		if (fn_check_exceptions(iii) == false) {
			if (one == true) {
				return false;
			}
		}
		 else {
			fn_check_new_modifiers(i);
		}
	}
	return true;
}

// *****************************************  P R O D U C T    P R I C E  **********************************/
//
// Changes product price concerning option modifiers
//

// This function updates the list price and currenct price difference under You save label
function fn_update_list_price(id, total)
{
	var save_a = list_price[id] - total;
	var save_p = (list_price[id] - total)/list_price[id]*100;
	// You save amount
	if (document.getElementById('save_' + id)) {
		document.getElementById('save_' + id).innerHTML = formatNum(save_a, 2);
	}
	// You saev amount in secondary currency
	if (document.getElementById('sec_save_' + id)) {
		document.getElementById('sec_save_' + id).innerHTML = formatNum(save_a / sec_curr_coef, 2);
	}
	// Percent in You save
	if (document.getElementById('save_p_' + id)) {
		document.getElementById('save_p_' + id).innerHTML = formatNum(save_p, 2);
	}
	return true;
}

// This function updates the discount values and other corresponding value
function fn_update_discount(id, total)
{
	var discount_a = total * pr_d[id]['P']/100 + pr_d[id]['A'];
	var discount_p = (total * pr_d[id]['P']/100 + pr_d[id]['A'])/total*100;
	// You save amount
	if (document.getElementById('save_' + id)) {
		document.getElementById('save_' + id).innerHTML = formatNum(discount_a, 2);
	}
	// You saev amount in secondary currency
	if (document.getElementById('sec_save_' + id)) {
		document.getElementById('sec_save_' + id).innerHTML = formatNum(discount_a / sec_curr_coef, 2);
	}
	// Percents in Label
	if (document.getElementById('discount_label_' + id)) {
		document.getElementById('discount_label_' + id).innerHTML = formatNum(discount_p, 0);
	}
	// Discounted price
	if (document.getElementById('discounted_price_' + id)) {
		document.getElementById('discounted_price_' + id).innerHTML = formatNum(total - discount_a, 2);
	}
	// Discounted price in secondary currency
	if (document.getElementById('sec_discounted_price_' + id)) {
		document.getElementById('sec_discounted_price_' + id).innerHTML = formatNum((total - discount_a) / sec_curr_coef, 2);
	}
	// Subtotal in cart
	if (document.getElementById('discounted_subtotal_' + id)) {
		document.getElementById('discounted_subtotal_' + id).innerHTML = formatNum(parseFloat(document.getElementById('amount_' + id).value) * (total-discount_a), 2);
	}
	// Subtotal in cart in secondary currency
	if (document.getElementById('sec_discounted_subtotal_' + id)) {
		document.getElementById('sec_discounted_subtotal_' + id).innerHTML = formatNum(parseFloat(document.getElementById('amount_' + id).value) * (total-discount_a) / sec_curr_coef, 2);
	}
	return true;
}
//[zenuch][start]
function fn_price_round(rate)
{
	if (rate < 100){
/*		var remainder = rate - Math.floor(rate);
		if (remainder == 0){
			remainder = 0.00;
		}else{
			if (remainder <= 0.25){
				remainder = 0.25;			
			}else{
				if (remainder <= 0.5){
					remainder = 0.5;							
				}else{
					if (remainder <= 0.75){
						remainder = 0.75;							
					}else{
						remainder = 1.00;
					}
					
				}
			}
		}
		var result = Math.floor(rate)+remainder;
		return result;*/
// [OlmersSign]
		var tmp = rate * 4;
		return Math.round(tmp) / 4;
// [/OlmersSign]
	}else{
		return Math.round(rate);		
	}
}
//[zenuch][end]
function fn_print_r(value)
{
	alert(fn_print_array(value));
}

function fn_print_array(arr,level) 
{
	var dumped_text = "";
	if(!level) {
		level = 0;
	}

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0; j < level+1; j++) {
		level_padding += "    ";
	}

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];

			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += fn_print_array(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = arr+" ("+typeof(arr)+")";
	}

	return dumped_text;
}

// This function calculates the new product price and updates values
function fn_update_product_price(id)
{
	var total;
	var modifiers = new Array();

	total = parseFloat(price[id]);
	for (i in pr_o[id]) {
		if (pr_o[id][i]['md'][pr_o[id][i]['selected_value']] != 0) {
			modifiers[i] = pr_o[id][i]['md_t'][pr_o[id][i]['selected_value']] + pr_o[id][i]['md'][pr_o[id][i]['selected_value']];
		}
		if (typeof(modifiers[i]) == 'undefined') {
			continue;
		}
		if (pr_o[id][i]['md_a']) {
			if (modifiers[i].substring(0, 1) == 'A' && document.getElementById(pr_o[id][i]['id']).disabled != true) {
// [OlmersSign] I have removed the Math.round()
				total += parseFloat(modifiers[i].substring(1, modifiers[i].length));
// [/OlmersSign]
			} else if(modifiers[i].substring(0, 1)  == 'P'  && document.getElementById(pr_o[id][i]['id']).disabled != true) {
				total += total * parseFloat(modifiers[i].substring(1, modifiers[i].length))/100;
			}
		}
	}
//[zenuch][start]
	//total = Math.round(total);
	total = fn_price_round(total);	
//[zenuch][end]	
	// Original currency
	if (document.getElementById('original_price_' + id)) {
		document.getElementById('original_price_' + id).innerHTML = formatNum(total, 2);
	}
	// Second currency of the price
	if (document.getElementById('sec_original_price_' + id)) {
		document.getElementById('sec_original_price_' + id).innerHTML = formatNum(total * sec_curr_coef, 2);
	}
	// Subtotal in cart
	if (document.getElementById('original_subtotal_' + id)) {
		document.getElementById('original_subtotal_' + id).innerHTML = formatNum(parseFloat(document.getElementById('amount_' + id).value) * total, 2);
	}
	// Second currency of the subtotal in the cart
	if (document.getElementById('sec_original_subtotal_' + id)) {
		document.getElementById('sec_original_subtotal_' + id).innerHTML = formatNum(parseFloat(document.getElementById('amount_' + id).value) * total * sec_curr_coef, 2);
	}
	if (pr_d[id]) {
		fn_update_discount(id, total);
	} else if (list_price[id])	{
		fn_update_list_price(id, total);
	}
	return true;
}

// Format price value
function formatNum(expr,decplaces) {
	var str = (Math.round(parseFloat(expr) * Math.pow(10,decplaces))).toString();

	while (str.length <= decplaces) {
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	if (decplaces == 0) {
		return str.substring(0, decpoint);
	}
	return str.substring(0, decpoint) + "." + str.substring(decpoint,str.length);
}

// *****************************************  A M O U N T   I N   S T O C K  **********************************/
//
// Changes amount in stock if product inventory is in track with options
//
function fn_change_amount(id)
{
	var key;
	
	key = '';
	for (i in pr_o[id]) {
		if (pr_o[id][i]['inventory'] == 'Y') {
			key += pr_o[id][i]['option_id'] + ":" + pr_o[id][i]['selected_value'] + "|";
		}
	}
	for (i in pr_a[id]) {
		if (key == i)	{
			if (pr_a[id][i]) {
				document.getElementById('product_amount_' + id).innerHTML = pr_a[id][i];
				return true;
			}
		}
	}
	if (pr_a[id] != '') {
		document.getElementById('product_amount_' + id).innerHTML = 0;
	}
	return true;
}

// **************************************************** I M A G E S ********************************************/
//
// Calculates options hash and check wether image is available for this combination
//
function fn_check_option_image(id)
{
	var key='';
	for (i in pr_o[id]) {
		if (pr_o[id][i]['inventory'] == 'Y') {
			key += pr_o[id][i]['option_id'] + ":" + pr_o[id][i]['selected_value'] + "|";
		}
	}
	if (detailed_changed[id] == "Y") {
		if (document.getElementById('detailed_href1_' + id)) {
			document.getElementById('detailed_href1_' + id).href = default_href[id];
		}
		if (document.getElementById('detailed_href2_' + id)) {
			document.getElementById('detailed_href2_' + id).href = default_href[id];
			document.getElementById('detailed_href3_' + id).href = default_href[id];
		}
		detailed_changed[id] = "";
	}
	for (i in pr_i[id]) {
		if (key == pr_i[id][i]['options'])	{
			if (fn_change_image(id, i)) return true;
		}
	}
	if (image_changed[id] == "Y") {
		document.getElementById('img_width_' + id).width = default_image[id]['width'];
		for (var ch in default_image[id]) {
			document.getElementById('det_img_' + id).setAttribute(ch, default_image[id][ch]);
		}
		image_changed[id] = "";
	}
	return false;
}

function fn_change_image(id, row)
{
	if (pr_i[id][row]['detailed_id'] != '0') {
		new_ref[id] = "javascript:fn_open_popup_image('image.php?mode=detailed&object_id="+id+"&image_id="+pr_i[id][row]['detailed_id']+"&window=popup', "+pr_i[id][row]['detailed_size']['image_x']+", "+pr_i[id][row]['detailed_size']['image_x']+"+ 30);"
		if (document.getElementById('detailed_href1_' + id)) {
			document.getElementById('detailed_href1_' + id).href = new_ref[id];
		}
		if (document.getElementById('detailed_href2_' + id)) {
			document.getElementById('detailed_href2_' + id).href = new_ref[id];
			document.getElementById('detailed_href3_' + id).href = new_ref[id];
		}
		detailed_changed[id] = "Y";
	} else {
		if (document.getElementById('detailed_href1_' + id)) {
			document.getElementById('detailed_href1_' + id).href = default_href[id];
		}
		if (document.getElementById('detailed_href2_' + id)) {
			document.getElementById('detailed_href2_' + id).href = default_href[id];
			document.getElementById('detailed_href3_' + id).href = default_href[id];
		}
		detailed_changed[id] = "";
	}
	if (pr_i[id][row]['image_id'] != '0') {
		document.getElementById('img_width_' + id).width = pr_i[id][row]['image_size']['image_x'];
		document.getElementById('det_img_' + id).src = pr_i[id][row]['image'].src.replace(/&amp;/gi, '&')
		document.getElementById('det_img_' + id).height = pr_i[id][row]['image_size']['image_y'];
		document.getElementById('det_img_' + id).width = pr_i[id][row]['image_size']['image_x'];
		document.getElementById('det_img_' + id).alt = pr_i[id][row]['image_size']['alt'];
		image_changed[id] = "Y";
		return true;
	}
	return false;

}

// ************************************************** E X C E P T I O N S ****************************************/
//
// Check new modifiers if no exceptions defined for the product
//
function fn_check_new_modifiers(id)
{
	// Define new modifiers. (Replaceing modifiers with ones defined in affect option)
	for (i in pr_o[id]) {
		if (pr_o[id][i]['type'] == 'S' || pr_o[id][i]['type'] == 'R' || pr_o[id][i]['type'] == 'H') {
			for (j in pr_o[id][i]['md_o']) {
				pr_o[id][i]['md'][j] = pr_o[id][i]['md_o'][j];
				if ((pr_o[id][i]['modifier_changer'][j])) {
					pr_o[id][i]['md'][j] = pr_o[id][i]['new_modifier'][j][document.getElementById('option_'+id+'_'+pr_o[id][i]['modifier_changer'][j]).value];
				}
			}
		}
	}
	// Define modifiers increments
	for (i in pr_o[id]) {
		if (pr_o[id][i]['type'] == 'S' || pr_o[id][i]['type'] == 'R' || pr_o[id][i]['type'] == 'H') {
			if (pr_o[id][i]['modifier_inc']) {
				for (j in pr_o[id][i]['modifier_inc']) {
					if (pr_o[id][pr_o[id][i]['modifier_inc'][j]]['md'][pr_o[id][pr_o[id][i]['modifier_inc'][j]]['selected_value']]) {
						for (var k in pr_o[id][i]['v']) {
							pr_o[id][i]['md'][k] *= (pr_o[id][pr_o[id][i]['modifier_inc'][j]]['md'][pr_o[id][pr_o[id][i]['modifier_inc'][j]]['selected_value']]+100)/100;
						}
					}
				}
			}
		}
	}
	// Assign calculated modifiers
	for (i in pr_o[id]) {
		if (pr_o[id][i]['type'] == 'S' || pr_o[id][i]['type'] == 'R' || pr_o[id][i]['type'] == 'H') {
			for (j in pr_o[id][i]['v']) {
				if (pr_o[id][i]['md'][j]) {
//					pr_o[id][i]['v'][j] = pr_o[id][i]['or_n'][j] + modifier_format(pr_o[id][i]['md'][j], pr_o[id][i]['md_t'][j]);
					pr_o[id][i]['v'][j] = pr_o[id][i]['or_n'][j];
				} else {
					pr_o[id][i]['v'][j] = pr_o[id][i]['or_n'][j];
				}
			}
		}
	}
	// Update base price
	price[id] = price_orig[id];
	for (i in pr_o[id]) {
		if (pr_o[id][i]['type'] == 'S' || pr_o[id][i]['type'] == 'R' || pr_o[id][i]['type'] == 'H') {
			if (pr_o[id][i]['b_p'] == "Y") {
				price[id] = apply_modifier(pr_o[id][i]['md_t'][pr_o[id][i]['selected_value']] + pr_o[id][i]['md'][pr_o[id][i]['selected_value']], price[id]);
			}
		}
	}

}
//
// Check all selected options variants in exceptions
//
function fn_check_exceptions(id)
{
var m = 0; n = 0; k = 0; index = 0;
	if (typeof(pr_o[id]) == 'undefined') {
		return true;
	}
	// Define Selected values for each option
	for (i in pr_o[id]) {
		if (pr_o[id][i]['type'] == 'S' || pr_o[id][i]['type'] == 'H') {
			pr_o[id][i]['selected_value'] = document.getElementById(pr_o[id][i]['id']).value;
		} else if (pr_o[id][i]['type'] == 'C') {	
			for (var iii in pr_o[id][i]['v']) {
				document.getElementById('unchecked_'+pr_o[id][i]['id']).value = iii;
				break;
			}
			if (document.getElementById(pr_o[id][i]['id']).checked) {
				pr_o[id][i]['selected_value'] = document.getElementById(pr_o[id][i]['id']).value;
			} else {
				pr_o[id][i]['selected_value'] = document.getElementById('unchecked_'+pr_o[id][i]['id']).value;
			}
		} else if (pr_o[id][i]['type'] == 'R'){
			for (var k = 0; k < document.getElementById(pr_o[id][i]['id']).getElementsByTagName("INPUT").length; k++) {
				if (document.getElementById(pr_o[id][i]['id']).getElementsByTagName("INPUT")[k].checked == true) {
					pr_o[id][i]['selected_value'] = document.getElementById(pr_o[id][i]['id']).getElementsByTagName("INPUT")[k].value;
				}		
			}
		}
	}
	// Update options modifiers
	fn_check_new_modifiers(id);

	// Checks the image for an option combination
	fn_check_option_image(id);
	// Checks the image for an option combination
	fn_change_amount(id);

	if (typeof(exceptions[id]) == 'undefined') {
		fn_rebuild_options(id);
		fn_update_product_price(id);
		return true;
	}
	// Resets all variants in the selectboxes
	fn_empty_selectboxes(id);
	// Function that calculates how many options in exception
	if (fn_calculate_matches(id) == false) {
		return false;
	}

	if (exception_style == 'warning') {
		document.getElementById('warning_' + id).style.display='none';
		fn_disable(id);
		return true;
	}

	// Disables options that have status disabled in exception and other exceptions sutisfied
	fn_disable(id);

	// This section fills the array with options considering the exceptions that should be hided
	for (m = 0; m < exceptions[id].length; m++) { // Cylcle all exceptions for the product
		if (except[id][m] == fn_array_length(pr_o[id]) - 1) {
			var k_inc = 0;
			if (!hide[id]) {
				hide[id] = new Array();
			}
			for (k in pr_o[id]) { // Cycle all options of the product
				if (!hide[id][k]) {
					hide[id][k] = new Array();
				}
				if ((pr_o[id][k]['selected_value'] != exceptions[id][m][k_inc]) && (exceptions[id][m][k_inc] != '-1')) {
					var j_inc = 0;
					for (var j in pr_o[id][k]['v']) { // Cycle all option variants
						if (j == exceptions[id][m][k_inc]) {
							if (pr_o[id][k]['type'] == 'S') {
								hide[id][k][j] = 'Y';
							}
							if (pr_o[id][k]['type'] == 'C') {
								document.getElementById('unchecked_'+pr_o[id][k]['id']).value = pr_o[id][k]['selected_value'];
								document.getElementById(pr_o[id][k]['id']).disabled = true;
							}
							if (pr_o[id][k]['type'] == 'R') {
								document.getElementById(pr_o[id][k]['id']).getElementsByTagName("INPUT")[j_inc].disabled = true;
							}
						}
						j_inc ++;
					}
				}
				k_inc ++;
			}
		}
	}

	fn_rebuild_options(id);

	// Update the prodcut price due to selected variants
	fn_update_product_price(id);
}

function fn_rebuild_options(id)
{
	// The sections fills the selectboxes with values considering excepted variants
	if (!hide[id])	{
		hide[id] = new Array();
	}
	for (m in pr_o[id]) {
		j = 0;
		if (!hide[id][m])	{
			hide[id][m] = new Array();
		}
		for (var k in pr_o[id][m]['v']) {
			if (!hide[id][m][k]) {
				hide[id][m][k] = '';
			}
			// If this variant shouldn't be hided and this option is selectbox than add this variant
			if ((hide[id][m][k] != 'Y') && (pr_o[id][m]['type'] == 'S')) {
				document.getElementById(pr_o[id][m]['id']).options[j] = new Option(pr_o[id][m]['v'][k], k);
				// Check if this variants was selected before the check exceptions function called
				if (pr_o[id][m]['selected_value'] == k) {
					document.getElementById(pr_o[id][m]['id']).options[j].selected = true;
				}
				j ++;
			}
			hide[id][m][k] = '';
		}
	}
}


function fn_calculate_matches(id)
{
	// Calculate the mathes between exceptions and selected variants at the moment
	except[id] = new Array;
	exceptnone[id] = new Array;
	if (exception_style == 'warning') {
		document.getElementById('warning_' + id).style.display='none';
	}
	for (var m = 0; m < exceptions[id].length; m++) {
		k = 0;
		n = 0;
		j = 0;
		except[id][m] = 0;
		exceptnone[id][m] = 0;
		for (var k in pr_o[id]) {
			if (pr_o[id][k]['selected_value'] == exceptions[id][m][j]) {
				n ++; 
				except[id][m] = n;
			}
			if (exceptions[id][m][j] == '-1') {
				n ++; 
				except[id][m] = n;
			}
			if (exceptions[id][m][j] == '-2') {
				exceptnone[id][m] += 1;
			}
			j ++;
	    }
	}
	for (var m = 0; m < exceptions[id].length; m++) {
		if (except[id][m] == fn_array_length(pr_o[id])) {
			if (exception_style == 'warning') {
				document.getElementById('warning_' + id).style.display='';
				fn_empty_selectboxes(id);
				fn_disable(id);
				return false;	
			}
			// Change selected variants to avoid exception
			fn_all_excepted(id);
		}
	}
}

//
// Cleans all values from selectboxes and enables all options
//
function fn_empty_selectboxes(id) 
{
	for (i in pr_o[id]) {
		if (pr_o[id][i]['type'] == 'S') {
			if (exception_style != 'warning') {
				document.getElementById(pr_o[id][i]['id']).options.length = 0;
			}
		   document.getElementById(pr_o[id][i]['id']).disabled = false;
		} else if (pr_o[id][i]['type'] == 'C') {
			document.getElementById(pr_o[id][i]['id']).disabled=false;	
		} else if (pr_o[id][i]['type'] == 'R'){
			for (var k = 0; k < document.getElementById(pr_o[id][i]['id']).getElementsByTagName("INPUT").length; k++) {
			   document.getElementById(pr_o[id][i]['id']).getElementsByTagName("INPUT")[k].disabled = false;
		   }
		}
	}
}

//
// Disables options if the the other exception variants are selected and the option has "disable" status
//
function fn_disable(id) 
{
	for (i = 0; i < exceptions[id].length; i++) {
		if (except[id][i] == fn_array_length(pr_o[id]) - exceptnone[id][i]) {
			j = 0;
			for (var k in pr_o[id]) {
				if (exceptions[id][i][j] == '-2') {
					if (pr_o[id][k]['type'] == 'S') {
						document.getElementById(pr_o[id][k]['id']).disabled = true;
					} else if (pr_o[id][k]['type'] == 'C') {	
						document.getElementById(pr_o[id][k]['id']).disabled = true;
					} else if (pr_o[id][k]['type'] == 'R'){
						for (var j = 0; j < document.getElementById(pr_o[id][k]['id']).getElementsByTagName("INPUT").length; j++) {
							document.getElementById(pr_o[id][k]['id']).getElementsByTagName("INPUT")[j].disabled = true;
						}				
					}
				}
			j ++;
			}
		}
	}
}
//
// Function that called if curent selected values are in exception
//
function fn_all_excepted(id)
{
	var opt_id = pr_o[id][fn_key_by_iter(pr_o[id], 0)]['option_id'];
	var var_id = fn_key_by_iter(pr_o[id][fn_key_by_iter(pr_o[id], 0)]['v'], 0);

	for (var i = 0; i < exceptions[id].length; i++) {
		if (except[id][i] == fn_array_length(pr_o[id])) {
			// if option has -disregard- exception variant
			if (exceptions[id][i][dni]=='-1') {
				  dni ++;
				  opt_id = pr_o[id][fn_key_by_iter(pr_o[id], dni)]['option_id'];
				  ind = 0;
				  var_id = fn_key_by_iter(pr_o[id][fn_key_by_iter(pr_o[id], dni)]['v'], ind);
			}
			/*
			if (pr_o[id][opt_id]['type'] == 'S') {
				//pr_o[id][opt_id]['v'][var_id]['hide'] = 'Y';
			} else if (pr_o[id][opt_id]['type'] == 'C') {
				document.getElementById(pr_o[id][opt_id]['id']).checked=true;
			} else if (pr_o[id][opt_id]['type'] == 'R') {
				document.getElementById(pr_o[id][opt_id]['id']).getElementsByTagName("INPUT")[var_id].disabled = true;
			}*/

			// Next option variant index is selected else Change option
			if (ind != fn_array_length(pr_o[id][opt_id]['v'])-1) {
				ind ++;
				var_id = fn_key_by_iter(pr_o[id][fn_key_by_iter(pr_o[id], dni)]['v'], ind);
			} else {
				if (pr_o[id][opt_id]['type'] == 'S') {
					pr_o[id][opt_id]['selected_value'] = fn_key_by_iter(pr_o[id][opt_id]['v'], ind);
				} else if (pr_o[id][opt_id]['type'] == 'C') {
					document.getElementById(pr_o[id][opt_id]['id']).disabled=false; 
					document.getElementById(pr_o[id][opt_id]['id']).checked=false;
				} else if (pr_o[id][opt_id]['type'] == 'R') {
					document.getElementById(pr_o[id][opt_id]['id']).getElementsByTagName("INPUT")[0].checked = true;		
				}
				dni ++;
				opt_id = pr_o[id][fn_key_by_iter(pr_o[id], dni)]['option_id'];
				ind = 0;
				var_id = fn_key_by_iter(pr_o[id][fn_key_by_iter(pr_o[id], dni)]['v'], ind);
			}

			// This section select the next variant in the option
			if (pr_o[id][opt_id]['type'] == 'S') {
				pr_o[id][opt_id]['selected_value'] = pr_o[id][opt_id]['v'][var_id]['variant_id'];
			} else if (pr_o[id][opt_id]['type'] == 'C') {
				document.getElementById(pr_o[id][opt_id]['id']).checked=true
				document.getElementById(pr_o[id][opt_id]['id']).disabled=true;
				for (var iii in pr_o[id][opt_id]['v']) {
					pr_o[id][opt_id]['selected_value'] = iii;
				}
				document.getElementById('unchecked_'+pr_o[id][opt_id]['id']).value = pr_o[id][opt_id]['selected_value'];
			} else if (pr_o[id][opt_id]['type'] == 'R') {
				document.getElementById(pr_o[id][opt_id]['id']).getElementsByTagName("INPUT")[ind].checked = true;
				pr_o[id][opt_id]['selected_value'] = var_id;
			}

		}
	}
	fn_calculate_matches(id);
}
//
// Functions to convert some values to price format
//
function modifier_format(value, type)
{
	if (value > 0) {
		sign = '+';
	} else {
		sign = '-';
	}

	if (type == 'A' || type == 'F') {
		return ' ('+sign+'$'+formatNumber(value, 0)+')';
	} else {
		return ' ('+sign+formatNumber(value, 0)+'%)';		
	}
}

function formatNumber (num, decplaces) {
    // convert in case it arrives as a string value
    num = parseFloat(num);
    // make sure it passes conversion
    if (!isNaN(num)) {
        // multiply value by 10 to the decplaces power;
        // round the result to the nearest integer;
        // convert the result to a string
        var str = "" + Math.round (eval(num) * Math.pow(10,decplaces));
        // exponent means value is too big or small for this routine
        if (str.indexOf("e") != -1) {
            return "Out of Range";
        }
        // if needed for small values, pad zeros
        // to the left of the number
        while (str.length <= decplaces) {
            str = "0" + str;
        }
        // calculate decimal point position
        var decpoint = str.length - decplaces;
        // assemble final result from: (a) the string up to the position of
        // the decimal point; (b) the decimal point; and (c) the balance
        // of the string. Return finished product.
        return Math.abs(str.substring(0,decpoint));// + "." + str.substring(decpoint,str.length);
    } else {
        return "NaN";
    }
}

// This function applies modidier to the price
function apply_modifier(mod, base)
{
	if (mod.substring(0, 1) == 'A') {
		result = base + parseFloat(mod.substring(1, mod.length));
	} else if(mod.substring(0, 1)  == 'P') {
		result = base * (parseFloat(mod.substring(1, mod.length))/100 + 1);
	}
	return result;
}

// Function that called if curent selected values are in exception
function fn_array_length(array)
{
	var result = 0;

	for (var i in array) {
		result ++;
	}
	return result;
}

// Function finds a key by it's index
function fn_key_by_iter(array, iter)
{
	var result = 0, j = 0, i = 0;

	for (var i in array) {
		if (j == iter) {
			return i;
		}
		j ++;
	}
	return false;
}