﻿
/*******************************/
$(document).ready(function () {

    AdicionarInjecao();
    $(".boxSelect").change(MudarFocusSelect);

    //Qualquer mudanca nas TextBox, Checkbox ou select muda a variavel para false
    $(":textbox, :checkbox, select").change(function () {
        valido = false;
    });

    //       $("input[type=text]").change(function(){
    //        valido = false;
    //       });

});

/************* FunÃ§Ãµes *********************/
function AdicionarInjecao() {

    $("input[type=text]").blur(BlurInvalidos);
    $("input[type=text]").keypress(function (e) {
        return KeyPress(e);
    });

    $("textarea").blur(BlurInvalidos);
    $("textarea").keypress(function (e) {
        return KeyPress(e);
    });
}

function MudarFocusSelect() {
    $(".boxTextUpdate").focus();
}

// Array para guardar divs e a posiÃ§Ã£o do scroll
var vetor = new Array();

// Guarda a PosiÃ§Ã£o do Scroll no Initialize da RequisiÃ§Ã£o do Ajax
function GuardarPosicaoScroll() {
    var updatePanel = $(".siglaPanel_body > div");
    if (updatePanel.length > 0) {
        if (updatePanel[0].getElementsByTagName('div').length > 0) {
            if (updatePanel.length != 0) {
                for (i = 0; i < updatePanel.length; i++) {
                    var divNaoEncontrada = 0;
                    if (updatePanel[0].getElementsByTagName('div')[0].scrollTop != null) {
                        if (vetor.length != 0) {
                            for (j = 0; j < vetor.length; j++) {
                                if (vetor[j].divId == updatePanel[i].id) {
                                    vetor[j].pos = updatePanel[i].getElementsByTagName('div')[0].scrollTop;
                                    divNaoEncontrada = 1;
                                    break;
                                }
                                else {
                                    divNaoEncontrada = 0;
                                }
                            }
                            if (divNaoEncontrada == 0) {
                                var panel = new Object();
                                panel.divId = updatePanel[i].id;
                                panel.pos = updatePanel[i].getElementsByTagName('div')[0].scrollTop;
                                vetor.push(panel);
                            }
                        }
                        else {
                            var panel = new Object();
                            panel.divId = updatePanel[i].id;
                            panel.pos = updatePanel[i].getElementsByTagName('div')[0].scrollTop;
                            vetor.push(panel);
                        }
                    }

                }
            }
        }
    }
}

