// $Id: compatibilities.js 2692 2007-02-01 09:54:46Z zeke $

var compatible_classes = new Array();
var error_message = 0;
var initial_product_id = 0;
var product_class = 0;
var compatible = 0;
var group_has_selected = 0;

//
// Check if user can go to the section
//
function fn_check_step(step_id, new_step_id)
{
	// If we use the "Next" button then find out the new section
	if (step_id == '') {
		step_id = location.hash.substr(1);
	}
	for (var i in sections)	{
		if (new_step_id == '' && j == step_id) {
			new_step_id = i;
		}
		var j = i;
	}
	// Check whether all required groups have products
	for (var groups_ in conf[step_id]) {
		selected_product = fn_define_selected_product(step_id, groups_);
		if (conf[step_id][groups_]['required'] == 'Y' && selected_product == false) {
			alert(text_required_group_product.replace('[group_name]', conf[step_id][groups_]['name']));
			fn_show_section(step_id, sections);
			return false;
		}
	}
	// if the last section is selected then hide the "Next" button, and show "Add to cart" button
	if (new_step_id == j) {
		if (document.getElementById('next_button')) {
			document.getElementById('next_button').style.display = 'none';
		}
		if (document.getElementById('add_to_cart_button')) {
			document.getElementById('add_to_cart_button').style.display = 'block';
		}
		if (document.getElementById('add_to_wish_button')) {
			document.getElementById('add_to_wish_button').style.display = 'block';
		}
	} else {
		if (document.getElementById('next_button')) {
			document.getElementById('next_button').style.display = 'block';
		}
	}

	fn_show_section(new_step_id, sections);
	return true;
}

// ************************************************** C O M P A T I B I L I T I E S ****************************************/
//
// Check all compatibilities
//
function fn_check_all_compatibilities()
{
	for (var step_id in conf) {
		for (var group_id in conf[step_id]) {
			selected_product = fn_define_selected_product(step_id, group_id)
			// If any product is selected then define compatibilities for it
			if (selected_product != false && selected_product.indexOf(':') != -1) {
				do {
					fn_check_compatibilities(group_id, selected_product.substring(0, selected_product.indexOf(':')), conf[step_id][group_id]['type'], false);
					selected_product = selected_product.substr(selected_product.indexOf(':')+1);
				} while (selected_product.indexOf(':') != -1);
			} else if (selected_product != false) {
				fn_check_compatibilities(group_id, selected_product, conf[step_id][group_id]['type'], false);
			}
		}
	}
	// Check whether refresh was clicked on thу last step
	if (s_section == step_id) {
		if (document.getElementById('next_button')) {
			document.getElementById('next_button').style.display = 'none';
		}
		if (document.getElementById('add_to_cart_button')) {
			document.getElementById('add_to_cart_button').style.display = 'block';
		}
		if (document.getElementById('add_to_wish_button')) {
			document.getElementById('add_to_wish_button').style.display = 'block';
		}
	}
	fn_update_conf_price(conf_product_id);
}

//
// Check selected product
//
function fn_check_compatibilities(group_id, product_id, type, update_price)
{

	// Define configuration products
	if (type == 'S') {
		initial_product_id = document.getElementById('group_'+group_id).value;
	} else if (type == 'R' || type == 'C') {
		initial_product_id = product_id;
	}
	
	// Hide selectbox 'details' link if 'none' option selected
	if (initial_product_id == 0 && type == 'S') {
		if (document.getElementById('select_'+group_id)) {
			document.getElementById('select_'+group_id).style.display = 'none';
		}
	} else {
		if (document.getElementById('select_'+group_id)) {
			document.getElementById('select_'+group_id).style.display = 'block';
		}
	} 

	// Define compatible classes and their configurator_group_id for selected product
	// Array сompatible_classes consists of [key] = compatible class id and [value] = configurator_group_id configurator group of this class 
	compatible_classes = new Array();

	if (initial_product_id != 0 && conf_prod[group_id][initial_product_id]['compatible_classes'] != 'undefined') {
		for (_class_id in conf_prod[group_id][initial_product_id]['compatible_classes']) {
			compatible_classes[_class_id] = conf_prod[group_id][initial_product_id]['compatible_classes'][_class_id];
		}
	} 

	if (compatible_classes.length != 0) { 
		fn_disable_incompatible(compatible_classes, group_id);		
	}
	// Update product price
	if (update_price != false) {
		fn_update_conf_price(conf_product_id);
	}
}

