﻿/*
	This file applies to product_info.php
	It applies to any products that have "Attributes" that will affect the price.
	It will create a "Total Price" that reflects the changes in price 
	from the base price, depending on selected attributes.
	
	Requires: jQuery.js

*/

$(document).ready(AddProductAttributesTogether);
function AddProductAttributesTogether(){
	if ($("#ProductOptions").length == 0) return;
	
	//Attach handlers to all the list boxes
	//IE fires Change like it should
	//FF doesn't fire change if it's a keypress, so we need to attach to keypress too!
	var AttributeListBoxes = $("#ProductOptions").find("select").change(CalculateProductTotal).keyup(CalculateProductTotal);
	CalculateProductTotal();
}
function CalculateProductTotal(){
	//Find the base price
	var basePriceElem = $(".productPrice, .productSpecialPrice").get(0);
	var basePrice = parseFloat(basePriceElem.innerHTML.substr(1));
	
	var attributeTotal = 0;
	//Find the value of each selected attribute:
	var AttributeListBoxes = $("#ProductOptions").find("select").each(function(){
		var selectedAttributeText = this.options[this.selectedIndex].text;
		var attributePrice = 0;
		if (selectedAttributeText.indexOf("$") == -1) {
			attributePrice = 0;
		} else {
			attributePrice = parseFloat( selectedAttributeText.substr(selectedAttributeText.indexOf("$")+1) );
			if (selectedAttributeText.substr(selectedAttributeText.indexOf("$")-1,1) == "-") {
				attributePrice = -attributePrice;
			}
		}
		attributeTotal += attributePrice;
	});
	
	if (attributeTotal == 0) {
		//clear the Total fields
		$("#ProductTotalLabel, #ProductTotal").css("visibility", "hidden");
	} else {
		$("#ProductTotalLabel, #ProductTotal").css("visibility", "visible");
	}
	//Put in the new total into the total field
	$("#ProductTotal").empty().append("$" + (attributeTotal + basePrice).toFixed(2));
}
