/*
	Kit.js
	Javascript used for kits (mix & match)
*/

var non_specific_selections = new Array();

/* -----[ BEGIN: module initialization ]------------------------------------- */
var com;
if (!com) com = {};
else if (typeof com != "object")
    throw new Error("com already exists and is not an object");
	
if (!com.bec) com.bec = {}
else if (typeof com.bec != "object")
    throw new Error("com.bec already exists and is not an object");
	
if (!com.bec.product) com.bec.product = {}
else if (typeof com.bec.product != "object")
    throw new Error("com.bec.product already exists and is not an object");
	
if (com.bec.product.Kit)
    throw new Error("com.bec.product.Kit already exists");
/* -----[ END: module initialization ]--------------------------------------- */

com.bec.product.Kit = function() {};

com.bec.product.Kit.itemPreview = new Object;
com.bec.product.Kit.itemPreviewText = new Object;

com.bec.product.Kit.max_allowed_items = new Object;
com.bec.product.Kit.number_of_sku_filled=new Object;
com.bec.product.Kit.maxlen = 30;

var previousQty = 1; 
var totalPrice = 0.0;

var monthAbbrevList = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var monthNameList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

/*  object for non-specific club selection items  */
function selectionItem(id, description, shortDescription, startDate, endDate, price, productImage)
{
    this.id = id;
	this.description = description;
	this.shortDescription = shortDescription;
	this.startDate = startDate;
	this.endDate = endDate;
	this.price = price;	
	this.productImage = productImage;
	
}

/**
 * return the display month name for the month selection dropdown
 */
com.bec.product.Kit.getMonthText = function(yearMonth)
{
	var monthIndx =	parseInt(yearMonth.substr(4));
	return monthNameList[monthIndx];

}

com.bec.product.Kit.addSelectItem = function(selectionObject)
{
	non_specific_selections[non_specific_selections.length] = selectionObject;

}

/**
 * get/create item selection list by date
 */
com.bec.product.Kit.itemListByDate = function(elemId, yearMonthElemId)
{
	var selectMonthElem = document.getElementById(yearMonthElemId);  // sku select list
	if (selectMonthElem.selectedIndex < 0)
		return;
	var selectYearMonth = selectMonthElem.options[selectMonthElem.selectedIndex].value;

	var selectListDiv = document.getElementById(elemId);  // sku select list
	dojo.dom.removeChildren(selectListDiv);

	var availIdx = 0;
	var previewFirstItem = null;

//	com.bec.product.Kit.itemPreview = new Object;
//	com.bec.product.Kit.itemPreviewText = new Object;

	for (var i = 0; i < non_specific_selections.length; i ++)
	{
		var component = non_specific_selections[i];

		if ((selectYearMonth >= component.startDate) && (selectYearMonth <= component.endDate))
		{
			var option = document.createElement('DIV');
			option.id = "KitSku_" + availIdx;
			dojo.html.setClass(option, "option");
			
			var monthIdx = selectYearMonth.substr(4);
			var selectDisplayMonth = monthAbbrevList[parseFloat(monthIdx)-1];   // get abbreviated month name			
			
			option.innerHTML = selectDisplayMonth + "-&nbsp;" + component.description + "&nbsp;&nbsp;" + component.price;

			option.onmouseover = new Function("com.bec.product.Kit.preview('preview'," + component.id + ", " + availIdx + ");\n return false;");
			option.onclick = new Function("com.bec.product.Kit.addMe('Kit_skus','Kit_basket', " + component.id + ",'Kit', '" + component.shortDescription + "', '" + component.price + "');\n return false;");
				
			selectListDiv.appendChild(option);
			
			com.bec.product.Kit.itemPreview[component.id] = component.productImage;
			com.bec.product.Kit.itemPreviewText[component.id] = component.shortDescription;
			
			if (previewFirstItem == null)
				previewFirstItem = component.id;
				
			availIdx ++;
		}
	}

	com.bec.product.Kit.preview('preview', previewFirstItem);


// add 'option'
/*
	<div class="option" id="KitSku_${num}" 
			onmouseover="com.bec.product.Kit.preview('preview', ${kitComp.catalogEntryIdChild}, '${kitLoop.index}');" 
			onclick="com.bec.product.Kit.addMe('Kit_skus','Kit_basket', '${kitComp.catalogEntryIdChild}','Kit','<c:out value="${shortDescriptionJS}" escapeXml="false"/>', '${compPrice}')"
	>
	
		<c:out value="${shortDescription}" escapeXml="false"/>
		&nbsp;@&nbsp;
		${compPrice}					
	</div>
				
	<script type="text/javascript">
		com.bec.product.Kit.itemPreview[${kitComp.catalogEntryIdChild}]="<bec:productImage fileName="${kitCompDB.description.thumbNail}" defaultTemplate="$mix$" alt="${shortDescriptionJS}" />";
		com.bec.product.Kit.itemPreviewText[${kitComp.catalogEntryIdChild}]="<c:out value="${shortDescription}" escapeXml="false"/>";
	</script>					
	
and set preview to first: 	
		if first
			<c:set var="previewFirstItem" value="${kitComp.catalogEntryIdChild}"/>

		com.bec.product.Kit.preview('preview', '${previewFirstItem}');

*/


}

