	// extend the current rules with new groovy ones

	// this one requires the text "buga", we define a default message, too
//	$.validator.addMethod("buga", function(value) {
//		return value == "buga";
//	}, 'Please enter "buga"!');
//
//	// this one requires the value to be the same as the first parameter
//	$.validator.methods.equal = function(value, element, param) {
//		return value == param;
//	};

$(document).ready(function(){
    // *** FORMULARZ 1
    if(document.frmCheckout_step1) {
        //alert('step1');

        $("#metoda_platnosci_pojemnik").fadeOut(0);
        $("#metoda_platnosci_pobranie").fadeOut(0);
        $(".shippingId_radio").click(function () {
            if(($(this).attr('value') == 'odbior_osobisty')||($(this).attr('value') == 'poczta_przedplata')||($(this).attr('value') == 'kurier_przedplata')) {
                //Typ platnosci : przy odbiorze - odbior oso, platnosc w platnosci
                $("option[value='platn_przy_odbiorze']").remove();
                $("#metoda_platnosci_pobranie").fadeOut(0);
                if($(this).attr('value') == 'odbior_osobisty') {
                    // Dodawanie opcji "platnosc przy odbiorze" do listy typow platnosci
                    $("select[name='pay_type']").append('<option value="platn_przy_odbiorze">Płatność przy odbiorze</option>');
                }
                $("#metoda_platnosci_pojemnik").fadeIn('slow');
                $("input[name='btnStep1']").attr("disabled","disabled");
                $("input[name='payment_type']").attr('value', $(this).attr('value'));
            } else if(($(this).attr('value') == 'kurier_pobranie')||($(this).attr('value') == 'poczta_pobranie')) {
                //Typ platnosci : Za pobraniem
                $("#metoda_platnosci_pojemnik").fadeOut(0);
                $("#metoda_platnosci_pobranie").fadeIn('fast');
                $("input[name='btnStep1']").removeAttr("disabled");
                $("select[name='pay_type'] option[value='']").attr('selected', 'selected');

                $("input[name='payment_type']").attr('value', $(this).attr('value'));

            } else {
                //usuwanie opcji platnosc przy odbiorze do listy typow platnosci
                // jezeli zostala wybranna inna opcja przesylki
                //$("option[value='platn_przy_odbiorze']").remove();
                alert('Niewlasciwy kod metody platnosci: '+$(this).attr('value'));
                $("#metoda_platnosci_pojemnik").hide();
                $("#metoda_platnosci_pobranie").hide();
                $("select[name='pay_type'] option[value='']").attr('selected', 'selected');
                $("input[name='btnStep1']").attr("disabled","disabled");
                $("input[name='payment_type']").attr('value', '');
            }
            var kwota_tmp = (parseInt(kwota_do_zaplaty)+parseInt($(this).attr('alt')))/100;

            $("#calkowita_kwota_pojemnik").text(kwota_tmp.numberFormat("? ??? ???.??"));
            //alert($(this).attr('title'));
            //alert((kwota_do_zaplaty/100).toFixed(2));
            //$("#status1").text('Ok');

        });//$(".shippingId_radio").click(function ()
        $("select[name='pay_type']").change(function () {
            if($(this).val()=='') {
                $("#status2").text('Proszę wybrać metodę płatności!');
                $("input[name='btnStep1']").attr("disabled","disabled");
            } else {
                $("#status2").text('Ok');
                $("input[name='btnStep1']").removeAttr("disabled");
            }
            }).change();
    }//if(document.frmCheckout_step1) {


// *** FORMULARZ 2
if(document.frmCheckout_step2) {
		$("#frmCheckout_step2").validate({

                rules: {
                    klient_imie: "required",
                    klient_nazwisko: "required",
                    klient_tel: "required",
                    klient_email: {required: true, email: true},
                    klient_ulica: {
                      required: function(element) {
                        var przesylka = zaznaczonaPrzesylka();
                        var platnosc = zaznaczonaMetodaPlatnosci();
                        if((przesylka=='odbior_osobisty')) {
                            // Pole nie wymagane
                            return false;
                        } else {
                            // Pole wymagane
                            return true;
                        };
                      }
                    },
                    klient_nr_domu: {
                      required: function(element) {
                        var przesylka = zaznaczonaPrzesylka();
                        var platnosc = zaznaczonaMetodaPlatnosci();
                        if((przesylka=='odbior_osobisty')) {
                            // Pole nie wymagane
                            return false;
                        } else {
                            // Pole wymagane
                            return true;
                        };
                      }
                    },
                    klient_kod_pocztowy: {
                      required: function(element) {
                        var przesylka = zaznaczonaPrzesylka();
                        var platnosc = zaznaczonaMetodaPlatnosci();
                        if((przesylka=='odbior_osobisty')) {
                            // Pole nie wymagane
                            return false;
                        } else {
                            // Pole wymagane
                            return true;
                        };
                      }
                    },
                    klient_miejscowosc: {
                      required: function(element) {
                        var przesylka = zaznaczonaPrzesylka();
                        var platnosc = zaznaczonaMetodaPlatnosci();
                        if((przesylka=='odbior_osobisty')) {
                            // Pole nie wymagane
                            return false;
                        } else {
                            // Pole wymagane
                            return true;
                        };
                      }
                    },
                    zezwolenie_na_przetwarzanie_danych: "required",
                    oswiadczenie_zapoznania_sie_z_regul: "required"
                },
                messages: {
                    klient_imie: '',
                    klient_nazwisko: '',
                    klient_tel: '',
                    klient_email: {
                        required : '',
                        email: 'Niewłaściwy email'
                        },
                    klient_ulica: '',
                    klient_nr_domu: '',
                    klient_kod_pocztowy: '',
                    klient_miejscowosc: '',
                    zezwolenie_na_przetwarzanie_danych: '',
                    oswiadczenie_zapoznania_sie_z_regul: ''
                }
                });
                // Inicjalizacja validacji
                $("#frmCheckout_step2").valid();
//                if ( $("#frmCheckout_step2").valid() ) {
//                    alert(' ok ');
//                    }
 }//if(document.frmCheckout_step2) {

  //DOC RDY
  });

 // =============== ************************** ================
