/**
 * @author ANtek
 */

 $(document).ready(function(){
 		var required = "To pole jest wymagane";
 		var evt = $.browser.msie ? "click" : "change";
			
				
				var doValidacji = {
					rules: {
						kontakt_email: {
							required: true,
							email: true
						},
						faktura_pelna_nazwa: {
							required: true,
							minlength: 2
						},
						faktura_adres: {
							required: true,
							minlength: 5
						},					
						faktura_miejscowosc: {
							required: true,
							minlength: 3
						},										
						faktura_kod_pocztowy: {
							required: true,
							postCode: true
						},
						faktura_nip: {
							required: true,
							nip: true
						},
						kontakt_imie: {
							required: true,
							minlength: 2
						},
						kontakt_nazwisko: {
							required: true,
							minlength: 2
						},								
						kontakt_telefon: {
							required: true,
							minlength: 9
						}							
					},
					messages: {
						kontakt_email: {
							required: "To pole jest wymagane",
							email: "Proszę podać właściwy email"
						},
						faktura_pelna_nazwa: {
							required: "To pole jest wymagane",
							minlength: "Proszę podać właściwą nazwę!"
						},							
						faktura_adres: {
							required: "To pole jest wymagane",
							minlength: "Proszę podać właściwy adres!"
						},						
						faktura_miejscowosc: {
							required: "To pole jest wymagane",
							minlength: "Proszę podać właściwą miejscowość!"
						},					
						faktura_kod_pocztowy: {
							required: "To pole jest wymagane",
							postCode: "Proszę podać właściwy kod pocztowy"
						},
						faktura_nip: {
							required: "To pole jest wymagane",
							nip: "Proszę podać właściwy nip"
						},
						kontakt_imie: {
							required: "To pole jest wymagane",
							minlength: "Proszę podać właściwe imię!"
						},
						kontakt_nazwisko: {
							required: "To pole jest wymagane",
							minlength: "Proszę podać właściwe nazwisko!"
						},
						kontakt_telefon: {
							required: "Pole wymagane ze względu na obsługę kurierską",
							minlength: "Proszę podać właściwy numer telefonu!"
						}			
					}			
				};		
				
	var adres_wysylkowy_var = function() {
			 		$("#wysylka_adresat").rules("add", {
			 			required: true,
			 			minlength: 3,
			 			messages: {
				 			required: "To pole jest wymagane",
							minlength: "Proszę podać właściwego adresata!"
						}
			 		});	
			 		$("#wysylka_adres").rules("add", {
			 			required: true,
			 			minlength: 5,
			 			messages: {
				 			required: "To pole jest wymagane",
							minlength: "Proszę podać właściwy adres!"
						}
			 		});	
			 		$("#wysylka_kod_pocztowy").rules("add", {
			 			required: true,
			 			postCode: true,
			 			messages: {
				 			required: "To pole jest wymagane",
							postCode: "Proszę podać właściwego adresata!"
						}
			 		});	
			 		$("#wysylka_miejscowosc").rules("add", {
			 			required: true,
			 			minlength: 3,
			 			messages: {
				 			required: "To pole jest wymagane",
							minlength: "Proszę podać właściwą miejscowość!"
						}
			 		});	

				};					
							
// Platnosci		
		var platnosc_1 = "<strong>Pobranie.</strong>  Płatność gotówką u kuriera przy odbiorze. Realizacja zamówienia w ciągu 1-2 dni.";
		var platnosc_2 = "<strong>Przelew.</strong>  W końcowym etapie zamówienia zostanie wyświetlona faktura proforma z numerem konta i należną kwotą. Realizacja zamówienia nastąpi po wpłynięciu należności na nasze konto.";
		var platnosc_3 = "<strong>Płatność kartą.</strong> Po zakończeniu procedury zamawiania zostaniesz przełączony do operatora DotPay.pl obsługującego płatności kartami poprzez internet. Połączenie będzie szyfrowane. Dane o karcie zostaną przekazane tylko do tego operatora i nie docierają do naszej firmy. Realizacja zamówienia w ciągu 1-2 dni.";
		var platnosc_4 = "<strong>Płatność mTransfer.</strong> Po zakończeniu procedury zamawiania zostaniesz przełączony do systemu mBank-u obsługującego płatność mTransfer. Połączenie będzie szyfrowane. Realizacja zamówienia w ciągu 1-2 dni.";
		
		if($("#platnosc_1:checked").val() != null) {
			$("#platnosc_opis").html(platnosc_1);
			zmianaCen(false);
			$("#infoDiscountMtrans").hide("fast");
		} else if($("#platnosc_2:checked").val() != null) {
			$("#platnosc_opis").html(platnosc_2);
			zmianaCen(false);
			$("#infoDiscountMtrans").hide("fast");
		} else if($("#platnosc_3:checked").val() != null) {
			$("#platnosc_opis").html(platnosc_3);
			zmianaCen(false);
			$("#infoDiscountMtrans").hide("fast");
		} else if($("#platnosc_5:checked").val() != null) {
			$("#platnosc_opis").html(platnosc_4);
			zmianaCen(true);
			$("#infoDiscountMtrans").show("fast");
		}
		$("#platnosc_1").bind(evt, function() {
			$("#platnosc_opis").html(platnosc_1);
			zmianaCen(false);
			$("#infoDiscountMtrans").hide("fast");
		});
		$("#platnosc_2").bind(evt, function() {
			$("#platnosc_opis").html(platnosc_2);
			zmianaCen(false);
			$("#infoDiscountMtrans").hide("fast");
		});	
		$("#platnosc_3").bind(evt, function() {
			$("#platnosc_opis").html(platnosc_3);
			zmianaCen(false);
			$("#infoDiscountMtrans").hide("fast");
		});		
		$("#platnosc_5").bind(evt, function() {
			$("#platnosc_opis").html(platnosc_4);
			zmianaCen(true);
			$("#infoDiscountMtrans").show("fast");
		});			
// end platnosci							

    	$("#dodatki_do_stempel_plus .add").click(function () {
    		var index = $(this).attr("title");
    		var selektor = "#stemp_plus_" + index;	
    		$(selektor).attr("value", zwrocZmienionaLiczbe(parseInt($(selektor).attr("value")), "add"));		
//    		updateCeny();

    	}); 
    	$("#dodatki_do_stempel_plus .sub").click(function () {
    		var index = $(this).attr("title");
    		var selektor = "#stemp_plus_" + index;    			
    		$(selektor).attr("value", zwrocZmienionaLiczbe(parseInt($(selektor).attr("value")), "sub"));		
//  			updateCeny();

    	});     

    	$(".programy_dodatki_mini .add").click(function () {
    		var index = $(this).attr("title");
    		var selektor = "#stemp_mini_" + index;	

    		$(selektor).attr("value", zwrocZmienionaLiczbe(parseInt($(selektor).attr("value")), "add"));

		   	if($("#dodatek_mini input").val() > 0) {  
		   		$("#dodatki_do_stempel_mini").slideDown("normal");	
		   	} else {
		   		$("#dodatki_do_stempel_mini").slideUp("normal");
		   	}
 
    		if(parseInt($("#stemp_mini_2").attr("value")) > parseInt($("#stemp_mini_1").attr("value"))) {
    			$("#stemp_mini_2").attr("value", $("#stemp_mini_1").attr("value"));
    		}  		   	    		
    	}); 
    	$(".programy_dodatki_mini .sub").click(function () {
    		var index = $(this).attr("title");
    		var selektor = "#stemp_mini_" + index;    			
    		$(selektor).attr("value", zwrocZmienionaLiczbe(parseInt($(selektor).attr("value")), "sub"));
		   	if($("#dodatek_mini input").val() > 0) {  
		   		$("#dodatki_do_stempel_mini").slideDown("normal");	
		   	} else {
		   		$("#dodatki_do_stempel_mini").slideUp("normal");
		   		$("#dodatki_do_stempel_mini input").attr("value",0);
		   	}    
		   	
    		if(parseInt($("#stemp_mini_2").attr("value")) > parseInt($("#stemp_mini_1").attr("value"))) {
    			$("#stemp_mini_2").attr("value", $("#stemp_mini_1").attr("value"));
    		}  			   			
		   	
    	});         	
   	
    	$("input.number").change(function() {
    		var value_num = $(this).attr("value");
    		if(!isNaN(Number(value_num))) {
    			$(this).attr("value", value_num);
    		} else {
    			$(this).attr("value", 0);
    		}	
    	});    	
    	
    	$("#stemp_mini_1").change(function() {
    		if(parseInt($("#stemp_mini_2").attr("value")) > parseInt($("#stemp_mini_1").attr("value"))) {
    			$("#stemp_mini_2").attr("value", $("#stemp_mini_1").attr("value"));
    		}  
    	});    	
    	
    	$("#stemp_mini_2").change(function() {
    		if(parseInt($("#stemp_mini_2").attr("value")) > parseInt($("#stemp_mini_1").attr("value"))) {
    			$("#stemp_mini_2").attr("value", $("#stemp_mini_1").attr("value"));
    		}  
    	});    	

		if($("#stemp_plus_1:checked").val() != null) {
			$("#dodatki_do_stempel_plus").show("normal");
		}
    	$("#stemp_plus_1").bind(evt, function () {
    	    if($("#stemp_plus_1:checked").val() != null) {
    			$("#dodatki_do_stempel_plus").slideDown("normal");
    			$("#stemp_plus_4").attr("value",0);
    			ustawNormalneCeny();
    		} else {
    			$("#dodatki_do_stempel_plus").slideUp("normal");
    		}
    	});

    	if($("#dodatek_mini input").val() > 0) {
    			$("#dodatki_do_stempel_mini").slideDown("normal");
    	}
    	else {
    	}
    	
    	// dodatek stempel mini
    	$("#dodatek_mini input").change(function() {
		   	if($("#dodatek_mini input").val() > 0) {  
		   		$("#dodatki_do_stempel_mini").slideDown("normal");	
		   	} else {
		   		$("#dodatki_do_stempel_mini").slideUp("normal");
		   		$("#dodatki_do_stempel_mini input").attr("value",0);
		   	}
    	});
    	
    	
    	if($("#adres_inny_wysylkowy:checked").val() != null) {
    		$("#order_firstStep_stemp").validate(doValidacji);
    		$("#hiddenBox").show(1, adres_wysylkowy_var);	  	
    		
    		$("#wysylka_adresat input").attr("class","required");	  			
    	}
    	$("#adres_inny_wysylkowy").bind(evt, function () {
    	    $("#order_firstStep_stemp").validate(doValidacji);	
			$("#hiddenBox").slideDown("normal",adres_wysylkowy_var);	
		});
    	$("#adres_wysylkowy").bind(evt, function () {
    		$("#order_firstStep_stemp").validate(doValidacji);
    		$("#hiddenBox input").attr("value", "");
			$("#hiddenBox").slideUp("normal");
			$("#wysylka_adresat").rules("remove");
			$("#wysylka_adres").rules("remove");
			$("#wysylka_kod_pocztowy").rules("remove");
			$("#wysylka_miejscowosc").rules("remove");
		});			
// Osoby prywatne / Firmy		

$.ajaxSetup({
	contentType: "application/x-www-form-urlencoded; charset=windows-1250"
});


    $("#stemp_mini_1").bind(evt, function () {
            $("#dodatki_do_stempel_plus").slideDown("normal");
            $("#dodatki_stemp_mini_ant").show();
            $("#dodatki_stemp_plus_ant input").removeAttr("checked");
            $("#dodatki_stemp_plus_ant").hide();
            $("#stemp_plus_1").removeAttr("checked");
            $("#stemp_plus_4").attr("value",0);

    });
    $("#stemp_plus_1").bind(evt, function () {
            $("#dodatki_do_stempel_plus").slideDown("normal");
            $("#dodatki_stemp_plus_ant").show();
            $("#dodatki_stemp_mini_ant input").removeAttr("checked");
            $("#dodatki_stemp_mini_ant").hide();
            $("#stemp_mini_1").removeAttr("checked");
            ustawNormalneCeny();
    });

    if($("#stemp_mini_1:checked").val() != null) {
            $("#dodatki_do_stempel_plus").show();
            $("#dodatki_stemp_mini_ant").show();
            $("#dodatki_stemp_plus_ant input").removeAttr("checked");
            $("#dodatki_stemp_plus_ant").hide();
            $("#stemp_plus_1").removeAttr("checked");
            $("#stemp_plus_4").attr("value",0);
    }

    if($("#stemp_plus_1:checked").val() != null) {
            $("#dodatki_do_stempel_plus").show();
            $("#dodatki_stemp_plus_ant").show();
            $("#dodatki_stemp_mini_ant input").removeAttr("checked");
            $("#dodatki_stemp_mini_ant").hide();
            $("#stemp_mini_1").removeAttr("checked");
            ustawNormalneCeny();
    }



//Przeliczanie produktow


		
		
		//Validation Step 1
//		$("#order_firstStep").validate();

		$("#dalej_st1").click( function () {			
			$("#order_firstStep_stemp").validate(doValidacji);
		});

		updateCeny();		
 });
 
 
