var PostBack;

function HideOrderingElements()
{
	//  This function hides a couple columns when the item need
	CheckIfElementExists = document.getElementById('PriceCell');
	if (CheckIfElementExists)
		document.getElementById('ItemDetail').style.display = '';
	if (CheckIfElementExists && (CheckIfElementExists.innerHTML != "<b><em>RFQ</em>&nbsp;</b>" &&
		CheckIfElementExists.innerHTML != "<B><EM>RFQ</EM> </B>" &&
		CheckIfElementExists.innerHTML != "<B><EM>RFQ</EM>&nbsp;</B>"))
	{
		CheckIfElementExists = document.getElementById('OrderingElements');
			CheckIfElementExists.style.display = '';				
	}
}

function SetItemDetailsPageDivPosition()
{
	//  This function sets the scrolltop value in a cookie when a user scrolls down the div.
	var intY;
	
	CheckIfElementExists = document.getElementById("ItemDataGridDiv");
	if(CheckIfElementExists)
		intY  = CheckIfElementExists.scrollTop;
	CheckIfElementExists = document.getElementById("ParametricGridDiv");
	if(CheckIfElementExists)
		intY  = CheckIfElementExists.scrollTop;
	document.cookie = "yDivPos=!^" + intY + "^!";
}

function ItemDetailsPageAutoScroll()
{
	//  This function reads the scrolltop value from a cookie to scroll to where the user left off.
	var strCook = document.cookie;
	if(strCook.indexOf("!^")!=-1)
	{
		if(PostBack == "FALSE")
		{
			PostBack = "TRUE";
			document.cookie = "yDivPos=!^0^!";
		}
		else
		{		
			var intS = strCook.indexOf("!^");
			var intE = strCook.indexOf("^!");
			var strPos = strCook.substring(intS+2,intE);
			CheckIfElementExists = document.getElementById("ItemDataGridDiv");
			if(CheckIfElementExists)
				CheckIfElementExists.scrollTop = strPos;
			CheckIfElementExists = document.getElementById("ParametricGridDiv");
			if(CheckIfElementExists)
				CheckIfElementExists.scrollTop = strPos;
		}
	}
}

function ShowLargeImage(NewImagePath)
{
	CheckIfElementExists = document.getElementById("ItemImage");
	CheckIfElementExists.src = NewImagePath;
}

