/*
*********************************************************************
**  File: product.js
**	Original Author:
**	Date:
**	Purpose: javascript functions for products
**
**
*********************************************************************
**	Revision History:
**	MH:8/28/09 moved product page scripts to this to help with page load times
**	MH: 2/26/10 commented out wait message function and function call as it was causing js error in ie
**	MH: 12/28/10 added to get backorder messages for items with no variants
**	MH: 1/18/12 added to show messages after rx select is made and hiding them
**	MH: 1/23/12 modified to help show backordered messages properly with rx messages
*********************************************************************
*/
var myvar, backorder_ajax_Content = new div_loader();
function LoadPage(totalProductCount)
{
	/* MH: 4/15/09 added code for showing select box on load if needed */
	if(document.forms['getCartInfo'].elements['prodRadioGroup'])
	{
		var radioObj = document.forms['getCartInfo'].elements['prodRadioGroup'], radioLength = radioObj.length;
		if(radioLength == undefined)
		{
			if(radioObj.checked)
			{var active_attribute1 = 'firstAttribute_'+1; var active_attribute2 = 'secondAttribute_'+1;
				if(document.getElementById(active_attribute1))
				{document.getElementById(active_attribute1).style.visibility = "visible"; document.getElementById(active_attribute1).style.display = "block";}
				if(document.getElementById(active_attribute2))
				{document.getElementById(active_attribute2).style.visibility = "visible"; document.getElementById(active_attribute2).style.display = "block";};}
		}
		else
		{
			for(var i = 0; i < totalProductCount; i++)
			{
				if(radioObj[i])
				{
					if(radioObj[i].checked)
					{ var x = i + 1;
						var active_attribute1 = 'firstAttribute_'+x; var active_attribute2 = 'secondAttribute_'+x;
						if(document.getElementById(active_attribute1))
						{document.getElementById(active_attribute1).style.visibility = "visible"; document.getElementById(active_attribute1).style.display = "block";}
						if(document.getElementById(active_attribute2))
						{document.getElementById(active_attribute2).style.visibility = "visible"; document.getElementById(active_attribute2).style.display = "block";}
					}
				}
			}
		}
	}
	if(document.getElementById('pharmacyProductSelect') && document.getElementById('pharmacyProductSelect').value != 'NULL')
	{
		var productid = document.getElementById('pharmacyProductSelect').value;
		var productCount = document.getElementById('pharmacyProductSelect').selectedIndex
		var use_product_list = productid.split("+"), use_productid = use_product_list[0], use_productsku = use_product_list[2], use_quantityDiscount = use_product_list[3];
		getBackorderedAjax(use_productsku,productCount,1);
		var quantityDisplay = 'quantityDisplay_'+use_productid;
		if(document.getElementById(quantityDisplay))
		{
			document.getElementById(quantityDisplay).style.visibility = "visible"; document.getElementById(quantityDisplay).style.display = "block";
			if(use_quantityDiscount == 0)
			{var index = document.getElementById('pharmacyProductSelect').selectedIndex; showPharmacyQuantityDiv(1,index);}
		}
	}
	contentObj = document.getElementById('priceCalculation'); var pharmacyPrice = document.getElementById('pharmacyPricingShow'); var pharmacyPriceAll = document.getElementById('pharmacyPricingShowAll');
}
function showProductPricing(productid,productIdList,productCount)
{/*24078+0050730+0050730000000+1+9N+45 24078 1*/
	getAjaxFile('NULL',0,1,0,0,0);
	var productSelectItem = document.getElementById('pharmacyProductSelect').value;
	var productvariableList = productSelectItem.split("+");
	var productPosition = productvariableList[6];
	var pharmacyQuantitySelect = 'pharmacyQuantitySelect_'+productPosition;
	var quantityRadioButton = document.forms['getCartInfo'].elements[pharmacyQuantitySelect]
	if(document.getElementById(pharmacyQuantitySelect)){
	if(quantityRadioButton.value != '1_1_1')
	{
		for(var i=0; i<quantityRadioButton.length; i++)
		{quantityRadioButton[i].checked = false;}
		hideButtons(0);
	}}
	if(productid != 'NULL')
	{
		if(document.getElementById(pharmacyQuantitySelect))
		{
			var use_product_list = productid.split("+"), use_productid = use_product_list[0], use_productsku = use_product_list[2], use_quantityDiscount = use_product_list[3], use_product_hide_list = productIdList.split("+");
			/* MH: 1/23/12 modified to make sure loop goes through all products */
			for(var i=0; i<=use_product_hide_list.length; i++)
			{
				var hide_productid = use_product_hide_list[i], quantityHide = 'quantityDisplay_'+hide_productid;
				if(document.getElementById(quantityHide))
				{document.getElementById(quantityHide).style.visibility = "hidden"; document.getElementById(quantityHide).style.display = "none";}
			/*  MH: 1/18/12 added to hide all messages before showing any that need to be shown after rx select is made */
				var hide_remove_box = 'rxmessagediv_'+i;
				if(document.getElementById(hide_remove_box))
				{
					document.getElementById(hide_remove_box).style.visibility = "hidden"; document.getElementById(hide_remove_box).style.display = "none";
				}
				/* MH: 1/23/12 added to hide all backorder divs before displaying the proper one */
				var hide_backorder_items = 'backordered_'+i;
				if(document.getElementById(hide_backorder_items))
				{
					document.getElementById(hide_backorder_items).style.visibility = "hidden"; document.getElementById(hide_backorder_items).style.display = "none";
				}
			}
			var quantityDisplay = 'quantityDisplay_'+use_productid;
			if(document.getElementById(quantityDisplay))
			{
				document.getElementById(quantityDisplay).style.visibility = "visible"; document.getElementById(quantityDisplay).style.display = "block";
				if(use_quantityDiscount == 0)
				{var index = document.getElementById('pharmacyProductSelect').selectedIndex; showPharmacyQuantityDiv(1,index,productCount,1,0);}
			}
			/*  MH: 1/18/12 added to show messages after rx select is made */
			var use_remove_box = 'rxmessagediv_'+productPosition;
			if(document.getElementById(use_remove_box))
			{
				document.getElementById(use_remove_box).style.visibility = "visible"; document.getElementById(use_remove_box).style.display = "block";
			}
			/* MH: 1/23/12 moved so all divs related are hidden before any are displayed */
			getBackorderedAjax(use_productsku,productPosition,1);
		}
	}
	else
	{
		if(document.getElementById(pharmacyQuantitySelect))
		{
			var use_product_hide_list = productIdList.split("+");
			for(var i=0; i<use_product_hide_list.length; i++)
			{
				var hide_productid = use_product_hide_list[i], quantityHide = 'quantityDisplay_'+hide_productid;
				if(document.getElementById(quantityHide))
				{document.getElementById(quantityHide).style.visibility = "hidden"; document.getElementById(quantityHide).style.display = "none";}
			}
		}
	}
}
var myvar, ajax_Price_Content = new div_loader();
function showPharmacyQuantityDiv(quantityBoxCount,product,totalProductCount,totalBoxCount,resetToOne)
{
	var thisQuantity = 1;
	var pharmacyQuantitySelect = 'pharmacyQuantitySelect_'+product;
	for(var x = 1; x <= totalProductCount; x++)
	{
		for(var i = 1; i <= totalBoxCount; i++)
		{
			var quantityBox = 'quantityDiv_'+i+'_'+x;
			var quantityBoxValue = 'pharmacyQuantityBox_'+i+'_'+x;
			if(resetToOne == 1 && i == quantityBoxCount && x == product){document.getElementById(quantityBoxValue).value = 1;}
			if(document.getElementById(quantityBox))
			{document.getElementById(quantityBox).style.visibility = "hidden"; document.getElementById(quantityBox).style.display = "none";}
			hideButtons(0);
		}
	}
	var activeBox = 'quantityDiv_'+quantityBoxCount+'_'+product, quantity = 'pharmacyQuantityBox_'+quantityBoxCount+'_'+product, isRx = 1, quantityRadioButton = document.forms['getCartInfo'].elements[pharmacyQuantitySelect]
	if(document.getElementById(activeBox)){document.getElementById(activeBox).style.visibility = "visible"; }
	if(document.getElementById(activeBox)){document.getElementById(activeBox).style.display = "block";}
	for(var i=0; i<quantityRadioButton.length; i++)
	{
		if(quantityRadioButton[i].checked == true)
		{var quantitySelect = quantityRadioButton[i].value; if(isNaN(parseFloat(document.getElementById(quantity).value)) || parseFloat(document.getElementById(quantity).value) == 0){var thisQuantity = 1;}else{var thisQuantity = Math.floor(parseFloat(document.getElementById(quantity).value));}var totalQuantity = thisQuantity * parseFloat(quantityRadioButton[i].value);}
	}
	if(totalBoxCount == 1){if(isNaN(parseFloat(document.getElementById(quantity).value)) || parseFloat(document.getElementById(quantity).value) == 0){var thisQuantity = 1;}else{var thisQuantity = parseFloat(document.getElementById(quantity).value);}var totalQuantity = thisQuantity * 1;}
	getAjaxFile(document.getElementById('pharmacyProductSelect').value,totalQuantity,isRx,0,0,thisQuantity,quantitySelect);
	hideButtons(1);
}

