/**
 * @author Marcin Kałamarski
 */
jQuery.validator.addMethod("phonePL", function(phone_number, element){
    return this.optional(element) || phone_number.match(/^0[0-9]{9}$/);
}, "Wprowadź wg formatu 0XXXXXXXXX");

$(document).ready(function(){

    $("#registration_button").click(function(){
        $("#register_form").submit();
    });
    
    $("#purchase_order_button").click(function(){
        $("#order_form").submit();
    });
	
    $('#save_my_order').click(function(){
        $("#edit_order_form").submit();
    });
	
    $("#save_my_account").click(function(){
        $("#my_account").submit();
    });
    
    $('#remind_form').validate({
        rules: {
            strLogin: {
                email: true,
                required: true
            }
        },
        messages: {
            strLogin: {
                required: "Wprowadź adres e-mail",
                email:  "Wprowadź prawidłowy adres e-mail"
            }
        }
    });
	
    $("#my_account").validate({
        rules: {
            cemail: {
                required: true,
                email: true
            },
            cmobile: {
                required: function(element){
                    return $.trim($("#cphone").val()).length == 0;
                },
                phonePL: true
            },
            cphone: {
                required: function(element){
                    return $.trim($("#cmobile").val()).length == 0;
                },
                phonePL: true
            },
            txtpassword1: {
            },
            txtpassword2: {
                equalTo: "#txtpassword1"
            }	
        },
        messages: {
            cmobile: {
                required: "Wprowadź przynajmniej jeden numer telefonu",
                phonePL:  "Wprowadź wg formatu 0xxxxxxxxx"
            },
            cphone: {
                required: "Wprowadź przynajmniej jeden numer telefonu",
                phonePL:  "Wprowadź wg formatu 0xxxxxxxxx"
            },
            cemail: "Wprowadź poprawny adres email",
            txtpassword1: "Wprowadź hasło",
            txtpassword2: {
                required: "Wprowadź ponownie hasło",
                equalTo:  "Wprowadź to samo hasło co powyżej"
            }
        }
    });
    
    $('#spostcode1').keypress(function(){
        var zip_code_1 = $('#spostcode1').val();
        if (zip_code_1.length == 2) {
            $('#spostcode1').val(zip_code_1);
            $('#spostcode2').focus();
        }
    });
    
    $('#ipostcode1').keypress(function(){
        var zip_code_1 = $('#ipostcode1').val();
        if (zip_code_1.length == 2) {
            $('#ipostcode1').val(zip_code_1);
            $('#ipostcode2').focus();
        }
    });
    
    /*
     * Rejestracja
     */
    $("#register_form").validate({
        rules: {
            txtlogin: {
                required: true,
                email: true
            },
            txtpassword1: {
                required: true
            },
            txtpassword2: {
                required: true,
                equalTo: "#txtpassword1"
            },
            cemail: {
                required: true,
                email: {
                    depends: function(element){
                        return element.value.toLowerCase()!="brak";
                    }
                }
            },
            cmobile: {
                required: function(element){
                    return $.trim($("#cphone").val()).length == 0;
                },
                phonePL: true
            },
            cphone: {
                required: function(element){
                    return $.trim($("#cmobile").val()).length == 0;
                },
                phonePL: true
            },
            chagree_1: {
                required: true
            },
            chagree_2: {
                required: true
            }
        },
        messages: {
            txtlogin: "Wprowadź poprawny adres E-mail",
            txtpassword1: "Wprowadź hasło",
            txtpassword2: {
                required: "Wprowadź ponownie hasło",
                equalTo: "Wprowadź to samo hasło co powyżej"
            },
            cmobile: {
                required: "Wprowadź przynajmniej jeden numer telefonu",
                phonePL: "Wprowadź wg formatu 0xxxxxxxxx"
            },
            cphone: {
                required: "Wprowadź przynajmniej jeden numer telefonu",
                phonePL: "Wprowadź wg formatu 0xxxxxxxxx"
            },
            cemail: {
                required: 'Jeśli nie posiadasz aktywnego adresu e-mail wpisz "brak"',
                email: 'Jeśli nie posiadasz aktywnego adresu e-mail wpisz "brak"'
            },
            chagree_1: "Należy zaakceptować",
            chagree_2: "Należy zaakceptować"
        }
    });
    
    $("#txtlogin").change(function(){
        $("#cemail").val($(this).val());
    });
    $("#txtlogin").blur(function(){
        $("#cemail").val($(this).val());
    });
    
    $("#btnCopyShipment").click(function(){
        if ($('#btnCopyShipment').attr('checked')) {
            $("#sname").val($("#iname").val());
            $("#saddress").val($("#iaddress").val());
            $("#spostcode1").val($("#ipostcode1").val());
            $("#spostcode2").val($("#ipostcode2").val());
            $("#spost").val($("#ipost").val());
        }
        else {
            $("#sname").val("");
            $("#saddress").val("");
            $("#spostcode1").val("");
            $("#spostcode2").val("");
            $("#spost").val("");
        }
    });
    
    $("#btnCopyInvoice").click(function(){
        $("#iname").val($("#sname").val());
        $("#iaddress").val($("#saddress").val());
        $("#ipostcode1").val($("#spostcode1").val());
        $("#ipostcode2").val($("#spostcode2").val());
        $("#ipost").val($("#spost").val());
    });
    
    /*
     * Składanie zamówienia
     */
    $("#order_form").validate({
        rules: {
            cmobile: {
                required: function(element){
                    return $.trim($("#cphone").val()).length == 0;
                },
                phonePL: true
            },
            cphone: {
                required: function(element){
                    return $.trim($("#cmobile").val()).length == 0;
                },
                phonePL: true
            },
            chagree_1: {
                required: true
            },
            chagree_2: {
                required: true
            },
            iname: {
                required: true
            },
            sname: {
                required: function(element){
                    if ($('input[name=pmethod]:checked').val()==0){
                        return false;
                    }
                    else {
                        return 	true;
                    }
                }
            },
            saddress: {
                required: function(element){
                    if ($('input[name=pmethod]:checked').val()==0){
                        return false;
                    }
                    else {
                        return 	true;
                    }
                }
            },
            spostcode1: {
                required: function(element){
                    if ($('input[name=pmethod]:checked').val()==0){
                        return false;
                    }
                    else {
                        return 	true;
                    }
                }
            },
            spostcode2: {
                required: function(element){
                    if ($('input[name=pmethod]:checked').val()==0){
                        return false;
                    }
                    else {
                        return 	true;
                    }
                }
            },
            spost: {
                required: function(element){
                    if ($('input[name=pmethod]:checked').val()==0){
                        return false;
                    }
                    else {
                        return 	true;
                    }
                }
            },
            iaddress: {
                required: true
            },
            ipostcode1: {
                required: true
            },
            ipostcode2: {
                required: true
            },
            ipost: {
                required: true
            },
            cemail: {
                required: true,
                email: {
                    depends: function(element){
                        return element.value.toLowerCase()!="brak";
                    }
                }
            }
        },
        messages: {
            cemail: {
                required: 'Jeśli nie posiadasz aktywnego adresu e-mail wpisz "brak"',
                email: 'Jeśli nie posiadasz aktywnego adresu e-mail wpisz "brak"'
            },
            sname: {
                required: 'Wprowadź imię i nazwisko'
            },
            saddress: {
                required: 'Wprowadź ulicę i numer domu/lokalu'
            },
            spostcode1: {
                required: ''
            },
            spostcode2: {
                required: 'Wprowadź kod pocztowy'
            },
            spost: {
                required: 'Wprowadź pocztę'
            },
            iname: {
                required: 'Wprowadź imię i nazwisko'
            },
            iaddress: {
                required: 'Wprowadź ulicę i numer domu lokalu'
            },
            ipostcode1: {
                required: ''
            },
            ipostcode2: {
                required: 'Wprowadź kod pocztowy'
            },
            ipost: {
                required: 'Wprowadź pocztę'
            },
            cmobile: {
                required: "Wprowadź przynajmniej jeden numer telefonu",
                phonePL: "Wprowadź wg formatu 0xxxxxxxxx"
            },
            cphone: {
                required: "Wprowadź przynajmniej jeden numer telefonu",
                phonePL: "Wprowadź wg formatu 0xxxxxxxxx"
            },
            chagree_1: "Należy zaakceptować",
            chagree_2: "Należy zaakceptować"
        }
    });
    
    $("#edit_order_form").validate({
        rules: {
            cmobile: {
                required: function(element){
                    return $.trim($("#cphone").val()).length == 0;
                },
                phonePL: true
            },
            cphone: {
                required: function(element){
                    return $.trim($("#cmobile").val()).length == 0;
                },
                phonePL: true
            },
            chagree_1: {
                required: true
            },
            chagree_2: {
                required: true
            },
            iname: {
                required: true
            },
            sname: {
                required: true
            },
            saddress: {
                required: true
            },
            spostcode1: {
                required: true
            },
            spostcode2: {
                required: true
            },
            spost: {
                required: true
            },
            iaddress: {
                required: true
            },
            ipostcode1: {
                required: true
            },
            ipostcode2: {
                required: true
            },
            ipost: {
                required: true
            }
        },
        messages: {
            sname: {
                required: 'Wprowadź imię i nazwisko'
            },
            saddress: {
                required: 'Wprowadź ulicę i numer domu/lokalu'
            },
            spostcode1: {
                required: ''
            },
            spostcode2: {
                required: 'Wprowadź kod pocztowy'
            },
            spost: {
                required: 'Wprowadź pocztę'
            },
            iname: {
                required: 'Wprowadź imię i nazwisko'
            },
            iaddress: {
                required: 'Wprowadź ulicę i numer domu lokalu'
            },
            ipostcode1: {
                required: ''
            },
            ipostcode2: {
                required: 'Wprowadź kod pocztowy'
            },
            ipost: {
                required: 'Wprowadź pocztę'
            },
            cmobile: {
                required: "Wprowadź przynajmniej jeden numer telefonu",
                phonePL: "Wprowadź wg formatu 0xxxxxxxxx"
            },
            cphone: {
                required: "Wprowadź przynajmniej jeden numer telefonu",
                phonePL: "Wprowadź wg formatu 0xxxxxxxxx"
            }
        }
    });
	
    /* 
     * Obsługa pokazywania u ukrywania kosztu przesyłki w zależności
     * od wybranej opcji dostarczenia towaru.
     */
    $("#form_dane_in_1").click(function(){
        $("#trShipmentCost").hide();
    });
    $("#form_dane_in_2").click(function(){
        $("#trShipmentCost").show();
    });
    $("#form_dane_in_3").click(function(){
        $("#trShipmentCost").show();
    });
    $("#registration_purchase_order_button").click(function(){
        $("#formNewOrder").submit();
    });
    
    /*
     * Autoryzacja w serwisie
     */
    $("#auth_form").validate({
        rules: {
            strlogin: {
                required: true,
                email: true
            },
            strpassword: {
                required: true
            }
        },
        messages: {
            strlogin: "Wprowadź adres e-mail",
            strpassword: "Wprowadź hasło"
        }
    });
    $('#auth_button').click(function(){
        $('#login_error').hide();
        $('#auth_form').submit();
    })
});