/**
 * dynamically add a new selection option if less than maximum
 */
com.bec.product.Kit.createSelectionOption = function(index)
{
	var option = document.createElement('DIV');
	option.id = "Kit_basket_" + index;
	option.className = "option";

	var compId = document.createElement('input');
	compId.id = "Kit_basket_input_" + index;
	compId.name = "componentId";
	compId.type = "hidden";
	option.appendChild(compId);	
	
	var compDate = document.createElement('input');
	compDate.id = "Kit_basket_date_" + index;
	compDate.name = "componentDate";
	compDate.type = "hidden";
	option.appendChild(compDate);		
	
	var compPrice = document.createElement('input');
	compPrice.id = "Kit_basket_price_" + index;
	compPrice.name = "componentPrice";
	compPrice.type = "hidden";
	option.appendChild(compPrice);		

	var boxTrash = document.createElement('div');
	boxTrash.id = "trashcan";
	boxTrash.className = "bin_off";
	boxTrash.onclick = function() {com.bec.product.Kit.removeMe('Kit_skus', option, 'Kit_basket'); return false;};
	
		var boxTrashImg = document.createElement('img');	
		boxTrashImg.src = '/images/spacer.gif';
		boxTrashImg.width = '1';	
		boxTrashImg.height = '1';		
		boxTrash.appendChild(boxTrashImg);			
	
	option.appendChild(boxTrash);		

	var box = document.createElement('div');
	box.id = "box";
	box.className = "box";
	option.appendChild(box);		

	var compQty = document.createElement('input');
	compQty.id = "boxqty_" + index;
	compQty.name = "componentQty";
	compQty.className = "boxqty";
	compQty.disabled = true;
	compQty.value = "";
	compQty.onkeyup = function() {com.bec.product.Kit.validateSelectQty('Kit', option, 'Kit_basket', null); return false;};
	compQty.onfocus = function() {com.bec.product.Kit.savePreviousQty('Kit', option, 'Kit_basket'); return false;};	
	option.appendChild(compQty);		

	var arrow = document.createElement('div');
	arrow.id = "arrow";
	arrow.className = "arrow_on";
	option.appendChild(arrow);	

	var clear = document.createElement('div');
	clear.className = "clear";
	option.appendChild(clear);	

	return option;	
}

/**
 * Check if the kit has been filled
 */
com.bec.product.Kit.checkKit = function()
{
	return com.bec.product.Kit.checkFilled('Kit_basket') && com.bec.product.Kit.checkBand();
}

/**
 * Check if the supplied field has been filled
 */
com.bec.product.Kit.checkFilled = function(name)
{
	var rtn = false;
	if ((com.bec.product.Kit.max_allowed_items[name] == undefined) || (com.bec.product.Kit.availableQty(null, name) == 0))
	{
		rtn = true;
	}
	else
	{
		var message = "One or more items have yet to be chosen, for the quantity you've selected.";
		com.bec.product.Kit.showMessageDialog(message);	
	}
	
	return rtn;
}

/**
 * Check if the band field has been filled
 */
