
$(document).ready(function() {
	
	//used on cart/checkout for shipping method dropdown
	if($.browser.msie) {
		$(".c-s-wrap select").focus(
			function() { $('.c-s-wrap select').css({width: 380}); }
		);
		$(".c-s-wrap select").blur(
			function() { $('.c-s-wrap select').css({width: 200}); }
		);
	}
	
	// Make a very pretty searchbox, only on Safari.
	if(!$.browser.safari)
		$("#searchbox")
			.val("keywords or part#")
			.focus( function() { 
				if($(this).val() == "keywords or part#") $(this).val(""); 
			})
			.blur( function() { 
				if($(this).val() == "") $(this).val("keywords or part#") 
			});

	// $("#product_detail_tab").tabs();
	
	$("form#feedbackform").submit( function() {
		if ($(".required[value='']").length > 0) { 
			alert("Please fill in all required fields.");
			return false;
		 };
		return true;
	});

	$("button.c-update").hide();
	
	$(".cart-products-list input[name^=qty]").change( function() {
		if($(this).val() <= 0 || $(this).val() == "") $(this).val(1);
		if($(this).val() >= 100) $(this).val(99);
		cart_ajax({ "qty[0]": $(this).val(), "idserial[0]": this.id });
	});

	$(".cart-products-list input[name^=qty]").keyup( function() {
		if($(this).val() != 0 && $(this).val() != "" && $(this).val() < 100) cart_ajax({ "qty[0]": $(this).val(), "idserial[0]": this.id });
	});

	$(".cart #zip").keyup(function() {
		if($(".cart #zip").val().match(/^\d{5}$/) == null ) blank_totals();
		else cart_ajax({ z: $(".cart #zip").val() });
	});


	$(".cart #method").change(function() {
		cart_ajax({ m: $(this).val() });
	});

	$("form.checkout input[name=m]").change(function() {
		cart_ajax({ m: $("form.checkout input[name=m]:checked").val() });
	});
	
	// Same as onclick=, only better!
	// $("table.products tr").click( function(event) { if(event.target.nodeName != "BUTTON") location.href = $(this).find("a").attr("href") });
	
	$("form#addtocart, form.addtocart").submit(function() {
		if (tekcust == null && $(this).children("input[name=fw]").val() == 1) {
			$.get("/store/notice.php?fb=1&idserial=" + $(this).children("input[name^=idserial]").val() + "&add=" + $(this).find("input[name^=add]").val(), 
				function(data) {
					$.facebox(data); 
					$(".buttons a#cancel").click( function() { $.facebox.close(); return false; });
				}
			);
			return false;
		}
		if ($(this).children("input[name=fw]").val() == 3) {
			$.get("/store/ns.php?fb=1", 
				function(data) {
					$.facebox(data); 
					$(".buttons a#cancel").click( function() { $.facebox.close(); return false; });
				}
			);
			return false;
		}
		else return true;
			
	}); 
	
	$("a.cvv").click(function(){
		$.get("/store/cvv.php?fb=1", 
			function(data) {
				$.facebox(data); 
				$(".buttons a#cancel").click( function() { $.facebox.close(); return false; });
			}
		);
		return false;
	});


});

function cart_ajax(data) {
	$.post("cart_ajax.php",	data, function(r,s) {
			if(s == "success") refresh_totals();
			else console.log("AJAX POST returned "+status);
		}
	);
}

function blank_totals() {
	$("#cart-totals-shipping").html("&mdash;");
	$("#cart-totals-tax").html("&mdash;");
	$("#cart-totals-grandtotal").html("&mdash;");
	$(".cart #method").html('<option value="0">(enter your zipcode)</option>');
}

function refresh_totals() {
	$.getJSON("ajax_cart_totals.php",null,function(r,s) {
		if(s == "success") {
			if(r.count == 1) $(".cart-total-item-copy").html("item");
			else $(".cart-total-item-copy").html("items");
			$(".cart-total-item-count").html(r.count);
			$("#cart-totals-subtotal").html(r.subtotal);
			$(".cart-total-item-subtotal").html(r.subtotal);
			if (r.shipping) $("#cart-totals-shipping").html(r.shipping);
			if (r.tax) $("#cart-totals-tax").html(r.tax);
			$("#cart-totals-grandtotal").html(r.grandtotal);
			
			// Shipping methods

			$(".cart #method").html(r.methods);

			// Item Totals

			for (i in r.item_totals) $("#item_total_"+i).text(formatCurrency(r.item_totals[i]));
			
		} else alert("AJAX GET returned "+status);
		
	});
		
}


function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

