//popup
function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    if (yScroll < windowHeight) pageHeight = windowHeight;
    else pageHeight = yScroll;
    if (xScroll < windowWidth) pageWidth = windowWidth;
    else pageWidth = xScroll;
    arrayPageSize = { pageWidth: pageWidth, pageHeight: pageHeight, windowWidth: windowWidth, windowHeight: windowHeight }
    return arrayPageSize;
}

var campoEscolhidoForm;

function abrirPopup(campoTexto) {

    campoEscolhidoForm = document.getElementById(campoTexto);

    if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
        $("body", "html").css({ height: "100%", width: "100%" });
        $("html").css("overflow", "hidden");
    }

    if (document.getElementById("mascara") === null) {
        $("body").append("<div id='mascara'></div><div id='caixa'></div>");
        $("#mascara").click(removePopup);
    }

    //pegando altura do body - ie6
    var pageSize = getPageSize();
    document.getElementById('mascara').style.height = (pageSize.pageHeight + 'px');
    document.getElementById('caixa').innerHTML = "<img align='absmiddle' src='Images/loader.gif' /> Carregando...";

    largura = 718 - 30;
    altura = 449 - 30;

    document.getElementById('caixa').style.width = largura + 'px';
    document.getElementById('caixa').style.height = altura + 'px';

    if (typeof document.body.style.maxHeight != "undefined") {
        document.getElementById('caixa').style.margin = "-" + altura / 2 + "px 0 0 -" + largura / 2 + "px";
    }
    else {
        var alturaScroll = document.documentElement.scrollTop;
        diferencaIE = alturaScroll - altura / 2;
        document.getElementById('caixa').style.marginLeft = -largura / 2 + "px";
        document.getElementById('caixa').style.marginTop = diferencaIE + "px";
    }


    $.ajax({
        url: 'clientes.html',
        cache: false,
        success: function(html) {

            marcadoi = 1;

            document.getElementById('caixa').innerHTML = '<div id="TB_closeWindow"><a>Fechar [x]</a></div>';
            $("#caixa").append(html);

            $("#TB_closeWindow").click(removePopup);

            //jQuery(document).bind('keydown', 'esc',removePopup);

            //marcando campos
            $("input[@type=text]").focus(function() {
                $(this).css("background-color", "#f7f3c6");
            });

            $("input[@type=text]").blur(function() {
                $(this).css("background-color", "");
            });

            $("textarea").focus(function() {
                $(this).css("background-color", "#f7f3c6");
            });

            $("textarea").blur(function() {
                $(this).css("background-color", "");
            });

        }
    });

    //aparecimento dos campos
    $("#mascara").show();
    $("#caixa").show();

}

function removePopup() {
    //tira função do fechar
    $("#TB_closeWindowButton").unbind("click");
    //efeito de transição de saida
    $("#mascara").fadeOut("fast");
    $("#caixa").fadeOut("fast", function() { $('#caixa,#mascara,#TB_HideSelect').trigger("unload").unbind().remove(); });
    if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
        $("body", "html").css({ height: "auto", width: "auto" });
        $("html").css("overflow", "");
    }

    return false;
}


function formataValor(campo) {
    campo.value = filtraCampo(campo);
    vr = campo.value;
    tam = vr.length;
    if (tam <= 2) {
        campo.value = vr;
    }
    if ((tam > 2) && (tam <= 5)) {
        campo.value = vr.substr(0, tam - 2) + ',' + vr.substr(tam - 2, tam);
    }
    if ((tam >= 6) && (tam <= 8)) {
        campo.value = vr.substr(0, tam - 5) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
    }
    if ((tam >= 9) && (tam <= 11)) {
        campo.value = vr.substr(0, tam - 8) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
    }
    if ((tam >= 12) && (tam <= 14)) {
        campo.value = vr.substr(0, tam - 11) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
    }
    if ((tam >= 15) && (tam <= 18)) {
        campo.value = vr.substr(0, tam - 14) + '.' + vr.substr(tam - 14, 3) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
    }

}

function filtraCampo(campo) {
    var s = "";
    var cp = "";
    vr = campo.value;
    tam = vr.length;
    for (i = 0; i < tam; i++) {
        if (vr.substring(i, i + 1) != "/" && vr.substring(i, i + 1) != "-" && vr.substring(i, i + 1) != "." && vr.substring(i, i + 1) != ",") {
            s = s + vr.substring(i, i + 1);
        }
    }
    campo.value = s;
    return cp = campo.value;
}

function verificaFormacao(objeto) {
    if (objeto.id == 'rdbPreco') {
        document.getElementById('rdbDesconto').checked = false;
        document.getElementById('txtDesconto1').style.display = 'none';
    }
    else {
        document.getElementById('rdbPreco').checked = false;
        document.getElementById('txtDesconto1').style.display = 'block';
    }
}
