function search_JS()
{
	search_value = (document.getElementById('search_value').value);

	$.ajax({
		type: "POST",
		url: "/process.php",
		data: {  
				'function': 'search_JS',
				'search_value': search_value
				},
				   
			dataType: "json",
			success: function(data)
			{
				var layout='';
				
				if (data.dane != null)
				{
					if (search_value != '')
					{
						document.getElementById('search_result').style.display = 'block';

						if (data.dane.length > 60)
						{
							layout = 'Zapytanie zwróciło zbyt wiele wyników. Prosimy wpisać więcej znaków w pole wyszukiwania.';
						}
						else
						{
							count_column = (data.dane.length / 3);
							search_value_array = search_value.split(' ');
							p = 0;
						
							for (var i = 0; i < data.dane.length; i++) 
							{
								p++;
								if (p == 1)
									layout += "<div style='float:left; width:350px;'>";
								
								var link = '';
								layout += "<div>";
								
								str = data.dane[i]['name'].toUpperCase();
								str_color = data.dane[i]['name'].toUpperCase();
								
								for (var j = 0; j < search_value_array.length; j++) 
								{
									str_color = str_color.replace(search_value_array[j].toUpperCase(), "<span style='color:red;'>"+search_value_array[j].toUpperCase()+"</span>");
								}
								
								url = data.dane[i]['name'].toLowerCase();
								pozycja = url.indexOf(' ');
								while (pozycja >= 0)
								{
									url = url.replace(" ", "-");
									url = url.replace("ę", "e");
									url = url.replace("ó", "o");
									url = url.replace("ą", "a");
									url = url.replace("ś", "s");
									url = url.replace("ł", "l");
									url = url.replace("ż", "z");
									url = url.replace("ź", "x");
									url = url.replace("ć", "c");
									url = url.replace("ń", "n");
									pozycja = url.indexOf(' ');
								}
								
								link += "<a href='/wyszukiwanie/"+url+","+data.dane[i]['ID']+".html'>";
								link += str_color;
								link += "</a>";
								
								layout += link;
								layout += "</div>";
								
								if (p < count_column)
								{
									layout += "</div>";
									p = 0;
								}
							}
						}
					}
					else
					{
						document.getElementById('search_result').style.display = 'none';
					}
				}
				else
					layout = 'Brak wyników wyszukiwania.'; 
				
				document.getElementById('search_result').innerHTML = layout;
			},
				
			error: function(xhr) 
			{
				alert(xhr.responseText);
	        },

			beforeSend: function()
			{
				document.getElementById('search_progress').style.display = 'inline';
			},

			complete: function(){
				document.getElementById('search_progress').style.display = 'none';
			},			
		});
}

function AddProduct(id_product, action)
{
	if (document.getElementById('ilosc') != null)
	{
	if (isNaN(document.getElementById('ilosc').value) == true)
		var ilosc = 1;
	else
		var ilosc = document.getElementById('ilosc').value;
	}
	else
		var ilosc = 0;

	
	$.ajax({
		type: "POST",
		url: "/process.php",
		data: {  
				'function': 'AddProduct',
				'id_product': id_product,
				'action': action, 
				'ilosc': ilosc
				},
				   
			dataType: "json",
			success: function(data)
			{
				window.location ="/koszyk.html";
			},
				
			error: function(xhr) 
			{
				alert(xhr.responseText);
	          }, 
		});
}

function DokumentSprzedazyJs()
{

	if(document.getElementById('DokumentSprzedazy_paragon').checked)
		document.getElementById('DaneDoFakturyVAT').style.display = 'none';
	else
		document.getElementById('DaneDoFakturyVAT').style.display = 'block';
} 

function SposobDoreczeniaPrzesylkiJS()
{

	if(document.getElementById('SposobDoreczeniaPrzesylki_osobisty').checked)
	{
		document.getElementById('DaneDoWysylki').style.display = 'none';
		document.getElementById('KosztPrzesylki').style.display = 'none';
	}
	else
	{
		document.getElementById('DaneDoWysylki').style.display = 'block';
		document.getElementById('KosztPrzesylki').style.display = 'block';
	}
} 

