// JavaScript Document
/*
0 = PID
1 = QTY
2 = MSRP
3 = Your Retail
4 = Flat Rate Shipping
5 = Main Image
6+  Attributes
*/
var array_shift = 6;

function attribute_hover(attribute){

	if(attribute.className != "product_view_attribute_select" && attribute.className != "product_view_attribute_none")
		attribute.className = "product_view_attribute_hover";
	if(attribute.className == "product_view_attribute_none")
		{
			check_availability(attribute);
		}
	attribute_current(attribute);
	
}

function attribute_out(attribute){//Restore all UNCLICKED
	deselect_all_but_selected();
	attribute_selected();
	document.getElementById("product_availability").style.display = 'none';
}

function attribute_select(attribute){
	if(attribute.className != "product_view_attribute_none")
	{
		deselect_attribute_group(attribute);
		attribute.className = "product_view_attribute_select";
		attribute_current(attribute);
		update_cart_values();
	}
	else if(category_attribute.length > 1)
	{
		deselect_all();
		attribute.className = "product_view_attribute_select";
		attribute_current(attribute);
		update_cart_values();
	}
}

function attribute_current(attribute){//SETS ALL OTHER ATTRIBUTES VARIATIONS ACCORDING TO PASSED ATTRIBUTE
	var current_attribute_id = attribute.parentNode.id;

	attribute_other = new Array();
	var j = 0;//attribute_other KEY
	for(i = 0; i < category_attribute.length; i++)//Find other attribute location
	{
		if(category_attribute[i] != current_attribute_id)
		{
			attribute_other[j] = i + array_shift;//assign location of other attributes  + array_shift to search product_children array
			j++;
		}
		else
		{
			var attribute_location = i + array_shift;//assign location of current attribute
		}
	}

		
	for(i = 0; i < attribute_other.length; i++)//Turn all other attributes off
	{
		for(j =0; j < product_children.length; j++)//search each child then turn off
		{
			id = product_children[j][attribute_other[i]];
			if(document.getElementById(id).className != "product_view_attribute_select")
				document.getElementById(id).className = "product_view_attribute_none";
		}
	}
	for(i = 0; i < attribute_other.length; i++)//Search for availability
	{
		for(j =0; j < product_children.length; j++)//search each child and compare - then turn on
		{
			if(product_children[j][attribute_location] == attribute.id && product_children[j][1] > 0)//Turn on the availability
			{
				id = product_children[j][attribute_other[i]];
				if(document.getElementById(id).className != "product_view_attribute_select")
					document.getElementById(id).className = "product_view_attribute";
			}
		}
	}
	
	if(attribute_other.length == 0)
		check_single_availability();
			
}

function check_single_availability(){
	var current_attribute_id = category_attribute[0];

	for(i = 0; i < category_attribute.length; i++)//Find current attribute location
	{
		if(category_attribute[i] == current_attribute_id)
		{
			var attribute_location = i + array_shift;//assign location of current attribute
		}
	}
	for(j =0; j < product_children.length; j++)//search each child then turn off
	{
		id = product_children[j][attribute_location];
		if(document.getElementById(id).className != "product_view_attribute_select")
			document.getElementById(id).className = "product_view_attribute_none";
	}
	
	for(j =0; j < product_children.length; j++)//search each child and compare - then turn on
	{
		if(product_children[j][1] > 0)//Turn on the availability
		{
			id = product_children[j][attribute_location];
			if(document.getElementById(id).className != "product_view_attribute_select")
				document.getElementById(id).className = "product_view_attribute";
		}
		else
		{
			id = product_children[j][attribute_location];
			document.getElementById(id).className = "product_view_attribute_none";
		}
	}
	
}

function deselect_attribute_group(attribute){//DESELECTS ALL ATTRIBUTES IN CURRENT GROUP
	var current_attribute_id = attribute.parentNode.id;
	attribute_array = new Array();
	var j = 0;//attribute_other KEY
	for(i = 0; i < category_attribute.length; i++)//Find current attribute location
	{
		if(category_attribute[i] == current_attribute_id)
		{
			attribute_array[j] = i + array_shift;//assign location of other attributes  + array_shift to search product_children array
			j++;
		}
	}
	for(i = 0; i < attribute_array.length; i++)//Turn all other attributes off
	{
		for(j =0; j < product_children.length; j++)//search each child then turn off
		{
			id = product_children[j][attribute_array[i]];
			if(document.getElementById(id).className == "product_view_attribute_select")
				document.getElementById(id).className = "product_view_attribute";
		}
	}	
}

