// $Id: exceptions.js 2474 2006-11-05 11:39:30Z zeke $

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 updated_price = new Array();
var list_price = new Array();
var dni = 0;
var ind = 0;
var i = 0; 
var j = 0;
var recursion = 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();

// Number of digits in decimal part 
var decplaces = 2;

//
// 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;
			}
		}
	}
	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, list_price)
{
	var save_a = parseFloat(fn_format_price(list_price - total, decplaces));
	var save_p = parseFloat(fn_format_price((list_price - total)/ list_price * 100, 0));

	// List Price
	if (document.getElementById('list_price_' + id)) {
		document.getElementById('list_price_' + id).innerHTML = fn_format_num(list_price, decplaces);
	}

	// You save amount
	if (document.getElementById('save_' + id)) {
		document.getElementById('save_' + id).innerHTML = fn_format_num(save_a, decplaces, true);
	}
	// You saev amount in secondary currency
	if (document.getElementById('sec_save_' + id)) {
		document.getElementById('sec_save_' + id).innerHTML = fn_format_num(save_a / sec_curr_coef, decplaces, false);
	}
	// Percent in You save
	if (document.getElementById('save_p_' + id)) {
		document.getElementById('save_p_' + id).innerHTML = fn_format_num(save_p, 0, true);
	}
	return true;
}

// This function updates the discount values and other corresponding value
function fn_update_discount(id, total)
{
	var discount_a = parseFloat(fn_format_price(total * pr_d[id]['P']/100 + pr_d[id]['A'], decplaces));
	var discount_p = parseFloat(fn_format_price((total * pr_d[id]['P']/100 + pr_d[id]['A'])/total*100, 0));

	var alt_discount = fn_format_price(discount_a / sec_curr_coef, decplaces);
	var alt_discounted_price = fn_format_price((total - discount_a) / sec_curr_coef, decplaces);

	// You save amount
	if (document.getElementById('save_' + id)) {
		document.getElementById('save_' + id).innerHTML = fn_format_num(discount_a, decplaces, true);
	}
	// You saev amount in secondary currency
	if (document.getElementById('sec_save_' + id)) {
		document.getElementById('sec_save_' + id).innerHTML = fn_format_num(alt_discount, decplaces, false);
	}
	// Percents in Label
	if (document.getElementById('discount_label_' + id)) {
		document.getElementById('discount_label_' + id).innerHTML = fn_format_num(discount_p, 0, true);
	}
	// Discounted price
	if (document.getElementById('discounted_price_' + id)) {
		document.getElementById('discounted_price_' + id).innerHTML = fn_format_num(total - discount_a, decplaces, true);
	}
	// Discounted price in secondary currency
	if (document.getElementById('sec_discounted_price_' + id)) {
		document.getElementById('sec_discounted_price_' + id).innerHTML = fn_format_num(alt_discounted_price, decplaces, false);
	}
	// Subtotal in cart
	if (document.getElementById('discounted_subtotal_' + id)) {
		document.getElementById('discounted_subtotal_' + id).innerHTML = fn_format_num(parseInt(document.getElementById('amount_' + id).value) * (total-discount_a), decplaces, true);
	}
	// Subtotal in cart in secondary currency
	if (document.getElementById('sec_discounted_subtotal_' + id)) {
		document.getElementById('sec_discounted_subtotal_' + id).innerHTML = fn_format_num(parseInt(document.getElementById('amount_' + id).value) * alt_discounted_price, decplaces, false);
	}
	return true;
}