function RelizujZamowienie()
{
	
	var field = ['DaneDoFakturyVAT_Firma','DaneDoFakturyVAT_NIP','DaneDoFakturyVAT_Ulica','DaneDoFakturyVAT_KodPocztowy','DaneDoFakturyVAT_Miasto','DaneDoWysylki_Imie','DaneDoWysylki_Nazwisko','DaneDoWysylki_Ulica','DaneDoWysylki_KodPocztowy', 'DaneDoWysylki_Miasto', 'email'];
	for (x=0; x<field.length; x++) 
	{
		document.getElementById(''+field[x]+'').style.backgroundColor = 'white';
	}
	
	DokumentSprzedazy_fakturaVAT = true;
	SposobDoreczeniaPrzesylki_kurier = true;
	email = true;
	total = true;
	
	if (document.getElementById('total').innerHTML == '0 zł')
	{
		alert('Twój koszyk jest pusty !');
		total = false;
	}
	
	if ( (document.getElementById('DokumentSprzedazy_fakturaVAT').checked) && (total == true) )
	{
		if (document.getElementById('DaneDoFakturyVAT_Firma').value == '')
		{
			document.getElementById('DaneDoFakturyVAT_Firma').style.backgroundColor = 'pink';
			alert('Pole - Dane do Faktury VAT - Firma - jest polem obowiązkowym i nie może być puste.');
			DokumentSprzedazy_fakturaVAT = false;
		}
		else
		if (document.getElementById('DaneDoFakturyVAT_NIP').value == '')
		{
			document.getElementById('DaneDoFakturyVAT_NIP').style.backgroundColor = 'pink';
			alert('Pole - Dane do Faktury VAT - NIP - jest polem obowiązkowym i nie może być puste.');
			DokumentSprzedazy_fakturaVAT = false;
		}
		else
		if (document.getElementById('DaneDoFakturyVAT_Ulica').value == '')
		{
			document.getElementById('DaneDoFakturyVAT_Ulica').style.backgroundColor = 'pink';
			alert('Pole - Dane do Faktury VAT - Ulica - jest polem obowiązkowym i nie może być puste.');
			DokumentSprzedazy_fakturaVAT = false;
		}
		else
		if (document.getElementById('DaneDoFakturyVAT_KodPocztowy').value == '')
		{
			document.getElementById('DaneDoFakturyVAT_KodPocztowy').style.backgroundColor = 'pink';
			alert('Pole - Dane do Faktury VAT - Kod pocztowy - jest polem obowiązkowym i nie może być puste.');
			DokumentSprzedazy_fakturaVAT = false;
		}
		else
		if (document.getElementById('DaneDoFakturyVAT_Miasto').value == '')
		{
			document.getElementById('DaneDoFakturyVAT_Miasto').style.backgroundColor = 'pink';
			alert('Pole - Dane do Faktury VAT - Miasto - jest polem obowiązkowym i nie może być puste.');
			DokumentSprzedazy_fakturaVAT = false;
		}
	}

	if( (document.getElementById('SposobDoreczeniaPrzesylki_kurier').checked) && (DokumentSprzedazy_fakturaVAT == true)  && (total == true) )
	{
		if (document.getElementById('DaneDoWysylki_Imie').value == '')
		{
			document.getElementById('DaneDoWysylki_Imie').style.backgroundColor = 'pink';
			alert('Pole - Dane do wysyłki - Imię - jest polem obowiązkowym i nie może być puste.');
			SposobDoreczeniaPrzesylki_kurier = false;
		}
		else
		if (document.getElementById('DaneDoWysylki_Nazwisko').value == '')
		{
			document.getElementById('DaneDoWysylki_Nazwisko').style.backgroundColor = 'pink';
			alert('Pole - Dane do wysyłki - Nazwisko - jest polem obowiązkowym i nie może być puste.');
			SposobDoreczeniaPrzesylki_kurier = false;
		}
		else
		if (document.getElementById('DaneDoWysylki_Ulica').value == '')
		{
			document.getElementById('DaneDoWysylki_Ulica').style.backgroundColor = 'pink';
			alert('Pole - Dane do wysyłki - Ulica - jest polem obowiązkowym i nie może być puste.');
			SposobDoreczeniaPrzesylki_kurier = false;
		}
		else
		if (document.getElementById('DaneDoWysylki_KodPocztowy').value == '')
		{
			document.getElementById('DaneDoWysylki_KodPocztowy').style.backgroundColor = 'pink';
			alert('Pole - Dane do wysyłki - Kod pocztowy - jest polem obowiązkowym i nie może być puste.');
			SposobDoreczeniaPrzesylki_kurier = false;
		}
		else
		if (document.getElementById('DaneDoWysylki_Miasto').value == '')
		{
			document.getElementById('DaneDoWysylki_Miasto').style.backgroundColor = 'pink';
			alert('Pole - Dane do wysyłki - Miasto - jest polem obowiązkowym i nie może być puste.');
			SposobDoreczeniaPrzesylki_kurier = false;
		}
	}
	
	if ( ( (document.getElementById('email').value == '') || ((ValidMail(document.getElementById('email').value) == false)) ) && (DokumentSprzedazy_fakturaVAT == true) && (SposobDoreczeniaPrzesylki_kurier == true)  && (total == true) )
	{
		document.getElementById('email').style.backgroundColor = 'pink';
		alert('Prosimy poprawnie wpisać adres email.');
		email = false;
	}

	
	if ( (DokumentSprzedazy_fakturaVAT == true) && (SposobDoreczeniaPrzesylki_kurier == true) && (email == true)  && (total == true) )
	{
		DokumentSprzedazy_fakturaVAT = 0;
		if (document.getElementById('DokumentSprzedazy_fakturaVAT').checked)
			DokumentSprzedazy_fakturaVAT = 1;
			
			SposobDoreczeniaPrzesylki_kurier = 0;
		if (document.getElementById('SposobDoreczeniaPrzesylki_kurier').checked)
			SposobDoreczeniaPrzesylki_kurier = 1;	
			
			kosztprzesyki_przelew = 0;
		if (document.getElementById('kosztprzesyki_przelew').checked)
			kosztprzesyki_przelew = document.getElementById('kosztprzesyki_przelew').value;	
			
			kosztprzesyki_pobranie = 0;
		if (document.getElementById('kosztprzesyki_pobranie').checked)
			kosztprzesyki_pobranie = document.getElementById('kosztprzesyki_pobranie').value;
		
		$.ajax({
		type: "POST",
		url: "/process.php",
		data: {  
				'function': 'RelizujZamowienie',
				'DokumentSprzedazy_fakturaVAT': DokumentSprzedazy_fakturaVAT,
				'DaneDoFakturyVAT_Firma': document.getElementById('DaneDoFakturyVAT_Firma').value, 
				'DaneDoFakturyVAT_NIP': document.getElementById('DaneDoFakturyVAT_NIP').value, 
				'DaneDoFakturyVAT_Ulica': document.getElementById('DaneDoFakturyVAT_Ulica').value, 
				'DaneDoFakturyVAT_KodPocztowy': document.getElementById('DaneDoFakturyVAT_KodPocztowy').value, 
				'DaneDoFakturyVAT_Miasto': document.getElementById('DaneDoFakturyVAT_Miasto').value, 
				'SposobDoreczeniaPrzesylki_kurier': SposobDoreczeniaPrzesylki_kurier, 
				'DaneDoWysylki_Imie': document.getElementById('DaneDoWysylki_Imie').value, 
				'DaneDoWysylki_Nazwisko': document.getElementById('DaneDoWysylki_Nazwisko').value, 
				'DaneDoWysylki_Ulica': document.getElementById('DaneDoWysylki_Ulica').value, 
				'DaneDoWysylki_KodPocztowy': document.getElementById('DaneDoWysylki_KodPocztowy').value, 
				'DaneDoWysylki_Miasto': document.getElementById('DaneDoWysylki_Miasto').value, 
				
				'kosztprzesyki_przelew': kosztprzesyki_przelew, 
				'kosztprzesyki_pobranie': kosztprzesyki_pobranie, 
				
				'telefon': document.getElementById('telefon').value, 
				'email': document.getElementById('email').value, 
				'uwagi': document.getElementById('uwagi').value
				},
				   
			dataType: "json",
			success: function(data)
			{
				//alert(data);
				//alert(data.dane);
				document.getElementById('Koszyk_DIV').style.padding = "10px";
				document.getElementById('Koszyk_DIV').innerHTML = data.dane;
			},
				
			error: function(xhr) 
			{
				
				document.getElementById('error').innerHTML = xhr.responseText;
				//alert(xhr.responseText);
	        }, 
		});
	}
}

