var fullBasePrice;//<- product cost with no personalisation or disocunt


////////////////////////////////////////////////////////////////////////////////
// showLargeImage()
////////////////////////////////////////////////////////////////////////////////

function showLargeImage(imagePath)
	{
	window.open (imagePath,'viewLargeImage',"menubar=1,resizable=1,width=450,height=550"); 
	}


////////////////////////////////////////////////////////////////////////////////
// displaySubCat()
////////////////////////////////////////////////////////////////////////////////

function displaySubCat(catId)
	{
	if(document.getElementById("mainIntroDiv" + catId))
		document.getElementById("rightListing").innerHTML = document.getElementById("mainIntroDiv" + catId).innerHTML
	}


////////////////////////////////////////////////////////////////////////////////
// clearSubCat()
////////////////////////////////////////////////////////////////////////////////

function clearSubCat()
	{
	document.getElementById("rightListing").innerHTML = "";
	}


////////////////////////////////////////////////////////////////////////////////
// showMenu()
////////////////////////////////////////////////////////////////////////////////

function showMenu(menuId)
	{
	document.getElementById(menuId).style.display = "block";
	}


////////////////////////////////////////////////////////////////////////////////
// hideMenu()
////////////////////////////////////////////////////////////////////////////////

function hideMenu(menuId)
	{
	document.getElementById(menuId).style.display = "none";
	}


////////////////////////////////////////////////////////////////////////////////
// updateCustomData()
////////////////////////////////////////////////////////////////////////////////

function updateCustomData(targetId, inputValue, productId)
	{
	document.getElementById(targetId).value = inputValue;

	changeStaticExtraPrice();

	if(typeof changeExtraPrice == 'function')
		changeExtraPrice(productId);
	else
		recalculateAndDisplayPrice(productId);
	}


////////////////////////////////////////////////////////////
// changeStaticExtraPrice()
////////////////////////////////////////////////////////////
// This function cludges the value in the 'priceextra' field
// for certain products using a selection of magic numbers.
////////////////////////////////////////////////////////////

function changeStaticExtraPrice()
	{
	var priceextra = 0;
	
	// update the price based on frame
	if(document.getElementById("frame") != null)
		if(document.getElementById("frame").value != "Modern Flat Wood")
			if(document.getElementById("frame").value == "Unframed")
				priceextra -= 20;

	// update the price based on details
	if(document.getElementById("addedDetailsBox") != null)
		if(document.getElementById("addedDetailsBox").checked == true)
			priceextra = priceextra;

	// update the price with additional price e.g. stools with optional date for 4.99
	if(document.getElementById("addedDetailsPrice") != null)
		if(document.getElementById("addedDetailsBox").checked == true)
			priceextra = priceextra + parseFloat(document.getElementById("addedDetailsPrice").value);
		
	// update the price based on options
	if(document.getElementById("option") != null)
		if(document.getElementById("option").value != "No personalisation")
			if(document.getElementById("jsProductTypeIndicator") && document.getElementById("jsProductTypeIndicator").value=="clock")
				priceextra += 10;
			else
				priceextra += + 8;

	// Update 'priceextra' or 'priceextra_PRODUCTID' field.
	if(document.getElementById("priceextra"))
		document.getElementById("priceextra").value = priceextra;
	else if(document.getElementById("priceextra_"+productId))
		document.getElementById("priceextra_"+productId).value = priceextra;
	}



////////////////////////////////////////////////////////////////////////////////
// recalculateAndDisplayPrice()
////////////////////////////////////////////////////////////////////////////////

