$().ready(function(){

	
	
	$(".basket_orderform").css({'display':'none'});
	$(".form_book").click(function(){
			$(".basket_orderform").slideDown("slow");
	});
	
	function whiteInput(){
			$(".basket_orderform input").css({
				'background-color':'#FFF', 
				'color':'#000'});
	}
	
	function redInput(data){
			data.css({
				'background-color':'#FF0000', 
				'color':'#FFF'});
	}
	
	$(".form_send").click(function(){
			var nameLastname = $(".form_nameLastname");
			var address = $(".form_address");
			var phone = $(".form_phone");
			var email = $(".form_email");
			
			if(nameLastname.val() == false){
				redInput(nameLastname);
				
			} else if(address.val() == false){
				whiteInput();
				redInput(address);
				
			} else if(phone.val() == false){
				whiteInput();
				redInput(phone);
				
			} else if(email.val() == false){
				whiteInput();
				redInput(email);
				
			} else {
				$(".basketForm").submit();
			}
	});
	
	$(".formDelivery").change(function(){
			var delivery = $(this).val();
			//var allPrice = parseInt($('.all_price').html());
			var allPrice = $('.all_price').html();
			//alert($('.all_price').html());
			var deliveryPrice = $('.priceDelivery').html();
			if(delivery !== 'delivery_one' && deliveryPrice !== '0'){
				$('.priceDelivery').html('0');
				$('.all_price').html(allPrice-3.79);
			} else if (delivery == 'delivery_one' && deliveryPrice == '0') {
				$('.priceDelivery').html('3.79');
				$('.all_price').html(parseInt(allPrice)+3.79);
			}
	});
	
	$(".itemDelete img").click(function(){
			//alert($(this).parent().parent().find("input").val());
			var itemId = parseInt($(this).parent().parent().find(".itemId").html());
			var itemPrice = parseInt($(this).parent().parent().find(".itemPrice").html());
			var itemAllPrice = parseInt($(".all_price").html());
			var op = $(this).parents("tr.basketItem");
			
			$.post("/index.php", {basketDelete:itemId}, function(data){
				$("#basket .items_count").html(data);
				$(".all_price").html(itemAllPrice-itemPrice);
				op.remove();
			});
			
	});
	
	/*
	$(".basketItem .itemQuantity").each(function(){
		$(this).keypress(function(e){
			
			 
			//Checking valid input 
			var _val = Number((String.fromCharCode(e.keyCode)));
			if (isNaN(_val)) {
				e.preventDefault
				return false;  
			};
			
			var _parent = $(this).parent().parent();
			var _itemQty = $(_parent).find("input.itemQuantity").val();
			//price 
			var _itemPrice = $(_parent).find("input.itemHidden").val();
			
			$(_parent).find("td.itemPrice").text(_itemQty*_itemPrice);
		});
	});*/
	
	$("#inpCalc").click(function(){
		//alert("fuck");
		var _totalAmount = 0;
		var _del = parseInt($(".priceDelivery").html());
		$.post("/index.php", {basketDestroy:true});
		
		$("tr.basketItem").each(function(){	
			var _iD = parseInt($(this).find(".itemId").html());
			var _itemQty = parseInt($(this).find("input.itemQuantity").val()); 
			if (isNaN(Number(_itemQty))) {
				_itemQty = 1;
			}
			var _itemPrice = parseInt($(this).find("input.itemHidden").val()); 
			_totalAmount += _itemQty*_itemPrice; 
			$(this).find("td.itemPrice").text(_itemQty*_itemPrice);
			
			$.post("/index.php", {basketEditId:_iD, basketEditQnt:_itemQty}, function(data){
				$("#basket .items_count").html(data);
				
			});
			
		});
		
		$(".all_price").html(_totalAmount+_del);
		
	});
	
});