com.bec.product.Kit.checkBand = function()
{
	var rtn = true;
	var bandInput = document.getElementById('Band_basket_input_0');
	if (bandInput)
	{
		if (bandInput.value == "")
		{
			rtn = false;
			var message = "Please select an 'Additional Gift Option'.";
			com.bec.product.Kit.showMessageDialog(message);	
		}
	}
	return rtn;
}

/**
 * Preview the item - shows image and text of item being hovered
 */
com.bec.product.Kit.preview = function(id,productId,selIdx)
{
	var o = document.getElementById(id);
	var previewImg  = com.bec.product.Kit.itemPreview[productId];
	var previewText = com.bec.product.Kit.itemPreviewText[productId];
		
	var imgEL = com.bec.product.Kit.findChildById(o, "previewImage");
	var txtEL = com.bec.product.Kit.findChildById(o, "previewText");	
		
	if (imgEL && txtEL)
	{	
		if (previewImg)
		{
			imgEL.innerHTML=previewImg;
			txtEL.innerHTML=previewText;
		}
		else
		{
			imgEL.innerHTML='<img src="/images/spacer.gif" width="1" height="1"/>';
			txtEL.innerHTML='';		
		}
	}
	
	if (dojo.render.html.ie55 || dojo.render.html.ie60)
	{	
		// find options and set "hover" styling
		var elemIdx = 0;
		var selectGroup = o.parentNode;
		
		for (var i = 0; i < selectGroup.childNodes.length; i ++)
		{
			if (dojo.html.hasClass(selectGroup.childNodes[i], "options"))		
			{
				var options = selectGroup.childNodes[i];
				for (var j = 0; j < options.childNodes.length; j ++)
				{					
					if (dojo.html.hasClass(options.childNodes[j], "option"))		
					{
						if (elemIdx == selIdx)
							dojo.html.addClass(options.childNodes[j], "hover");	
						else
							dojo.html.removeClass(options.childNodes[j], "hover");				
					
						elemIdx ++;
					}
				}
				break;
			}
		}
	}
}

/**
 * Find a child Node by id
 */
com.bec.product.Kit.findChildById = function(element, idName)
{
	for (var i = 0; i < element.childNodes.length; i ++)
	{
		if (element.childNodes[i].id == idName)
			return element.childNodes[i];
	}

	return null;
}

/**
	get cart entries as an array of divs 
*/
com.bec.product.Kit.getChildrenByClassName = function(element, className)
{
	var retnodes = [];
if (element == null)
{

return retnodes;
}

	for (var i = 0; i < element.childNodes.length; i++)
	{
		if (element.childNodes[i].className == className)
			 retnodes.push(element.childNodes[i]);
	}
	return retnodes;
} 

/**
	get cart entry as an object of: arrow, trashcan and cart item
*/
com.bec.product.Kit.getCartEntryElements = function(basketList, position, selectYearMonth, groupName)
{
	var cartEntry = new Object();
	if (position >= 0)
		element = basketList[position];	
	else
	{
		element = basketList; // really an element
		position = -1;   // position unknown (and not needed)
	}	
	
    cartEntry.position = position;

	if (selectYearMonth)  // shift down for date order
	{
		for (i = basketList.length-1; i >= 0; i--)
		{
			var currentEntry = com.bec.product.Kit.getCartEntryElements(basketList, i);	
			if (currentEntry.cartItem.innerHTML)
			{			
				var nextEntry = com.bec.product.Kit.getCartEntryElements(basketList, i+1);		
				
				if (currentEntry.boxDate.value <= selectYearMonth)
				{
					cartEntry = nextEntry;
					element = null;
					break;
				}
				nextEntry.boxDate.value = currentEntry.boxDate.value;				
				
				nextEntry.cartItem.innerHTML = currentEntry.cartItem.innerHTML;
			
				if (currentEntry.cartQty)
					nextEntry.cartQty.value = currentEntry.cartQty.value;

				if (currentEntry.boxPrice)
					nextEntry.boxPrice.value = currentEntry.boxPrice.value;
			
				if (currentEntry.boxDate)
					nextEntry.boxDate.value = currentEntry.boxDate.value;
								
				nextEntry.boxId.value = currentEntry.boxId.value;			
				
				if (i == 0)  // insert to first entry
				{
					cartEntry = currentEntry;
					element = null;				
					break;
				}
			}
		}
	}

	if (element != null)
	{	
		for (var i = 0; i < element.childNodes.length; i++)
		{
			var id = element.childNodes[i].id;
			if (id)
			{
				if (id == "arrow")
					 cartEntry.arrow = element.childNodes[i];
				else	 
				if (id == "trashcan")
					 cartEntry.trashcan = element.childNodes[i];
				else	 		
				if (id == "box")
					 cartEntry.cartItem = element.childNodes[i];		
				else	 		
				if (id.indexOf("boxqty_") == 0)
					 cartEntry.cartQty = element.childNodes[i];	
				else	 
				if (element.childNodes[i].name == "componentId")
					 cartEntry.boxId = element.childNodes[i];							 	
				else	 
				if (element.childNodes[i].name == "componentPrice")
					 cartEntry.boxPrice = element.childNodes[i];							 	
				else	 
				if (element.childNodes[i].name == "componentDate")
					 cartEntry.boxDate = element.childNodes[i];	
				else	 
				if ((id.length > 19) && (id.substr(0,19) == "AddOn_basket_input_"))
					 cartEntry.boxId = element.childNodes[i];	
				else	 
				if (id == "Band_basket_input_0")				
					 cartEntry.boxId = element.childNodes[i];							 						 
			}
		}
	}
	
	return cartEntry;
}