function recalculateAndDisplayPrice(productId)
	{
	// Vars
	var fullPrice;//<- product cost with personalisation
	var discount_fullPrice;//<- product cost with personalisation and discount
	var discount_fullBasePrice;//<- product cost with discount but no personalisation 


	// Get full base price (from strike through if present). This is the full price without personalisation
	if(!fullBasePrice)
		{

		if(document.getElementById("price_old_" + productId) && isset("discountType"))
			fullBasePrice = document.getElementById("price_old_" + productId).innerHTML;
		else
			fullBasePrice = document.getElementById("price_" + productId).innerHTML;

		fullBasePrice = removeCurrency(fullBasePrice);
		fullBasePrice = parseFloat(fullBasePrice);
		}

	fullPrice = fullBasePrice;

	// update the price based extras (found in the priceextras)
	//if(document.getElementById("priceextra"))
	fullPrice +=  parseFloat(document.getElementById("priceextra").value);

	// get discounted amount
	discount_fullPrice = getDiscountedPrice(fullPrice);
	discount_fullBasePrice = getDiscountedPrice(fullBasePrice);

	// update the price based on gift wrap...
	// NOTE: gift wrap is always £3 - it is not discounted!!
	var giftWrap = document.getElementById("giftWrapSelector");
	if(giftWrap==null)
		giftWrap = document.getElementById("giftWrapSelector_"+productId);

	if(giftWrap != null)
		if(giftWrap.options[giftWrap.selectedIndex].value != "None" )
			{
			discount_fullPrice +=3;
			fullPrice += 3;
			}
	
	// Set buy price. This is always the discountPrice (if there is no discount then discount_fullPrice will be full amount)
	document.getElementById("price_" + productId).innerHTML = "&pound;" + discount_fullPrice.toFixed(2);
	
	// Update the strike through price (i.e. the 'was' price). This is always the fullPrice
	if(document.getElementById("price_old_" + productId) && isset("discountType"))
		document.getElementById("price_old_" + productId).innerHTML = "&pound;" + fullPrice.toFixed(2);
	
	// Update the extras price (the price for personalisation). 
	// This is the price without personalisation minus price with personalisation 
	var priceextra = (discount_fullPrice - discount_fullBasePrice).toFixed(2);
		
		if(document.getElementById("woodenletterprice_"+productId) != null)// if wooden letters there are more priceextras
			priceextra = parseFloat(priceextra)+parseFloat(document.getElementById("woodenletterprice_"+productId).value);
	
	if(document.getElementById("priceextra"))
		document.getElementById("priceextra").value = priceextra;
	else if(document.getElementById("priceextra_"+productId))
		document.getElementById("priceextra_"+productId).value = priceextra;
	}


////////////////////////////////////////////////////////////////////////////////
// getDiscountedPrice()
////////////////////////////////////////////////////////////////////////////////

function getDiscountedPrice(price)
	{
	if(!isset("discountType") || !isset("discountAmount"))
		return price;
		
	if(discountType=="percentage")
		price = (price / 100) * (100 - discountAmount);
	else// fixed reduction
		price = price - discountAmount;
	
	return price;
	}


////////////////////////////////////////////////////////////////////////////////
// removeCurrency()
////////////////////////////////////////////////////////////////////////////////

function removeCurrency(price)
	{
	if(price.substr(0,1).charCodeAt(0) == "163" || price.substr(0,1) == "£")
		return price.substr(1);
	else
		return price;
	}


////////////////////////////////////////////////////////////////////////////////
// toggleAddedDetails()
////////////////////////////////////////////////////////////////////////////////
var callToggleCount = 0;
var curToggleCount = 0;
function toggleAddedDetails(productId)
	{
		
	/*if(document.getElementById("priceextra"))
		document.getElementById("priceextra").value = 0;
	else if(document.getElementById("priceextra_"+productId))
		document.getElementById("priceextra_"+productId).value = 0;
	
	recalculateAndDisplayPrice(productId);*/
	
	if(document.getElementById("addedDetailsBox").checked == false)
		{
		document.getElementById("added_details").value = "";
		document.getElementById("added_details").disabled = "disabled";
		}
	else
		{
		document.getElementById("added_details").value = "";
		document.getElementById("added_details").disabled = "";
		}
		
	callToggleCount++;
	
		if(callToggleCount<=1 && typeof changeExtraPrice == 'function'){
			changeExtraPrice(productId);
		}
		else
			callToggleCount = 0;
	}


////////////////////////////////////////////////////////////////////////////////
// toggleGiftWrappingTag()
////////////////////////////////////////////////////////////////////////////////

function toggleGiftWrappingTag(productid)
	{
	if(productid)
		var giftWrap = document.getElementById("giftWrapSelector_"+productid);
	else
		var giftWrap = document.getElementById("giftWrapSelector");
	
	if(productid)
		var giftWrapTag = document.getElementById("giftWrapTag_"+productid);
	else
		var giftWrapTag = document.getElementById("giftWrapTag");
	
	if(giftWrap.options[giftWrap.selectedIndex].value != "None")
		giftWrapTag.style.display = "block";
	else
		giftWrapTag.style.display = "none";
	}


////////////////////////////////////////////////////////////////////////////////
// turnOffInput()
////////////////////////////////////////////////////////////////////////////////

function turnOffInput(divId)
	{

	if(divId == "addedDetailsInput")
		{
		document.getElementById("nameInput").style.display = "block";
		document.getElementById(divId).style.display = "none";
		}

	if(divId == "none")
		{
		document.getElementById("addedDetailsInput").style.display = "block";
		document.getElementById("nameInput").style.display = "block";
		}

	if(divId == "all")
		{
		document.getElementById("addedDetailsInput").style.display = "none";
		document.getElementById("nameInput").style.display = "none";
		}
	}


////////////////////////////////////////////////////////////////////////////////
// isset()
////////////////////////////////////////////////////////////////////////////////

function isset(varname)
	{
  return(typeof(window[varname])!='undefined');
	}
