$(document).ready(function(){
	$("#addProductForm").submit(function(){
		$("#cartInfo").validate();
		
		qty= parseFloat($("#qty").val());
		if(qty)
		{
			if(qty < 1)
			{
				qty = 1;
				$("#qty").val(1);
			}
		}else{
			qty = 1;
			$("#qty").val(1);
		}
		sku = $("#sku").val();
		width = $("#width").val();
		height = $("#height").val();
		type = $("input[name=type]:checked").val();
		typeLabel = type == "bevel" ? "Beveled" : "Polished";
		
		if(type && Calculator.isValidSku(sku))
		{
			prices = Calculator.getPrices(sku,width,height);
			var newRow = "" +
				"<div class=\"itemWrap\">" +
					"<div class=\"qty\">" + qty + "</div>" +
					"<div class=\"item\">" +
						"<h6>"+sku+"</h6>" +
						"<span class=\"width\">"+width+"</span>\" x <span class=\"height\">"+height+"</span>\" (<span class=\"type\">"+typeLabel+"</span>)" +
					"</div>" +
					"<div class=\"price\">" +
						formatAsMoney(prices[type]) + 
					"</div>" +
					"<div class=\"controls\">" +
						"<a class='remove' href='javascript:;'><img src=\"img/remove.gif\" alt=\"Remove Item\" /></a>" +
					"</div>" +
				"</div>";
			$("#cartWrap","#cart_wrap").append(newRow);
			
			updateTotal();
		}
		return false;
	});
	$("#qty").val($("#qty").val() ? $("#qty").val() : 1);
	$("#sku").blur(updatePrice);
	$("#width").change(updatePrice);
	$("#height").change(updatePrice);
	
	$("a.remove").live("click",removeRow);
	
	jQuery.validator.addMethod("cartCount", function(value, element, params) {
		valid = true;
		try{
			valueData = JSON.parse(value);
			if(valueData.total == "" || valueData.cart.length < 1) valid = false;
		} catch(e)
		{
			valid = false;
		}
    	return this.optional(element) || valid; 
	}, $.format("You need to add at least one item.")); 

	$("#infoForm").validate({
		rules:{
			name:{
				required:true
			},
			phone:{
				required:true
			},
			email:{
				required:true,
				email:true
			}
		},
		submitHandler:sendEmail,
		errorPlacement:function(){},
		errorClass:"error"
	});
	$("#submitBtn").click(function(){$("#infoForm").submit()});
	
	updatePrice();
});

function sendEmail()
{
	cart = serializeCart();
	$("#cartInfo").val(JSON.stringify(cart));

	if(cart.cart.length > 0)
	{
		$.ajax({
			url:'mailer.php',
			type:'post',
			data:$("#infoForm").serialize(),
			success:function(data)
			{
				$("#result_wrap").hide();
				$(".message","#result_wrap").hide();
				try{
					data = JSON.parse(data);
					if(data.result)
					{
						$(".success","#result_wrap").show();
					}else{
						$("span.message","div.error").html(data.message);
						$(".error","#result_wrap").show();
					}
				}catch(err)
				{
					// Something bad happened
					$(".message","div.error").html("Unknown");
					$(".error","#result_wrap").show();
				}
				$("#result_wrap").slideDown();
			}
		});
	}
}

function resetForm(clearForms)
{
	if(clearForms == null) clearForms = false;
	
	$("#cartWrap").children(".itemWrap").remove();
	$("#result_wrap").slideUp();
	if(clearForms)
	{
		$("#addProductForm").clearForm();
		$("#infoForm").clearForm();
	}
	updateTotal();
}