//
// This function disable incompatible products using array "compatible_classes"
//
function fn_disable_incompatible(compatible_classes, group_id) 
{
	var i = 0;
	var set_next = 0;
	var s_selected = '';

	// Restore currrent group products
	if (document.getElementById('group_'+group_id) && document.getElementById('group_'+group_id).options) {
		s_selected = document.getElementById('group_'+group_id).selectedIndex;
		sbox = document.getElementById('group_'+group_id);
		sbox.options.length = 0;
	}

	for (restore_product in conf_prod[group_id]) {
		if (conf_prod[group_id][restore_product]['type'] == 'R') {
			document.getElementById('group_' + group_id + '_product_'+restore_product).disabled = false;
		} else if (conf_prod[group_id][restore_product]['type'] == 'C') { 
			document.getElementById('group_' + group_id + '_product_'+restore_product).disabled = false;
		} else if (conf_prod[group_id][restore_product]['type'] == 'S') { 
			sbox.options[i] = new Option(conf_prod[group_id][restore_product]['product_name'], restore_product);
			if (i == s_selected) {
				sbox.options[i].selected = true
			}
			i++;
		}
	}

	// Check all other groups corresponding to the current
	for(check_group_id in conf_prod) {
		if (check_group_id != group_id) {
			//If checked group is selectbox then delete all selectbox variants for showing only compatible variants 
			if (document.getElementById('group_'+check_group_id) && document.getElementById('group_'+check_group_id).options) {
				sbox = document.getElementById('group_'+check_group_id);
				s_selected = sbox.value; // save currently selected value to select it after rebuiding options
				sbox.options.length = 0;
			}
		
			i = 0;
			for(check_product_id in conf_prod[check_group_id]) {	
				compatible = 1;
				// If checking group is in comp classes and this product has any class and this class is not in compatible classes then this product is incompatible
				for (check_class_id in compatible_classes) {
					if ((check_group_id == compatible_classes[check_class_id]) && (conf_prod[check_group_id][check_product_id]['class_id'] != '') && (conf_prod[check_group_id][check_product_id]['class_id'] != check_class_id)) {
						compatible = 0;
					} 
				}
				// If current product is compatible then release it
				if (compatible == 1) { 
					if (conf_prod[check_group_id][check_product_id]['type'] == 'R') {
						document.getElementById('group_' + check_group_id + '_product_'+check_product_id).disabled = false;
						// Set next variant if existing is disabled
						if (set_next == 1) {
							document.getElementById('group_' + check_group_id + '_product_'+check_product_id).checked = true;
							set_next = 0;
						}
					} else if (conf_prod[check_group_id][check_product_id]['type'] == 'C') { 
						document.getElementById('group_' + check_group_id + '_product_'+check_product_id).disabled = false;
					} else if (conf_prod[check_group_id][check_product_id]['type'] == 'S') { 
						if (i==0 && conf_prod[check_group_id][check_product_id]['required'] != 'Y') {
							sbox.options[i] = new Option('None',0);
							i++;
						}
						sbox.options[i] = new Option(conf_prod[check_group_id][check_product_id]['product_name'], check_product_id);
						i++;
					}
				// if current product is incompatible then disable it
				} else { 
					if (conf_prod[check_group_id][check_product_id]['type'] == 'R') {
						document.getElementById('group_' + check_group_id + '_product_'+check_product_id).disabled = true;
						if (document.getElementById('group_' + check_group_id + '_product_'+check_product_id).checked == true) {
							document.getElementById('group_' + check_group_id + '_product_'+check_product_id).checked = false;
							set_next = 1;
						}
					} else if (conf_prod[check_group_id][check_product_id]['type'] == 'C') {
						document.getElementById('group_' + check_group_id + 'product_'+check_product_id).disabled = true;
						if (document.getElementById('group_' + check_group_id + '_product_'+check_product_id).checked == true) {
							document.getElementById('group_' + check_group_id + '_product_'+check_product_id).checked = false;
						}
					} 
				}		
			}
			if (set_next == 1) {
				for(c_product_id in conf_prod[check_group_id]) {
					if (document.getElementById('group_'+check_group_id+'_product_'+c_product_id).disabled == false && set_next == 1) {
						document.getElementById('group_'+check_group_id+'_product_'+c_product_id).checked = true;
						set_next = 0;
					}
				}
			}
			if (document.getElementById('group_'+check_group_id) && document.getElementById('group_'+check_group_id).options) {
				sbox.value = s_selected;
			}
		}
	}
}

