$(document).ready(function() {
	$("#btn_isvalyti").click(function() {
		var lan = $("#lan").val();
		var msg = new Array();
		msg['lt'] = 'Ar tikrai norite išvalyti krepšelį?';
		msg['en'] = 'Do you really want to empty cart?';
		msg['ru'] = 'Дествительно ли вы хочете удалить все покупки?';
		msg['pl'] = '...?';
		if (confirm(msg[lan])) {
			location.assign('index.php?a=clear_cart');
//			$.get('index.php?a=clear_cart', null, function(data) {
//				if (data == 'OK') {
//					$("#cart_amount").html('Prekių krepšelyje: 0');
//					$("#cart_price").html('0.00 Lt');
//				}
//			});
		}
	});
	$("#btn_uzsakyti").click(function() {
		location.assign('index.php?a=show_cart');
	});

	$(".LeftMenuMain a.toggleg").each(function() {
		$(this).click(function(e) {
			$(this).next('div.Make').slideToggle("fast");
			e.preventDefault();
		});
	});
	$(".LeftMenuMain a.togglep").each(function() {
		$(this).click(function(e) {
			$(this).next().next().slideToggle("fast");
			e.preventDefault();
		});
	});
	if ($("#mid").val().length > 0) {
		$("#"+$("#mid").val()).parent().parent().slideDown("fast");
		$("#"+$("#mid").val()).parent().slideDown("fast");
		$("#"+$("#mid").val()).wrap("<span class=\"activelink\"></span>");
	}

	$("#fikrepseli #btn_add2cart").click(function(e) {
		var kiek = 0;
		$("#fikrepseli #kiekis").each(function() {
			if ($(this).val().length > 0)
				kiek++;
		});
		if (kiek == 0)
			e.preventDefault();
	});
	$("#fikrepseli #ppic").lightBox();

	$("#fkrepselis #btn_update").click(function(e) {
		var kiek = 0;
		$("#fkrepselis #kiekis").each(function() {
			if ($(this).val().length > 0)
				kiek++;
		});
		if (kiek == 0)
			e.preventDefault();
		else {
			$("#fkrepselis").attr('action', 'index.php?a=update_cart');
			$("#fkrepselis").submit();
		}
	});
	$("#fkrepselis #btn_submit").click(function(e) {
		var kiek = 0;
		$("#fkrepselis #kiekis").each(function() {
			if ($(this).val().length > 0)
				kiek++;
		});
		if (kiek == 0)
			e.preventDefault();
	});
	$("#fkrepselis #ppic").lightBox();

	$("#fuzsakymas #btn_empty").click(function() {
		var lan = $("#lan").val();
		var msg = new Array();
		msg['lt'] = 'Ar tikrai norite anuliuoti?';
		msg['en'] = 'Do You really want to empty cart?';
		msg['ru'] = 'Дествительно ли вы хочете удалить все покупки?';
		msg['pl'] = '...?';
		if (confirm(msg[lan]))
			location.assign('index.php?a=empty_cart');
	});
	$("#fuzsakymas #btn_finish").click(function(e) {
		var lan = $("#lan").val();
		var ok = true;
		var err = new Array();
		if ($("#fuzsakymas #pavadinimas").val().length == 0) {
			ok = false;
			err['lt'] = 'Įveskite vardą pavardę arba pavadinimą';
			err['en'] = 'Please enter Your name or Company Name';
			err['ru'] = 'Просим ввести ваше имя или имя компании';
			err['pl'] = '';
			alert(err[lan]);
			$("#fuzsakymas #pavadinimas").focus();
		}
		if (ok && $("#fuzsakymas #kodas").val().length == 0) {
			ok = false;
			err['lt'] = 'Įveskite kodą';
			err['en'] = 'Please enter code';
			err['ru'] = 'Просим ввести код';
			err['pl'] = '';
			alert(err[lan]);
			$("#fuzsakymas #kodas").focus();
		}
		if (ok && $("#fuzsakymas #tel").val().length == 0) {
			ok = false;
			err['lt'] = 'Įveskite telefoną';
			err['en'] = 'Please enter telephone number';
			err['ru'] = 'Просим ввести номер телефона';
			err['pl'] = '';
			alert(err[lan]);
			$("#fuzsakymas #tel").focus();
		}
		if (ok && $("#fuzsakymas #addr").val().length == 0) {
			ok = false;
			err['lt'] = 'Įveskite adresą';
			err['en'] = 'Please enter address';
			err['ru'] = 'Просим ввести адрес';
			err['pl'] = '';
			alert(err[lan]);
			$("#fuzsakymas #addr").focus();
		}
		if (ok && $("#fuzsakymas #email").val().length == 0) {
			ok = false;
			err['lt'] = 'Įveskite el.paštą';
			err['en'] = 'Please enter email';
			err['ru'] = 'Просим ввести адрес електронной почты';
			err['pl'] = '';
			alert(err[lan]);
			$("#fuzsakymas #email").focus();
		}
		if (ok) {
			$.post('index.php?a=finish_cart', $("#fuzsakymas").serialize(), function(data) {
				if (data == null || data == '') {
					data = 'error';
				}
				$("#form1").fadeOut("fast", function() {
					$("#form2").fadeOut("fast", function() {
						$("#form3").fadeOut("fast", function() {
							if (data != 'error') $.get('index.php?a=clear_cart', null, function(data) {
								if (data == 'OK') {
									var msg = new Array();
									msg['lt'] = 'Prekių krepšelyje: 0';
									msg['en'] = 'Cart amount: 0';
									msg['ru'] = 'Покупков в корзине: 0';
									msg['pl'] = '...: 0';
									$("#cart_amount").html(msg[lan]);
									$("#cart_price").html('0.00 Lt');
								}
							});
							$("#msg_"+data).fadeIn("slow");
						});
					});
				});
			});
		}
		e.preventDefault();
	});
	$("#search").focus(function() {
		var lan = $("#lan").val();
		var msg = new Array();
		msg['lt'] = 'Produkto paieška';
		msg['en'] = 'Product search';
		msg['ru'] = 'Поиск продукта';
		msg['pl'] = 'Product search PL';
		if ($("#search").val() == msg[lan])
			$("#search").val('');
	});
	$("#search").blur(function() {
		var lan = $("#lan").val();
		var msg = new Array();
		msg['lt'] = 'Produkto paieška';
		msg['en'] = 'Product search';
		msg['ru'] = 'Поиск продукта';
		msg['pl'] = 'Product search PL';
		if ($(this).val() == '')
			$(this).val(msg[lan]);
	});
});