function deselect_all()//DESELECTS ALL ATTRIBUTES
{
	attribute_array = new Array();
	var j = 0;//attribute_other KEY
	for(i = 0; i < category_attribute.length; i++)//Find other attribute location
	{
		attribute_array[j] = i + array_shift;//assign location of other attributes  + array_shift to search product_children array
		j++;
	}
	for(i = 0; i < attribute_array.length; i++)//Turn all other attributes off
	{
		for(j =0; j < product_children.length; j++)//search each child then turn off
		{
			id = product_children[j][attribute_array[i]];
			document.getElementById(id).className = "product_view_attribute";
		}
	}		
}

function deselect_all_but_selected()
{
	attribute_array = new Array();
	var j = 0;//attribute_other KEY
	for(i = 0; i < category_attribute.length; i++)//Find other attribute location
	{
		attribute_array[j] = i + array_shift;//assign location of other attributes  + array_shift to search product_children array
		j++;
	}
	for(i = 0; i < attribute_array.length; i++)//Turn all other attributes off
	{
		for(j =0; j < product_children.length; j++)//search each child then turn off
		{
			id = product_children[j][attribute_array[i]];
			if(document.getElementById(id).className != "product_view_attribute_select")
				document.getElementById(id).className = "product_view_attribute";
		}
	}	

	if(attribute_array.length == 1)
	check_single_availability();

}

function attribute_selected()//RUNS ATTRIBUTE_CURRENT(ID) FOR ALL SELECTED ATTRIBUTES
{
	attribute_array = new Array();
	selected_array = new Array();
	sa = 0;
	var jj = 0;//attribute_other KEY
	for(ii = 0; ii < category_attribute.length; ii++)//Find attribute locations
	{
		attribute_array[jj] = ii + array_shift;//assign location of other attributes  + array_shift to search product_children array
		jj++;
	}
	for(ii = 0; ii < attribute_array.length; ii++)//Turn all other attributes off
	{
		for(jj = 0; jj < product_children.length; jj++)//search each child then turn off
		{
			id = product_children[jj][attribute_array[ii]];
			if(document.getElementById(id).className == "product_view_attribute_select")
			{
				selected_array[sa] = document.getElementById(id);
				sa++;
			}
		}
	}
	if(selected_array.length > 0)
	{
		turned_on = new Array();
		times = new Array();
		to = 0;
		for(sa = 0; sa < selected_array.length; sa++)
		{
			attribute = selected_array[sa];
			
		
			var current_attribute_id = attribute.parentNode.id;
		
			attribute_other = new Array();
			var j = 0;//attribute_other KEY
			for(i = 0; i < category_attribute.length; i++)//Find other attribute location
			{
				if(category_attribute[i] != current_attribute_id)
				{
					attribute_other[j] = i + array_shift;//assign location of other attributes  + array_shift to search product_children array
					j++;
				}
				else
				{
					var attribute_location = i + array_shift;//assign location of current attribute
				}
			}
						
		
			for(i = 0; i < attribute_other.length; i++)//Turn all other attributes off
			{
				for(j =0; j < product_children.length; j++)//search each child then turn off
				{
					id = product_children[j][attribute_other[i]];
					if(document.getElementById(id).className != "product_view_attribute_select")
						document.getElementById(id).className = "product_view_attribute_none";
				}
			}

			for(i = 0; i < attribute_other.length; i++)//Search for availability
			{
				for(j =0; j < product_children.length; j++)//search each child and compare - then turn on
				{
					if(product_children[j][attribute_location] == attribute.id && product_children[j][1] > 0)//Turn on the availability
					{
						id = product_children[j][attribute_other[i]];
						if(document.getElementById(id).className != "product_view_attribute_select" || document.getElementById(id).className == "product_view_attribute")
						{
							document.getElementById(id).className = "product_view_attribute";
							turned_on[to] = id;
							to++
						}
					}
				}
			}
				

		}
	//turn off times smaller than length
	
	for(i = 0; i < turned_on.length; i++)
	{
		count = 0;
		for(j = 0; j < turned_on.length; j++)
		{
			if(turned_on[j] == turned_on[i])
				count = count + 1;
		}
		if(count < selected_array.length)
			document.getElementById(turned_on[i]).className = "product_view_attribute_none";
	}
	
	}
}

