var products = new Array();		// Empty array to hold product ids
var prod_types = new Array();		// Empty array to hold product types
var compare_num_limit = 3; 		// Limit of products that can be compared
var compare_num = 0;				// Number of products selected to be compared

function selected_compare(prod_id, prod_type, div_element) {

	if ($(div_element).getStyle('borderColor') == 'yellow' || $(div_element).getStyle('borderColor') == 'yellow yellow yellow yellow') {
		
		if (b_version.indexOf('MSIE 6.0') >= 0) {
			$(div_element).setStyle({ borderColor: 'orange' });
		} else {
			$(div_element).setStyle({ borderColor: 'transparent' });
		}
		
		compare_num--;
		remove_prod_to_compare(prod_id);
		remove_product_type(prod_type);
	} else {
		if ( compare_num == 3) {
			alert("You can only select " + compare_num_limit + " products to compare.");
			return false;
		} 
		$(div_element).setStyle({ borderColor: 'yellow' });
		compare_num++;
		add_prod_to_compare(prod_id);
		add_product_type(prod_type);
	}
}
// Purpose: appends product id to array
function add_prod_to_compare(prod_id) {
	products[products.length] = prod_id;
}
// Purpose: removes product id from array
function remove_prod_to_compare(prod_id) {
	for (var x=0; x < products.length; x++) {
		if (products[x] == prod_id) {
			products.splice(x, 1);
			break;
		}
	}
}
// Purpose: appends product type to array
function add_product_type(prod_type) {
	prod_types[prod_types.length] = prod_type;
}
// Purpose: removes product type from array
function remove_product_type(prod_type) {
	for (var x=0; x < prod_types.length; x++) {
		if (prod_types[x] == prod_type) {
			prod_types.splice(x, 1);
			break;
		}
	}
}
function validate_compare() {
	if (products.length > compare_num_limit) {
		alert("You can only select " + compare_num_limit + " products to compare.");
		return false;
	} else if (products.length <= 1) {
		alert("You must select at least 2 products to compare.");
		return false;
	} else if ( validate_prod_type() ) {
		alert("Products must be of the same type to compare.");
		return false;
	}
	
	
	$('compare').value = products.join('_'); 	// Assign value to hidden field - each value sperated by underscore
	return true;	// Passed validation
}


function validate_prod_type() {
	var prod_types_string = '';
	for (var x=0; x < prod_types.length; x++) {
		if (x == (prod_types.length-1)) {
			prod_types_string = prod_types_string + prod_types[0] 
		} else {
			prod_types_string = prod_types_string + prod_types[0] + "_";
		}
	}
	
	if (prod_types.join('_') == prod_types_string) {
		return false;		// Passed
	} else {
		return true;	// Failed
	}
	
}
