function openW(strona,width_,height_,sc){
         new_window = window.open(strona,'strona','width='+width_+',height='+height_+',toolbars=no,resizeble="no",scrollbars='+sc);
}

function Focus(control) {
  control.focus();
  control.select();
}

function IsEmailCorrect(email) {
  return email.match(/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$/)!=null;
}

function IsEmpty(pole) {
  if (pole=='') 
  	return true
	else
	return false
}

function sprawdz_pola(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ć miejscowość');
    Focus(form.miejscowosc);
    return false;
  }  
if (IsEmpty(form.kod_pocztowy.value)) {
    alert('Proszę podać kod pocztowy');
    Focus(form.kod_pocztowy);
    return false;
  } 
if (form.wojewodztwo.selectedIndex==0) {
    alert('Proszę wybrać województwo');
    return false;
  } 
if (!IsEmailCorrect(form.mail.value)) {
    alert('Proszę podać poprawny adres e-mail');
    Focus(form.mail);
    return false;
  }
if (IsEmpty(form.telefon.value)) {
    alert('Proszę podać numer telefonu');
    Focus(form.telefon);
    return false;
  } 
if (form.haslo.value.length<3) {
    alert('Prosze podać hasło\n Hasło musi posiadać conajmniej 3 znaki');
    Focus(form.haslo);
    return false;
  }
if (form.haslo.value!=form.haslo_pow.value) {
    alert('Podane hasła powinny być identyczne');
    Focus(form.haslo);
    return false;
  }   
if (!$('#wyrazam1:checked').length) {
    alert('Musisz wyrazić zgodę na przechowywanie i przetwarzanie moich danych przez SKLADWIN.PL w celu realizacji zamówienia.');
    return false;
  } 

if ($('#faktura:checked').length) {	
	if (IsEmpty(form.nip.value)) {
    	alert('Proszę podać NIP');
    	Focus(form.nip);
    	return false;
  	  }
	if (IsEmpty(form.nazwa_firmy.value)) {
    	alert('Proszę podać nazwę firmy');
    	Focus(form.nazwa_firmy);
    	return false;
  	  }
	if (IsEmpty(form.faktura_imie.value)) {
    	alert('Proszę podać imię');
    	Focus(form.faktura_imie);
    	return false;
  	  }
	if (IsEmpty(form.faktura_nazwisko.value)) {
    	alert('Proszę podać nazwisko');
    	Focus(form.faktura_nazwisko);
    	return false;
  	  }
	if (IsEmpty(form.faktura_ulica.value)) {
    	alert('Proszę podać ulicę');
    	Focus(form.faktura_ulica);
    	return false;
  	  }
	if (IsEmpty(form.faktura_numer_domu.value)) {
    	alert('Proszę podać numer domu');
    	Focus(form.faktura_numer_domu);
    	return false;
  	  }
	if (IsEmpty(form.faktura_miejscowosc.value)) {
    	alert('Proszę podać miejscowość');
    	Focus(form.faktura_miejscowosc);
    	return false;		
  	  }
	if (IsEmpty(form.faktura_kod_pocztowy.value)) {
    	alert('Proszę podać kod pocztowy');
    	Focus(form.faktura_kod_pocztowy);
    	return false;
  	  }	
	if (form.faktura_wojewodztwo.selectedIndex==0) {
    	alert('Proszę wybrać województwo');
    	return false;
  	  } 
  }
  
return true;
}

function kopiuj(form)
{
document.zamowienie.faktura_imie.value=document.zamowienie.imie.value;
document.zamowienie.faktura_nazwisko.value=document.zamowienie.nazwisko.value;
document.zamowienie.faktura_ulica.value=document.zamowienie.ulica.value;
document.zamowienie.faktura_numer_domu.value=document.zamowienie.numer_domu.value;
document.zamowienie.faktura_miejscowosc.value=document.zamowienie.miejscowosc.value;
document.zamowienie.faktura_kod_pocztowy.value=document.zamowienie.kod_pocztowy.value;
document.zamowienie.faktura_wojewodztwo.value=document.zamowienie.wojewodztwo.value;
}