//
// This defines the selected product in the current group
//
function fn_define_selected_product(step_id, group_id)
{
	var selected_product = false;
	// Define which product is selected in the group
	if (conf[step_id][group_id]['type'] == 'S') {
		selected_product = document.getElementById('group_'+group_id).value;
	} else if (conf[step_id][group_id]['type'] == 'R') {
		var d_form = document.getElementById('group_'+group_id).getElementsByTagName("INPUT");
		for(var elem=0; elem < d_form.length; elem++) {
			if (d_form[elem].type == "radio" && d_form[elem].checked == true) {
				selected_product = d_form[elem].value;
			}
		}
	} else if (conf[step_id][group_id]['type'] == 'C') {
		var d_form = document.getElementById('group_'+group_id).getElementsByTagName("INPUT");
		for(var elem=0; elem < d_form.length; elem++) {
			if (d_form[elem].type == "checkbox" && d_form[elem].checked == true) {
				if (selected_product == false) {
					selected_product = ''
				}
				selected_product += d_form[elem].value + ':';
			}
		}
	}
	return selected_product;
}

//
// This function disable incompatible products using array "compatible_classes"
//
function fn_update_conf_price(id)
{
	var selected_product;
	var new_price = parseFloat(fn_format_price(updated_price[id], decplaces));
	for (var step_id in conf) {
		for (var group_id in conf[step_id]) {
			selected_product = fn_define_selected_product(step_id, group_id);
			// If any product is selected then define compatibilities for it
			if (selected_product != false)	{
				if (selected_product.indexOf(':') != -1) {
					do {
						new_price += conf_prod[group_id][selected_product.substring(0, selected_product.indexOf(':'))]['price'];
						selected_product = selected_product.substr(selected_product.indexOf(':')+1);
					} while (selected_product.indexOf(':') != -1);
				} else {
					new_price += conf_prod[group_id][selected_product]['price'];
				}
			}
		}
	}

	new_price = parseFloat(fn_format_price(new_price, decplaces));
	var alt_price = parseFloat(fn_format_price(new_price / sec_curr_coef, decplaces));

	// Original currency
	if (document.getElementById('original_price_' + id)) {
		document.getElementById('original_price_' + id).innerHTML = fn_format_num(new_price, 2, 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, 2, false);
	}
	if (pr_d[id]) {
		var discount_a = parseFloat(fn_format_price(updated_price[id] * pr_d[id]['P']/100 + pr_d[id]['A'], decplaces));
		var alt_discounted_price = fn_format_price((updated_price[id] - discount_a) / sec_curr_coef, decplaces);

		// Discounted price
		if (document.getElementById('discounted_price_' + id)) {
			document.getElementById('discounted_price_' + id).innerHTML = fn_format_num(new_price - 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);
		}
	}
}
