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

function showLargeImage(imagePath) {
	window.open (imagePath,'viewLargeImage',"menubar=1,resizable=1,width=450,height=550"); 
}
function displaySubCat(catId) {
	document.getElementById("rightListing").innerHTML = document.getElementById("mainIntroDiv" + catId).innerHTML
}
function clearSubCat() {
	document.getElementById("rightListing").innerHTML = "";
}
function showMenu(menuId) {
	document.getElementById(menuId).style.display = "block";
}
function hideMenu(menuId) {
	document.getElementById(menuId).style.display = "none";
}


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


function recalculateAndDisplayPrice(productId) {
	
	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 on frame
	if(document.getElementById("frame") != null) {
		if(document.getElementById("frame").value != "Modern Flat Wood") {
			if(document.getElementById("frame").value == "Unframed") {
				fullPrice = fullPrice - 20;
			}
		}
	}
	
	/////////////////////////////////////////
	// update the price based on details
	if(document.getElementById("addedDetailsBox") != null) {
		if(document.getElementById("addedDetailsBox").checked == true) {
			fullPrice = fullPrice + 5;
		}
	}
	
	/////////////////////////////////////////
	// update the price based on options
	if(document.getElementById("option") != null) {
		if(document.getElementById("option").value != "No personalisation") {
			fullPrice = fullPrice + 8;
		}
	}
	
	/////////////////////////////////////////
	// 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) {
		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 
	document.getElementById("priceextra").value = (discount_fullPrice - discount_fullBasePrice).toFixed(2);
}

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;
}

function removeCurrency(price) {
	if(price.substr(0,1).charCodeAt(0) == "163" || price.substr(0,1) == "£") {
		return price.substr(1);
	} else {
		return price;
	}
}
function toggleAddedDetails(productId) {
	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 = "";
	}
}
function toggleGiftWrappingTag() {
	
	var giftWrap = document.getElementById("giftWrapSelector");
	
	
	if(giftWrap.options[giftWrap.selectedIndex].value != "None") {
		document.getElementById("giftWrapTag").style.display = "block";
	} else {
		document.getElementById("giftWrapTag").style.display = "none";
	}
}
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";
	}
}

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