function get_ship_opts(){};

$(function() {

	$(".view_cart_btn").click(function(){
		window.location = "https://www.thermosoft.com/cart/checkout/";
	});

	$(".price_list tbody tr:even").addClass("odd");

	$(".price_list :text").keyup(function(e){
		if (e.which == 40)
			this.value = parseInt(this.value) - 1 || 0;
		else if (e.which == 38)
			this.value = parseInt(this.value) + 1 || 0;
		
		if (parseInt(this.value) > 0) {
			$(this).closest("tr,li").addClass("selected");
		}
		else {
			$(this).closest("tr,li").removeClass("selected");
		}
		
		if (this.value.match(/^\d+$/) && parseInt(this.value) >= 0 && this.value != "") {
			var $table = $(this).parents("table:eq(0)");
			var i = -1;
			var relIndxs = {};		// index in jquery object : relative index
			var $calc_row = $("tfoot tr.calcs > *", $table).filter(function(indx){
				if (this.colSpan != 1) {
					i += this.colSpan;
					return false;
				}
				else if ($(this).attr("class").indexOf("calc") == -1) {
					i++;
					return false;
				}
				else {
					i++;
					relIndxs[indx-1] = i;
					return true;
				}
			});
			
			$calc_row.each(function(indx0){
				var $field = this;
				var num = 0;
				if ($($field).attr("class").indexOf("qty_sum") != -1) {
					$("tbody tr", $table).each(function(indx1){
						num += parseFloat(/[-+]?\d*\.?\d+/.exec($("td:eq(" + relIndxs[indx0] + ")", this).textChildren(0).replace(",", "") || 0)[0]) * $(":input", this).val();
					});
				}
				else if ($($field).attr("class").indexOf("_sum") != -1) {
					$("tbody tr", $table).each(function(indx1){
						num += parseFloat(/[-+]?\d*\.?\d+/.exec($("td:eq(" + relIndxs[indx0] + ") :text", this).val() || 0)[0])	// dirty;
					});
				}
				else if ($($field).attr("class").indexOf("qty_avg") != -1) {
					// this is technically not a proper qty avg...need to rename in future
					num = $("tr.calcs th.disc_total span", $table).text().replace(",","") / $("tr.calcs th:eq(1) span", $table).text().replace(",","");
/*					$("tbody tr", $table).each(function(indx1){
						var qty = $(":text", this).val();
						num += parseFloat(/[-+]?\d*\.?\d+/.exec($("td:eq(" + relIndxs[indx0] + ")", this).text() || 0)[0])	// dirty;
					});
*/				}

				var decplcs = $($field).attr("class").substr(-1,1);	// bad
				$("span",$field).text(number_format(num, decplcs));
			});
		}
		else {
			this.value = "";
		}
		
		$("#cart_set").addClass("update_queue");
	});
	
	$("#disc_overrides :text").keyup(function(){
		$("#cart_set").addClass("update_queue");
	});
	
//	$("#disc_overrides :checkbox").click(function(){
//		$("#cart_set").addClass("update_queue");
//	});
	
	$("table.price_list :text").keyup();
	$("#cart_set").removeClass("update_queue");
	
	$('#btn_checkout').live("click", function() {
		window.location = 'https://www.thermosoft.com/cart/checkout/';
	});
});