// Seta PosiÃ§Ã£o do Scroll no Final da RequisiÃ§Ã£o do Ajax
function SetarPosicaoScroll() {
    var updatePanel = $(".siglaPanel_body > div");
    if (updatePanel.length > 0) {
        if (updatePanel[0].getElementsByTagName('div').length > 0) {
            if (updatePanel[0].getElementsByTagName('div')[0].scrollTop != null) {
                if (updatePanel.length != 0) {
                    for (i = 0; i < updatePanel.length; i++) {
                        for (j = 0; j < vetor.length; j++) {
                            if (vetor[j].divId == updatePanel[i].id) {
                                updatePanel[i].getElementsByTagName('div')[0].scrollTop = vetor[j].pos;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}
function AbrirPopUp(url) {
    window.open(url, null, 'fullscreen=yes,scrollbars=no,toolbar=no,menubar=no, status=no,resizable=yes');
}

function AbrirPopUpFechar(url) {
    window.open(url, null, 'fullscreen=no,scrollbars=no,toolbar=no,menubar=yes, status=no,resizable=yes');
}

function CalcularAltura(obj) { }

function CalcularAlturaMaxima(obj) {
    obj = "#" + obj;

    var objsubBody = obj + "_subBody";
    var hB = $(objsubBody).css("height");
    var h = $(obj).css("height");

    hB = parseInt(hB.replace("px", ""));
    h = parseInt(h.replace("px", ""));

    var novaH = hB + h + 2;

    $(obj).css("height", novaH);
}

function CalcularAlturaMinima(obj) {
    obj = "#" + obj;

    var objsubBody = obj + "_subBody";
    var hB = $(objsubBody).css("height");
    var h = $(obj).css("height");

    hB = parseInt(hB.replace("px", ""));
    h = parseInt(h.replace("px", ""));

    var novaH = h - (hB - 2);

    $(obj).css("height", novaH);
}


function ShowHide(idObjeto, efeito, velocidade, imageShow, ImageHide) {
    if (document.getElementById(idObjeto) != null) {

        //Nome da Div Body que o JQuery possa usar nativamente
        var idBody = '#' + idObjeto + "_body";

        //Nome da Imagen que o JQuery possa usar nativamente
        var idImg = idObjeto + "_img";

        if (velocidade == null || velocidade == '') {
            velocidade = 'normal';
        }

        if ($(idBody).css('display') == 'block') {

            if (efeito == 'slide') {

                $(idBody).slideUp(velocidade, function () {
                    $(idBody).css("border-bottom", "solid 0px #DEE1E4");
                });
            }
            else if (efeito == 'fade') {

                $(idBody).fadeOut(velocidade);
            }
            else {
                $(idBody).hide(velocidade);
            }

        }
        else {

            if (efeito == 'slide') {
                $(idBody).slideDown(velocidade, function () {
                    //$(idBody).css("border-bottom","solid 1px #DEE1E4");
                });
            }
            else if (efeito == 'fade') {
                $(idBody).fadeIn(velocidade);
            }
            else {
                $(idBody).show(velocidade);
            }
        }
        AlternateImage(idImg, imageShow, ImageHide);
    }
}



function AlternateImage(idImage, path1, path2) {
    if (document.getElementById(idImage) != null) {

        var idImage = '#' + idImage;
        var pathAtual = $(idImage).attr('src');

        if (pathAtual == path1) {
            $(idImage).attr('src', path2);
        }
        else if (pathAtual == path2) {
            $(idImage).attr('src', path1);
        }

    }
}

function BlurInvalidos() {
    var compara = ""
    var re = new RegExp(compara, 'gim');
    var resultado = this.value.replace(re, '');
    this.value = resultado.replace(/^\s+|\s+$/g, "");
}

function KeyPress(e) {
    strCheck = "";
    var tecla = e.keyCode;
    var key = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;  // Enter
    //if (whichCode == 38) return false;  // &
    key = String.fromCharCode(whichCode);  // Get key value from key code
    if (strCheck.indexOf(key) != -1) {
        return false; // Not a valid key
    }
}

function contar(obj, label, maximo) {
    //Pega a quantidade de caracteres da caixa de texto (ou outro objeto)
    //utilizado como parametro.
    var qtd = $(obj).val().length;

    //Subtrai a quantidade de caracteres do objeto de origem
    //pela quantidade mÃ¡xima de caracteres.
    var atual = maximo - qtd;

    var valor;

    //Verifica se a quantidade de caracteres que foram digitadas
    if (atual < 0) {
        // Se a quantidade de caractesres for maior do que o mÃ¡ximo permitido.
        // Ã© removido os exedidos (removendo os Ãºltimos caracteres digitados).
        $(obj).val($(obj).val().substring(0, maximo));
    }
    else if (atual == 0) {
        // O JavaScript em alguns momentos considera o zero como vazio, e nÃ£o mostra nada,
        // essa linha passa o zero como String, para que nÃ£o ocorra esse problema.
        valor = "0";
    }
    else {
        valor = atual;
    }

    // Valida se o objeto que deve receber a quantidade de caracteres disponÃ­veis
    // para a digitaÃ§Ã£o existe.
    if (document.getElementById(label) != null) {

        // Se ele existir, recebe o valor que representa a quantidade de caracteres
        // restantes.
        $("#" + label).html(valor);
    }

}


//jQuery.fn.corner = function(options) { 
//         
//        var settings = { 
//          tl: { radius: 10 }, 
//          tr: { radius: 10 }, 
//          bl: { radius: 10 }, 
//          br: { radius: 10 }, 
//          antiAlias: true, 
//          autoPad: true, 
//          validTags: ["div","table"] }; 
//        if ( options && typeof(options) != 'string' ) 
//            jQuery.extend(settings, options); 
// 
//    return this.each(function() { 
//        new curvyObject(settings,this).applyCorners(); 
//        //new curvyCorners(settings, this ).applyCornersToAll(); 
//    });    

//}; 

function redirecionar(url, time) {
    window.setTimeout("self.location.href = '" + url + "'", time);
}
function carregando(contador, url, tag_id) {

    if (contador == 0) {
        if (url == 'voltar') {
            history.back();
        }
        else {
            self.location.href = url;
        }
    }
    else {
        $("#Time").html(contador);
        contador--;
        setTimeout(function () { carregando(contador, url, tag_id); }, 1000);
    }
}


function InjetarValidador() {
    $(".confirmQuestion a, a.confirmQuestion").click(function () {
        if (!valido) {
            return (confirm('Deseja realmente sair deste registro ?'))
        }
    });

    //	$("a.confirmQuestion").click(function(){
    //        if(!valido)
    //	    {
    //            return (confirm('Deseja realmente sair deste registro ?'))
    //        }
    //	 });
}