function updateTotal()
{
	oldTotal = $("#total").getFloatValue();
	var total = 0;
	rows = $("#cartWrap").children(".itemWrap");
	rows.each(function(){
		total += $(".price",this).getFloatValue() * $(".qty",this).html();
	});
	
	// Round the total
	total = Math.round(total*100)/100;
	if(oldTotal != total)
	{
		$("#total").html(formatAsMoney(total)).not(":animated").effect("highlight",{},1000);
	}
}
function updatePrice()
{
	// Validate the sku number
	if($("#sku").val())
	{
		$("#sku").val($("#sku").val().toUpperCase()); // Capitalize the sku first
		if(Calculator.isValidSku($("#sku").val()))
		{
			$("#sku_error").hide();
		}else{
			$("#sku_error").show();
		}
	}
	if($("#sku").val() && $("#width").val() && $("#height").val())
	{
		
		prices = Calculator.getPrices($("#sku").val(), $("#width").val(), $("#height").val());
		if(prices)
		{
			$("#beveledPrice").html(formatAsMoney(prices.bevel));
			$("#polishedPrice").html(formatAsMoney(prices.polish));
		}else{
			clearPrices();
		}
	}else{
		clearPrices();
	}
}

function clearPrices()
{
	$("#beveledPrice").html("");
	$("#polishedPrice").html("");
}

function removeRow(e)
{
	$(this).parents(".itemWrap").fadeOut("normal",_removeRow);
}
function _removeRow()
{
	$(this).remove();
	updateTotal();
}

function serializeCart()
{
	selections = [];
	$("#cartWrap").children(".itemWrap").each(function(){
		cols = $(this).children("div");
		currSelection = {
			sku:$("h6",this).html(),
			width:$(".item span.width",this).html(),
			height:$(".item span.height",this).html(),
			style:$(".item span.type",this).html(),
			price:$(".price",this).html(),
			qty:$(".qty",this).html()
		}
		selections.push(currSelection);
	});
	
	return {total:$("#total").html(),cart:selections};
}

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    mnt = (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
    return "$"+mnt;
}

$.fn.getFloatValue = function()
{
	_html = $(this).html();
	return parseFloat(_html.replace(/\$/g,""));
}

var Calculator = {
	isValidSku:function(sku)
	{
		if(typeof Prices[sku] == 'object')
		{
			return true;
		}else{
			return false;
		}
	},
	getPrices:function(sku,width,height)
	{
		width = parseFloat(width);
		height = parseFloat(height);
		
		if(this.isValidSku(sku))
		{
			var props = Prices[sku];
			var listCostPerFoot = props[2];
			var totalLength = ((width*2) + (height*2)) / 12;
			
			var beveledPrice = ((((width * height)/144)*8.93)+12.5)+(((width*2)+(height*2))*.5)+(listCostPerFoot*totalLength);
			var polishPrice = ((((width * height)/144)*6.06)+12.5)+(listCostPerFoot*totalLength);
			// Round the final numbers to two decimal places
			beveledPrice = Math.round(beveledPrice*100)/100;
			polishPrice = Math.round(polishPrice*100)/100;
			return {polish:polishPrice,bevel:beveledPrice}; 
		}else{
			return false;
		}	
	}
};

// Extend jQuery to add a form clearing convienience function
$.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

