function calculate_attributes(attribute)
{
	
	for (var counter = 1; counter < attributes.length; counter++)
	{
		if ((attributes[counter].length > 0) && (attribute != 0) && (get_attribute_object(counter)))
		{
			selected[counter] = get_attribute_value(counter);
		
			if ((counter != attribute) || (selected[counter] == 0))
				clear_attribute_object(counter);
		}
		else
		{
			selected[counter] = 0;
		}
	}

	for (var counter = 0; counter < stock.length; counter++)
	{
		for (var counter2 = 1; counter2 < (attribute_max + 1); counter2++)
		{
			if ((attributes[counter2].length > 0) && (stock[counter][counter2] != 0) && (get_attribute_object(counter2))) 
			{
				var add = true;
							
				for (var counter3 = 1; counter3 < attributes.length; counter3++)
				{				
					if ((selected[counter3] != 0) && (attributes[counter3].length > 0))
					{
						if ((stock[counter][counter3] != selected[counter3]) && (counter2 != counter3))
						{
							//alert("Stock line " + counter + " attribute " + counter3 + " = " + stock[counter][counter3] + " and the selected value is " + selected[counter3]);
							//alert("disregard");
							add = false;
						}
					}
				}
				
				if ( add ) 
					add_attribute_object(counter2, attributes[counter2][stock[counter][counter2]], stock[counter][counter2]);
			}
		}
	}
	
	sort_attributes();
		
	for (var counter = 1; counter < attributes.length; counter++)
	{	
		if ((attributes[counter].length > 0) && (get_attribute_object(counter)))
		{
			for (var counter2 = 0; counter2 < get_attribute_object(counter).options.length; counter2++)
			{
				if (selected[counter] != 0)
				{
					if (selected[counter] == get_attribute_object(counter).options[counter2].value)
						get_attribute_object(counter).options[counter2].selected = true;
				}
				else
				{
					//if ((get_attribute_object(counter).options.length == 2) && (counter != attribute))			
					//	get_attribute_object(counter).options[1].selected = true;
				}
			}
		}
	}
}

function sort_attributes()
{
	for (var counter = 1; counter < attributes.length; counter++ )
	{
		if ((attributes[counter].length > 0) && (get_attribute_object(counter)))
		{
			var options = [];
			
			for ( var counter2 = 0; counter2 < get_attribute_object(counter).options.length; counter2++)
			{
				options[counter2] = {text:get_attribute_object(counter).options[counter2].text, value:get_attribute_object(counter).options[counter2].value};
			}
			
			options.sort(sort_helper);
			
			get_attribute_object(counter).options.length = 0;
			add_attribute_object(counter, select_text, 0);
			
			for (var counter2 = 0; counter2 < options.length; counter2++) 
			{
				add_attribute_object(counter, options[counter2].text, options[counter2].value);
			}
		}
	}
}

function sort_helper(record1, record2)
{
	if ((record1.value == 0) || (record2.value==0))
		return 1;
	var value1 = record1.value;
	var value2 = record2.value;
	if (value1 > value2) return(1);
	if (value1 < value2) return(-1);
	return(0);
}

function get_attribute_value(attribute)
{
	return get_attribute_object(attribute).value
}

function get_attribute_object(attribute)
{
	return document.getElementById('attribute_selector' + attribute);
}

function clear_attribute_object(attribute)
{
	for (var counter = get_attribute_object(attribute).options.length - 1; counter >= 0; counter--)
    {
        get_attribute_object(attribute).options[counter] = null;
    }
}

function add_attribute_object(attribute, text, value)
{	
	for (var counter = 0; counter < get_attribute_object(attribute).options.length; counter++)
	{
		if (get_attribute_object(attribute).options[counter].value == value)
			return;
	}
	
	var option = document.createElement("OPTION");
	option.text = text;
	option.value = value;

	get_attribute_object(attribute).options.add(option);
}