$(document).ready(function(){
	updateCart();

	
	$("#add_to_cart").click(function(){
		
		var curID = $("div.prodid").attr('id');
		$.get("/core/addcart.php?id="+curID.toString());
		updateCart();
		$(".cart_field").fadeOut('medium');
		$(".cart_field").fadeIn('medium');
	});
		
	
	function updateCart(){
		$.get("/core/updatecart.php", function(incart){
			$("#updatecart").html(incart+" produkter");
			
		});
	}
	
	
	
	
});

function removeCart(id){
	$.get("/core/removeCart.php?id="+id,function(removeres){
		if(removeres == "1"){
			window.location.reload();
		} else {
			alert(removeres); }
	});
}

function updCartq(id){
	var a = $("#upd_"+id).val();
	$.get("/core/updateCartq.php?id="+id+"&a="+a,function(res){
		if(res == "1"){
			window.location.reload();
		} else {
			alert(res);
		}
	});
}

function ProceedCheckout(){
	
	var Checker = true;
		
		if($("#name").val() == ""){
			$("#name").css('background-color','#f2b5b5');
			Checker = false;
		}
		
		if($("#address").val() == ""){
			$("#address").css('background-color','#f2b5b5'); Checker = false;
		}
		
		
		if($("#email").val() == ""){
			$("#email").css('background-color','#f2b5b5'); Checker = false;
		}
		
		if($("#email2").val() == ""){
			$("#email2").css('background-color','#f2b5b5'); Checker = false;
		}
		
		if($("#zip").val() == ""){
			$("#zip").css('background-color','#f2b5b5'); Checker = false;
		}
		
		if($("#city").val() == ""){
			$("#city").css('background-color','#f2b5b5'); Checker = false;
		}
		
		if($("#country").val() == ""){
			$("#country").css('background-color','#f2b5b5'); Checker = false;
		}
		
		if($("#phone").val() == ""){
			$("#phone").css('background-color','#f2b5b5'); Checker = false;
		}
		
		
		if(Checker == true){
			
			var ProceedURL = 
			'?ordermd5='+$("#ordermd5").val()+
			'&ordertime='+$("#ordertime").val()+
			'&name='+$("#name").val()+
			'&address='+$("#address").val()+
			'&email='+$("#email").val()+
			'&zip='+$("#zip").val()+
			'&city='+$("#city").val()+
			'&phone='+$("#phone").val()+
			'&country='+$("#country").val();
			
			$.get('core/ProceedCheckout.php'+ProceedURL,function(response){
				if(response == "1"){
					$("#PCbtn").attr("disable","disabled");
					$(".checkout_1 input").each(function(){
						$(this).attr('disabled', true);
					});
					$(".checkout_1").hide();
					$("#KortaFrame").show();
					return true;
				} else { alert(response); }
			});
			
			
			
			
				
		} else {
			$("#KortaFrame").hide();
			return false; 
		}
		
		
}

function changeLang(id){
	$.get("/core/changeLang.php?lid="+id,function(changeres){
		if(changeres == "1"){
			window.location.reload();
		} else {
			alert(changeres);
		}
	});
}

function emptyCart(){
	$.get("/core/emptyCart.php",function(res){
		if(res == "1"){
			window.location.reload();	
		} else {
			alert(res);
		}
	});
}

