$(document).ready(function(){

mwartosc = new Array();
$("form#zamowienie").find("input:text:enabled[rel='mliczba']").each(function(i) {
	mwartosc[$(this).attr('name')]=($(this).val()*1);
	//alert(i+" "+$(this).attr('name')+" "+$(this).val())	
	})
value_pola_tekstowe = new Array();
$("form#zamowienie").find("input:text:enabled").each(function(i) {
	value_pola_tekstowe[i]=$(this).val();	
})
/*************************************************** KROK 1 ****************************************************/
	$(".powrot_btn").click(function(){
		if ($("centruj0:hidden") && $("#centruj:visible"))
		{
			//alert('dfsdf');
			$("#centruj").slideUp(500, function () {
				$("#centruj0").slideDown(500, function () {
					$("#centruj").addClass('hidden');
					$("#centruj0").removeClass('hidden');
					//alert($("#centruj0").width()+" "+$("#centruj0").height())
					//resizeOuterTo(($("#centruj0").width()+42),($("#centruj0").height()+118));
					//alert($("#centruj0").innerWidth(true)+" => "+$("#centruj0").innerHeight(true))
					//alert($("#centruj0").outerWidth(true)+" => "+$("#centruj0").outerHeight(true))
					resizeOuterTo(($("#centruj0").outerWidth(true)+30),($("#centruj0").outerHeight(true)+100));

				});
			})
		}	
	})
	
	$("#zamowienie:not(.dwa)").submit(function(){
	
	//sel=document.getElementsByName(zmienna)[0].selectedIndex;
	//sel=document.getElementsByTagName("input");
	
	
/* 	$.each($(":radio"), function(index, value) {
		//if(index==0){sel=value[0];}
		//raport+=index+" => "+value[0]+" => "+value[1]+"\n";
		//opt+="<option value='"+value[0]+"'>"+value[1]+"</option>"
		raport+=index+" => "+value+"\n";
	});
	alert(raport)
 */	
	//alert('trzy');
		$(this).pk_sprawdzForm();
		/* $.each(zmienna, function(i,item){
                    alert(i+" "+item)
                }); */ 
		if($("#zamowienie :input[name='wysylka']").length>0)
		{
			//alert('jest wysylka');
			$("#wysylka_tmp").remove();
			$("#odbior_tmp").remove();

			//if($("#zamowienie input[name='wysylka']:checked").length>0)
			//if($("#zamowienie input[name='wysylka']:checked").length>0 && $("#zamowienie input[name='wysylka']:checked").val()>0)
			if(($("#zamowienie input[name='wysylka']:checked").length>0 && $("#zamowienie input[name='wysylka']:checked").val()>0) || $("#zamowienie :input[name='wysylka']").val()>0)
			{
				//alert('wysylka zaznaczona');
				$("#wysylka").attr("checked",true);//wysylka
				$("#wysylka").before("<input id='wysylka_tmp' type='hidden' name='wysylka' value='1'>");
				$("#odbior").attr("checked",false);
				$("#odbior").attr("disabled",true);
				$("#wysylka").attr("disabled",true);
				$("#odbior_box").hide();
				startuj();
			}
			else
			{
				$("#wysylka").attr("checked",false);
				$("#odbior").attr("checked",true);//odbior
				$("#wysylka").before("<input id='odbior_tmp' type='hidden' name='odbior' value='1'>");
				$("#wysylka").attr("disabled",true);
				$("#odbior").attr("disabled",true);
				$("#odbior_box").show();
				startuj();				
			}
		}
		if(zweryfikowano_frm && zweryfikowano_frm_pliki)
		{
			//return true;			
			if ($("centruj0:visible") && $("#centruj:hidden"))
			{
				$("#centruj0").slideUp(500, function () {
					$("#centruj").slideDown(500, function () {
						$("#centruj0").addClass('hidden');
						$("#centruj").removeClass('hidden');
						//alert($("#centruj").width()+" "+$("#centruj").height())
						//resizeOuterTo(($("#centruj").width()+42),($("#centruj").height()+118));
						resizeOuterTo(($("#centruj").outerWidth(true)+30),($("#centruj").outerHeight(true)+100));
					});
				})
			}		
			return false;
		}
		else
		{					
			if(zweryfikowano_frm==false){alert("Wypełnij prawidłowo wszystkie pola w czerwonej ramce!");}
			if(zweryfikowano_frm_pliki==false){alert("Załącz obowiązkowe pliki!");}			
			return false;
		}
		
	});
	
	$("#zamowienie.dwa").submit(function(){
	//alert('dwa');
		$(this).pk_sprawdzForm();
		/* $.each(zmienna, function(i,item){
                    alert(i+" "+item)
                }); */ 
		if($("#zamowienie :input[name='wysylka']").length>0)
		{
			//alert('jest wysylka');
			$("#wysylka_tmp").remove();
			$("#odbior_tmp").remove();

			//if($("#zamowienie input[name='wysylka']:checked").length>0)
			//if($("#zamowienie input[name='wysylka']:checked").length>0 && $("#zamowienie input[name='wysylka']:checked").val()>0)
			if(($("#zamowienie input[name='wysylka']:checked").length>0 && $("#zamowienie input[name='wysylka']:checked").val()>0) || $("#zamowienie :input[name='wysylka']").val()>0)
			{
				//alert('wysylka zaznaczona 0');
				$("#wysylka").attr("checked",true);//wysylka
				$("#wysylka").before("<input id='wysylka_tmp' type='hidden' name='wysylka' value='1'>");
				$("#odbior").attr("checked",false);
				$("#odbior").attr("disabled",true);
				$("#wysylka").attr("disabled",true);
				$("#odbior_box").hide();
				startuj();
			}
			else
			{
				//alert('wysylka zaznaczona >0');
				$("#wysylka").attr("checked",false);
				$("#odbior").attr("checked",true);//odbior
				$("#wysylka").before("<input id='odbior_tmp' type='hidden' name='odbior' value='1'>");
				$("#wysylka").attr("disabled",true);
				$("#odbior").attr("disabled",true);
				$("#odbior_box").show();
				startuj();				
			}
		}
		if(zweryfikowano_frm && zweryfikowano_frm_pliki)
		{
			//var wysylaj=Sprawdz();
			//alert(wysylaj)
			//if(wysylaj)
			//{
				//$("#zamowienie").ajaxSubmit(options_wyslij2);
				if(confirm("Uwaga formularz zostanie wysłany!\nKontynuować?"))
				{
					$("#loader").show(500);
					//$(this).ajaxSubmit(options_wyslij);
					//********** DWA KROKI WYSYLKA************/
					$("#zamowienie").ajaxSubmit({
						url: 'ajax.php',
						resetForm: false,
						success: showResponse_tekst3
						//data: [ { name: 'id_zamowienia', value: responseText } ]	
						//data: {'id_zamowienia' : responseText }	
					});
					//********** DWA KROKI WYSYLKA END ************/
				}
			//}
			return false;		
			
			
			
			// ****************** old
			//return true;			
			/*
			if ($("centruj0:visible") && $("#centruj:hidden"))
			{
				$("#centruj0").slideUp(500, function () {
					$("#centruj").slideDown(500, function () {
						$("#centruj0").addClass('hidden');
						$("#centruj").removeClass('hidden');
						//alert($("#centruj").width()+" "+$("#centruj").height())
						//resizeOuterTo(($("#centruj").width()+42),($("#centruj").height()+118));
						resizeOuterTo(($("#centruj").outerWidth(true)+30),($("#centruj").outerHeight(true)+100));
					});
				})
			}		
			return false;
			*/
			// ******************** old end
		}
		else
		{					
			if(zweryfikowano_frm==false){alert("Wypełnij prawidłowo wszystkie pola w czerwonej ramce!");}
			if(zweryfikowano_frm_pliki==false){alert("Załącz obowiązkowe pliki!");}			
			return false;
		}
		
	});	
/*  $("form#zamowienie :file").MultiFile({  
  //accept: 'pdf,xml,xmz,doc,xls',
  
  STRING: { 
   //remove: '<img src="images/bin.gif" height="16" width="16" alt="x"/>'  
    list: '#kolejka_plikow',
	//list: $(this).next(),
	remove: '<input type="button" value="usuń">',	
	//remove:'usuń',
    denied:'Nie możesz wybrać pliku typu $ext.\nSpróbuj ponownie...',
    file:'$file',
    selected:'Wybrany plik: $file',
    duplicate:'Ten plik jest już wybrany:\n$file'   
  } 
 }); */	
 $("input[type=file]").MultiFile();
/****************************************** KROK 1 END ****************************************************/	

	$("input[name='d_2'],input[name='f_2']").mask("99-999");
	
 $("input[name='plik[]']").MultiFile({  
  //accept: 'pdf,xml,xmz,doc,xls',
  
  STRING: { 
   //remove: '<img src="images/bin.gif" height="16" width="16" alt="x"/>'  
    list: '#kolejka_plikow',
	remove: '<input type="button" value="usuń">',	
	//remove:'usuń',
    denied:'Nie możesz wybrać pliku typu $ext.\nSpróbuj ponownie...',
    file:'$file',
    selected:'Wybrany plik: $file',
    duplicate:'Ten plik jest już wybrany:\n$file'   
  } 
 });
 
	  name_pola = new Array();
	  value_pola = new Array();
	  $("form :text").each(function (i) {
				name_pola[i]=$("form :text").eq(i).attr('name');
				value_pola[i]=$("form :text").eq(i).val();
				
				$("form :text").eq(i).focus(function () {
					if(this.value==value_pola[i])this.value='';
					//alert('i '+(i-1))
					//alert('eq '+$(this).eq())
				})
				//$("input[name='"+name_pola+"']").
				$("form :text").eq(i).blur(function () {
					if(this.value=='')this.value=value_pola[i];
				})
      });
	  value_pola_dostawa = new Array();
	  $("#dostawa_box :text").not("input[name='d_6']").each(function (i) {
	  value_pola_dostawa[i]=$(this).val();
	  })
	  
	  value_pola_faktura = new Array();
	  $("#faktura_box :text").not("input[name='f_6']").each(function (i) {
	  value_pola_faktura[i]=$(this).val();
	  })	  

function Sprawdz() {
	
	//alert($(":input[name='kkk']").val())
	var regname = /^[A-Za-z ęĘóÓąĄśŚłŁżŻĄŹćĆńŃ-]{3,100}$/;
	var regphone = /^[0-9 -/+]{7,30}$/;
	var regemail = /^[a-z0-9][\w\.-]*\w@([\w-]+\.)*[a-z]{2,4}$/;
	var regtxt = /^[A-Za-z ęĘóÓąĄśŚłŁżŻĄŹćĆńŃ 0-9 -=.,+]{1,1000}$/;
	var regnip = /^[0-9 -]{10,13}$/;
	
	var name = $("input[name='nadawca']").val();
	var telefon = $("input[name='telefon']").val();
	var nip = $("input[name='f_7']").val();
	//var phone = document.getElementById("phone").value;
	var email = $("input[name='email']").val()
	//var txt = document.getElementById("txt").value;
	
	var zweryfikowano=true;
	if($("#wysylka").is(":checked"))
	{
	  //var zweryfikowano=true;
	  $("#dostawa_box :text").not("input[name='d_6']").each(function (i) {
				
				//value_pola[i]=$("#dostawa_box :text").not("input[name='d_6']").eq(i).val();
				//alert(value_pola_dostawa[i])
				if(jQuery.trim($(this).val())=='' || $(this).val()==value_pola_dostawa[i])
				{
					//alert($(this).attr('name')+" not ok");
					$(this).css({"color":"#f00"});
					zweryfikowano=false;
				}
				else
				{
					$(this).css({"color":"#000"});
					//alert($(this).attr('name')+" ok");
				}
      });
	}
	if($("#faktura").is(":checked"))
	{
	  //var zweryfikowano=true;
	  $("#faktura_box :text").not("input[name='f_6']").each(function (i) {
				
				if(jQuery.trim($(this).val())=='' || $(this).val()==value_pola_faktura[i])
				{
					//alert($(this).attr('name')+" not ok");
					$(this).css({"color":"#f00"});
					zweryfikowano=false;
				}
				else
				{
					$(this).css({"color":"#000"});
					//alert($(this).attr('name')+" ok");
				}
				if(!regnip.test(nip))
				{$("input[name='f_7']").css({"color":"#f00"});zweryfikowano=false;}
				else{$("input[name='f_7']").css({"color":"#000"});}				
      });		
	}
	
	//("input:not(:checked) + span")
	if($("#wysylka").is(":not(:checked)") && $("#faktura").is(":not(:checked)"))
	{
		zweryfikowano=true;
	}
	
	if(!regname.test(name) || name==value_pola[0])
	{$("input[name='nadawca']").css({"color":"#f00"});zweryfikowano=false;}
	else{$("input[name='nadawca']").css({"color":"#000"});}
	
	if(!regphone.test(telefon) && telefon!=value_pola[1])
	{$("input[name='telefon']").css({"color":"#f00"});zweryfikowano=false;}
	else{$("input[name='telefon']").css({"color":"#000"});}
	
	if(!regemail.test(email))
	{$("input[name='email']").css({"color":"#f00"});zweryfikowano=false;}
	else{$("input[name='email']").css({"color":"#000"});}
	
	/*
	var zweryfikowano_pliki=false;
	$("form :file").each(function (i) {
		//alert($(this).val())
		if(jQuery.trim($(this).val())!=''){zweryfikowano_pliki=true;}
		else{}
	})
	*/
	var zweryfikowano_pliki=true;
	
	
	startuj();
	if(zweryfikowano_pliki==false){alert('Musisz załączyć plik')}
	if(zweryfikowano==false){alert('Wypełnij poprawnie pola zaznaczone na czerwono')}
	
	//alert(zweryfikowano_pliki+" "+zweryfikowano)
	if(zweryfikowano_pliki==true && zweryfikowano==true)
	{
		//$("form.ajax").ajaxSubmit(options_wyslij);
		//alert('wysłano')
		return true;
	}
	else
	{
		//alert('nie wysłano')
		return false;
	}
	
	//if(!regphone.test(phone)) return ShowMessage("Podaj numer telefonu");
	//if(!regemail.test(mail)) return ShowMessage("Podaj prawidłowy adres email");
	//if(!regtxt.test(txt)||txt=='Wiadomo¶ć') return ShowMessage("Wpisz tre¶ć wiadomo¶ci");
	//return true;
	//return false;
}
	  
	//alert('nazwy pól tekstowych:\n'+nazwy_pol)
	
	//$("form").siblings(":text").css({"color":"red"});

	//$("form").find(":text").
/* 	$("form").siblings(":text").focus(function () {
			if(this.value==$(this).val())this.value='';
			//alert($(this).val())
		}) */
		


 
startuj();
 function startuj()
 {
		//alert('startuj')
		if($("#wysylka").is(":checked"))
		{
			//komunikat='checked';
			$("#dostawa_box input").attr('disabled','');
			$("#odbior").attr('checked','');
			//$("#odbior_box").animate( {"opacity": "0"},500 );	
			//$("#odbior_box").animate({"height": "toggle", "opacity": "toggle"}, { duration: "slow" });
		}
		else
		{
			//komunikat='not checked';
			$("#dostawa_box input").attr('disabled','disabled');
			$("#dostawa_box").css({'background':'#f1f1f1'});
			$("#dostawa_box :text").css({'color':'#000'});
			$("#odbior").attr('checked','checked');	
			//$("#odbior_box").animate( {"opacity": "100"},500 );
			//$("#odbior_box").animate({"height": "toggle", "opacity": "toggle"}, { duration: "slow" });	
		} 

		if($("#faktura").is(":checked"))
		{
			//komunikat='checked';
			$("#faktura_box input").attr('disabled','');
		}
		else
		{
			//komunikat='not checked';
			$("#faktura_box input").attr('disabled','disabled');
			$("#faktura_box").css({'background':'#f1f1f1'});
			$("#faktura_box :text").css({'color':'#000'});			
		}

		if($("#odbior").is(":checked"))
		{
			//komunikat='checked';
			$("#dostawa_box input").attr('disabled','disabled');
			$("#dostawa_box").css({'background':'#f1f1f1'});			
			$("#wysylka").attr('checked','');
			//$("#odbior_box").animate( {"opacity": "100"},500 );
			//$("#odbior_box").animate({"height": "toggle", "opacity": "toggle"}, { duration: "slow" });	
		}
		else
		{
			//komunikat='not checked';
			$("#dostawa_box input").attr('disabled','');
			$("#wysylka").attr('checked','checked');
			//$("#odbior_box").animate( {"opacity": "0"},500 );
			//$("#odbior_box").animate({"height": "toggle", "opacity": "toggle"}, { duration: "slow" });	
		}		
 }
 
 
	$("#wysylka").click(function () {
		
		if($("#wysylka").is(":checked"))
		{
			//komunikat='checked';
			$("#dostawa_box input").attr('disabled','');
			$("#dostawa_box").css({'background':'transparent'});
			$("#odbior").attr('checked','');
			//$("#odbior_box").animate( {"opacity": "0"},500 );
			$("#odbior_box").animate({"height": "toggle", "opacity": "toggle"}, { duration: "300" });	
		}
		else
		{
			//komunikat='not checked';
			$("#dostawa_box input").attr('disabled','disabled');
			$("#dostawa_box").css({'background':'#f1f1f1'});
			$("#odbior").attr('checked','checked');	
			//$("#odbior_box").animate( {"opacity": "100"},500 );	
			$("#odbior_box").animate({"height": "toggle", "opacity": "toggle"}, { duration: "300" });	
		}
		
		//komunikat='test';
		//alert(komunikat);
	})
	
	$("#faktura").click(function () {
		
		if($("#faktura").is(":checked"))
		{
			//komunikat='checked';
			$("#faktura_box input").attr('disabled','');
			$("#faktura_box").css({'background':'transparent'});
		}
		else
		{
			//komunikat='not checked';
			$("#faktura_box input").attr('disabled','disabled');
			$("#faktura_box").css({'background':'#f1f1f1'});	
		}
		
		//komunikat='test';
		//alert(komunikat);
	})	
	
	$("#odbior").click(function () {
		
		if($("#odbior").is(":checked"))
		{
			//komunikat='checked';
			$("#dostawa_box input").attr('disabled','disabled');
			$("#dostawa_box").css({'background':'#f1f1f1'});
			$("#wysylka").attr('checked','');
			//$("#odbior_box").animate( {"opacity": "100"},500 );
			$("#odbior_box").animate({"height": "toggle", "opacity": "toggle"}, { duration: "300" });	
		}
		else
		{
			//komunikat='not checked';
			$("#dostawa_box input").attr('disabled','');
			$("#dostawa_box").css({'background':'transparent'});
			$("#wysylka").attr('checked','checked');
			//$("#odbior_box").animate( {"opacity": "0"},500 );
			$("#odbior_box").animate({"height": "toggle", "opacity": "toggle"}, { duration: "300" });	
		}
		
		//komunikat='test';
		//alert(komunikat);
	})

	$("#k_z_d").click(function () {
		if($("#faktura").is(":checked"))
		{		
			for(i=1;i<=6;i++)
			{
				$("input[name='f_"+i+"']").val($("input[name='d_"+i+"']").val());
			}
		}
		else
		{
			alert('Musisz wybrać fakturę VAT');
		}		
	})
	$("#k_z_f").click(function () {
		if($("#wysylka").is(":checked"))
		{		
			for(i=1;i<=6;i++)
			{
				$("input[name='d_"+i+"']").val($("input[name='f_"+i+"']").val());
			}
		}
		else
		{
			alert('Musisz wybrać wysyłkę');
		}		
	})	


	
    var options_wyslij = {
          //target:        '#myList',   // target element(s) to be updated with server response
          //beforeSubmit:  showRequest,  // pre-submit callback
          url: 'ajax.php',
		  /*resetForm: true,*/
		  resetForm: false,
          success:       showResponse_tekst
      };
    var options_wyslij2 = {
          //target:        '#myList',   // target element(s) to be updated with server response
          //beforeSubmit:  showRequest,  // pre-submit callback
          url: 'ajax.php',
		  resetForm: false,
		  data: {'opts_json' : optiony2},
          success:       showResponse_tekst2
      };	  
  function showResponse_tekst(responseText, statusText)  {

	//if(responseText=='sukces'){alert('Dziękujemy, zamówienie zostało wysłane');document.location.href=document.location.href;}
	//if(responseText=='porazka'){alert('Wystąpił błąd przy wysyłaniu wiadomości. Spróbuj jeszcze raz');document.location.href=document.location.href;}
	//alert(responseText);
	$("#zamowienie").ajaxSubmit({
        url: 'ajax.php',
		resetForm: false,
        success: showResponse_tekst2,
		//data: [ { name: 'id_zamowienia', value: responseText } ]	
		data: {'id_zamowienia' : responseText, 'opts_json' : optiony2}	
		//data: [ { name: 'id_zamowienia', value: responseText }, optiony2]	
	});
  }
  
  function showResponse_tekst2(responseText, statusText)  {

	//if(responseText=='sukces'){alert('Dziękujemy, zamówienie zostało wysłane');document.location.href=document.location.href;}
	//if(responseText=='porazka'){alert('Wystąpił błąd przy wysyłaniu wiadomości. Spróbuj jeszcze raz');document.location.href=document.location.href;}	
	//alert(responseText);
	$("#centruj2 .podsumowanie").html(responseText);
	$("#loader").hide(500);
	$("#centruj").slideUp(500, function () {
			$("#centruj2").slideDown(500, function () {
			$("#centruj").addClass('hidden');
			$("#centruj2").removeClass('hidden');
			//resizeOuterTo(($("#centruj2").width()+42),($("#centruj2").height()+118));
			resizeOuterTo(($("#centruj2").outerWidth(true)+30),($("#centruj2").outerHeight(true)+100));
		});
	})
  }  

  function showResponse_tekst3(responseText, statusText)  {

	//if(responseText=='sukces'){alert('Dziękujemy, zamówienie zostało wysłane');document.location.href=document.location.href;}
	//if(responseText=='porazka'){alert('Wystąpił błąd przy wysyłaniu wiadomości. Spróbuj jeszcze raz');document.location.href=document.location.href;}	
	//alert(responseText);
	$("#centruj2 .podsumowanie").html(responseText);
	$("#loader").hide(500);
	$("#centruj0").slideUp(500, function () {
			$("#centruj2").slideDown(500, function () {
			$("#centruj0").addClass('hidden');
			$("#centruj2").removeClass('hidden');
			//resizeOuterTo(($("#centruj2").width()+42),($("#centruj2").height()+118));
			resizeOuterTo(($("#centruj2").outerWidth(true)+30),($("#centruj2").outerHeight(true)+100));
		});
	})
  }  
var optiony2='';// = new String;  
//var optiony2 = "dupa dupa";  
$("form.ajax").submit(function(){
	/*
	var imie = document.getElementById("imie").value;
	var nazwisko = document.getElementById("nazwisko").value;
	var ulica = document.getElementById("ulica").value;
	var miasto = document.getElementById("miasto").value;
	var kod = document.getElementById("kod").value;
	
   if(imie!='' && nazwisko!='' && ulica!='' && miasto!='' &&kod!='')
   {
	alert("Wiadomość została wysłana.");
	$(this).ajaxSubmit(options_wyslij);
   }
   else
   {
	alert("Wiadomość nie została wysłana.\n Wypełnij pola z gwiazdką.");
   }
   */
   //$(this).ajaxSubmit(options_wyslij);
   //Sprawdz();
   //var wysylaj=true;
   //var formularz = $(this);
   var wysylaj=Sprawdz();
   //alert(wysylaj)
   if(wysylaj)
   {
	//$("#zamowienie").ajaxSubmit(options_wyslij2);
	var options_tab=[];
	$("#zamowienie").find("option:selected").each(function(i) {
		//options_tab.push('"select['+$(this).parent("select").attr('name')+']" : "'+$(this).text()+'"');
		//options_tab.push('"'+$(this).parent("select").attr('name')+'" : "'+$(this).text()+'"');
		//options_tab.push('\"'+$(this).parent("select").attr('name')+'\" : \"'+$(this).text()+'\"');
		//options_tab.push('"'+$(this).parent("select").attr('name')+'" : "'+$(this).text()+'"');
		//options_tab.push("\\\""+$(this).parent("select").attr('name')+"\\\" \\\: \\\""+$(this).text()+"\\\"");
		//optiony+=(', "'+$(this).parent("select").attr('name')+'" : "'+$(this).text()+'"');
		//options_tab.push('{name: "'+$(this).parent("select").attr('name')+'" , value:"'+$(this).text()+'"}');
		options_tab.push("'"+$(this).parent("select").attr('name')+"' : '"+$(this).text()+"'");
	})
	//optiony2 = "\\\{"+options_tab.join(',')+"\\\}"; 
	optiony2 = "{"+options_tab.join(',')+"}"; 
	//optiony2 = options_tab.join(',');
	//alert(optiony2);
	
	if(confirm("Uwaga formularz zostanie wysłany!\nKontynuować?"))
	{
		$("#loader").show(500);
		$(this).ajaxSubmit(options_wyslij);
	}
   }
   
   //$(this).ajaxSubmit(Sprawdz);
   return false;
  }); 
	
})