// This function calculates the new product price and updates values
function fn_update_product_price(id)
{
	var total;
	var modifiers = new Array();

	total = parseFloat(fn_format_price(price[id], decplaces));
	list_pr = (typeof(list_price[id]) != 'undefined') ? parseFloat(fn_format_price(list_price[id], decplaces)) : 0;
	for (i in pr_o[id]) {
		if (!document.getElementById(pr_o[id][i]['id'])) {
			continue;
		}
		modifiers[i] = pr_o[id][i]['m'][pr_o[id][i]['selected_value']];
		if (typeof(modifiers[i]) == 'undefined') {
			continue;
		}
		if (modifiers[i].substring(0, 1) == 'A') {
			total += parseFloat(fn_format_price(modifiers[i].substring(1, modifiers[i].length-1), decplaces));
			list_pr += parseFloat(fn_format_price(modifiers[i].substring(1, modifiers[i].length-1), decplaces));
		} else if(modifiers[i].substring(0, 1)  == 'P') {
			total += parseFloat(fn_format_price(price[id] * parseFloat(modifiers[i].substring(1, modifiers[i].length-1))/100, decplaces));
			list_pr += parseFloat(fn_format_price(price[id] * parseFloat(modifiers[i].substring(1, modifiers[i].length-1))/100, decplaces));
		}
	}

	updated_price[id] = fn_format_num(total, decplaces, true);
	var alt_price = parseFloat(fn_format_price(total / sec_curr_coef, decplaces));

	// Original currency
	if (document.getElementById('original_price_' + id)) {
		document.getElementById('original_price_' + id).innerHTML = fn_format_num(total, decplaces, true);
	}
	// Second currency of the price
	if (document.getElementById('sec_original_price_' + id)) {
		document.getElementById('sec_original_price_' + id).innerHTML = fn_format_num(alt_price, decplaces, false);
	}
	// Subtotal in cart
	if (document.getElementById('original_subtotal_' + id)) {
		document.getElementById('original_subtotal_' + id).innerHTML = fn_format_num(parseInt(document.getElementById('amount_' + id).value) * total, decplaces, true);
	}
	// Second currency of the subtotal in the cart
	if (document.getElementById('sec_original_subtotal_' + id)) {
		document.getElementById('sec_original_subtotal_' + id).innerHTML = fn_format_num(parseInt(document.getElementById('amount_' + id).value) * alt_price, decplaces, false);
	}

	if (pr_d[id]) {
		fn_update_discount(id, total);
	} else if (list_pr)	{
		fn_update_list_price(id, total, list_pr);
	}
	return true;
}

// *****************************************  Product code  **********************************/
//
// Changes product code to the code of combination
//
function fn_change_sku(id)
{
	var key;
	
	if (!document.getElementById('product_code_' + id))	{
		return false;
	}

	key = '';
	for (i in pr_o[id]) {
		if (!document.getElementById(pr_o[id][i]['id'])) {
			continue;
		}
		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]['product_code']) {
				document.getElementById('product_code_' + id).innerHTML = pr_a[id][i]['product_code']; 
			}
		}
	}
	return true;
}

// *****************************************  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;
	
	if (!document.getElementById('product_amount_' + id)) {
		return false;
	}

	key = '';
	for (i in pr_o[id]) {
		if (!document.getElementById(pr_o[id][i]['id'])) {
			continue;
		}
		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]['amount']) {
				if (pr_a[id][i]['amount'] != 0) {
					document.getElementById('product_amount_' + id).innerHTML = pr_a[id][i]['amount'] + '&nbsp;' + lang_items; 
				} else {
					document.getElementById('product_amount_' + id).innerHTML = '<span class="price">' + lang_out_of_stock + '</span>';
				}
				return true;
			}
		}
	}
	if (pr_a[id]['amount'] != '') {
		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 (!document.getElementById(pr_o[id][i]['id'])) {
			continue;
		}
		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)) {
		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_index+"?object_type=detailed&image_id="+pr_i[id][row]['detailed_id']+"&window=popup', "+pr_i[id][row]['detailed']['image_x']+", "+pr_i[id][row]['detailed']['image_y']+"+ 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)) {
		document.getElementById('img_width_' + id).width = pr_i[id][row]['icon']['image_x'];
		document.getElementById('det_img_' + id).src = pr_i[id][row]['icon']['src'];
		document.getElementById('det_img_' + id).height = pr_i[id][row]['icon']['image_y'];
		document.getElementById('det_img_' + id).width = pr_i[id][row]['icon']['image_x'];
		document.getElementById('det_img_' + id).alt = pr_i[id][row]['icon']['alt'];
		image_changed[id] = "Y";
		return true;
	}
	return false;

}

// ************************************************** E X C E P T I O N S ****************************************/
//
// 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 (!document.getElementById(pr_o[id][i]['id'])) {
			continue;
		}
		if (pr_o[id][i]['type'] == 'S') {
			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 the prodcut price due to selected variants
	fn_update_product_price(id);
	// Checks the image for an option combination
	fn_check_option_image(id);
	// Change the amount
	fn_change_amount(id);
	// Change the sku
	fn_change_sku(id);
	// If product is configurable then update conf price
	if (typeof(conf_product_id) != 'undefined' &&  conf_product_id == id) {
		fn_update_conf_price(id);
	}


	if (typeof(exceptions[id]) == 'undefined') {
		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)) {
		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);
	if (recursion == false)	{
		recursion = true;
		fn_check_exceptions(id);
	}
	return true;
}

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 (!document.getElementById(pr_o[id][i]['id'])) {
			continue;
		}
		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);
}

// 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;
}
