//-------------------------------------------
// Funções usadas pelos UserControls Tlantic.
//-------------------------------------------
function RemoveEnter(e) {
    var key = (window.event ? e.keyCode : (e.which ? e.which : 0));
    if (key == 13)
        return false;
    return true;
}

function ChangeTab(midiaType) {
    var strClass = '';

    switch (midiaType) {
        case 'Comments':
            strClass = 'boxComentarios';
            break;
        case 'Photos':
            strClass = 'boxFotos';
            break;
        case 'Videos':
            strClass = 'boxVideos';
            break;
    }

    $(document).ready(function() { $('ul.abas').removeClass('boxComentarios').addClass(strClass) });
}

function Mask(e, src, mascara) {

    var key = (document.all) ? e.keyCode : e.which;
    if (key > 47 && key < 59 || key == 8 || key == 0) {
        var campo = src.value.length;
        var saida = mascara.substring(0, 1);
        var texto = mascara.substring(campo);
        if (texto.substring(0, 1) != saida) {
            if (key == 8) {
                event.keyCode = 0;
            }
            else {
                src.value += texto.substring(0, 1);
            }
        }
    }
    else {
        patron = /[\d]/;
        return patron.test(String.fromCharCode(key));
    }
}


function onlyNumber(e) {
    var key = (document.all) ? e.keyCode : e.which;

    if (key > 47 && key < 59 || key == 8 || key == 0) {
        if (key == 8) {
            event.keyCode = 0;
        }
        else {
            return true;
        }
    }
    else {
        patron = /[\d]/;
        return patron.test(String.fromCharCode(key));
    }
}

function Sleep(miliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > miliseconds) {
            break;
        }
    }
}

function ctrlC(e) {

    var key;
    var isCtrl;

    if (window.event) {
        var key = (document.all) ? e.keyCode : e.which;
        if (window.event.ctrlKey)
            isCtrl = true;
        else
            isCtrl = false;
    }
    else {
        var key = (document.all) ? e.keyCode : e.which;
        if (e.ctrlKey)
            isCtrl = true;
        else
            isCtrl = false;
    }
    if (isCtrl) {
        return false;
    }

    return true;
}

function onlyText(e) {
    var key = (document.all) ? e.keyCode : e.which;

    if (key < 47 && key > 59 || key == 8 || key == 0 || key == 32) {
        if (key == 8) {
            event.keyCode = 0;
        }
        else {
            return true;
        }
    }
    else {
        patron = /([aA-zZ])/g;
        return patron.test(String.fromCharCode(key));
    }
}

function stateRegistration(e, obj) {
    var key = (document.all) ? e.keyCode : e.which;

    if (key > 47 && key < 58 || key == 8 || key == 0) {
        if (key == 8) {
            event.keyCode = 0;
        }
        else {
            return true;
        }
    }
    else {

        var value = obj.value.toUpperCase();

        if (value == '') {
            return true;
        }
        if (value == 'I') {
            return true;
        }
        if (value == 'IS') {
            return true;
        }
        if (value == 'ISE') {
            return true;
        }
        if (value == 'ISEN') {
            return true;
        }
        if (value == 'ISENT') {
            return true;
        }
        return false;
    }
}


var submitOnEnterControl = '';


//Verifica se o Enter foi pressionado e faz o submit do controle.
//---------------------------------------------------------------
function submitOnEnter_keydown(event) {


    var id = '';
    var isCEP = false;

    if (event.srcElement) {
        if (event.srcElement.id)
            id = event.srcElement.id;
    }
    else {
        id = event.target.id;
    }

    var control = document.getElementById(id);

    if (control && control.className.indexOf('tlanticCEP') >= 0)
        isCEP = true;

    var keyCode = (event.which === undefined) ? event.keyCode : event.which;

    if (keyCode == '13' && !isCEP) {
        __doPostBack(submitOnEnterControl, '');

        return false;
    }

    if (keyCode == '13' && isCEP) {
        return false;
    }

};



//Faz o ENTER em qualquer lugar da página executar o submit de um controle específico
//-----------------------------------------------------------------------------------
function submitOnEnter(controlId) {
    var control = document.getElementById(controlId);
    if (control) {
        submitOnEnterControl = control.name;

        if (theForm.addEventListener) // Firefox, Google Chrome, Safari, Opera
        {
            //Adiciona o EventListener.
            theForm.onkeydown = submitOnEnter_keydown;
        }
        else {
            if (theForm.attachEvent) //Internet Explorer, Opera
            {
                theForm.detachEvent('onkeydown', submitOnEnter_keydown);
                theForm.attachEvent('onkeydown', submitOnEnter_keydown);
            }
        }
    }
}

//Seta o foco para o controle quando a página é carregada
//-------------------------------------------------------
function setFocusTo(controlId) {
    var control = document.getElementById(controlId);
    if (control && !control.disabled) {
        control.focus();
    }
}

// PageRequestManager Extension
var PageRequestManagerEx =
{
    _initialized: false,

    init: function() {
        if (!PageRequestManagerEx._initialized) {
            var _callQueue = new Array();
            var _executingElement = null;
            var prm = Sys.WebForms.PageRequestManager.getInstance();

            _prm.add_initializeRequest(initializeRequest);
            _prm.add_endRequest(endRequest);

            PageRequestManagerEx._initialized = true;
        }

        function initializeRequest(sender, args) {
            if (_prm.get_isInAsyncPostBack()) {
                //if we are here that means there already a call pending.

                //Get the element which cause the postback
                var postBackElement = args.get_postBackElement();

                //We need to check this otherwise it will abort the request which we made from the
                //end request
                if (_executingElement != postBackElement) {
                    //Does not match which means it is another control
                    //which request the update, so cancel it temporary and 
                    //add it in the call queue
                    args.set_cancel(true);
                    Array.enqueue(_callQueue, postBackElement);
                }

                //Reset it as we are done with our matching
                _executingElement = null;
            }
        }

        function endRequest(sender, args) {
            //Check if we have a pending call
            if (_callQueue.length > 0) {
                //Get the first item from the call queue and setting it
                //as current executing item
                _executingElement = Array.dequeue(_callQueue);

                //Now Post the from which will also fire the initializeRequest
                _prm._doPostBack(_executingElement.id, '');
            }
        }
    }
}

if (typeof (Sys) != 'undefined') {
    Sys.Application.notifyScriptLoaded();
}

/*
Garante que o mesmo element não gerará postbacks subsequentes até o 
processamento da primeira requisição ser finalizado.
*/
$(document).ready(function() {
    prm = Sys.WebForms.PageRequestManager.getInstance();

    prm.add_initializeRequest(function(sender, args) {

        var _processingRequest = $(args.get_postBackElement()).attr("_processingRequest");

        if (_processingRequest)
            args.set_cancel(true);
        else
            $(args.get_postBackElement()).attr("_processingRequest", true);
    });

    prm.add_endRequest(function(sender, args) {
        $(sender._postBackSettings.sourceElement).removeAttr("_processingRequest");
    });
});


//funcao para disparar eventos GA a partir de modais
document.ModalLoad = function(modalName) {
    $(document).trigger('modalLoad', modalName);
};
