/* 
*********************************************************************
**  File: build_your_own_kit.js
**	Original Author: 
**	Date: 
**	Purpose: javascript functions for build_your_own_kit 
**  
**
*********************************************************************
**	Revision History:
**	Who		Date		Description
** 
*********************************************************************
*/
var ajax_Price = new div_loader();
/* MH: 11/20/09 this function sets the popup window */
var easyBuyPopUp = function(pcatid,scientificid,chartlink,image,imageAlt) 
{
	var totalLink = 'build_your_own_kit_more_information.cfm?pcatid='+pcatid+'&scientificid='+scientificid+'&chartlink='+chartlink+'&image='+image+'&alt='+imageAlt;
	var windowName = 'build_your_own_kit',
	title = '',
	config = new Object();
	config.height = 475;
	config.width = 475;
	config.modal = false;
	config.resizable = false;
	config.center = true;
	config.callbackHandler=initTabs_v2;
	ColdFusion.Window.create(windowName,title,totalLink,config);
	ColdFusion.Window.onHide(windowName,cleanup);
}
/* MH: 11/20/09 destroys popup window*/
var cleanup = function() 
{
	ColdFusion.Window.destroy('build_your_own_kit',true);
}
/* MH: 11/20/09 initializes the popup tabs*/
function popUpWindow(){}
var initTabs_v2 = function() 
{
selectArticle(2);
}
/* MH: 11/20/09 changes the selected tab after using the wtm_clear to reset all tabs */
function selectArticle(i)
{
	wtm_clear();
	if(document.getElementById('content_inc_'+i)){document.getElementById('content_inc_'+i).style.display='block'; document.getElementById('content_inc_'+i).style.visibility='visible';}
	if(document.getElementById('content_inc_'+i+'a')){document.getElementById('content_inc_'+i+'a').style.display='block'; document.getElementById('content_inc_'+i+'a').style.visibility='visible';}
	if(document.getElementById('tab_inc_'+i)){document.getElementById('tab_inc_'+i).style.display='none'; document.getElementById('tab_inc_'+i).style.visibility='hidden';}
	if(document.getElementById('no_tab_inc_'+i)){document.getElementById('no_tab_inc_'+i).style.display='block'; document.getElementById('no_tab_inc_'+i).style.visibility='visible';}
	if(document.getElementById('mainContainer')){document.getElementById('mainContainer').style.height='375px';}
}
function wtm_clear()
{
	if(document.getElementById('mainContainer')){document.getElementById('mainContainer').style.height='375px';}
	for(var i=1; i<4; i++){
		if(document.getElementById('content_inc_'+i)){document.getElementById('content_inc_'+i).style.display='none'; document.getElementById('content_inc_'+i).style.visibility='hidden';}
		if(document.getElementById('content_inc_'+i+'a')){document.getElementById('content_inc_'+i+'a').style.display='none'; document.getElementById('content_inc_'+i+'a').style.visibility='hidden'; }
		if(document.getElementById('content_inc_'+i+'b')){document.getElementById('content_inc_'+i+'b').style.display='none'; document.getElementById('content_inc_'+i+'b').style.visibility='hidden';}
		if(document.getElementById('no_tab_inc_'+i)){document.getElementById('no_tab_inc_'+i).style.display='none'; document.getElementById('no_tab_inc_'+i).style.visibility='hidden';}
		if(document.getElementById('tab_inc_'+i)){document.getElementById('tab_inc_'+i).style.display='block'; document.getElementById('tab_inc_'+i).style.visibility='visible';}
	}
}
var myvar, ajax_Price_Content = new div_loader(); 
/* MH: 11/20/09 displays the price information section for items with more than one product associated */
function showPricingDiv(productInfo,totalcount)
{
	var use_product = productInfo.split("+"), use_productnum = use_product[0], use_count = use_product[1];
	var showPricing = new div_loader();
	if(use_productnum != 0)
	{
		var use_product_list = productInfo.split("+"), use_productnum = use_product_list[0], use_pcatid = use_product_list[1], 
			use_count = use_product_list[2], use_price = use_product_list[3], use_sale_price = use_product_list[4], use_color = use_product_list[5], use_prefix = use_product_list[6],
			use_show_stock = use_product_list[7];
		var showStockStatus = 'showStock_'+use_count;
		var showNoStockStatus = 'showNoStock_'+use_count;
		var quantity_reset = 'Quantity_'+use_count;
		var itemprice_reset = 'itemPrice_'+use_count;
		var totalItemprice_reset = 'totalItemPrice_'+use_count;
		if(document.getElementById(quantity_reset)){document.getElementById(quantity_reset).value = 0;}
		if(document.getElementById(itemprice_reset)){document.getElementById(itemprice_reset).value = 0;}
		if(document.getElementById(totalItemprice_reset)){document.getElementById(totalItemprice_reset).innerHTML = '&nbsp;<strong>=($0.00)</strong>';}
		showTotalPrice(use_price,totalcount,use_count);
		if(use_show_stock == 1)
		{
			if(document.getElementById(showStockStatus)){document.getElementById(showStockStatus).style.visibility = "visible"; }
			if(document.getElementById(showStockStatus)){document.getElementById(showStockStatus).style.display = "block";}
			if(document.getElementById(showNoStockStatus)){document.getElementById(showNoStockStatus).style.visibility = "hidden"; }
			if(document.getElementById(showNoStockStatus)){document.getElementById(showNoStockStatus).style.display = "none";}
		}
		else
		{
			if(document.getElementById(showStockStatus)){document.getElementById(showStockStatus).style.visibility = "hidden"; }
			if(document.getElementById(showStockStatus)){document.getElementById(showStockStatus).style.display = "none";}
			if(document.getElementById(showNoStockStatus)){document.getElementById(showNoStockStatus).style.visibility = "visible"; }
			if(document.getElementById(showNoStockStatus)){document.getElementById(showNoStockStatus).style.display = "block";}
		}
		var showproductpricing = 'show_Pricing_'+use_count;
		showPricing.requestFile = '/product/build_your_own_kit_ajax.cfm?productnum='+use_productnum+'&use_ajax=yes&pcatid='+use_pcatid+'&count='+use_count+'&price='+use_price+'&sale_price='+use_sale_price+'&color='+use_color+'&show_stock='+use_show_stock+'&prefix='+use_prefix;
		showPricing.runAJAX();
		var showqtydiv = 'qtyDisplay_'+use_count;
		if(document.getElementById(showqtydiv)){document.getElementById(showqtydiv).style.visibility = "visible"; }
		if(document.getElementById(showqtydiv)){document.getElementById(showqtydiv).style.display = "block";}
		showPricing.elementObj = document.getElementById(showproductpricing);
		//showPricing.onCompletion = backorder_showContent;
	}
	else
	{
		var showqtydiv = 'qtyDisplay_'+use_count;
		if(document.getElementById(showqtydiv)){document.getElementById(showqtydiv).style.visibility = "hidden"; }
		if(document.getElementById(showqtydiv)){document.getElementById(showqtydiv).style.display = "none";}
		var quantity_reset = 'Quantity_'+use_count;
		var itemprice_reset = 'itemPrice_'+use_count;
		var totalItemprice_reset = 'totalItemPrice_'+use_count;
		if(document.getElementById(quantity_reset)){document.getElementById(quantity_reset).value = 0;}
		if(document.getElementById(itemprice_reset)){document.getElementById(itemprice_reset).value = 0;}
		if(document.getElementById(totalItemprice_reset)){document.getElementById(totalItemprice_reset).innerHTML = '&nbsp;<strong>=($0.00)</strong>';}
		showTotalPrice(use_price,totalcount,use_count);
	}
}
/* MH: 11/20/09 calculates item price */
function getItemPrice(quantity,productnum,prefix,count,totalCount,price,sale_price)
{
	if(isNaN(quantity) || quantity == '')
		{
			var quantityValue = 0;
		}
		else
		{
			var quantityValue = quantity;
		}
		var pricefunc = new build_your_own_kit_function();
		var price = pricefunc.getPrice(productnum,prefix,quantityValue,price,sale_price);
		var quantityPrice = 'totalItemPrice_'+count;
		if(document.getElementById(quantityPrice))
		{
			document.getElementById(quantityPrice).innerHTML = '&nbsp;<strong>=('+price+')</strong>';
			return price;
		}
}
/* MH: 11/20/09 calculates total price for all items on the page*/
function showTotalPrice(price,totalCount,count)
{
	var totalPrice = 0;
	var itemPrice = 'itemPrice_'+count;
	//document.getElementById(itemPrice).onchange();
	for (i=1;i<=totalCount;i++)
	{
		var currentPrice = 'itemPrice_'+i;
		if(i == count)
		{
			var itemPrice = 'itemPrice_'+count;
			if(!document.getElementById(itemPrice))
			{
				var price = 0;
				var	productPrice = price.toString().replace(/\$|\,/g,'');
				if(!(isNaN(parseFloat(productPrice)))){}else{productPrice = 0;}
				var totalPrice = parseFloat(productPrice) + parseFloat(totalPrice);	
			}
			else
			{
				var price = document.getElementById(itemPrice).value;
				var	productPrice = price.toString().replace(/\$|\,/g,'');
				if(!(isNaN(parseFloat(productPrice)))){}else{productPrice = 0;}
				var totalPrice = parseFloat(productPrice) + parseFloat(totalPrice);	
			}
		}
		else
		{
			 if(document.getElementById(currentPrice))
			 {
		 		if(document.getElementById(currentPrice).value != 0)
			 	{
			 		var price = document.getElementById(currentPrice).value;
					var	productPrice = price.toString().replace(/\$|\,/g,'');
					if(!(isNaN(parseFloat(productPrice)))){}else{productPrice = 0;}
					var totalPrice = parseFloat(productPrice) + parseFloat(totalPrice);	
			 	}	
			 }
		}
	}
	var use_innerHtml = '$'+totalPrice.toFixed(2);
	if(document.getElementById('subTotalPrice_1')){document.getElementById('subTotalPrice_1').innerHTML = use_innerHtml;}
	if(document.getElementById('subTotalPrice_2')){document.getElementById('subTotalPrice_2').innerHTML = use_innerHtml;}
	return use_innerHtml;
}	
/* MH: 11/20/09 checks to see if the minimum price has been met either in the shopping basket or on the page or both.  if not it pops up and displays that the minimum was not met. */
function checkPrice(form,use_minprice)
{
	var totalPrice = document.getElementById('subTotalPrice_1').innerHTML;
	//confirm(totalPrice,2,2);
	var basketTotal = 0;
	var price_basket_total = 0;
	var	use_totalPrice = totalPrice.toString().replace(/\$|\,/g,'');
	if(use_minprice != '' && use_minprice > 0)
	{
		if(document.getElementById("totalInBasket"))
		{
			var basketTotal = document.getElementById("totalInBasket").value;
			var price_basket_total = basketTotal + use_totalPrice;
		}
		if((Math.min(use_minprice,use_totalPrice) == use_totalPrice)&& (Math.min(use_minprice,price_basket_total) == price_basket_total))
		{
			var difference = use_minprice - use_totalPrice;
			var	use_minprice = parseFloat(use_minprice);
			var	difference = parseFloat(difference);
			var show_difference = '$'+difference.toFixed(2);
			var minprice = '$'+use_minprice.toFixed(2);
			var answer = alert(minprice+' minimum total required.  You still need '+show_difference+' to purchase this kit.');
			//if(answer){form.submit();}else{window.close();}
		}
		else
		{
			form.submit();
		}
	}
	else
	{
		form.submit();
	}
}
/* MH: 11/20/09 preselects options on the page on refresh or return to the page */
function preselect(productid)
{
	var use_product = productid;
	for(x=0;x<use_product.length;x++)
	{
		if(use_product.selectedIndex != 0)
		{
			if(use_product.options[x].selected == true)
			{
				//alert(use_product.value);
				showPricingDiv(use_product.value);
				break;
			}
		}
	}
}
/* MH: 11/20/09 reloads the prices on the page on refresh */
function showOnLoadPrice(totalCount)
{
	for(x=0;x<totalCount;x++)
	{
		showTotalPrice(0,totalCount,x)	
	}
}
var getVariants1_v2 = function(id,i,type) {
	if(type == 1)
	{
		var itemDiv = 'itemNumber_'+i;
		document.getElementById(itemDiv).style.visibility = "hidden";
		document.getElementById(itemDiv).style.display = "none";
	}
	var p = new prodClass(),
		result = p.getAttributes(id,1),
		attrib1count = result.length;
	var attrib1div = 'attrib1div_'+i;
	if ((attrib1count > 0) && (result[0][0] != 0)) {
		document.getElementById(attrib1div).style.display = 'block';
		document.getElementById(attrib1div).style.visibility = 'visible';
		}
	else {
		document.getElementById('attrib1div_'+i).style.display = 'none';
		document.getElementById('attrib1div_'+i).style.visibility = 'hidden';
		}
	return result; 
}
var getVariants2_v2 = function(id,i) 
{
	var ProductID = 'ProductID_'+i;
	var productid=document.getElementById(ProductID).value;
	var p = new prodClass(),
		result = p.getAttributes(productid,2),

		attrib2count = result.length;
	if ((attrib2count > 0) && (result[0][0] != 0)) 
	{
		var attrib2div = 'attrib2div_'+i;
		document.getElementById(attrib2div).style.display = 'block';
		document.getElementById(attrib2div).style.visibility = 'visible';
		// document.getElementById('attrib2name').innerHTML = 'Select ' + p.getAttributeName(id,2);
	}
	else 
	{
		var attrib2div = 'attrib2div_'+i;
		document.getElementById(attrib2div).style.display = 'none';
		document.getElementById(attrib2div).style.visibility = 'hidden';
	}
	return result; 
}