// flow dough cart additions
// nick poulos 7/1/09


function update_payment_method(product_number, session_val) {

	//var checkbox_id=$(this).attr("id").toLowerCase();
	//var product_number=checkbox_id.slice(checkbox_id.lastIndexOf("-") + 1);
	//var session_val = $(this).val();

	$.post("scripts/php/update_product_payment_method.php", { session_id:session_val, product_id:product_number, apply_flowdough:1 }, function(return_data) {
		//alert(return_data);
		if (return_data.indexOf("update-payment:success") > -1) {
	
			$("#products-flowdough").append('<tr id="product-' + product_number + '">' + $("#products-standard #product-" + product_number).html() + '</tr>');
			$("#products-standard #product-" + product_number).remove();
			
			var standard_products_remaining=$("#products-standard tr[id*='product-']").length;
			
			if (standard_products_remaining==0)
				$("#products-standard").append('<tr id="no-standard-products"><td colspan="6" align="center" style="padding-top: 5px; padding-bottom: 5px;" class="cartItems">All Products Are Now Being Ordered Using Flow Dough As Payment.</td></tr>');
			
			$("#products-flowdough input[id*='chk-apply-']").attr("checked", "checked");																					
		
		}
																							
	});

}

function update_quantity(new_quantity, product_ID) {
	
	$.post("scripts/php/update_quantity.php", {quantity:new_quantity, product_id:product_ID}, function (return_data) {
		
		if (return_data.indexOf("update-quantity:success") > -1) {
		
		} else {
			
		}
		
		/*	update-quantity:success		*/
																															
	});
	
	
}

//=========================================================================================================
//										Assign Functions to DOM											  =
//=========================================================================================================

$(document).ready(function() {

	$(".flowdough-apply").live("click", function() {
		
		var checkbox_id=$(this).attr("id").toLowerCase();
		var product_number=checkbox_id.slice(checkbox_id.lastIndexOf("-") + 1);
		var session_val = $(this).val();
		var current_quantity = $("#txt_quantity-" + product_number).val();
		
		tb_open_new("flowdough_order.php?sessionid=" + session_val + "&productid=" + product_number + "&quantity=" + current_quantity + "&TB_iframe=true&height=200&width=450&modal=true&");

	});
	
	$("#cart-product-list input[id*='txt_quantity-']").live("keyup", function() { 
		
		var textbox_id=$(this).attr("id").toLowerCase();
		var product_id=textbox_id.slice(textbox_id.lastIndexOf("-") + 1);
		var new_quantity=$("#txt_quantity-" + product_id).val();
		
		update_quantity(new_quantity, product_id);
	
		
	});
	
	$("#cart-flowdough-list input[id*='txt_quantity-']").live("keyup", function() { 
		
		var textbox_id=$(this).attr("id").toLowerCase();
		var product_id=textbox_id.slice(textbox_id.lastIndexOf("-") + 1);
		var new_quantity=$(this).val();
		
		$.post("scripts/php/update_quantity_flowdough.php", { product:product_id, quantity:new_quantity	}, function(return_data) {
				
				if (return_data.length > 150) {
					$("#flowdough-replacer").empty().html(return_data);	
				}
					
																		 
																		 
		});
		
		//update_flowdough_quantity(new_quantity, product_id);
	
		
	});
	
	$("#button-next-1").live("click", function() {	window.location="main.php?page=cart&step=2";	});
	
	$("#button-next-2").live("click", function() {
		
		$("input[id*='txt_quantity-']").each(function() {
			
			var textbox=$(this).attr("id").toLowerCase();
			var product=textbox.slice(textbox.lastIndexOf("-") + 1);
			var quantity_flowdough=$(this).val();
			var quantity_ordered=parseInt($("#ordered-" + product).text());
			if (quantity_flowdough <= quantity_ordered) {
				
				var new_quantity = quantity_ordered - quantity_flowdough;
				
				$.post("scripts/php/update_quantity_step2.php", { product_id:product, quantity:new_quantity }, function (return_data) {
				;	
					if (return_data.indexOf("update-quantity:success") > -1) {
						window.location="main.php?page=cart&step=3";	
					} else {
						$("#error-message").text("So sorry...there was an error while updating your cart! Please try again.").fadeIn("slow");
						// set the status message to fade out after 4 seconds, and clear it for next time.
						setTimeout(function() { $("#error-message").fadeOut("slow", function() { $("#error-message").text(''); });	}, 4000);
					}
																														 
																														 
				});
				
			}
				
			
		});
		
			
		
	});		
	
	$("#button-next-2b").live("click", function() {	window.location="main.php?page=cart&step=3";	});	


	$("#button-next-3").live("click", function() {	
	
		var c_title = $("#opt_title").val();
		var c_first = $("#txt_first").val();
		var c_last = $("#txt_last").val();
		var c_address = $("#txt_address").val();
		var c_city = $("#txt_city").val();
		var c_state = $("#lst_state").val();
		var c_zip = $("#txt_zip").val();
		var c_phone = $("#txt_phone").val();
		var c_email = $("#txt_email").val();
		var c_practice = $("#txt_practice_name").val();
		var c_dealer = $("#txt_dealer_name").val();
		var c_dealer_city = $("#txt_dealer_city").val();
		var c_dealer_state = $("#lst_dealer_state").val();

		$.post("scripts/php/step4.php", 
			   {
				
				title:c_title,
				first:c_first,
				last:c_last,
				address:c_address,
				city:c_city,
				state:c_state,
				zip:c_zip,
				phone:c_phone,
				email:c_email,
				practice:c_practice,
				dealer:c_dealer,
				dealer_city:c_dealer_city,
				dealer_state:c_dealer_state
				}, 
		
		
		function(return_data) {												   
			if (return_data.indexOf("shipping:success") > -1) {
				window.location="main.php?page=cart&step=4";	
			} else {
				$("#account-status").empty().html(return_data);
			}															   
		});

	
	});						   
	
	$("#button-next-4").live("click", function() {	
		//alert("button4");
		var flowdough_remaining=$("#flowdough-remaining").text();
		$.post("scripts/php/place-order.php", { remaining:flowdough_remaining }, function(return_data) {
		//alert(return_data);
			if (return_data.indexOf("order:success") > -1) {
				window.location="main.php?page=placeordersuccess";	
			} else {
				window.location="main.php?page=placeordererror";				
			}													  													  
		});
	});						   
});
