/*global document, window, $, jQuery, tinyMCE, tinyMCEconfigs, TabsHelper, undefinedValue, stopPropagation, hideRow, showRow */
/*jslint strict: true, bitwise: true, eqeqeq: true, immed: true, newcap: true, nomen: true, onevar: true, plusplus: true, regexp: true, undef: true, white: true */

"use strict";

// msg
function hidemsg() {
    window.setTimeout(function () { $('#msg').fadeOut(); }, 6000);
}

function msg(txt) {
    $("#msg").fadeIn();
    $("#msg").html(txt);
    hidemsg();
}

// dodajemy produkty do koszyka
function AddToBasket(idp) {
	var ile = parseInt($('#ilosc_' + idp).val(), 10);
	if (isNaN(ile) || ile < 1) {
		ile = 1;
		$('#ilosc_' + idp).val(ile);
	}
	$.ajax({
		url: "/ajax.php",
		async: true,
		type: 'POST',
		data: "item=" + idp + "&ile=" + ile + "&act=AddToBasket",
		success: function (html) {
			msg('Dodano do zamówienia');
			$("#podm").html(html);
		}
	});
	// ptasznik dodal
	window.scroll(0,0);
}

// usuwamy z koszyka i reload
function RemoveFromBasket(idp) {
    $.ajax({
		url: "/ajax.php",
		async: true,
		type: 'POST',
		data: "item=" + idp + "&act=RemoveFromBasket",
		success: function (html) {
			$("#r" + idp).remove();
			msg('usunięto z koszyka');
			$("#podm").html(html);
			if (!$('ul.listing > li').length) {
				window.location.reload();
			}
		}
    });
}

function RecalcBasket() {
    $("#nn").val('recalc');
    document.forms.koszyk.submit();
}

function is_nip(value) {
	if (/^\d{10}$/.test(value)) {
		var arr = value.split(''),
			dlugosc = arr.length - 1,
			suma = 0,
			wagi = [6, 5, 7, 2, 3, 4, 5, 6, 7],
			j;

		for (j = 0; j < dlugosc; j++) {
			suma += parseInt(arr[j], 10) * wagi[j];
		}

		return ((suma % 11) === parseInt(arr[dlugosc], 10));
	}
	else {
		return false;
	}
}

function CheckForm(form) {
    var vf = ["nazwisko", "adres", "kod", "miasto", "telefon", "email"/*, "haslo"*/],
		ok = true,
		ile, a, haslo, vf1, ile1, nip;

	ile = vf.length
    for (a = 0; a < ile; a++) {
		if ($("input[name='" + vf[a] + "']", form).val() === '') {
			window.alert("Musisz wypełnić to pole: " + vf[a] + ".");
			$("input[name='" + vf[a] + "']", form).focus();
			ok = false;
			break;
		}
    }

	if ($('input[name=kod]', form).val() !== '' && !$('input[name=kod]', form).val().match(/^(\d{2}-\d{3})$/)) {
		window.alert('Podaj poprawny kod.');
		ok = false;
	}

	if ($('input[name=email]', form).val() !== '' && !$('input[name=email]', form).val().match(/^([A-Z0-9\._%\+\-]+@[A-Z0-9\.\-]+\.[A-Z]{2,4})$/i)) {
		window.alert('Podaj poprawny adres email.');
		ok = false;
	}
/*
	haslo = $('input[name=haslo]', form).val();
	if (!(haslo.match(/\d/) && haslo.match(/[A-Z]/) && haslo.match(/[a-z]/) && haslo.length > 5)) {
		window.alert('Hasło powinno być nie krótsze niż 6 znaków i zawierać małe i wielkie litery oraz cyfry.');
		ok = false;
	}

    if ($("input[name='haslo']", form).val() !=  $("input[name='haslop']", form).val()) {
		window.alert('Hasło i jego powtórzenie muszą do siebie pasować !');
		ok = false;
    }
*/
	nip = $('input[name=nip]', form).val();
	if (nip.length > 0 && !is_nip(nip)) {
		window.alert('Podaj poprawny NIP (same cyfry).');
		ok = false;
	}

	if ($('#adres_wysylkiT').attr('checked')) {
		vf1 = ["adres", "kod", "miasto"];
		ile1 = vf.length;

		for (a = 0; a < ile1; a++) {
			if ($("input[name='" + vf1[a] + "_w']", form).val() === '') {
				window.alert("Musisz wypełnić to pole: " + vf1[a] + " wysyłki.");
				$("input[name='" + vf1[a] + "'_w]", form).focus();
				ok = false;
				break;
			}
		}

		if ($('input[name=kod_w]', form).val() !== '' && !$('input[name=kod_w]', form).val().match(/^(\d{2}-\d{3})$/)) {
			window.alert('Podaj poprawny kod adresu do wysyłki.');
			ok = false;
		}
	}

    return ok;
}

function adresWysylki(val) {
	switch (val) {
		case '0':
			$('#div_adres_wysylki').css('display', 'none');
			$('#div_adres_wysylki').css('display', 'none');
			$('input[name=adres_w], input[name=kod_w], input[name=miasto_w]').val('');
			break;
		case '1':
			$('#div_adres_wysylki').css('display', 'block');
			break;
	}
}

