jQuery(document).ready(function(){
	updateProductPrice();

	jQuery('#calligraphy_1').keyup(function(){
		updateProductPrice();
	});
	jQuery('#calligraphy_1dt').keyup(function(){
			updateProductPrice();
	});
	jQuery('#calligraphy_2').keyup(function(){
		updateProductPrice();
	});
	jQuery('#design').keyup(function(){
		updateProductPrice();
	});
	jQuery('#design').change(function(){
		updateProductPrice();
	});
	jQuery('#eb_comments').keyup(function(){
		updateProductPrice();
	});
	jQuery('#ribbonstyle').keyup(function(){
		updateProductPrice();
	});
	jQuery('#robboncolour').keyup(function(){
		updateProductPrice();
	});
	jQuery('#boxshape').keyup(function(){
		updateProductPrice();
	});

	/*jQuery('#eb_quantity').keyup(function(){
		updateProductPrice();
	});
	*/
});
function updateProductPrice()
{
	var tr=document.getElementById('tablerate');
	if(tr==null){return;}

	var price=0; var rates="";
	price=parseFloat(jQuery('#base_price').val());
	quantity=parseFloat(jQuery('#eb_quantity').val());
	if(isNaN(quantity)){quantity=1;}
	specialPrice="";

	if(jQuery('#tablerate').val()!="")
	{
		//this item is using table rate pricing
		rates=jQuery('#tablerate').val();
		tableRates=rates.split("|");
  		for(var n in tableRates)
  		{
  			if(tableRates[n]!=""){
				rateDetails=tableRates[n].split(",");
				if (quantity>=rateDetails[0]){price=parseFloat(rateDetails[1]); specialPrice=" @ " + rateDetails[1] + "ea";}
			}
		}
	}




	var customText="";
	//customText=customText + "Quantity: " + quantity + "\n";
	if(jQuery('#design').length>0) {customText=customText+"Design:" + jQuery('#design').val() + "\n";}

	if(jQuery('#calligraphy_1').length>0) {
		if(jQuery('#calligraphy_1').val().length>0) {
			price=price+9.90;customText=customText+"Calligraphy1:" + jQuery('#calligraphy_1').val() + "\n";
			customText=customText+"Calligraphy Date:" + jQuery('#calligraphy_1dt').val() + "\n";
		}
	}
	if(jQuery('#calligraphy_2').length>0) {
		if(jQuery('#calligraphy_2').val().length>0) {
			price=price+4.50;customText=customText+"Calligraphy2:" + jQuery('#calligraphy_2').val() + "\n";
		}
	}
	if(jQuery('#boxshape').length>0) {
		if(jQuery('#boxshape').val().length>0) {
			customText=customText+"Box Shape:" + jQuery('#boxshape').val() + "\n";
		}
	}
	if(jQuery('#ribbonstyle').length>0) {
		if(jQuery('#ribbonstyle').val().length>0) {
			customText=customText+"Ribbon Style:" + jQuery('#ribbonstyle').val() + "\n";
		}
	}
	if(jQuery('#ribboncolour').length>0) {
		if(jQuery('#ribboncolour').val().length>0) {
			customText=customText+"Ribbon Colour:" + jQuery('#ribboncolour').val() + "\n";
		}
	}

	//jQuery('#eb_comments').text(customText);

	if(jQuery('#eb_comments').length) {customText=customText+"Comments:" + jQuery('#eb_comments').val() + "";}


	price = price * quantity;

	jQuery('#finalproductcost').html(jQuery('#currencySymbol').val() + price.toFixed(2) + specialPrice);
	jQuery('#donation_price_' + jQuery('#productId').val()).val(price.toFixed(2));
	jQuery('textarea[name=custom_text]').val(customText);
}