//  $(document).ready(function(){
//
//	jQuery.validator.addMethod("password", function( value, element ) {
//		var result = this.optional(element) || value.length >= 6 && /\d/.test(value) && /[a-z]/i.test(value);
//		if (!result) {
//			element.value = "";
//			var validator = this;
//			setTimeout(function() {
//				validator.blockFocusCleanup = true;
//				element.focus();
//				validator.blockFocusCleanup = false;
//			}, 1);
//		}
//		return result;
//	}, "Your password must be at least 6 characters long and contain at least one number and one character.");
//
//	// a custom method making the default value for companyurl ("http://") invalid, without displaying the "invalid url" message
//	jQuery.validator.addMethod("defaultInvalid", function(value, element) {
//		return value != element.defaultValue;
//	}, "");
//
//	jQuery.validator.addMethod("billingRequired", function(value, element) {
//		if ($("#bill_to_co").is(":checked"))
//			return $(element).parents(".subTable").length;
//		return !this.optional(element);
//	}, "");
//
//	jQuery.validator.messages.required = "";
//	$("#frmCheckout_step2").validate({
//		invalidHandler: function(e, validator) {
//			var errors = validator.numberOfInvalids();
//			if (errors) {
//				var message = errors == 1
//					? 'You missed 1 field. It has been highlighted below'
//					: 'You missed ' + errors + ' fields.  They have been highlighted below';
//				$("div.error span").html(message);
//				$("div.error").show();
//			} else {
//				$("div.error").hide();
//			}
//		},
//		onkeyup: false,
//		submitHandler: function() {
//			$("div.error").hide();
//			alert("submit! use link below to go to the other step");
//		},
//		messages: {
//			password2: {
//				required: " ",
//				equalTo: "Please enter the same password as above"
//			},
//			email: {
//				required: " ",
//				email: "Please enter a valid email address, example: you@yourdomain.com",
//				remote: jQuery.validator.format("{0} is already taken, please enter a different address.")
//			}
//		},
//		debug:true
//	});
//
////  $(".resize").vjustify();
////  $("div.buttonSubmit").hoverClass("buttonSubmitHover");
////
////  if ($.browser.safari) {
////    $("body").addClass("safari");
////  }
////
////  $("input.phone").mask("(999) 999-9999");
////  $("input.zipcode").mask("99999");
////  var creditcard = $("#creditcard").mask("9999 9999 9999 9999");
////
////  $("#cc_type").change(
////    function() {
////      switch ($(this).val()){
////        case 'amex':
////          creditcard.unmask().mask("9999 999999 99999");
////          break;
////        default:
////          creditcard.unmask().mask("9999 9999 9999 9999");
////          break;
////      }
////    }
////  );
//
//  // toggle optional billing address
//  var subTableDiv = $("div.subTableDiv");
//  var toggleCheck = $("input.toggleCheck");
//  toggleCheck.is(":checked")
//  	? subTableDiv.hide()
//	: subTableDiv.show();
//  $("input.toggleCheck").click(function() {
//      if (this.checked == true) {
//        subTableDiv.slideUp("medium");
//        $("form").valid();
//      } else {
//        subTableDiv.slideDown("medium");
//      }
//  });
//
//
//});
//
////$.fn.vjustify = function() {
////    var maxHeight=0;
////    $(".resize").css("height","auto");
////    this.each(function(){
////        if (this.offsetHeight > maxHeight) {
////          maxHeight = this.offsetHeight;
////        }
////    });
////    this.each(function(){
////        $(this).height(maxHeight);
////        if (this.offsetHeight > maxHeight) {
////            $(this).height((maxHeight-(this.offsetHeight-maxHeight)));
////        }
////    });
////};
//
//$.fn.hoverClass = function(classname) {
//	return this.hover(function() {
//		$(this).addClass(classname);
//	}, function() {
//		$(this).removeClass(classname);
//	});
//};