$(document).ready(function(){
	
	/* disable enter key */
	document.onkeypress = function(e){
		var e = (e) ? e : ((event) ? event : null);
  	var node = (e.target) ? e.target : ((e.srcElement) ? e.srcElement : null);
  	if ((e.keyCode == 13) && (node.type=="text") && (node.id == "cc"))  
  	{ 
  		$("#change").click();
  		return false;
  	} else if((e.keyCode == 13) && (node.type=="text")){ return false }
	}
	
	$("[name=shipping]:first").attr("checked","checked");
	
	$("[name=couponupdate]").click(function(){
		
			document.cartform.submit();
			return false;
		
	});
	
	function addCommas(nStr)
	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
	
	function updatetotal(){
		
		var total = 0;
		var weight = 0;
		var discount = 0;
		var h = 'S';
		
		$("[name^=qty_]").each(function(){
			
			var qty = parseInt($(this).val());
			var price = parseFloat($(this).nextAll("[name^=pr_]").val() * qty);
			
			weight += parseFloat($(this).nextAll("[name^=w_]").val()) * qty;
			total += price;
		});
		
		$("[name=weight]").val(weight);
		ship = $("#shiptotal").html().replace(",","").substr(1);
		
		if(weight < 1.05 && total < 99 && ($("#country :selected").val() == "United States"))
		{
			
			$("#i-00").parent().css('display','block');
			$("#i-00").removeAttr("disabled");
		
		}else {
			
			$("#i-00").attr("disabled", "disabled").parent().css('display','none');
		}
		
		if(ship == "BD")
			ship = 0.00;
		
		if($("#coupontotal").size())
		{
			if($("[name=coupontype]").size())
			{
				
				poff = parseFloat($("[name=coupontype]").val() / 100);
				discount = parseFloat((total * 1) * (poff * 1) * -1).toFixed(2);
				$("#coupontotal").html("$" + discount);
				
			}	else {
			
				discount = $("#coupontotal").html().substr(1);
		
			}
		}
		total = (total * 1) + (ship * 1) + (discount * 1);
		$("#total").html(addCommas(parseFloat(total).toFixed(2)));
		
	}
	
	$("#postal").keyup(function(){
		
		var h = 'S';
		
		$("[name^=h_]").each(function(){
			
			if($(this).val() == 'H')
				h = 'H';
			
		});
		if(($(this).val().length > 2) && ($("#country :selected").val() == "United States"))
		{
			switch($(this).val().substr(0, 3))
			{
				case '967':
				case '968':
				case '995':
				case '996':
				case '997':
				case '998':
				case '999':
					if(h == 'H')
					{
						$("#i-99").attr("disabled", "disabled").parent().css('display','none');
						$("[name=shipping]").each(function(){
							
							if(!$(this).attr("disabled"))
							{
								$(this).attr("checked", "checked").click();
								return false;
							}
							
						});
					} else {
						$("#i-99").parent().css('display','block');
						$("#i-99").removeAttr("disabled");
					}
					//$("[name=shipping]:enable:first").attr("checked","checked");
					break;
				default:
					$("#i-99").parent().css('display','block');
					$("#i-99").removeAttr("disabled");
				break;
			}
			
		}
		
		
		if(($(this).val().length > 4) && ($("#country :selected").val() == "United States"))
		{
			
			$("#movythingy").append("<img id='loader' src='/_/loader.gif'>");
			
			var zip = $(this).val();
			var weight = $("[name=weight]").val();
			$.ajaxSetup({ cache: false });
			$.getJSON("/services/ups_ship.php?zip=" + zip + "&weight=" + weight, function(data){
				
				for(var code in data){
					
					if($("#id-" + code).length == 1)
					{
						$("#id-" + code).html("-- $" + parseFloat(data[code]['00']).toFixed(2)).css({'font-size':'10pt','font-style':'normal'});
						$("#i-" + code).val(parseFloat(data[code]['00']).toFixed(2));
						$("#i-" + code).parent("li").css('display', 'block');
					}	
				}
				
				$("[id^=i-]").each(function(){
				
					code = $(this).attr("id").substr(2);
					if(code == '99' || code == '00' || code == '98')
						return;
					
					if(data[code] == null)
						$(this).parent("li").css('display','none');
						
					
				});
			
				$("[name=shipping]").each(function(){
					
					if($(this).attr("checked") == true)
						$(this).click();
					
				})
			
				updatetotal();
			
				$("#loader").remove();
			});
				
		}
		
	});
	
	$("[name=shipping]").click(function(){
		
		if(isNaN(parseFloat($(this).val())))
		{
			$("#shiptype").val($(this).attr("id").substr(2));
			$("#shiptotal").html('TBD');
		}else{
			$("#shiptype").val($(this).attr("id").substr(2));
			$("#shiptotal").html("$" + $(this).val());
			updatetotal();
		}
	});
	
	$("#country").change(function(){
		
		updatetotal();
		if($(this).val() == "United States")
		{
			
			$("#intl").css({"display":"none"});
			$("[name=shipping]").attr("disabled", "");
			$("[name=shipping]").next().css("color","black");
			$("[name=shipping]:first").attr("checked","checked").click();
			$("#postal").keyup();
			updatetotal();
			
		} else {
			
			$("#intl").css({"display":"inline"});
			$("[name=shipping]").attr("disabled", "disabled");
			$("[name=shipping]").next().css("color","gray");
			$("#i-98").attr({"checked":"checked","disabled":""}).next().css("color","black");
			
			var weight = $("[name=weight]").val();
			if(weight < 2.1){
			
					$("#shiptotal").html('$15.00');
					$("#s-98").html('15.00');
					$("#shiptype").val('98');
			
			} else {
				
					$("#shiptotal").html("$" + addCommas(parseFloat((weight * 3.36) + (14.00 * 1)).toFixed(2)));
					$("#s-98").html(addCommas(parseFloat((weight * 3.36) + (14.00 * 1)).toFixed(2)));
					$("#shiptype").val('98');
			}
			updatetotal();
			
		}
		
	});
	
	
	$("[name=change]").click(function(){
		
		code = $(this).nextAll("[name^=qty_]").attr("name").substr(4);
		
		if($("[name=q_" + code + "]").length)
		{
			var qty = $("[name=q_" + code + "]").val();
			$("#qty_" + code).html(qty)
			
			$("#qty_" + code).load("/services/setQty.php", {"code":code, "qty":qty }, function(){
				
				
				var qty = $("#qty_" + code).html();
				
				if(qty == '0')
					window.location.reload();
				
				$("[name=qty_" + code + "]").val(qty);
				
				var price = parseFloat($(this).nextAll("[name=pr_" + code + "]").val());
				
				$("#line_" + code).html(addCommas(parseFloat(price * qty).toFixed(2)));
				$("#qty_" + code).nextAll("[name=change]").html("Change");
				
				if($("#country :selected").val() == "United States")
				{
					updatetotal();
					$("#postal").keyup();
					
				} else {
						
					updatetotal();
					$("#country").change();
				
				}
				
				updatetotal();
				
			});
						
		} else {
			
			$("#qty_" + code).empty().append("<input type='text' maxlength=2 style='width:20px;text-align:center' name='q_" + code + "'>");
			$(this).html("Update");
		}
		return false;
		
	});
	
	$("#country").change();
	
	$("[name=shipping]").each(function(){
		if($(this).parent().css('display') != 'none' && ($("#country :selected").val() == "United States"))
		{
			$(this).attr("checked", "checked").click();
			updatetotal();
			return false;
		}
				
	});
	
});