function ValidMail(src)
{
var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
return regex.test(src);
} 

function FilterAttribute_JS(filter, ID, url)
{
	tab_url_1 = url.split('.html');
	tab_url = tab_url_1[0].split(',');
	tab_filter = filter.split(':');
	filter_znaleziono = false;
	filter_zm = '';
	
	if (tab_filter[0] > 0)
	{
		for (x=0; x<tab_filter.length; x++) 
		{
			if (tab_filter[x] == ID)
			{
				filter_znaleziono = true;
			}
			else
			{
				if (filter_zm == '')
					filter_zm = tab_filter[x];
				else
					filter_zm += ':'+tab_filter[x];
			}
		}
	}
	
	if (filter_znaleziono == false)
	{
		if (filter_zm == '')
			filter_zm = ID;
		else
			filter_zm += ':'+ID;
	}
	
	
	url = '';
	if (filter_zm != '')
	{
		url = tab_url[0]+','+tab_url[1]+','+filter_zm+'.html';
	}
	else
	if (tab_filter[0] > 0 && filter_zm == '')
	{
		url = tab_url[0]+','+tab_url[1]+'.html';
	}
			
	window.location = url;
}

function VisibleHidden_JS(ID)
{
	if ( (document.getElementById(ID).style.display == 'none') || (document.getElementById(ID).style.display == '') ) 
		document.getElementById(ID).style.display = 'block';
	else
		document.getElementById(ID).style.display = 'none';
}

