// $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 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;

			}

		}

	}

	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;

}



// 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]) {

		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(modifiers[i].substring(1, modifiers[i].length-1));

		} else if(modifiers[i].substring(0, 1)  == 'P') {

			total += price[id] * parseFloat(modifiers[i].substring(1, modifiers[i].length-1))/100;

		}

	}

	// 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 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]['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);

	// Checks the image for an option combination

	fn_change_amount(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).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)

}



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);

}



// 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;

}