function check_availability(attribute){
	var current_attribute_id = attribute.parentNode.id;	
	
	for(i = 0; i < category_attribute.length; i++)//find current_attribute_location
	{
		if(current_attribute_id == category_attribute[i])
			id_location = i + array_shift;
	}
	
	found = false;
	//check for selectted attribute in ID
	for(i = 0; i < product_children.length; i++)
	{
		if(document.getElementById(product_children[i][id_location]).className == "product_view_attribute_select")
			found = true;
	}
	
	if(found == false)
	{
		selected_values = get_selected();
		if(selected_values.length > 0)
		{
			document.getElementById("product_availability").style.display = 'inline';
			html = document.getElementById("product_sorry");
			html.innerHTML = "Sorry, this item is not available in:<br><br>";
			html.innerHTML += attribute.id;
			for(i = 0; i < selected_values.length; i++)
			{
				html.innerHTML += "<br>" + selected_values[i][0];
			}
		}
		else
		{
			document.getElementById("product_availability").style.display = 'inline';
			html = document.getElementById("product_sorry");
			html.innerHTML = "Sorry, this item is not available in:<br> " + attribute.id;
		}
	}
	
}

function get_selected() {
	selected_values = new Array();
	k = 0;
	for(i = 0; i < product_children.length; i++)
	{
			for(j = array_shift; j < product_children[0].length; j++)
			{
				if(document.getElementById(product_children[i][j]).className == "product_view_attribute_select")
				{
					unique = true;
					for(l = 0; l < selected_values.length; l++)
					{
						if(selected_values[l][0] == product_children[i][j])
						{
							unique = false;
						}
					}
						
					if(unique == true)
					{
						selected_values[k] = new Array();
						selected_values[k][0] = product_children[i][j];
						selected_values[k][1] = j;//location array
						k++;
					}
				}
			}
	}
	
	
	return selected_values;
}

function update_cart_values(){
	clear_cart();
	selected_values = get_selected();
	for(i = 0; i < selected_values.length; i++)
	{
		var current_attribute_id = document.getElementById(selected_values[i][0]).parentNode.id;
		document.getElementById(current_attribute_id + "_cart").innerHTML = "&nbsp;" + selected_values[i][0];
	}
	if(selected_values.length == category_attribute.length) //find and update product id
	{
		product_location = get_product_location();
		if(product_location != -1)
		{
			document.getElementById("product_id").value = product_children[product_location][0];
			document.getElementById("your_price").innerHTML = "$" + addCommas(parseFloat(product_children[product_location][3]).toFixed(2));
			if(document.getElementById("msrp"))
				document.getElementById("msrp").innerHTML = "$" + addCommas(parseFloat(product_children[product_location][2]).toFixed(2));
			if(document.getElementById("product_savings_value"))
				document.getElementById("product_savings_value").innerHTML = "$" + addCommas((parseFloat(product_children[product_location][2]) - parseFloat(product_children[product_location][3])).toFixed(2));
			
			//change image
			if(product_children[product_location][5] != '')
			{
				MM_swapImage('main_image','','../scripts/product_thumb.php?dimension=' + LabelMainSize + '&image_name=' + product_children[product_location][5],1);
				swap_a('../uploads/product_images/' + product_children[product_location][5]);
			}
		}
	}
}

function clear_cart(){
	for(i = 0; i < category_attribute.length; i++)
	{
		document.getElementById(category_attribute[i] + "_cart").innerHTML = "";
	}
	
	document.getElementById("product_id").value = "";
}

function get_product_location(){
	selected_values = get_selected();
	for(i = 0; i < product_children.length; i++)
	{
		pass = true;
		for( j = 0; j < selected_values.length; j++)
		{
			if(selected_values[j][0] != product_children[i][selected_values[j][1]])
				pass = -1;
		}
		if(pass == true)
			return i;
	}
	return -1;
}

function check_attributes(){
		check_button = document.getElementById("add_cart_button");
		not_allowed = document.getElementById("add_cart_button_not_allowed");
		if(category_attribute.length > 0)
		{
			selected_values = get_selected();
			if(selected_values.length != category_attribute.length)
			{
				Tip('Please choose from the options below the product pricing', LEFT, true, ABOVE, true, FADEIN, 400, FADEOUT, 400)
				check_button.style.display = "none";
				not_allowed.style.display = "inline";
			}
			else
			{
				check_button.style.display = "inline";
				not_allowed.style.display = "none";
				
			}
		}
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

