jQuery(document).ready(function() {
	if (jQuery(".middle .koszyk .goradol").size()>0)
		setGoraDol();
});

jQuery(window).load(function() {
	alignElementsbottom();
});

function setGoraDol() {
	jQuery(".middle .koszyk .goradol").each(function() {
		jQuery(this).find('.gora').click(function() {
			val = getValue(this);
			set = val + 1;
			setValue(this,set);
		});
		jQuery(this).find('.dol').click(function() {
			val = getValue(this);
			if (val<2)
				set = 1;
			else 
				set = val - 1;
			setValue(this,set);
		});								 
	});
}

function alignElementsbottom() {
	jQuery('.align-container .align-with').each(function(i) {
		container = jQuery(this).parents('.align-container');
		alignWith = jQuery(container).find('.align-with').height();
		alignWhat = jQuery(container).find('.align-what').height();
		if (alignWhat<alignWith) {
			jQuery(container).find('.align-what').addClass('align-position');
			jQuery(container).find('.align-with').addClass('aligned-with');
		}
	});
}

function getValue(element) {
	val = jQuery(element).parent().parent().children(".ile").children(".inpt").val();
	val = parseInt(val);
	if (isNaN(val) || val<1)
		val = 1;
	return val;
}

function setValue(element,val) {
	jQuery(element).parent().parent().children(".ile").children(".inpt").val(val);
}

function wyswietl_platnosci(platnosci,dostawa) {
	numer = platnosci.split(" ");
	jQuery('.input_platnosci').hide();
	jQuery('.input_platnosc_legend').hide();
	jQuery('.dostawa_suma').hide();
	jQuery('.input_platnosci input').attr("checked",false);
	for (y in numer) {
		jQuery('.input_platnosc_'+numer[y]).show();
	}
	jQuery('.dostawa_suma_'+dostawa).show();
}


function formularz_sprawdz(form) {
	if (jQuery(form).find('[name="dostawa"]:checked').size()==0) {
		alert('Proszę podać formę dostawy');
		return false;
	}
	if (jQuery(form).find('[name="platnosci2"]:checked').size()==0) {
		alert('Proszę podać formę płatności');
		return false;
	}
	return true;
}

function check_form(form,edytuj) {
	if (edytuj=='0') {
		email = jQuery(form.email).val();
		if (!IsEmailCorrect(email)) {
			alert('Proszę podać poprawny adres e-mail');
			Focus(form.email);
			return false;
		}
		pass = jQuery(form.password).val();
		passRe = jQuery(form.passRe).val();
		if (pass == '') {
			alert('Proszę podać poprawne hasło');
			Focus(form.password);
			return false;
		} 
		if (!IsPasswordsCorrect(pass,passRe)) {
			alert('Podane hasła się różnią');
			Focus(form.passRe);
			return false;
		}
	}
	if (!IsSelected(form.przetwarzanie_danych1) || !IsSelected(form.przetwarzanie_danych2)) {
		alert('Proszę wyrazić zgodę na przetwarzanie danych');
		return false;
	}
	ret = sprawdz_katalog(form);
	if (!ret)
		return false;
	name2 = jQuery(form.imie2).val();
	if (!IsEmpty(name2) && IsEmpty(form.nip.value)) {
		alert('Proszę podać NIP');
		Focus(form.nip);
		return false;
	}
	return true;
}

function sprawdzKopiuj(pole) {
	nazwa = jQuery(pole).attr('name');
	val = jQuery(pole).val();
	form = jQuery(pole).parents('form');
	if (IsSelected(jQuery(form).find('input[name="jw"]'))) {
		jQuery(form).find('input[name="'+nazwa+'2"]').val(val);
	}
}

function zaznaczWyzej(pole) {
	pola = new Array('imie','nazwisko','firma','ulica','numer_domu','miejscowosc','kod_pocztowy','panstwo');
	if (IsSelected(pole)) {
		for (iter in pola) {
			nazwa = pola[iter];
			val = jQuery('input[name="'+nazwa+'"]').val();
			elem = jQuery('input[name="'+nazwa+'2"]');
			jQuery(elem).attr('disabled',true).val(val);
		}
	} else {
		for (iter in pola) {
			nazwa = pola[iter];
			jQuery('input[name="'+nazwa+'2"]').attr('disabled',false);
		}
	}
}	

function IsSelected(input) {
	return jQuery(input).attr('checked');
}

function NewsletterOnSubmit(form) {
	if (!IsEmailCorrect(form.email.value)) {
		alert('Podaj poprawny adres e-mail');
		Focus(form.email);
		return false;
	}
	return true;
}

function IsEmailCorrect(email) {
	return email.match(/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$/)!=null;
}

function IsPasswordsCorrect(pass,passRe){
	if(pass == passRe) 
		return true;
	else 
		return false;
}

function IsEmpty(pole) {
	if (pole=='') 
		return true
	else
		return false
}

function Focus(control) {
	control.focus();
	control.select();
}

function sprawdz_katalog(form) {
	if (IsEmpty(form.imie.value)) {
		alert('Proszę podać imię');
		Focus(form.imie);
		return false;
	}
	if (IsEmpty(form.nazwisko.value)) {
		alert('Proszę podać nazwisko');
		Focus(form.nazwisko);
		return false;
	}  
	if (IsEmpty(form.ulica.value)) {
		alert('Proszę podać ulicę');
		Focus(form.ulica);
		return false;
	}  
	if (IsEmpty(form.numer_domu.value)) {
		alert('Proszę podać numer domu');
		Focus(form.numer_domu);
		return false;
	}  
	if (IsEmpty(form.miejscowosc.value)) {
		alert('Proszę podać miasto');
		Focus(form.miejscowosc);
		return false;
	}  
	if (IsEmpty(form.kod_pocztowy.value)) {
		alert('Proszę podać kod pocztowy');
		Focus(form.kod_pocztowy);
		return false;
	} 
	if (IsEmpty(form.panstwo.value)) {
		alert('Proszę podać państwo');
		Focus(form.panstwo);
		return false;
	}
	if (!IsSelected(form.przetwarzanie_danych1) || !IsSelected(form.przetwarzanie_danych2)) {
		alert('Proszę wyrazić zgodę na przetwarzanie danych');
		return false;
	}
	return true;
}