/**
 * Adds items to basket input
 */
com.bec.product.Kit.addMe = function(fromTableId, toTableId, insertSKU, groupName, insertText, price)
{
	from_table = document.getElementById(fromTableId);
	to_table   = document.getElementById(toTableId);
	var selectYearMonth = null;
	
	if (fromTableId == 'Kit_skus')
	{
		var selectMonthElem = document.getElementById('monthList');  // month selection
		if (selectMonthElem)
		{
			selectYearMonth = selectMonthElem.options[selectMonthElem.selectedIndex].value;
			var monthIdx = selectYearMonth.substr(4);
			var selectDisplayMonth = monthAbbrevList[parseFloat(monthIdx)-1];   // get abbreviated month name
//			var selectDisplayMonth = selectMonthElem.options[selectMonthElem.selectedIndex].innerHTML;  // get full month name
			insertText = dojo.string.trim(selectDisplayMonth) + "- " + insertText + ' ' + price;
		}	
	}
	
	var basketList = com.bec.product.Kit.getChildrenByClassName(to_table, "option");	
	
	if (insertText.length > com.bec.product.Kit.maxlen)
	{
		insertText = insertText.substr(0,com.bec.product.Kit.maxlen-1)+"...";
	}

	//check if there's still room in the toTableId
	var filled_rows = com.bec.product.Kit.number_of_sku_filled[toTableId];
	var availQty = com.bec.product.Kit.availableQty(null, toTableId);
	
	if (availQty == 0)
	{
		var message = 'You have already selected ' + com.bec.product.Kit.max_allowed_items[toTableId] +' item(s).';
		com.bec.product.Kit.showMessageDialog(message);	
		return;
	}

		// if a club, this will keep the entries in date order, returns position in the cart entry
	var cartEntry = com.bec.product.Kit.getCartEntryElements(basketList, filled_rows, selectYearMonth, groupName);
		
	cartEntry.boxId.value = insertSKU;
	cartEntry.cartItem.innerHTML = insertText;
	
	if (cartEntry.cartQty)
	{
		cartEntry.cartQty.value = '1';
		cartEntry.cartQty.disabled = false;			
		var remainingQtyElem = document.getElementById('remainingQty_' + groupName);
		if (remainingQtyElem)
		{
			remainingQtyElem.innerHTML = availQty - 1;	
		}
	}			
	
	if (selectYearMonth)
	{
		cartEntry.boxDate.value = selectYearMonth;	
	}
	
	cartEntry.trashcan.className = "bin_on";  // add trashcan
	
	var lastEntry = com.bec.product.Kit.getCartEntryElements(basketList, filled_rows);
	lastEntry.trashcan.className = "bin_on";  // add trashcan	
	lastEntry.arrow.className = "arrow_off"; 	// remove arrow	
	if (lastEntry.cartQty)				
		lastEntry.cartQty.disabled = false;				
	
	if (filled_rows < com.bec.product.Kit.max_allowed_items[toTableId]-1)
	{
		if (basketList.length <= filled_rows + 1)
		{
			var nextEntry = com.bec.product.Kit.createSelectionOption(filled_rows + 1);
			
			var endOfList = document.getElementById('kitQty');  // end of sku select list
			
			to_table.insertBefore(nextEntry, endOfList);
		}
		else
		{
			var nextEntry = com.bec.product.Kit.getCartEntryElements(basketList, filled_rows+1);

			nextEntry.arrow.className = "arrow_on";  // add arrow
		}
	}

	cartEntry.arrow.className = "arrow_off"; 	// remove arrow				

	com.bec.product.Kit.number_of_sku_filled[toTableId] = com.bec.product.Kit.number_of_sku_filled[toTableId]+1;

	if (cartEntry.boxPrice)
	{
		cartEntry.boxPrice.value = price;
		var totalPriceElem = document.getElementById('totalPrice');
		if (totalPriceElem)
		{
			totalPriceElem.innerHTML = com.bec.product.Kit.calculateTotal(toTableId);								
		}
	}

}
/**
 * removes item from basket input
 */	