function ustawNormalneCeny() {
	netto = '174,00';
	brutto = '212,28';

	$("#dodatki_do_stempel_plus .cenna_netto").text(netto+" PLN");
	$("#dodatki_do_stempel_plus .cenna_brutto").text(brutto+" PLN");
	
	$("#dodatkowe_stanowisko_cena_netto").attr("value",netto);
	$("#dodatkowe_stanowisko_cena_brutto").attr("value",brutto);
	
}


function updateCeny() {
			var ilosc = 0;

			ilosc = parseInt($("#stemp_plus_4").attr("value"));

			var netto, brutto;
			if(ilosc<5) {
				netto = '174,00';
				brutto = '212,28';
				
				$("#dodatki_do_stempel_plus .cenna_netto").text(netto+" PLN");
				$("#dodatki_do_stempel_plus .cenna_brutto").text(brutto+" PLN");
				
				$("#dodatkowe_stanowisko_cena_netto").attr("value",netto);
				$("#dodatkowe_stanowisko_cena_brutto").attr("value",brutto);
			}
			else if(ilosc<10 && ilosc >=5) {
				netto = '116,00';
				brutto = '141,52';
				
				$("#dodatki_do_stempel_plus .cenna_netto").text(netto+" PLN");
				$("#dodatki_do_stempel_plus .cenna_brutto").text(brutto+" PLN");
				
				$("#dodatkowe_stanowisko_cena_netto").attr("value",netto);
				$("#dodatkowe_stanowisko_cena_brutto").attr("value",brutto);
			}
			else if(ilosc>=10) {
				netto = '87,00';
				brutto = '106,14';
			
				$("#dodatki_do_stempel_plus .cenna_netto").text(netto+" PLN");
				$("#dodatki_do_stempel_plus .cenna_brutto").text(brutto+" PLN");
				
				$("#dodatkowe_stanowisko_cena_netto").attr("value",netto);
				$("#dodatkowe_stanowisko_cena_brutto").attr("value",brutto);
			}		
	$('#alert').jqm({overlay: 0, modal: true, trigger: false});
	
	// trigger an alert whenever links of class alert are pressed.
	$('a.alert').click(function() { 
	  alert('You Have triggered an alert!'); 
	  return false;
	});			
			

}// End of jquery.ready

