function getE(name) {
    if (document.getElementById)
        var elem = document.getElementById(name);
    else if (document.all)
        var elem = document.all[name];
    else if (document.layers)
        var elem = document.layers[name];
    return elem;
}

function OpenWindow(query, w, h, scroll) {
    var l = (screen.width - w) / 2;
    var t = (screen.height - h) / 2;

    winprops = 'resizable=0, height=' + h + ',width=' + w + ',top=' + t + ',left=' + l + 'w';
    if (scroll) winprops += ',scrollbars=1';
    var f = window.open(query, "_blank", winprops);
}

$(document).ready(function () {
    imageZoom();
    buscarCEP();
    lightBox();
    colorPickerClick();
});

function buscarCEP() {
    $('input.txtZipPostalCode').blur(function () {

        $.getScript("http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep=" + $("input.txtZipPostalCode").val(), function () {
            if (resultadoCEP["resultado"]) {
                // troca o valor dos elementos  
                $("input.txtAddress1").val(unescape(resultadoCEP["tipo_logradouro"]) + " " + unescape(resultadoCEP["logradouro"]));
                $("input.txtNeighborhood").val(unescape(resultadoCEP["bairro"]));
                $("input.txtCity").val(unescape(resultadoCEP["cidade"]));
                $("select.ddlStateProvince option").each(function () {
                    if (unescape(resultadoCEP["uf"]) == $(this).text()) {
                        $(this).attr('selected', 'selected');
                    }
                });

            } else {
                alert("Endereço não encontrado");
            }
        });
    });
}

function validateRG(source, args) {
    var rg = args.Value;
    $("span.rgCheck").html('');
    $("span.rgCheck").html('Verificando...');
    $.ajax({
        async: true,
        type: "POST",
        url: "/services/RegisterValidator.asmx/ValidateRG",
        data: "rg=" + rg,
        success: function (data, textStatus) {
            var req;
            req = data.documentElement.firstChild.nodeValue;
            if (req == 'true') {
                $("span.rgCheck").html('Ok');
                args.IsValid = true;
            }
            else {
                $('span.rgCheck').html('RG j&aacute; utilizado');
                args.IsValid = false;
            }
        }
    });
}

function validateCPF(source, args) {
    var cpf = args.Value;
    $("span.cpfCheck").html('');
    $("span.cpfCheck").html('Verificando...');
    $.ajax({
        async: true,
        type: "POST",
        url: "/services/RegisterValidator.asmx/ValidateCPF",
        data: "cpf=" + cpf,
        success: function (data, textStatus) {
            var req;
            req = data.documentElement.firstChild.nodeValue;
            if (req == 'true') {
                $("span.cpfCheck").html('Ok');
                args.IsValid = true;
            }
            else {
                $('span.cpfCheck').html('J&aacute; usado ou inv&aacute;lido');
                args.IsValid = false;
            }
        }
    });
}

function lightBox() {
    var boxOptions = {
        'opacity': true,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'centerOnScroll': 'true'
    };

    if ($("a.labelInfoOpener").length > 0)
        $("a.labelInfoOpener").fancybox(boxOptions);
}

function imageZoom() {
    if ($("a.defaultImageZoom").length > 0) {
        var options = {
            zoomWidth: 400,
            zoomHeight: 300,
            zoomType: 'reverse',
            showPreload: false
        };

        $('a.defaultImageZoom').jqzoom(options);
    }
}

// Utilizado para o efeito de destacar a cor selecionada
function colorPickerClick() {
    $('div.color-picker img').click(function () {
        $('div.color-picker').each(function () {
            $(this).removeClass("color-picker-selected");
        });

        $(this).parent().addClass("color-picker-selected");

        //ie7 workaround
        $(this).parent().parent().prev().attr('checked', true);
    });
}