com.bec.product.Kit.removeMe = function(groupName, basketEntry, tableId)
{
	to_table = document.getElementById(tableId);
	var basketList = com.bec.product.Kit.getChildrenByClassName(to_table, "option");	
	var rowIndex = basketList.length;
	
	for (var i = 0; i < basketList.length; i ++)
	{
		if (basketList[i] == basketEntry)
		{
			rowIndex = i;
			break;
		}
	}

	if (rowIndex >= com.bec.product.Kit.number_of_sku_filled[tableId])
		return;
	
	var cartEntry = com.bec.product.Kit.getCartEntryElements(basketList, rowIndex);	
	
	if (cartEntry.cartQty)
	{
		cartEntry.cartQty.value = "";
		cartEntry.cartQty.disabled = false;
	}
	
	cartEntry.cartItem.innerHTML="";
	
	com.bec.product.Kit.shiftRows(rowIndex, basketList, groupName, tableId);
	var filled_rows = com.bec.product.Kit.number_of_sku_filled[tableId] - 1;
	kitSkuInputObj = eval('document.getElementById("'+groupName+'_basket_input_'+filled_rows+'")');
	kitSkuInputObj.value = "";	
	com.bec.product.Kit.number_of_sku_filled[tableId] = com.bec.product.Kit.number_of_sku_filled[tableId]-1;
	
	var remainingQtyElem = document.getElementById('remainingQty_' + groupName);
	if (remainingQtyElem)
	{
		var maxQty = com.bec.product.Kit.availableQty(null, tableId);	
		remainingQtyElem.innerHTML = maxQty;	
	}

	if (cartEntry.boxPrice)
	{
		var totalPriceElem = document.getElementById('totalPrice');
		if (totalPriceElem)
		{
			totalPriceElem.innerHTML = com.bec.product.Kit.calculateTotal(tableId);								
		}
	}
	
}
/**
 * Shift items up when item removed from basket input
 */
com.bec.product.Kit.shiftRows = function(rowIndex, basketList, groupName, tableId)
{
	var i = rowIndex;
	for (i; i < basketList.length-1; i++)
	{
		var nextEntry = com.bec.product.Kit.getCartEntryElements(basketList, i+1);	
		if (nextEntry.cartItem.innerHTML)
		{
			var currentEntry = com.bec.product.Kit.getCartEntryElements(basketList, i);		
			currentEntry.cartItem.innerHTML = nextEntry.cartItem.innerHTML;
			
			if (currentEntry.cartQty)
				currentEntry.cartQty.value = nextEntry.cartQty.value;				

			if (currentEntry.boxPrice)
				currentEntry.boxPrice.value = nextEntry.boxPrice.value;

			if (currentEntry.boxDate)
				currentEntry.boxDate.value = nextEntry.boxDate.value;
			
			currentEntry.boxId.value = nextEntry.boxId.value;			
		}
		else
		{
			break;
		}		
	}
	
	var currentEntry = com.bec.product.Kit.getCartEntryElements(basketList, i);			
	currentEntry.cartItem.innerHTML = '';

	if (currentEntry.cartQty)
	{
		dojo.dom.removeChildren(currentEntry.cartQty);
		currentEntry.cartQty.disabled = true;			
		currentEntry.cartQty.value = "";
	}

	if (i >= 0 && i < com.bec.product.Kit.max_allowed_items[tableId])
	{
		currentEntry.arrow.className = "arrow_on";  // add arrow
		currentEntry.trashcan.className = "bin_off";  // turn off trashcan		

		if (i + 1 < basketList.length)		
		{
			var nextEntry = com.bec.product.Kit.getCartEntryElements(basketList, i+1);				
			nextEntry.arrow.className = "arrow_off";  // remove arrow
		}
	}
	
}