/*  <!--- MH: 4/1/09 ---> this section handles backordered ajax.  it displays wether or not an item is on backorder through ajax.*/
function backorder_showContent()
{
	if(backorder_ajax_Content.response == ''){hideButtons(1);}else{hideButtons('x');}
}
/*MH: 2/26/10 commented out wait message function and function call as it was causing js error in ie */
//function backorder_showWaitMessage(productCount){var backorderedItem = 'backordered_'+productCount;document.getElementById(backorderedItem).innerHTML = '<div>&nbsp;</div>';}
function getBackorderedAjax(productsku,productCount,isRx)
{
	if(productCount != 0)
	{
		var oneproductbackorderdiv = 'oneProductBackordered_'+productCount;
		/*MH: 6/14/11 added if statement to not show backordered message for single line items if the single item backorder message is already displayed*/
		if(!document.getElementById(oneproductbackorderdiv) ||(document.getElementById(oneproductbackorderdiv) && document.getElementById(oneproductbackorderdiv).style.visibility == 'hidden'))
		{
			var backorderedItem = 'backordered_' + productCount;
			backorder_ajax_Content.requestFile = '/product/product_ajax.cfm?productsku=' + productsku + '&use_ajax=yes&isRx=' + 0;
			backorder_ajax_Content.runAJAX();
			//if(isRx ==0)
			//{
			//	backorder_ajax_Content.onLoading = backorder_showWaitMessage(productCount);
			//}
			backorder_ajax_Content.elementObj = document.getElementById(backorderedItem);
			backorder_ajax_Content.onCompletion = backorder_showContent;
			/* MH: 1/23/12 added to show proper backordered div */
			document.getElementById(backorderedItem).style.visibility = "visible"; document.getElementById(backorderedItem).style.display = "block";
			
		}
	}
}
/* MH: 4/1/09 this section handles when an attribute is selected */
function attributeSelect(productCount)
{
	var backorderedDivName = 'backordered_'+productCount, backorderedContent = document.getElementById(backorderedDivName), get_selected_attribute1 = 'attribute1_'+productCount, get_selected_attribute2 = 'attribute2_'+productCount, buttonDiv = document.getElementById('addToCartDiv');
	/* MH: 4/1/09 this section gets the selected attributes value and splits it, which is similar to list get at, but it puts it into a javascript array assiging each element to an array element.*/
	if(document.getElementById(get_selected_attribute1))
	{
		if(document.getElementById(get_selected_attribute2))
		{
			var use_product_list2 = document.getElementById(get_selected_attribute2).value.split(","), use_product_list1 = document.getElementById(get_selected_attribute1).value.split(","), attrib1Value = use_product_list1[4], productnum = use_product_list1[3], attrib2Value = use_product_list2[4];
		}
		else
		{
			var use_product_list1 = document.getElementById(get_selected_attribute1).value.split(","), attrib2Value = '000', attrib1Value = use_product_list1[4], productnum = use_product_list1[3];
		}
	}
	else
	{
		if(document.getElementById(get_selected_attribute2))
		{
			var use_product_list2 = document.getElementById(get_selected_attribute2).value.split(","), attrib1Value = '000', attrib2Value = use_product_list2[4], productnum = use_product_list2[3];
		}
	}
	/* MH: 4/1/09 after we have the array values we send them to the backorderd ajax call which uses the information from above to determine wether or not the item is on backorder. */
	backorderedContent.style.visibility = "visible";backorderedContent.style.display = "block";
	var use_sku = productnum+attrib1Value+attrib2Value; getBackorderedAjax(use_sku,productCount,0);
}
/*  MH: 4/1/09 this section handles the changing of the add to cart button based on backordered information */
function hideButtons(hide)
{
	var buttonDiv = document.getElementById('addToCartDiv'), hideAddDiv = document.getElementById('hideAddDiv');
	if(hide == 1)
	{hideAddDiv.style.visibility = "hidden"; hideAddDiv.style.display = "none"; buttonDiv.style.visibility = "visible"; buttonDiv.style.display = "block";}
	else
	{
		if(document.getElementById('use_hideOrderButton') || hide == 0)
		{hideAddDiv.style.visibility = "visible"; hideAddDiv.style.display = "block"; buttonDiv.style.visibility = "hidden"; buttonDiv.style.display = "none";}
		else
		{hideAddDiv.style.visibility = "hidden"; hideAddDiv.style.display = "none"; buttonDiv.style.visibility = "visible"; buttonDiv.style.display = "block";}
	}
}
/*  MH: 4/1/09 this section handles the hiding and showing of the different product attributes/variants.  based on which radio button is clicked on this function handles the hiding and showing of attributes, monogramming, add to cart buttons.  all based on which information is passed over into this function, then this function calls the show variants function */
function hideVariants(productnum,productid,qtydiscount,price,saleprice,monogramcost,productvariantid,productCount,isRx,purchaseable,no_allow,use_catalogprefix,use_catalogid,monogrammable,backorderable,backordered,totalProductCount)
{//alert(productnum+' '+productid+' '+qtydiscount+' '+price+' '+saleprice+' '+monogramcost+' '+productvariantid+' '+productCount+' '+isRx+' '+purchaseable+' '+no_allow+' '+use_catalogprefix+' '+use_catalogid+' '+monogrammable+' '+backorderable+' '+backordered+' '+totalProductCount);
	var radioObj = document.forms['getCartInfo'].elements['prodRadioGroup'], use_backordered = 'backordered_'+productCount;
	/*  MH: 4/1/09 this area hides the attributes */
	for(var i = 0; i < totalProductCount; i++)
	{
			var active_attribute1 = 'firstAttribute_'+i, active_attribute2 = 'secondAttribute_'+i;
			if(document.getElementById(active_attribute1))
			{document.getElementById(active_attribute1).style.visibility = "hidden"; document.getElementById(active_attribute1).style.display = "none";}
			if(document.getElementById(active_attribute2))
			{document.getElementById(active_attribute2).style.visibility = "hidden"; document.getElementById(active_attribute2).style.display = "none";}
			/* MH: 4/15/09 added to set unchecked radio button's related select box to the default value.  */
			var attributeSelect1 = 'attribute1_'+i, attributeSelect2 = 'attribute2_'+i, buttonDiv = document.getElementById('addToCartDiv'), hideAddDiv = document.getElementById('hideAddDiv'), oneProductBackordered=  'oneProductBackordered_'+i;
			//if( i != productCount)
			//{
				if(document.getElementById(attributeSelect1))
				{document.getElementById(attributeSelect1).selectedIndex = 'PASS';}
				if(document.getElementById(attributeSelect2))
				{document.getElementById(attributeSelect2).selectedIndex = 'PASS';}
			//}
	}
	/* MH: 4/3/09 this area hides the backordered */
	for(var i = 1; i < totalProductCount; i++)
	{
		var hide_backorderedDiv = 'backordered_'+i;
		if(document.getElementById(hide_backorderedDiv))
		{document.getElementById(hide_backorderedDiv).style.visibility = "hidden"; document.getElementById(hide_backorderedDiv).style.display = "none";}
	}
	/* MH: 4/3/09 this section hides and shows the add to cart button and monogram box based on what radio button was clicked */
	var buttonDiv = document.getElementById('addToCartDiv'), hideAddDiv = document.getElementById('hideAddDiv');
	if(no_allow == 1)
	{hideAddDiv.style.visibility = "visible"; hideAddDiv.style.display = "block"; buttonDiv.style.visibility = "hidden"; buttonDiv.style.display = "none";}
	else if(no_allow == 0)
	{
		var attributeSelect1 = 'attribute1_'+productCount;
		var attributeSelect2 = 'attribute2_'+productCount;
		if(document.getElementById(attributeSelect1)){
		document.getElementById(attributeSelect1).selectedIndex = 'PASS';}
		if(document.getElementById(attributeSelect2))
		{document.getElementById(attributeSelect2).selectedIndex = 'PASS';}
		if((document.getElementById(attributeSelect1) && document.getElementById(attributeSelect1).value == 'PASS')  || (document.getElementById(attributeSelect2) && document.getElementById(attributeSelect2).value == 'PASS'))
		{hideAddDiv.style.visibility = "visible"; hideAddDiv.style.display = "block"; buttonDiv.style.visibility = "hidden"; buttonDiv.style.display = "none";}
		else
		{hideAddDiv.style.visibility = "hidden"; hideAddDiv.style.display = "none"; buttonDiv.style.visibility = "visible"; buttonDiv.style.display = "block";}
	}
	if(monogrammable == 1)
	{
		if(document.getElementById('personalize'))
		{document.getElementById('personalize').style.visibility = "visible"; document.getElementById('personalize').style.display = "block";}
	}
	else
	{
		if(document.getElementById('personalize'))
		{document.getElementById('personalize').style.visibility = "hidden"; document.getElementById('personalize').style.display = "none";}
	}
	/* MH: 12/28/10 added to get backorder messages for items with no variants */
	get_selected_attribute1 = 'attribute1_'+productCount;
	get_selected_attribute2 = 'attribute2_'+productCount;
	if(!document.getElementById(get_selected_attribute1) && !document.getElementById(get_selected_attribute2))
	{
		var backorderedDivName = 'backordered_'+productCount;
		backorderedContent = document.getElementById(backorderedDivName);
		buttonDiv = document.getElementById('addToCartDiv');
		backorderedContent.style.visibility = "visible";backorderedContent.style.display = "block";
		var use_sku = productnum+'000000'; getBackorderedAjax(use_sku,productCount,0);
	}
	showVariants(productnum,productid,qtydiscount,price,saleprice,monogramcost,productvariantid,productCount,isRx,purchaseable,no_allow,use_catalogprefix,use_catalogid,totalProductCount);
}
function showVariants(productnum,productid,qtydiscount,price,saleprice,monogramcost,productvariantid,productCount,isRx,purchaseable,no_allow,use_catalogprefix,use_catalogid,totalProductCount)
{
	/*attributeSelect(productCount);*/
	/* MH: 4/3/09 this section shows items based on what was clicked */
	var firstAttribute = 'firstAttribute_'+productCount, secondAttribute = 'secondAttribute_'+productCount, backorderedItem = 'backOrdered_'+productCount;
	if(document.getElementById(firstAttribute))
	{document.getElementById(firstAttribute).style.visibility = "visible"; document.getElementById(firstAttribute).style.display = "block";}
	if(document.getElementById(secondAttribute))
	{document.getElementById(secondAttribute).style.visibility = "visible"; document.getElementById(secondAttribute).style.display = "block";}
	if(document.getElementById(backorderedItem))
	{document.getElementById(backorderedItem).style.visibility = "visible"; document.getElementById(backorderedItem).style.display = "block";}
	/* MH: 4/3/09 this calculates quantity if a different radio button was clicked */
	if(document.getElementById('quantity'))
	{
		/* MH: 4/15/09 changed quantity to 0 */
		var fieldQuantity = document.getElementById('quantity').value;
		if(fieldQuantity > 0)
		{getCheckedValue(document.forms['getCartInfo'].elements['prodRadioGroup'],fieldQuantity,isRx,use_catalogprefix,use_catalogid,totalProductCount);}
	}
}
/* MH: 4/3/09 this checked for which radio button is currently checked and sends the info to the ajax call */
function getCheckedValue(radioObj,qty,isRx,use_catalogprefix,use_catalogid,totalProductCount)
{
	if(isNaN(qty)){var qty = 1;}
	var radioLength = radioObj.length;
	if(radioLength == undefined)
	{
		if(radioObj.checked)
		{getAjaxFile(radioObj.value,qty,isRx,use_catalogprefix,use_catalogid);}
	}
	else
	{
		for(var i = 0; i < totalProductCount; i++)
		{
			if(radioObj[i])
			{
				if(radioObj[i].checked)
				{getAjaxFile(radioObj[i].value,qty,isRx,use_catalogprefix,use_catalogid);}
			}
		}
	}
}
/* MH: 4/3/09 this is the ajax call section.  there is showContent which actually displays the ajax result.  ShowWaitMessage, which shows an image, text, or other while the ajax is running, and there is the getAjaxFile, which calls the other files, and sets the url for the ajax file */
var myvar, ajax_Content = new div_loader();
function showWaitMessage1(){var contentObj = document.getElementById('priceCalculation'); contentObj.innerHTML = '<table><tr><td>Price Per Item:&nbsp;</td><td>0</td><tr><td>Subtotal:&nbsp;</td><td>0</td></tr></table>';}
function showWaitMessage(){var contentObj = document.getElementById('priceCalculation'); contentObj.innerHTML = '<div style="font-weight:bold; font-size:12px;">&nbsp;&nbsp;Total Qty:</div><div style="font-weight:bold; font-size:12px;">&nbsp;&nbsp;Price Per Item:</div><div style="font-weight:bold; font-size:12px;">&nbsp;&nbsp;Subtotal:</div>';}
function showWaitMessage2(){var contentObj = document.getElementById('priceCalculation'); contentObj.innerHTML = '';}
function getAjaxFile(productId,qty,isRx,use_catalogprefix,use_catalogid,use_quantity,quantitySelect)
{
	if(isRx ==0)
	{if(isNaN(qty)){var qty = 1;} ajax_Content.requestFile = '/product/product_ajax.cfm?qty='+qty+'&productId='+productId+'&use_ajax=yes&isRx=0&catalogprefix='+use_catalogprefix+'&catalogid='+use_catalogid;if(!isNaN(parseFloat(productId))){ajax_Content.onLoading = showWaitMessage1;}else{ajax_Content.onLoading = showWaitMessage2;}}
	else
	{ajax_Content.requestFile = '/product/product_ajax.cfm?qty='+qty+'&productId='+productId+'&use_ajax=yes&isRx=1&use_quantity='+use_quantity+'&quantitySelect='+quantitySelect;if(!isNaN(parseFloat(productId))){ajax_Content.onLoading = showWaitMessage;}else{ajax_Content.onLoading = showWaitMessage2;}}
	ajax_Content.runAJAX();
	ajax_Content.elementObj = document.getElementById('priceCalculation');
}
