// Displays a product/book's table of contents
var ToggleTOC = function(){
	var TheDiv = document.getElementById('divTOC');
	var CurrDisplayVal = TheDiv.style.display;
	
	if (CurrDisplayVal == '')
		TheDiv.style.display = 'none';
	else
		TheDiv.style.display = '';
}

// Updates the displayed price for a rental based on the selected rental period
var UpdateRentalPrice = function(idRentPerSel,idRentalPriceSpan,idQty,idRentalTotalPriceSpan) {
	var objRentPerSel = document.getElementById(idRentPerSel);
	var RentPerIDPrice = objRentPerSel[objRentPerSel.selectedIndex].value;
	var arrRentPerIDPrice = RentPerIDPrice.split('|');
	var Price = arrRentPerIDPrice[1];
	
	// Update the rental period price
	document.getElementById(idRentalPriceSpan).innerHTML = '$' + CurrencyFormat(Price);
	
	// If ids for qty and total price have been provided, update the total price too
	if (idQty && idRentalTotalPriceSpan) {
		// Get the rental qty
		var Qty = document.getElementById(idQty).value;
		// Calc the total price
		var TotalPrice = Qty * Price;
		// Set the total price span
		document.getElementById(idRentalTotalPriceSpan).innerHTML = '$' + CurrencyFormat(TotalPrice);
	}
}

// Updates the New price when an attribute is changed
var AttribOnChange = function(ProdID, ProdAttribTypeID, ProdAttribSelOptID, retailPrice, idNewPrice, ShowRetail, PriceClass){
	// If PriceClass wasn't supplied, set a default value
	if (!PriceClass)
		PriceClass = 'CopyRed';
	// A non-numeric ProdAttribSelOptID value means we need to get the value from a normal select, and that ProdAttribSelOptID is actually the id of the select.
	// Image selects provide the value on their own.
	if (IsNumeric(ProdAttribSelOptID)==false) {
		var objSelect = document.getElementById(ProdAttribSelOptID);
		ProdAttribSelOptID = objSelect[objSelect.selectedIndex].value;
	}
	// TRIED USING ColdFusion.navigate USING A CALL TO A CFC FUNCTION, BUT HTML IN FUNCTION RESULT WAS NOT RENDERED BY RECEIVING CFDIV - THE RAW HTML WAS DISPLAYED
	// create an instance of the proxy. 
	var e = new Products();
	e.setCallbackHandler(AttribOnChangeResult);
	e.setErrorHandler(myErrorHandler);
	e.FormatProdAttribPrice(ProdID,ProdAttribTypeID,ProdAttribSelOptID,retailPrice,idNewPrice,ShowRetail,PriceClass);
}

var AttribOnChangeResult = function(Res){
	// Update the New price, if we got a price
	if (Trim(Res.PRICESTR) != '' && document.getElementById(Res.IDNEWPRICE)) {
		document.getElementById(Res.IDNEWPRICE).innerHTML = Res.PRICESTR;
	}
}

// Used by select-type attributes that use images for selection - handles visual indication of the selected image and sets the value of a hidden field
var AttribOnClick = function(idHiddenFld, attribValue, idDiv, idImg){
	var objDiv = document.getElementById(idDiv);
	var elms = objDiv.getElementsByTagName("img");
	
	// Remove any borders on all img elements inside the div
	for (var i = 0, maxI = elms.length; i < maxI; ++i) {
		var elm = elms[i];
		elm.className = 'ImgNotSelected';
	}
	
	// Add a border to the clicked on img
	document.getElementById(idImg).className = 'ImgSelected';
	
	// Put the attribute value into the hidden field
	document.getElementById(idHiddenFld).value = attribValue;
}