jQuery(document).ready(function()
{
	
	$(".col-del").click(function() {
		id = $(this).parent().find('input').attr('id');
		element=$(this);
		$.ajax({
		   type: "POST",
		   url: "/ajax.php",
		   data: {type: 'koszyk_usun', id: id},
		   dataType: 'json',
		   success: function(msg){
				if (msg.info=='OK')
					{						
						//$(input).parent().parent().find('.col-4').html(msg.cena_produktu);
						$(element).parent().remove();
						$('.suma').html(msg.suma);
						$('.iloscwin').html(msg.ilosc);
						$('.suma_dostawa span').html(msg.suma_dostawa);
						$('.koszt_dostawy').html(msg.koszt_dostawy);
						//$('#top-2 .sztuk span').html(msg.ilosc);
						//$('#top-2 .wartosc span').html(msg.cena);
						//alert('Produkt zostal dodany do koszyka');
					}				
		   }
		});	
	})
								 
	
	$(".col-item input[type=text]").blur(function() {
		val=parseInt($(this).val());
		if (!parseInt(val)) val=1;
		if (val<1) val=1;
		$(this).val(val);
		ilosc = val;
		input=$(this);
		id = $(this).attr('id');
		$.ajax({
		   type: "POST",
		   url: "/ajax.php",
		   data: {type: 'koszyk_zmien', id: id, ilosc: ilosc},
		   dataType: 'json',
		   success: function(msg){
				if (msg.info=='OK')
					{
						$(input).parent().parent().find('.col-4').html(msg.cena_produktu);
						$('.suma').html(msg.suma);
						$('.iloscwin').html(msg.ilosc);
						$('.suma_dostawa span').html(msg.suma_dostawa);
						$('.koszt_dostawy').html(msg.koszt_dostawy);
						//$('#top-2 .sztuk span').html(msg.ilosc);
						//$('#top-2 .wartosc span').html(msg.cena);
						//alert('Produkt zostal dodany do koszyka');
					}				
		   }
		});		
	})

	$("#top-1 .newsletter .zapisz").click(function() {
		$("#top-1 .newsletter .form input[name=zapisz]").val(1);
		$(".newsletter form").submit();
		return false;
	})
		
	$("#top-1 .newsletter .wypisz").click(function() {
		$("#top-1 .newsletter .form input[name=wypisz]").val(1);
		$(".newsletter form").submit();
		return false;
	})
	
	$("#top-1 .newsletter_link").click(function() {
		$("#top-1 .newsletter").toggle();
		return false;
	})
	
	$(".buy-button").click( function() {
		$.ajax({
		   type: "POST",
		   url: "/ajax.php",
		   data: {type: 'koszyk_dodaj', numer: $(this).attr('name')},
		   dataType: 'json',
		   success: function(msg){
				if (msg.info=='OK')
					{
						$('#top-2 .sztuk span').html(msg.ilosc);
						$('#top-2 .wartosc span').html(msg.cena);
						alert('Produkt zostal dodany do koszyka');
					}				
		   }
		});		
		return false
	});

	$("#szukaj_wino_submit").click(function () {
		$("#szukaj_wino").submit();
		return false;
	})
	
	$("#szukaj_wino_text").click(function() {
		$(this).parent().submit();
		return false;
	})
	
	$("#reset_wino_submit").click(function () {
		
		values='type=search';
		$(this).parent().find('.menu-2 > li > a').each(function() {
			values = values + '&' + $(this).attr('rel') + '=-1';
			$(this).html($(this).attr('rel'));
		})
		
		$.ajax({
		   type: "POST",
		   url: "/ajax.php",
		   data: values,
		   dataType: 'json',
		   success: function(msg){
				if (msg.info=='OK')
					{
						$('.select-menu .kraj ul').replaceWith(msg.kraj);
						$('.select-menu .region ul').replaceWith(msg.regjon);
						$('.select-menu .producent ul').replaceWith(msg.producent);
						$('.select-menu .szczep ul').replaceWith(msg.szczep);
						$('.select-menu .rodzaj ul').replaceWith(msg.rodzaj);
						$('.select-menu .kolor ul').replaceWith(msg.kolor);
						$('.select-menu .potrawy ul').replaceWith(msg.potrawy);
						$('.select-menu .cena ul').replaceWith(msg.ceny);
					}
		   }
		});		
		return false;
	})
	
	jQuery("#wine-list .img").hover(
		function ()
		{
			
			$(this).parent().find(".tooltip").css( 'display' , 'block' );
			
		}, 
		function ()
		{
			
			$(this).parent().find(".tooltip").css( 'display' , 'none' );
			
		}
    );
	
	jQuery(".select-menu > li > a").click(function ()
	{ 
		$(this).parent().find('.filter-option').toggle("slow"); 
		return false;
    });	
	
	jQuery(".select-menu li li a").live("click",function ()
	{
		name = $(this).html();
		value = $(this).attr('rel');
		if ($(this).attr('rel')=='all') 
			{
				name = $(this).parent().parent().parent().find(' > a').attr('rel');
				value='-1';
			}
		$(this).parent().parent().parent().find(' > a').html(name).attr('name',value);
		$(this).parent().parent().parent().find('.filter-option').toggle("slow"); 
		$("#szukaj_wino input[name=" + $(this).parent().parent().parent().find(' > a').attr('rel') + "]").val(value);
		
		var values = new Array();
		values='type=search';
		$(this).parent().parent().parent().parent().find(' > li > a').each(function() {
			values = values + '&' + $(this).attr('rel') + '=' + $(this).attr('name');
		})
		
		values= values + '&actual=' + $(this).parent().parent().parent().find(' > a').attr('rel') + '&actual_id=' + $(this).parent().parent().parent().find(' > a').attr('name');
		
		$.ajax({
		   type: "POST",
		   url: "/ajax.php",
		   data: values,
		   dataType: 'json',
		   success: function(msg){
				if (msg.info=='OK')
					{
						$('.select-menu .kraj ul').replaceWith(msg.kraj);
						$('.select-menu .region ul').replaceWith(msg.regjon);
						$('.select-menu .producent ul').replaceWith(msg.producent);
						$('.select-menu .szczep ul').replaceWith(msg.szczep);
						$('.select-menu .rodzaj ul').replaceWith(msg.rodzaj);
						$('.select-menu .kolor ul').replaceWith(msg.kolor);
						$('.select-menu .potrawy ul').replaceWith(msg.potrawy);
						$('.select-menu .cena ul').replaceWith(msg.ceny);
					}
		   }
		});		
		
		return false;
	});
	
	jQuery(".select-menu > li").bind("mouseleave",function () {
		$(this).find('.filter-option:visible').hide('slow');
	});	
	
});