var Prices = {
	'AB288-2125':[24,1.25,11.235],
	'AB304-2125':[24,2,17.01],
	'AB354-3029':[26,1.5,12.635],
	'AB3543041-20':[26,1.5,12.635],
	AB7001:[24,2.25,20.02],
	AB7002:[24,2.5,20.02],
	AB7004:[28,1.25,11.235],
	AB7005:[28,1.25,11.235],
	AB7006:[25,1.25,10.71],
	'AB7006-20':[28,1.25,11.235],
	AB7008:[28,1.875,11.795],
	AB7010:[25,1.5,11.235],
	'AB7010-20':[28,1.375,11.795],
	AB7038:[25,2.25,20.02],
	'AB7038-19':[24,2.25,20.02],
	'AB7038-20':[24,2.25,20.02],
	'AB7038-21':[24,2.25,20.02],
	'AB815-2169':[28,3.75,30.73],
	'AB847-2186':[28,2,17.85],
	'AB847-2187':[28,2,17.85],
	'AB885-2186':[28,1.25,11.235],
	'AB885-3086':[28,1.25,11.235],
	ABN2031:[26,2.5,21.665],
	ABN2033:[25,1.5,17.29],
	ABN2034:[25,1,9.59],
	'ABN2034-19':[24,1,10.71],
	'ABN2034-20':[24,1,10.71],
	ABN2035:[25,1.25,10.71],
	ABN7007:[28,1.5,11.795],
	B2101:[19,0.75,5.705],
	B2102:[19,0.75,5.705],
	B2103:[19,0.75,6.055],
	B3208:[13,1,9.345],
	B3209:[13,1,9.345],
	B3210:[13,1,9.345],
	B3211:[13,1,9.345],
	B3212:[13,1,9.345],
	B3213:[13,1,9.345],
	B3401:[21,1,10.15],
	B3402:[21,1,10.15],
	B3403:[21,1,10.15],
	B3404:[21,1,10.15],
	B3405:[21,1,10.15],
	B3406:[25,1.125,11.235],
	B3407:[25,0.75,9.87],
	B3408:[13,0.875,16.205],
	B3409:[13,0.875,16.205],
	B3410:[13,0.875,16.205],
	B3411:[13,0.875,16.205],
	B3412:[13,0.875,16.205],
	B3413:[13,0.875,16.205],
	B4101:[19,1.25,6.335],
	B4101G:[19,1.5,6.685],
	B4102:[19,1.25,6.335],
	B4102G:[19,1.25,6.685],
	B4103:[19,1.25,7.21],
	B4401:[21,1.625,14.56],
	B4402:[21,1.625,14.56],
	B4403:[21,1.625,14.56],
	B4404:[21,1.625,14.56],
	B4405:[21,1.625,14.56],
	B4944:[13,2,16.73],
	B4945:[25,2,16.73],
	B4975:[12,1.5,16.73],
	B4976:[12,1.5,16.73],
	B4977:[12,1.5,16.73],
	B4978:[12,1.5,16.73],
	B5001G:[19,1.5,11.235],
	B5002G:[19,1.5,11.235],
	B5101:[19,2,10.885],
	B5101G:[20,2,10.885],
	B5102:[19,2,10.885],
	B5102G:[20,2,10.885],
	B5103:[19,2,11.97],
	B5103G:[20,2,11.97],
	B5401:[22,2.125,18.935],
	B5402:[22,2.125,18.935],
	B5403:[22,2.125,18.935],
	B5404:[22,2.125,18.935],
	B5405:[22,2.125,18.935],
	B5720:[17,2,14],
	B5721:[17,2,14],
	B5914:[27,2,16.205],
	B5915:[27,2,16.205],
	B5916:[10,2,24.43],
	B5917:[10,2,24.43],
	B5918:[10,2,24.43],
	B5919:[10,2,24.43],
	B5920:[9,2.25,16.205],
	B5921:[10,1.625,13.44],
	B5922:[9,2,24.43],
	B5923:[9,2,16.73],
	B5983:[8,2,18.095],
	B5984:[8,2,18.095],
	B5985:[8,2,18.095],
	B5986:[3,1.875,22.225],
	B5987:[3,2.125,22.785],
	B5988:[14,2.25,18.095],
	B6101:[20,2.5,18.725],
	B6101G:[20,2.5,18.725],
	B6102:[20,2.5,18.725],
	B6102G:[20,2.5,18.725],
	B6103:[20,2.5,19.6],
	B6103G:[20,2.5,20.16],
	B6103S:[20,2.5,20.16],
	B6720:[16,2.75,18.095],
	B6721:[16,2.75,18.095],
	B6722:[17,2.5,18.655],
	B6723:[17,2.5,18.655],
	B6727:[23,2.75,18.095],
	B6732:[18,2.5,15.925],
	B6733:[18,2.5,15.925],
	B6734:[18,2.5,15.925],
	B6735:[1,2.5,15.925],
	B6904:[6,2.5,19.215],
	B6905:[6,2.5,19.215],
	B6906:[25,2.625,21.42],
	B6911:[9,2.5,21.665],
	B6912:[2,2.5,32.935],
	B6914:[8,2.5,22.225],
	B6915:[8,2.5,22.225],
	B6917:[15,2.5,17.85],
	B6918:[4,2.5,18.655],
	B6919:[4,2.5,18.655],
	B761:[11,3.625,24.43],
	B762:[11,3.625,24.43],
	B7720:[17,3,21.42],
	B7721:[17,3,21.42],
	B7722:[17,3,21.42],
	B7737:[23,3.75,23.59],
	B7751:[27,3,23.87],
	B7753:[22,2.875,18.655],
	B7760:[27,3,21.14],
	B7761:[27,3,21.14],
	B7762:[27,3,21.14],
	B7777:[10,3,18.935],
	B7778:[9,3,21.42],
	B7783:[8,3,27.16],
	B7784:[8,3,27.16],
	B7785:[8,3,27.16],
	B7786:[15,3.25,20.3],
	B7787:[15,3.25,20.3],
	B7788:[15,3.25,20.3],
	B7789:[5,3.125,19.495],
	B7790:[5,3.125,19.495],
	B7791:[5,3.125,19.495],
	B7792:[4,3.875,25.235],
	B7793:[4,3.875,25.235],
	B7794:[2,3,17.85],
	B8720:[16,3.75,26.6],
	B8721:[16,3.75,26.6],
	B8727:[18,3.375,23.59],
	B8728:[18,3.375,23.59],
	B8729:[18,3.375,23.59],
	B8730:[23,3.75,26.6],
	B8731:[14,4,26.355],
	B8732:[14,4,26.355],
	B8733:[14,4,26.355],
	B8734:[23,4.125,25.235],
	B8746:[7,4,27.16],
	B8747:[7,4,26.6],
	B8748:[7,4,26.6],
	B8749:[7,3.5,30.17],
	B8750:[9,4,22.785],
	B8751:[14,3.5,23.87],
	B8752:[2,4,30.45],
	B8753:[2,4,30.45],
	B8754:[3,4.625,33.74],
	B9700:[11,5.25,32.655],
	B9701:[11,5.25,32.655],
	B9702:[12,5.25,32.655],
	B9703:[12,5.25,32.655],
	B9704:[1,4.625,32.655],
	B9705:[1,4.625,32.655],
	B9708:[3,4.25,31.815],
	B9709:[5,4.25,30.45],
	B9710:[5,4.25,30.45],
	B9711:[6,4.375,26.355],
	B9712:[6,4.375,26.355],
	M5207:[26,1.75,16.205],
	M5208:[26,1.75,16.205],
	M5217:[26,1.75,19.495],
	R410:[44,1.25,4.13],
	R419:[44,1.25,4.13],
	R435:[44,1.25,4.13],
	R451:[53,1.25,4.13],
	R452:[53,1.25,4.13],
	R453:[53,1.375,5.495],
	R454:[53,1.375,5.495],
	R455:[53,1.25,6.3],
	R504:[35,2,7.42],
	R505:[35,2,7.42],
	R506:[53,2,7.42],
	R509:[53,2,7.14],
	R510:[53,2,7.14],
	R511:[48,2,7.42],
	R512:[48,2,7.42],
	R513:[46,2,8.505],
	R514:[46,2,8.505],
	R538:[35,2,7.42],
	R590:[39,2,6.3],
	R5900:[41,2.5,10.43],
	R591:[39,2,6.3],
	R5910:[41,2.5,10.43],
	R592:[39,2,6.3],
	R5920:[42,2.625,10.43],
	R593:[39,2,6.3],
	R5930:[41,2.125,9.87],
	R594:[44,1.75,5.775],
	R5940:[41,2.125,9.87],
	R595:[44,1.75,5.775],
	R5950:[41,1.625,8.225],
	R596:[44,1.75,5.775],
	R5960:[41,1.625,8.225],
	R597:[44,1.75,5.775],
	R5970:[41,1.625,8.225],
	R598:[44,1.75,5.775],
	R5980:[41,1.625,8.225],
	R599:[44,1.75,5.775],
	R602:[31,2.25,7.42],
	R603:[31,2.25,7.42],
	R654:[36,2.375,13.16],
	R655:[36,2.375,13.16],
	R656:[36,2.375,13.16],
	R657:[42,2.75,12.635],
	R658:[42,2.75,12.635],
	R670:[39,2.5,8.505],
	R671:[39,2.5,8.505],
	R672:[38,2.5,10.71],
	R673:[38,2.5,10.71],
	R674:[38,2.5,10.71],
	R675:[38,2.75,8.505],
	R676:[38,2.75,8.505],
	R677:[38,2.75,8.505],
	R678:[29,2.5,10.99],
	R679:[29,2.5,10.99],
	R680:[29,2.5,10.99],
	R681:[29,2.5,10.99],
	R682:[29,2.5,10.99],
	R683:[48,2.75,12.88],
	R684:[48,2.75,12.88],
	R685:[48,2.75,12.88],
	R686:[48,2.5,13.72],
	R687:[48,2.5,13.72],
	R688:[46,2.25,8.505],
	R689:[46,2.25,8.505],
	R690:[46,2.25,8.505],
	R691:[46,2.25,8.505],
	R692:[46,2.25,8.505],
	R693:[54,2.5,20.72],
	R694:[54,2.5,20.72],
	R695:[54,2.5,20.72],
	R750:[42,3,12.635],
	R751:[42,3,12.635],
	R752:[42,3,12.635],
	R753:[31,3.25,19.495],
	R754:[31,3.25,19.495],
	R755:[30,3.625,21.14],
	R756:[30,3.625,21.14],
	R757:[30,3.625,21.14],
	R758:[30,3.625,21.14],
	R761:[29,3.625,21.14],
	R762:[29,3.625,21.14],
	R763:[54,3.5,21.14],
	R764:[54,3.5,21.14],
	R765:[54,3.5,21.14],
	R766:[49,3,12.88],
	R767:[49,3,12.88],
	R768:[49,3,12.88],
	R769:[49,3,12.88],
	R770:[49,2.875,11.795],
	R771:[49,2.875,11.795],
	R772:[49,2.875,11.795],
	R773:[47,3,17.29],
	R774:[47,3,17.29],
	R775:[47,3,17.29],
	R818:[43,4,21.14],
	R819:[43,4,21.14],
	R820:[43,3.75,17.57],
	R821:[43,3.75,17.57],
	R822:[43,3.75,17.57],
	R830:[40,4,12.635],
	R831:[40,4,12.635],
	R832:[40,4,12.635],
	R833:[40,4,12.635],
	R834:[40,4,12.635],
	R835:[40,4,12.635],
	R836:[40,4,12.635],
	R838:[34,3.75,17.85],
	R839:[32,4.25,36.505],
	R840:[32,4.25,36.505],
	R841:[33,4.25,15.365],
	R843:[33,4.75,19.74],
	R844:[33,4.75,19.74],
	R845:[32,4.25,23.31],
	R846:[37,4.625,23.065],
	R847:[37,4.625,23.065],
	R848:[37,4.625,23.065],
	R849:[37,4.625,23.065],
	R853:[52,4.625,25.795],
	R854:[52,4.625,25.795],
	R855:[52,4.625,25.795],
	R856:[52,4.625,25.795],
	R857:[51,4.75,18.655],
	R858:[51,4.75,18.655],
	R859:[51,4.75,18.655],
	R860:[51,4.75,18.655],
	R861:[51,4.75,18.655],
	R862:[50,4.25,16.73],
	R863:[50,4.25,16.73],
	R864:[50,4.25,16.73],
	R865:[50,4.25,16.73],
	R866:[47,4,26.88],
	R867:[47,4,26.88],
	R868:[47,4,26.88],
	R869:[45,4,14.28],
	R870:[45,4,14.28],
	R871:[45,4.25,13.72],
	R900:[34,5.125,23.87],
	R901:[34,5.125,31.57],
	R902:[35,5.125,27.72],
	R903:[35,5.125,27.72],
	R904:[36,5.5,27.72],
	R905:[36,5.5,27.72],
	SB9999:[13,1.625,6.3]
}