/* Overriding Javascript's Alert Dialog */

function alert(msg) {
  $('#alert')
    .jqmShow()
    .find('div.jqmAlertContent')
      .html(msg);
}

$().ready(function() {

});


/* Overriding Javascript's Confirm Dialog */

// NOTE; A callback must be passed. It is executed on "cotinue". 
//  This differs from the standard confirm() function, which returns
//   only true or false!

// If the callback is a string, it will be considered a "URL", and
//  followed.

// If the callback is a function, it will be executed.


function confirm(msg,callback) {
  $('#confirm')
    .jqmShow()
    .find('p.jqmConfirmMsg')
      .html(msg)
    .end()
    .find(':submit:visible')
      .click(function(){
        if(this.value == 'zakończ')
          (typeof callback == 'string') ?
            window.location.href = callback :
            document.last_step.submit();
        $('#confirm').jqmHide();
      });
}

function zmianaCen(zm) {
	var tmp;
	var main_selektor = "#programy_";
	
	for(var i=1;i<2;++i) {
	//	document.write($(main_selektor + i + " .cenna_netto").text());
		if ((parseFloat($(main_selektor + i + " .cenna_netto").text()) > parseFloat($(main_selektor + i + " .cenna_netto_tmp").attr("title"))) && zm == true) {
			switchFieldWithPrice(main_selektor, i, ".cenna_netto", ".cenna_netto_tmp");
			switchFieldWithPrice(main_selektor, i, ".cenna_brutto", ".cenna_brutto_tmp");	
		} else if ((parseFloat($(main_selektor + i + " .cenna_netto").text()) < parseFloat($(main_selektor + i + " .cenna_netto_tmp").attr("title"))) && zm == false) {
			switchFieldWithPrice(main_selektor, i, ".cenna_netto", ".cenna_netto_tmp");
			switchFieldWithPrice(main_selektor, i, ".cenna_brutto", ".cenna_brutto_tmp");				
		}
	}
	// Dla stempla mini
	var main_selektor = "#dodatek_mini";	
	var i = '';
	if ((parseFloat($(main_selektor + i + " .cenna_netto").text()) > parseFloat($(main_selektor + i + " .cenna_netto_tmp").attr("title"))) && zm == true) {
		switchFieldWithPrice(main_selektor, i, ".cenna_netto", ".cenna_netto_tmp");
		switchFieldWithPrice(main_selektor, i, ".cenna_brutto", ".cenna_brutto_tmp");	
	} else if ((parseFloat($(main_selektor + i + " .cenna_netto").text()) < parseFloat($(main_selektor + i + " .cenna_netto_tmp").attr("title"))) && zm == false) {
		switchFieldWithPrice(main_selektor, i, ".cenna_netto", ".cenna_netto_tmp");
		switchFieldWithPrice(main_selektor, i, ".cenna_brutto", ".cenna_brutto_tmp");				
	}

}
function switchFieldWithPrice(main_selektor, counter, main_field, tmp_field) {
	tmp = $(main_selektor + counter + " " + main_field).text();
	$(main_selektor + counter + " " + main_field).text($(main_selektor + counter + " " + tmp_field).attr("title"));
	$(main_selektor + counter + " " + tmp_field).attr("title",tmp);
}

$().ready(function() {
  $('#confirm').jqm({overlay: 88, modal: true, trigger: false});
  
  // trigger a confirm whenever links of class alert are pressed.
  $('.confirm').click(function() { 
    confirm('Jeżeli chcesz jeszcze coś poprawić lub uzupełnić kliknij wróć, jeżeli wszystko jest w porządku, zakończ zamówienie.', 1); 
    return false;
  });
});

// End of  Overriding Javascript's Confirm Dialog


 function zwrocZmienionaLiczbe(liczba, dzialanie) {
 	if (isNaN(liczba)) {
 		return 0;
 	}
 	switch(dzialanie) {
 		case 'add' : {
 			if(liczba < 99) {
 				return ++liczba;
 			} else {
 				return liczba;
 			}
 		} break;
 		case 'sub' : {
 			if(liczba > 0) {
 				return --liczba;
 			} else {
 				return liczba;
 			}
 		} break;
 	}
 	return 0;
 }
 
 function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}
 	

function isNumeric(vTestValue)
{

}