com.bec.product.Kit.getCartEntryObject = function(basketEntry, tableId)
{
	to_table = document.getElementById(tableId);
	var basketList = com.bec.product.Kit.getChildrenByClassName(to_table, "option");	
	var rowIndex = basketList.length;
	
	for (var i = 0; i < basketList.length; i ++)
	{
		if (basketList[i] == basketEntry)
		{
			rowIndex = i;
			break;
		}
	}

	if (rowIndex >= com.bec.product.Kit.number_of_sku_filled[tableId])
		return;
	
	var cartEntry = com.bec.product.Kit.getCartEntryElements(basketList, rowIndex);	
	return cartEntry;
}

/**
 * return available quantity
 */
com.bec.product.Kit.availableQty = function(basketEntry, tableId)
{
	to_table = document.getElementById(tableId);
	var basketList = com.bec.product.Kit.getChildrenByClassName(to_table, "option");	
	var rowIndex = basketList.length;
	var totalQty = 0;	
	
	for (var i = 0; i < com.bec.product.Kit.number_of_sku_filled[tableId]; i ++)
	{
		var cartEntry = com.bec.product.Kit.getCartEntryElements(basketList, i);	
		
		if (cartEntry.cartQty == null)  // quantity not used
		{
			totalQty = 	com.bec.product.Kit.number_of_sku_filled[tableId];
			break;			
		}
	
		if (basketList[i] != basketEntry)
		{
			var curQty = parseInt(cartEntry.cartQty.value);
			if (isNaN(curQty))
				curQty = 1;		

			totalQty += curQty;
		}
	}		
	
	return com.bec.product.Kit.max_allowed_items[tableId] - totalQty;

}


/**
 * create the options for the kit component quantity
 */
com.bec.product.Kit.savePreviousQty = function(groupName, basketEntry, tableId)
{
	var cartEntry = com.bec.product.Kit.getCartEntryElements(basketEntry);	
	if (!cartEntry)
		return;

	var curQty = parseInt(cartEntry.cartQty.value);
	previousQty = 1;
//console.log('Previous qty ' + curQty + ", " + cartEntry.boxId.value);			
	
	if (!isNaN(curQty))
		previousQty = curQty;
}

/**
 * create the options for the kit component quantity
 */
com.bec.product.Kit.validateSelectQty = function(groupName, basketEntry, tableId, event)
{
	var cartEntry = com.bec.product.Kit.getCartEntryElements(basketEntry);	
	if (!cartEntry)
		return;

	var selectedQty = 1;

	var availQty = com.bec.product.Kit.availableQty(basketEntry, tableId);

	var enteredQty = cartEntry.cartQty.value;
	for (i = 0; i < enteredQty.length; i ++)
	{
		if ((enteredQty.charAt(i) < '0') || (enteredQty.charAt(i) > '9'))
		{
			enteredQty = 'x';   // trigger nan
			break;
		}
	} 
	
	var curQty = parseInt(enteredQty);

	if (isNaN(curQty))	
	{
		cartEntry.cartQty.value = "" + previousQty;
	}
	else
	if (curQty == 0)
	{
		com.bec.product.Kit.removeMe(groupName, basketEntry, tableId);	
	}
	else	
	if (curQty > availQty)
	{
		com.bec.product.Kit.showMessageDialog("The maximum quantity for this item is " + availQty);	
		cartEntry.cartQty.value = "" + previousQty;		
	}
	else
	{
		var remainingQtyElem = document.getElementById('remainingQty_' + groupName);
		if (remainingQtyElem)			
		{
			remainingQtyElem.innerHTML = availQty - curQty;
		}
		
		if (cartEntry.boxPrice)
		{
			var totalPriceElem = document.getElementById('totalPrice');
			if (totalPriceElem)
			{
				totalPriceElem.innerHTML = com.bec.product.Kit.calculateTotal(tableId);						
			}
		}		
		previousQty = curQty;
	}

}

