
$(document).ready(function(){
	
	$(".copyFields").click(function(){
				
				$("#shipping_name").attr("value",$("#name").attr("value"));
				$("#shipping_first_name").attr("value",$("#first_name").attr("value"));
				$("#shipping_last_name").attr("value",$("#last_name").attr("value"));
				$("#shipping_company").attr("value",$("#company").attr("value"));
				$("#shipping_address").attr("value",$("#address").attr("value"));
				$("#shipping_address_line_2").attr("value",$("#address_line_2").attr("value"));
				$("#shipping_city").attr("value",$("#city").attr("value"));
				$("#shipping_state").attr("value",$("#state").attr("value"));
				$("#shipping_zip").attr("value",$("#zip").attr("value"));
				$("#shipping_phone").attr("value",$("#phone").attr("value"));
				return false;
			});
	
	
	function get_pricing()
	{
		var _textual = "";
		
		$(".product").each(function(){
			
			if($(this).attr("value") != "" && $(this).attr("value") != 0)
			{
				_textual += $(this).attr("name") + "=" + $(this).attr("value")+"&";
			} else {
				_textual += $(this).attr("name") + "=" +"&";
			}
			
		});
		
		if(_textual == "")
		{
			return;
		}
		
		
		_textual += "disc_code="+$("#disc_code").attr("value")+"&";
		
		var _discCode = $("#disc_code").attr("value");
		
		if($("#shipping_to").attr("value") != "")
		{
			_textual += "shipping_to="+$("#shipping_to").attr("value")+"&";
		} else {
			_textual += "shipping_to=null&";
		}
		
		_textual += "shipping_type=1&"; // Modify for multiple shipping types.
		
		/*
		if($("#club_member").attr("checked"))
		{
			_textual += "club_member=" + $("#club_member").attr("value");
		} else {
			_textual += "club_member=";
		}
		*/
		
		if($("#club_member").attr("value"))
		{
			_textual += "club_member=" + $("#club_member").attr("value");
		} else {
			_textual += "club_member=";
		}
		
		$.ajax({
			type: "POST",
			url: "/wine_store/handler",
			data: _textual,
			success: function(ret){
				
				if($("clear_pricing", ret).text() == "1")
				{
					$("#subtotal").text( "--" );
					$("#tax").text( "--" );
					$("#shipping").text( "--" );
					$("#grandTotal").text( "--" );
					$("#discount").parent().hide();
				} else {					
					$("#subtotal").text( "$" + $("subtotal", ret).text() );
					
					val = $(".preorderInfo #shipping_to").val();
					for(i=0; i<localTaxStates.length; i++) {
						if(val == localTaxStates[i]) {
							$("#tax").text("**");
							break;
						}
						$("#tax").text( "$" + $("tax", ret).text() );
					}
					$("#shipping").text( "$" + $("shipping", ret).text() );
					$("#grandTotal").text(  "$" + $("grandTotal", ret).text() );
										
					if( $("discount", ret).text() != "0.00" )
					{
						var type = $("discountUsed", ret).text();
						var typeU = type;
						type = type.toLowerCase();
						
						if(type.indexOf('vinsidercase') != -1 || type.indexOf('club') != -1)
						{
							$("#discount_label").text("VINsider Case Discount: ");
						} else if(type.indexOf('vinsider') != -1 || type.indexOf('club') != -1) {
							$("#discount_label").text("VINsider Discount: ");
						} else if(type.indexOf('vindependentcase') != -1 || type.indexOf('club') != -1) {
							$("#discount_label").text("VINdependent Case Discount: ");
						} else if(type.indexOf('vindependent') != -1 || type.indexOf('club') != -1) {
							$("#discount_label").text("VINdependent Discount: ");
						} else if(type.indexOf('case') != -1) {
							$("#discount_label").text("Case Discount: ");
						} else {
							$("#discount_label").text("Discount: ");
						}

						$("#discount").text( "($" + $("discount", ret).text() + ")" ).parent().show();

					} else {
						$("#discount").parent().hide();
					}

					if( $("discountCodeValid", ret).text() == "true" )
					{
						$("#discCodeValid").html("<strong style=\"color:#5F9E3C\">(Valid)</strong>").parent().show("normal");
					} else {
						if(_discCode.length > 3)
						{
							$("#discCodeValid").html("<strong style=\"color:#FF0000\">(Not Valid)</strong>").parent().show("normal");
						} else {

							$("#discCodeValid").html("&nbsp;").parent().hide("normal");
						}
					}
					
					if( $("items_removed", ret).text() == "Yes")
					{
						$(".message").text("There were restrictions on some of the wines in your cart, and some of them were removed.").slideDown();
						
					} else {
						$(".message").slideUp();
					}
				}
				
				
				
				
			}, error: function (req, textStatus, errorThrown) {
				
				//alert("ERROR: "+ req.readyState + " " + errorThrown);
			}
			
			});
		
	}
	
	
	val = $(".preorderInfo #shipping_to").val();
	for(i=0; i<localTaxStates.length; i++) {
		if(val == localTaxStates[i]) {
			$("#tax").text("**");
			break;
		}
	}
	
	$(".product").keyup(function(e){
		
		if(e.keyCode != 9)
		{
			get_pricing();
		}
		
	});
	
	
	$("#is_gift").change(function(){

			if($(this).attr("checked") == true)
			{
				$("#gift_message").parent().slideDown();
			} else {
				$("#gift_message").parent().slideUp();
			}

		});
		
	$("#pickup_option").change(function(){

		if($(this).attr("checked") == true)
		{
			$(".ship_info").slideUp();
		} else {
			$(".ship_info").slideDown();
		}

	});
		
	$("#pickup_option").each(function(){

		if($(this).attr("checked") == true)
		{
			$(".ship_info").hide();
		}

	});
	
	
	
	$("#shipping_to").change(get_pricing);
	
	$("#shipping_type").change(get_pricing);
	
	$("#club_member").change(get_pricing);
	
	$("#disc_code").keyup(function(e){
		
		var _currValue = $(this).attr("value");
		
		if(_currValue.length > 3)
		{
			if(e.keyCode != 9)
			{
				get_pricing();
			}
		} else {
			
			$("#discCodeValid").html("&nbsp;").parent().hide("normal");
		}
		
		
		
	});
	
	get_pricing();
	
});