function sortNumberDesc(a, b)
{
return b - a;
}
function sortNumberAsc(a, b)
{
return a - b;
}
function sortFirstNumberDesc(a, b)
{
return b[0] - a[0];
}
function licz(tab,pole,typ)
{
	//alert(tab+" "+pole+" "+typ);
	//wartosc=$("form#zamowienie :input[name="+pole+"]").val();
	//alert("tab.length="+tab.length);
	//tab = jQuery.unique(tab);
	//alert("tab.length="+tab.length);
	wartosc=undefined;
	var raport = 'pole = '+pole+"\n";
	 /* $.each(tab, function(index, value) {
		raport+=index+" => "+value+"\n";
	}); */ 
	
	tab.sort(sortFirstNumberDesc);
	
	
	 raport+="PO SORTOWANIU\n";
	/* $.each(tab, function(index, value) {
		raport+=index+" => "+value+"\n";
	}); */ 
	
	$.each(tab, function(index, value) {
		if(pole>value[0])
		{
			raport+="Wybrano z tablicy: "+value[1]+"\n";
			//break;
			wartosc = value[1];
			return false;
		}
	});
	
	//tab.reverse();
	
	

	/*
	var test=0;
	for(i=0;i<=tab.length;i++)
	{
		res=wartosc;
	}
	*/
	//alert(test);
	
	if(typ==1 || typ==undefined)
	{
		if(wartosc==undefined){wartosc=pole;}
		var res=wartosc;
		raport+="---------------\ntyp: "+typ+"\nwartosc: "+wartosc+"\npole: "+pole+"\nres: "+res;
	}
	else if(typ==2)
	{
		if(wartosc==undefined){wartosc=0;}
		res=pole*(100-wartosc)/100;
		raport+="---------------\ntyp: "+typ+"\nwartosc: "+wartosc+"\npole: "+pole+"\nres: "+res;
	}
	//alert(raport);
	return res;
}