function Highlight(ItemNumber)
{	
	//  This function set the row color to yellow in the main grid for the selected item.
	// Reset rows background color
	if(document.getElementById('_ctl15_ParametricGrid'))
	{
		// loop thru table elements
		for(var j = 2; j < document.getElementById('_ctl15_ParametricGrid').rows.length + 2; j++)
		{
			if(document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_OrigColor').innerHTML != '')
			{
					document.getElementById('_ctl15_ParametricGrid').rows[j-2].style.backgroundColor =
						document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_OrigColor').innerHTML;
					document.getElementById('_ctl15_ParametricGrid').rows[j-2].bgColor =
						document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_OrigColor').innerHTML;
			}
		}
		
		// loop thru table elements
		for(var j = 2; j < document.getElementById('_ctl15_ParametricGrid').rows.length + 2; j++)
		{
			if(document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_Item_Number').innerHTML == ItemNumber)
			{
				if(document.getElementById('_ctl15_ParametricGrid').rows[j-2].bgColor == '' && 
					document.getElementById('_ctl15_ParametricGrid').rows[j-2].style.backgroundColor == '')
				{
					document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_OrigColor').innerHTML = '#FFFFFF';
					document.getElementById('_ctl15_ParametricGrid').rows[j-2].bgColor = '#FFFFE0';
					document.getElementById('_ctl15_ParametricGrid').rows[j-2].style.backgroundColor = '#FFFFE0';
				}else if(document.getElementById('_ctl15_ParametricGrid').rows[j-2].style.backgroundColor != '')
				{
					document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_OrigColor').innerHTML = 
						document.getElementById('_ctl15_ParametricGrid').rows[j-2].style.backgroundColor;
					document.getElementById('_ctl15_ParametricGrid').rows[j-2].style.backgroundColor = '#FFFFE0';
				}else
				{
					document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_OrigColor').innerHTML = 
						document.getElementById('_ctl15_ParametricGrid').rows[j-2].bgColor;
					document.getElementById('_ctl15_ParametricGrid').rows[j-2].bgColor = '#FFFFE0';
				}
			}
		}	
	}
	if(document.getElementById('_ctl15_ItemGrid'))
	{
		// loop thru table elements
		for(var j = 2; j < document.getElementById('_ctl15_ItemGrid').rows.length + 1; j++)
		{
			if(document.getElementById('_ctl15_ItemGrid__ctl' + j + '_ItemOrigColor').innerHTML != '')
			{
					document.getElementById('_ctl15_ItemGrid').rows[j-1].style.backgroundColor =
						document.getElementById('_ctl15_ItemGrid__ctl' + j + '_ItemOrigColor').innerHTML;
					document.getElementById('_ctl15_ItemGrid').rows[j-1].bgColor = 
						document.getElementById('_ctl15_ItemGrid__ctl' + j + '_ItemOrigColor').innerHTML;
			}
		}
		
		// loop thru table elements
		for(var j = 2; j < document.getElementById('_ctl15_ItemGrid').rows.length + 1; j++)
		{
			if(document.getElementById('_ctl15_ItemGrid__ctl' + j + '_Item_number').innerHTML == ItemNumber)
			{
				if(document.getElementById('_ctl15_ItemGrid').rows[j-1].bgColor == '' && 
					document.getElementById('_ctl15_ItemGrid').rows[j-1].style.backgroundColor == '')
				{
					document.getElementById('_ctl15_ItemGrid__ctl' + j + '_ItemOrigColor').innerHTML = '#FFFFFF';
					document.getElementById('_ctl15_ItemGrid').rows[j-1].bgColor = '#FFFFE0';
					document.getElementById('_ctl15_ItemGrid').rows[j-1].style.backgroundColor = '#FFFFE0';
				}else if(document.getElementById('_ctl15_ItemGrid').rows[j-1].style.backgroundColor != '')
				{
					document.getElementById('_ctl15_ItemGrid__ctl' + j + '_ItemOrigColor').innerHTML = 
						document.getElementById('_ctl15_ItemGrid').rows[j-1].style.backgroundColor;
					document.getElementById('_ctl15_ItemGrid').rows[j-1].style.backgroundColor = '#FFFFE0';
				}else
				{
					document.getElementById('_ctl15_ItemGrid__ctl' + j + '_ItemOrigColor').innerHTML = 
						document.getElementById('_ctl15_ItemGrid').rows[j-1].bgColor;
					document.getElementById('_ctl15_ItemGrid').rows[j-1].bgColor = '#FFFFE0';
				}
			}
		}			
	}
}

// AJAX Section
function MainGridAddToCart_Clicked(ItemIndex)
{
	/*  This function handles the logic for adding items to the cart from the main grid.
		It alows for multiple items added simeoultaneously by putting quantities in the quantity boxes.  */

	//  Reset Success and Error Labels
	if(document.getElementById('_ctl15_ItemGrid'))
	{
		document.getElementById('ItemDataGridAddedLabel').style.display = 'none';
		document.getElementById('ItemDataGridErrorLabel').style.display = 'none';
	}else
	{
		document.getElementById('ItemDataGridAddedLabelParm').style.display = 'none';
		document.getElementById('ItemDataGridErrorLabelParm').style.display = 'none';
	}

	ItemIndex = ItemIndex + 2;
	StringBeingSent = '', ItemCount = 0;
	// loop thru table elements and look for other quantities that were entered
	if(document.getElementById('_ctl15_ParametricGrid'))
	{
		for(var j = 2; j < document.getElementById('_ctl15_ParametricGrid').rows.length + 2; j++)
		{
			document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_QuantityBoxParm').bgColor = '';
			document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_QuantityBoxParm').style.backgroundColor = '';
			if(document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_QuantityBoxParm').value != '' ||
			   j == ItemIndex)
			{
				if(Regex('QuantityBoxParm', document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_QuantityBoxParm').value, AllNumericRex))
				{
					//  Capture the itemnumber and the quantity to send to the dll
					StringBeingSent += document.getElementById('_ctl15_ParametricGrid__ctl'+j+'_Item_Number').innerHTML +
									':' + document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_QuantityBoxParm').value +
									':';
					ItemCount++;
				}else
				{
					//  There was a bad quantity.  Show error message and Hightlight the quantity box red.
					document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_QuantityBoxParm').bgColor = '#ff0000';
					document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_QuantityBoxParm').style.backgroundColor = '#ff0000';
					document.getElementById('ItemDataGridErrorLabelParm').style.display = '';
				}
			}
		}
		if(ItemCount > 1)
			document.getElementById('ItemDataGridAddedLabelParm').innerHTML = ItemCount + " Items Added to cart. <a href='../public/cart.aspx' style='FONT-SIZE: 10px'>View Cart</a>";
		else if(ItemCount == 1)
			document.getElementById('ItemDataGridAddedLabelParm').innerHTML = ItemCount + " Item Added to cart. <a href='../public/cart.aspx' style='FONT-SIZE: 10px'>View Cart</a>";		
		if(ItemCount >= 1)
			IMS_Web_Gold.Public.ItemDetailsPage.ItemDetailDataList_AddToCart(StringBeingSent, MainGridAddToCart_Clicked_CallBack);
	}
	if(document.getElementById('_ctl15_ItemGrid'))
	{
		for(var j = 2; j < document.getElementById('_ctl15_ItemGrid').rows.length + 1; j++)
		{
			document.getElementById('_ctl15_ItemGrid__ctl' + j + '_QuantityBoxParm').bgColor = '';
			document.getElementById('_ctl15_ItemGrid__ctl' + j + '_QuantityBoxParm').style.backgroundColor = '';
			if(document.getElementById('_ctl15_ItemGrid__ctl' + j + '_QuantityBoxParm').value != '' ||
			   j == ItemIndex)
			{
				if(Regex('QuantityBoxParm', document.getElementById('_ctl15_ItemGrid__ctl' + j + '_QuantityBoxParm').value, AllNumericRex))
				{
					//  Capture the itemnumber and the quantity to send to the dll
					StringBeingSent += document.getElementById('_ctl15_ItemGrid__ctl'+j+'_Item_number').innerHTML +
									':' + document.getElementById('_ctl15_ItemGrid__ctl' + j + '_QuantityBoxParm').value +
									':';
					ItemCount++;
				}else
				{
					//  There was a bad quantity.  Show error message and Hightlight the quantity box red.
					document.getElementById('_ctl15_ItemGrid__ctl' + j + '_QuantityBoxParm').bgColor = '#ff0000';
					document.getElementById('_ctl15_ItemGrid__ctl' + j + '_QuantityBoxParm').style.backgroundColor = '#ff0000';
					document.getElementById('ItemDataGridErrorLabel').style.display = '';
				}
			}
		}
		
		//  Show the number of items added to cart and view cart link.
		if(ItemCount > 1)
			document.getElementById('ItemDataGridAddedLabel').innerHTML = ItemCount + " Items Added to cart. <a href='../public/cart.aspx' style='FONT-SIZE: 10px'>View Cart</a>";
		else if(ItemCount == 1)
			document.getElementById('ItemDataGridAddedLabel').innerHTML = ItemCount + " Item Added to cart. <a href='../public/cart.aspx' style='FONT-SIZE: 10px'>View Cart</a>";		
		if(ItemCount >= 1)
			IMS_Web_Gold.Public.ItemDetailsPage.ItemDetailDataList_AddToCart(StringBeingSent, MainGridAddToCart_Clicked_CallBack);
	}	
}

function MainGridAddToCart_Clicked_CallBack(response)
{
	if(response.error != null)
	{
		//alert(response.error);
		return;
	}else
	{		
		if(document.getElementById('_ctl15_ParametricGrid'))
		{
			document.getElementById('ItemDataGridAddedLabelParm').style.display = '';
			for(var j = 2; j < document.getElementById('_ctl15_ParametricGrid').rows.length + 2; j++)
			{
				//  Clear text boxes except those with bad quantities.
				if(document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_QuantityBoxParm').bgColor != '#ff0000')
					document.getElementById('_ctl15_ParametricGrid__ctl' + j + '_QuantityBoxParm').value = '';
			}
			//  Update the View Cart link on the header.
			document.getElementById('IMS_Header1_CartLabel').innerHTML = '  View Cart ('+ 
				response.value + ')';
				document.getElementById('IMS_Header1_CartLabel').style.color = '#000000';
				document.getElementById('IMS_Header1_CartLabel').style.fontWeight = 'bold';
		}else
		{
			document.getElementById('ItemDataGridAddedLabel').style.display = '';		
			for(var j = 2; j < document.getElementById('_ctl15_ItemGrid').rows.length + 1; j++)
			{
				//  Clear text boxes except those with bad quantities.
				if(document.getElementById('_ctl15_ItemGrid__ctl' + j + '_QuantityBoxParm').bgColor != '#ff0000')
					document.getElementById('_ctl15_ItemGrid__ctl' + j + '_QuantityBoxParm').value = '';
			}
			//  Update the View Cart link on the header.
			document.getElementById('IMS_Header1_CartLabel').innerHTML = '  View Cart ('+ 
				response.value + ')';
				document.getElementById('IMS_Header1_CartLabel').style.color = '#000000';
				document.getElementById('IMS_Header1_CartLabel').style.fontWeight = 'bold';
		}
	}
}

function AddToCart_Clicked()
{
	if(Regex('QuantityTextBox', document.getElementById('QuantityTextBox').value, AllNumericRex))
	{
		//  Add to cart clicked on the detail data list.
		StringBeingSent = document.getElementById('DataListItemNumber').innerHTML + ':' + document.getElementById('QuantityTextBox').value;	
		IMS_Web_Gold.Public.ItemDetailsPage.ItemDetailDataList_AddToCart(StringBeingSent, AddToCart_Clicked_CallBack);
		document.getElementById('QuantityTextBox').bgColor = '#ffffff';
		document.getElementById('QuantityTextBox').style.backgroundColor = '#ffffff';		
	}
	else
	{
		//  There was a bad quantity.  Show error message and Hightlight the quantity box red.
		document.getElementById('QuantityTextBox').bgColor = '#ff0000';
		document.getElementById('QuantityTextBox').style.backgroundColor = '#ff0000';
		document.getElementById('QuantityErrorLabel').style.display = '';
	}
}

function AddToCart_Clicked_CallBack(response)
{
	document.getElementById('SuccessfullyAddedLabel').style.display = 'none';
	document.getElementById('QuantityErrorLabel').style.display = 'none';
	document.getElementById('QuantityTextBox').value = '';	
	
	if(response.error != null)
	{
		//alert(response.error);
		return;
	}else
	{
		// Report if add to cart was successful or not
		if(response.value == 'BadQuantity')
		{
			document.getElementById('QuantityErrorLabel').style.display = '';			
		}else
		{
			//  Update the View Cart link on the header.
			document.getElementById('SuccessfullyAddedLabel').style.display = '';
			document.getElementById('IMS_Header1_CartLabel').innerHTML = ' View Cart ('+ 
				response.value + ')';
				document.getElementById('IMS_Header1_CartLabel').style.color = '#000000';
				document.getElementById('IMS_Header1_CartLabel').style.fontWeight = 'bold';
		}
	}
}

function ParametricGrid_ItemNumberClicked(ItemNumber)
{
	//  Clicked main grid item to show item details
	IMS_Web_Gold.Public.ItemDetailsPage.ParametricGrid_ItemNumberClicked("'" + ItemNumber + "'", ParametricGrid_ItemNumberClicked_Clicked_CallBack);
	if(ItemNumber.indexOf('WEB') == -1)
		setTimeout('Highlight(' + ItemNumber + ')',700);
}

function ItemDetailGrid_ItemNumberClicked(ItemNumber)
{
	//  Clicked main grid item to show item details
	IMS_Web_Gold.Public.ItemDetailsPage.ItemDetailGrid_ItemNumberClicked("'" + ItemNumber + "'", ItemDetailGrid_ItemNumberClicked_Clicked_CallBack);
	if(ItemNumber.indexOf('WEB') == -1)
		setTimeout('Highlight(' + ItemNumber + ')',700);
}

function ItemDetailGrid_ItemNumberClicked_Clicked_CallBack(response)
{
	if(response.error != null)
	{
		alert(response.error.Type);
		return;
	}else
	{
		//  Set the item detail div with the data list's returned html.
		document.getElementById("ItemDetail").innerHTML = response.value;
		HideOrderingElements();
		document.getElementById("ItemDetail").style.display = '';
	}
}

function ParametricGrid_ItemNumberClicked_Clicked_CallBack(response)
{
	if(response.error != null)
	{
		alert(response.error.Type);
		return;
	}else
	{
		//  Set the item detail div with the data list's returned html.
		document.getElementById("ItemDetail").innerHTML = response.value;
		HideOrderingElements();
		document.getElementById("ItemDetail").style.display = '';
	}
}

function DisplayNavTitle()
{
	if(document.getElementById('_ctl15_ItemGrid__ctl2_NAV_TITLE') && 
		document.getElementById('BreadTrail').innerHTML == '')
		BreadTrail.innerHTML = 'Product Category : <b>' + document.getElementById('_ctl15_ItemGrid__ctl2_NAV_TITLE').innerHTML + '</b>';
	if(document.getElementById('_ctl15_ParametricGrid__ctl2_NAV_TITLE') && 
		document.getElementById('BreadTrail').innerHTML == '')
		BreadTrail.innerHTML = 'Product Category : <b>' + document.getElementById('_ctl15_ParametricGrid__ctl2_NAV_TITLE').innerHTML + '</b>';
}

function FormatHeaders()
{
	if(document.getElementById('_ctl15_ParametricGrid'))
	{
		var i = 0;
		for(var nIndex = 0; nIndex < document.getElementById('_ctl15_ParametricGrid').rows.length; nIndex++)
		{
			for(i=0; i< document.getElementById('_ctl15_ParametricGrid').rows[nIndex].cells.length; i++)
			{
				if(nIndex < 2 && i < 14)
				{
					if(!document.getElementById('ParametricHeaderGrid').rows[0].cells[i])
						document.getElementById('ParametricHeaderGrid').rows[0].insertCell(document.getElementById('ParametricHeaderGrid').rows[0].cells.length);
					if(document.getElementById('_ctl15_ParametricGrid').rows[nIndex].cells[i].offsetWidth > 0)
						document.getElementById('ParametricHeaderGrid').rows[0].cells[i].width = document.getElementById('_ctl15_ParametricGrid').rows[nIndex].cells[i].offsetWidth;
					document.getElementById('ParametricHeaderGrid').rows[0].cells[i].style.align = document.getElementById('_ctl15_ParametricGrid').rows[nIndex].cells[i].style.align;
					if(document.getElementById('_ctl15_ParametricGrid').rows[nIndex].cells[i].offsetWidth > 0)
						document.getElementById('ParametricHeaderGrid').rows[0].cells[i].style.width = document.getElementById('_ctl15_ParametricGrid').rows[nIndex].cells[i].offsetWidth;
					//document.getElementById('ParametricHeaderGrid').rows[0].cells[i].style.border = 'ridge 1px #FF0000';
					//document.getElementById('_ctl15_ParametricGrid').rows[nIndex].cells[i].style.border = 'ridge 1px #FF0000';
				}			
			}
			if(i == 13 && document.getElementById('_ctl15_ParametricGrid').rows[nIndex].cells[13] &&
				document.getElementById('_ctl15_ParametricGrid').rows[nIndex].cells[13].innerHTML != '&nbsp;')
			{
				document.getElementById('ParametricHeaderGrid').rows[0].cells[13].innerHTML = 'OOOOOO';
				document.getElementById('ParametricHeaderGrid').rows[0].cells[13].style.visibility = 'hidden';
			}
		}

		document.getElementById('ParametricHeaderGrid').style.width = '98.25%';
		document.getElementById('ParametricHeaderGrid').style.display = '';
	}
}