$(document).ready(function() {

    var url = '/anmeldung/jsonCountry.json';
    var countries = Array;
    var data;
    $.ajax({
        type: "GET",
        url: url,
        cache: false,
//        dataType: "Array",
//        data: Array,
        dataType: 'json',
        data: data,
        success: function(data) {
            $.each(data, function(i, item) {
                var id = item.id;
//                countries[id] = item.vat_code;
                countries[item.id] = item.vat_code;
            })
        }
    });

    $('#customer_vat_id').live('change', function() {
        var vatId = $('#customer_vat_id').val();
        var newVat = vatId.replace(/ /g, '');
        $('#customer_vat_id').val(newVat);
    })

    $('#customer_country_id').live('change', function() {
        var parentTag = $('#customer_vat_id').parent();
        if ($('#customer_country_id').val() == 1) {
            parentTag.hide();
//            (parentTag).next().hide();
            $('#vatidInfo').hide();
            return;
        }
        else {
            parentTag.show();
//            (parentTag).next().show();
            $('#vatidInfo').show();
        }
        var ustname = countries[$(this).val()];
//        $('#customer_vat_id').val(ustname);
        $('#customer_vat_id').prev().val(ustname);
        $('#customer_vat_id').prev().text(ustname);
    })

    $('form#fregister p label').addClass('labelAbsolutePosition');
    $('form#fregister p select').prev().hide();

//    If page reloads with non-empty fields because of errors, don't display the labels in those fields
    $('form#fregister p input').filter(function() {
        if ($(this).val() != "") {
            $(this).prev().hide();
        }
    })

    $('form#fregister p input').focus(function() {
        $(this).prev().hide();
    })
    $('form#fregister p input').blur(function() {
        if ($(this).val() == "") {
            $(this).prev().show();
        }
    })

//    $('#customer_name').focus(
//        function() {
//            var flabel = "Name";
//            if ($(this).attr("initial") == undefined) {
//                $(this).attr("initial", $(this).val());
//            }
//            if ($(this).val() == flabel) {
//                $(this).val("");
//            }
//        }).blur(function() {
//            var flabel = "Name";
//            if ($(this).val() == "") {
//                $(this).val(flabel)
//            }
//        });

//    $('#customer_company').attr("placeholder", "Firma");


    if ($('#customer_country_id').val() == 1) {
        $('#customer_vat_id').parent().hide();
        $('#vatidInfo').hide();
    }

    if ($('#customer_vat_id').val() == "") {
        var country_id = $('#customer_country_id').val();
        var ustname = countries[country_id];
        $('#customer_vat_id').val(ustname);
//        $('#customer_vat_id').prev().val(ustname);
    }


//    if (supports_input_placeholder()) {
//        alert('Supports placeholder')
//    }
//    else {
//        alert('Does not support placeholder')
//    }


//    $('#register #main form p.pcheckbox input').live('click', function() {
//        $('#register #main form p.pcheckbox input').removeClass('active');
//    });


    /**
     * Registration form
     */

    $('#register #main form .rightcol fieldset.fsliefer').addClass('invisible');

    if ($('#customer_has_different_shipping:checked').val()) {
        $('#customer_has_different_shipping:checked').addClass('active');
        $('#register #main form .rightcol fieldset').removeClass('invisible');
    }

    $('#customer_has_different_shipping').live('click', function() {
        checkMinVals();
        $('#register #main form .rightcol fieldset.fsliefer').toggleClass('invisible');
//        $('#zamethod ul.radio_list li input').removeClass('active');
        $(this).toggleClass('active');
    })

    $('#customer_has_self_returnaddress').live('click', function() {
        $(this).toggleClass('active');
    })

});

function supports_input_placeholder() {
    var i = document.createElement('input');
    return 'placeholder' in i;
}