function pozycja(zmienna)
{
	/*
	//alert('ddfdf');
	alert(document.getElementsByName(zmienna)[0].selectedIndex);
	var sel;
	$.each($("select[name='"+zmienna+"'] option"), function(index, value) {
		if(value.selected)
		{
			sel=index;
			return sel;
		}
	})
	return sel;
	*/
	return document.getElementsByName(zmienna)[0].selectedIndex;
}
function podmien(zmienna,tab,gdzie)
{
	//alert("podmien: "+zmienna);	
/*
	var j=0;
	for(var i=1;i<=1000;i++)
	{
		j++;
	}
*/	
	var raport="";
	var sel;
	var opt = "";
	var komunikat="";
/*
	$.each($("select[name='"+zmienna+"'] option"), function(index, value) {
		komunikat+=index+" => "+value.selected+"\n";
		if(value.selected)
		{
			sel=index;
		}
	})
*/	
	sel=document.getElementsByName(zmienna)[0].selectedIndex;
	
	$.each(tab[sel], function(index, value) {
		//if(index==0){sel=value[0];}
		raport+=index+" => "+value[0]+" => "+value[1]+"\n";
		opt+="<option value='"+value[0]+"'>"+value[1]+"</option>"
		//raport+=index+" => "+value+"\n";
	});
	
	//komunikat="gdzie.val =>"+$("select[name='"+gdzie+"']").val()+"\n";
	//komunikat+="gdzie.eq =>"+$("select[name='"+gdzie+"']").eq()+"\n";
	
	
	
	
	//alert(komunikat)
	//alert("zmienna: "+zmienna+"\n"+sel+"\ngdzie: "+gdzie+"\n\n"+raport);
	//res='{"podmien":"'+sel+'"}';
	res='{"podmien":"'+opt+'"}';
	return res;
}
function przelicz(dane)
{
	var regliczba = /^[0-9.]{1,}$/;
	var dane = unescape(dane);
	komendy=dane.split(';');
	//alert(dane+"\n"+komendy.length);
	for(nr_kom=0;nr_kom<komendy.length;nr_kom++)
	{
		if(jQuery.trim(komendy[nr_kom])!='')
		{
			//alert(nr_kom);
			//daneW=dane.split('=', 2);
			daneW=komendy[nr_kom].split('=', 2);
			//var arr = new Array();
			arr = daneW[0].match(/'{1}[\w]+'{1}/g);
			var wart = new Array();
			var dzialanie=daneW[0];
			//alert(dzialanie);
			testtxt0='';
			//alert(arr.length); 
			if(arr!=null)
			{
				for(i=0;i<arr.length;i++)
				{
					
					//if ($(this).is(":contains('Peter')"))
					//$("div:has(p)").addClass("test");
					//if($(this).is("input[rel='mliczba']"))
					if($("form#zamowienie :input[name="+arr[i]+"][rel='mliczba']").length>0)
					{
						//alert(arr[i]+" "+mwartosc[arr[i].replace(/'/gi, "")]);
						if($("form#zamowienie :input[name="+arr[i]+"]").val()<mwartosc[arr[i].replace(/'/gi, "")])
						{
							//alert(arr[i]);
							//$(this).val(mwartosc[$(this).attr('name')]);
							wart[i]=mwartosc[arr[i].replace(/'/gi, "")];
							$("form#zamowienie :input[name="+arr[i]+"]").val(mwartosc[arr[i].replace(/'/gi, "")])
						}
						else
						{
							wart[i]=$("form#zamowienie :input[name="+arr[i]+"],form#zamowienie input[name="+arr[i]+"]").val();
						}			
					}
					else
					{
						//alert(arr[i]);
						if($("form#zamowienie :radio[name="+arr[i]+"]:enabled:checked").length>0)
						{
							wart[i]=$("form#zamowienie :input[name="+arr[i]+"]:checked").val();	
							testtxt0+="radio: "+arr[i]+" = "+wart[i]+"\n";		
						}
						else if($("form#zamowienie :checkbox[name="+arr[i]+"]:enabled:checked").length>0)
						{
							wart[i]=$("form#zamowienie :input[name="+arr[i]+"]:checked").val();	
							testtxt0+="checkbox: "+arr[i]+" = "+wart[i]+"\n";				
						}
						else if($("form#zamowienie select[name="+arr[i]+"] option:selected").length>0)
						{
							wart[i]=$("form#zamowienie select[name="+arr[i]+"] option:selected").val();	
							testtxt0+="select: "+arr[i]+" = "+wart[i]+"\n";	
						}
						else if($("form#zamowienie :text[name="+arr[i]+"]:enabled").length>0)
						{
							wart[i]=$("form#zamowienie :input[name="+arr[i]+"]").val();	
							testtxt0+="text: "+arr[i]+" = "+wart[i]+"\n";				
						}
						else if($("form#zamowienie :password[name="+arr[i]+"]:enabled").length>0)
						{
							wart[i]=$("form#zamowienie :password[name="+arr[i]+"]").val();	
							testtxt0+="password: "+arr[i]+" = "+wart[i]+"\n";				
						}			
						else if($("form#zamowienie input[type='hidden'][name="+arr[i]+"]").length>0)
						{
							wart[i]=$("form#zamowienie :input[name="+arr[i]+"]").val();	
							testtxt0+="hidden: "+arr[i]+" = "+wart[i]+"\n";				
						}
						else
						{
							wart[i]=0;
						}			
						
						//wart[i]=$("form#zamowienie :input[name="+arr[i]+"],form#zamowienie input[name="+arr[i]+"]").val();
					}
					if(!regliczba.test($("form#zamowienie :input[name="+arr[i]+"]").val())){return alert("wypełnij pola");}
					dzialanie=dzialanie.replace(arr[i], wart[i]);
				}
			}
			
			test_txt='';
			/* for(i=0;i<arr.length;i++)
			{
				test_txt+=arr[i]+" => "+wart[i]+"\n";
			} */
			
			test_txt+=dzialanie;
			//alert(testtxt0+"OBLICZENIA:\n"+test_txt);
			//alert("dzialanie: "+eval(dzialanie));
			
			//alert("testuje");
			
			//alert(dzialanie);
			var daneobj;
			//if(!isNaN(eval(dzialanie))){
			//alert(dzialanie);
			//alert(eval(dzialanie));
			if(!isNaN(eval(dzialanie))){
				//alert("liczba"+eval(dzialanie))
				daneobj = eval("("+eval(dzialanie)+")");
				if(daneobj.podmien)
				{
					//alert("daneobj.podmien: "+daneobj.podmien);
					$("form#zamowienie select[name="+daneW[1]+"]").html(daneobj.podmien);
				}			
				else if($("form#zamowienie :input[name="+daneW[1]+"][rel='mliczba']").length>0)
				{
						if(eval(dzialanie)<mwartosc[daneW[1].replace(/'/gi, "")])
						{
							if($("form#zamowienie :input[name="+daneW[1]+"]").attr("accept")>=0 && $("form#zamowienie :input[name="+daneW[1]+"]").attr("accept")!=''){var koma=$("form#zamowienie :input[name="+daneW[1]+"]").attr("accept")}else{var koma=2;}
							$("form#zamowienie :input[name="+daneW[1]+"]").val(mwartosc[daneW[1].replace(/'/gi, "")].toFixed(koma));
							$("form#zamowienie select[name="+daneW[1]+"]").val(mwartosc[daneW[1].replace(/'/gi, "")]);
						}
						else
						{
							if($("form#zamowienie :input[name="+daneW[1]+"]").attr("accept")>=0 && $("form#zamowienie :input[name="+daneW[1]+"]").attr("accept")!=''){var koma=$("form#zamowienie :input[name="+daneW[1]+"]").attr("accept")}else{var koma=2;}
							wynik_dzialania=eval(dzialanie);wynik_dzialania=Number(wynik_dzialania)//.toFixed(2)
							$("form#zamowienie :input[name="+daneW[1]+"]").val(wynik_dzialania.toFixed(koma));
							$("form#zamowienie select[name="+daneW[1]+"]").val(wynik_dzialania);
						}	
				}
				else
				{
					if($("form#zamowienie :input[name="+daneW[1]+"]").attr("accept")>=0 && $("form#zamowienie :input[name="+daneW[1]+"]").attr("accept")!=''){var koma=$("form#zamowienie :input[name="+daneW[1]+"]").attr("accept")}else{var koma=2;}
					wynik_dzialania=eval(dzialanie);wynik_dzialania=Number(wynik_dzialania)
					//alert(wynik_dzialania)
					$("form#zamowienie :input[name="+daneW[1]+"]").val(wynik_dzialania.toFixed(koma));
					$("form#zamowienie select[name="+daneW[1]+"]").val(wynik_dzialania);
				}				
			}
			else if(dzialanie.match("podmien"))
			{
				var json=eval(dzialanie);
				//alert(json+"\n"+jQuery.isPlainObject(json)+"\n"+jQuery.isFunction(podmien));
				//if(jQuery.parseJSON(json)){alert('json');}else{alert('nie json');}
				//var obj = jQuery.parseJSON(json);
				//alert( obj.podmien+" === true" );
				//alert(dzialanie+"\n"+eval(dzialanie));
				daneobj = eval('('+eval(dzialanie)+')');
				if(daneobj.podmien)
				{
					//alert("daneobj.podmien: "+daneobj.podmien);
					$("form#zamowienie select[name="+daneW[1]+"]").html(daneobj.podmien);
				}			
			}
			else
			{
				//alert("string");
				//if(!isNaN(eval(dzialanie))){}
				//alert(dzialanie);
				//alert(jQuery.isFunction(dzialanie)+"\n"+dzialanie+"\n"+eval(dzialanie))
				//alert(jQuery.isEmptyObject(eval(dzialanie)));
				//alert(eval(dzialanie)+"\n"+jQuery.isPlainObject(eval(dzialanie))+"\n"+jQuery.isFunction(podmien));
				//alert(dzialanie.match("podmien"));
				
				//else
				//{
				$("form#zamowienie :input[name="+daneW[1]+"]").val(eval(dzialanie));
				$("form#zamowienie select[name="+daneW[1]+"]").val(eval(dzialanie));
				//} 
			}
			//daneobj = eval("("+eval(dzialanie)+")");
			//daneobj = jQuery.parseJSON(dzialanie);
			//alert(daneobj)

		}
	}	
}



function resizeOuterTo(w,h) {
 if (parseInt(navigator.appVersion)>3) {
   if (navigator.appName=="Netscape") {
    top.outerWidth=w;
    top.outerHeight=h;
   }
   else top.resizeTo(w,h);
 }
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}