/**
 * create the options for the kit component quantity
 */
com.bec.product.Kit.calculateTotal = function(tableId)
{
	to_table = document.getElementById(tableId);
	var basketList = com.bec.product.Kit.getChildrenByClassName(to_table, "option");	
	var rowIndex = basketList.length;
	var qty = 1;	
	var totalAmount = 0;		
	
	for (var i = 0; i < com.bec.product.Kit.number_of_sku_filled[tableId]; i ++)
	{
		var cartEntry = com.bec.product.Kit.getCartEntryElements(basketList, i);	
		
		if (cartEntry.cartQty != null)  // quantity used
		{
			qty = parseInt(cartEntry.cartQty.value);
		}

		if ((cartEntry.boxPrice) && (cartEntry.boxPrice.value))  // price used
		{
			var price =	cartEntry.boxPrice.value;
			price = parseFloat(price.substr(1));		
			totalAmount += (price * qty);
		}
	}
	
	return formatCurrency(Number(totalAmount), true);

}

/**
 * create the options for the kit component quantity
 */
com.bec.product.Kit.fillSelectQty = function(groupName, basketEntry, tableId)
{
	var cartEntry = com.bec.product.Kit.getCartEntryObject(basketEntry, tableId);	
	if (!cartEntry)
		return;

	var selectedQty = 1;
	var optionCount = 1;
	
	if (cartEntry.cartQty.childNodes.length > 0)
	{
		var optionList = cartEntry.cartQty.childNodes;

		for (j = 0; j < optionList.length; j ++)
		{
			if (optionList[j].nodeName == 'option')
			{
				if (optionList[j].selected)
				{
					selectedQty = optionCount;
					break;		
				}
				optionCount ++;
			}
		}

		dojo.dom.removeChildren(cartEntry.cartQty);
	}

	var maxQty = com.bec.product.Kit.availableQty(basketEntry, tableId);
	for (var i = 1; i <= maxQty; i ++)
	{
		var option = document.createElement("option");
		option.value = "" + i;
		option.innerHTML= "" + i;
		option.selected = false;	
				
		if (i == selectedQty)
			option.selected = true;	
			
		cartEntry.cartQty.appendChild(option);			
	}
	
	cartEntry.cartQty.selectedIndex = selectedQty - 1;

}

com.bec.product.Kit.showMessageDialog = function (messageText) 
{
	var content = '<p class="addressError"><img src="/images/erroricon_whitex.gif" border="0" align="left" style="padding-right:5px"/>'+messageText+'</p>'; 

	var id = "dynamicKitAlert_1";
	var title = "Item Collection Creation Error";
	var width = 350;
	var height = 100;

	var newPopup = dojo.widget.byId(id);
	if(!newPopup)
	{	//lazy create - only do one time.
		newPopup = dojo.widget.createWidget("ModalFloatingPane", // widgetType
     		{	widgetId: id,
     			bgColor: "white",
     			bgOpacity: "0.4",
     			closeOnBackgroundClick: true,
     			hasShadow: true,
     			displayCloseAction: true,
     			titleBarDisplay: true,     			
     			resizable: false,
     			title: title,
     			templateCssPath: eSiteStyleDir + "/dojo/widget/templates/FloatingPane.css"
     		}, // widget attributes, for example {title: "Some Title"}
     		dojo.byId(POPUP_PLACEHOLDER_ID), // reference to a DOM node that 
        	"after"	// new widget will be placed after
    	);	
    	
    	//set the content
    	newPopup.setContent(content);
    	newPopup.resizeTo(width, height);
    }
    else
    {
    	newPopup.setContent(content);	
    }
	newPopup.show();	
}

