﻿function Util() {
    this.keyAuth = new String();

    // Descobre o Scroll Width (Cross-brownser)
    this.getScrollWidth = function(){
        var w = window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;
        return w ? w : 0;
    }
    // Descobre o Scroll Height (Cross-brownser)
    this.getScrollHeight = function(){
        var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
        return h ? h : 0;
    }

    // Exibe o Div de Carregando
    this.showLoading = function() {
        // Mostra o Carregando
        // Descobre o Tamanho da Tela
        var winH = $(window).height();
        var winW = $(window).width();
        // Descobre a posicao do Scroll
        var scrollH = this.getScrollHeight();
        var scrollW = this.getScrollWidth();
        // Descobre o Top do div
        var divTop = ((winH / 2) - ($('#maskCarregando').height() / 2)) + scrollH;
        var divLeft = ((winW / 2) - ($('#maskCarregando').width() / 2)) + scrollW;
        $('#maskCarregando').css('top', divTop);
        $('#maskCarregando').css('left', divLeft);
        $('#maskCarregando').css('z-index', '9999');
        $('#mask').css({ 'width': $(window).width(), 'height': $(document).height() });
        $('#mask').fadeTo(0, 0.8);
        $('#mask').show();
        // Mostra o Carregando
        $('#maskCarregando').show();
    }
    // Esconde o Div de Carregando
    this.hideLoading = function() {
        $('#maskCarregando').hide();
        $('#mask').hide();
    }
    // Da o Submit de um Link mostrando o alerta de carregando
    this.submitLink = function(urlLink) {
        // Mostra o Carregando
        this.showLoading();
        // Da o redirect
        setTimeout(function() {
            top.location = urlLink;
        }, 20);
        // retorna void(0) para nao executar a acao de submit
        return false;
    }

    // Submit de TagCloud
    this.submitTagCloud = function(tag) {
        // Mostra o Carregando
        this.showLoading();
        setTimeout(function() {
            top.location = window.location.protocol + "//" + window.location.host + "/Busca/" + encodeURIComponent(tag.replace(".", "").replace("%", "")).replace(/%20/g, "+").replace(/%26/g, "e");
        }, 20);
        return false;
    }


    this.setKey = function(_key) {
        this.keyAuth = _key;
    }


    this.submitLinkProduto = function(pIdAnuncioProduto, pUrlDestino) {
        // Cria as Variaveis de Controle
        var authenticationKey = new String();
        var idClickType = new Number();
        var idAnuncioProduto = new Number();
        var urlAtual = new String();
        var urlDestino = new String();
        var extra = new String();
        // Seta o valor das Variaveis Automaticos
        authenticationKey = this.keyAuth;
        idClickType = 1
        urlAtual = window.location;
        extra = "";

        // Seta os valores das Variavies recebidas
        idAnuncioProduto = pIdAnuncioProduto;
        urlDestino = pUrlDestino;

        // Variaveis de Controle do AJAX
        var lData = new String();
        var caminhoWS = new String();

        // Seta os Dados a Enviar
        lData = "authenticationKey=" + authenticationKey + "&pIdClickType=" + idClickType + "&pIdAnuncioProduto=" + idAnuncioProduto + "&pUrlAtual=" + urlAtual + "&pUrlDestino=" + urlDestino + "&pExtra=" + extra;
        // Seta o Caminho do WS
        caminhoWS = window.location.protocol + "//" + window.location.host + "/" + "ws/anuncioProduto.asmx/SaveClick";

        // Monta o Ajax
        $.ajax({
            type: "POST",
            url: caminhoWS,
            data: lData,
            dataType: "html",
            beforeSend: function(XMLHttpRequest) {
                Util.showLoading();
            },
            complete: function() {
                Util.hideLoading();
            },
            error: function(errorRequest) {
                alert('Erro' + errorRequest);
                Util.hideLoading();
                // Da o redirect
                setTimeout(function() {
                    top.location = urlDestino;
                }, 20);
            },
            success: function(loadedContentHTML) {
                // Da o redirect
                setTimeout(function() {
                    top.location = urlDestino;
                }, 20);
            }
        });

        return false;
    }

    this.submitLinkAnuncio = function(pIdAnuncio, pUrlDestino) {
        // Cria as Variaveis de Controle
        var authenticationKey = new String();
        var idClickType = new Number();
        var idAnuncio = new Number();
        var urlAtual = new String();
        var urlDestino = new String();
        var extra = new String();
        // Seta o valor das Variaveis Automaticos
        authenticationKey = this.keyAuth;
        idClickType = 1
        urlAtual = window.location;
        extra = "";

        // Seta os valores das Variavies recebidas
        idAnuncio = pIdAnuncio;
        urlDestino = pUrlDestino;

        // Variaveis de Controle do AJAX
        var lData = new String();
        var caminhoWS = new String();

        // Seta os Dados a Enviar
        lData = "authenticationKey=" + authenticationKey + "&pIdClickType=" + idClickType + "&pIdAnuncio=" + idAnuncio + "&pUrlAtual=" + urlAtual + "&pUrlDestino=" + urlDestino + "&pExtra=" + extra;
        // Seta o Caminho do WS
        caminhoWS = window.location.protocol + "//" + window.location.host + "/" + "ws/anuncio.asmx/SaveClick";

        // Monta o Ajax
        $.ajax({
            type: "POST",
            url: caminhoWS,
            data: lData,
            dataType: "html",
            beforeSend: function(XMLHttpRequest) {
                Util.showLoading();
            },
            complete: function() {
                Util.hideLoading();
            },
            error: function(errorRequest) {
                alert('Erro' + errorRequest);
                Util.hideLoading();
                // Da o redirect
                setTimeout(function() {
                    top.location = urlDestino;
                }, 20);
            },
            success: function(loadedContentHTML) {
                // Da o redirect
                setTimeout(function() {
                    top.location = urlDestino;
                }, 20);
            }
        });

        return false;
    }

    this.SendSimpleMail = function(pFromEmail, pToEmail, pSubject, pFileTemplate, pNameValue, pExtraInfo) {
        // Cria as Variaveis de Controle
        var authenticationKey = new String();
        var fromEmail = new String();
        var toEmail = new String();
        var subject = new String();
        var fileTemplate = new String();
        var nameValue = new String();
        var extraInfo = new String();
        var retorno = false;

        // Seta o valor das Variaveis Automaticos
        authenticationKey = this.keyAuth;
        fromEmail = pFromEmail;
        toEmail = pToEmail;
        subject = pSubject;
        fileTemplate = pFileTemplate;
        nameValue = pNameValue;
        extraInfo = pExtraInfo;

        // Variaveis de Controle do AJAX
        var lData = new String();
        var caminhoWS = new String();

        // Seta os Dados a Enviar
        lData = "authenticationKey=" + authenticationKey + "&pFromEmail=" + fromEmail + "&pToEmail=" + toEmail + "&pSubject=" + subject + "&pFileTemplate=" + fileTemplate + "&pNameValue=" + nameValue + "&pExtraInfo=" + extraInfo;
        // Seta o Caminho do WS
        caminhoWS = window.location.protocol + "//" + window.location.host + "/" + "ws/util.asmx/SendSimpleMail";

        // Monta o Ajax
        $.ajax({
            type: "POST",
            url: caminhoWS,
            data: lData,
            dataType: "html",
            beforeSend: function(XMLHttpRequest) {
                Util.showLoading();
            },
            complete: function() {
                Util.hideLoading();
            },
            error: function(errorRequest) {
                alert('Erro' + errorRequest);
                Util.hideLoading();
                retorno = false;
            },
            success: function(loadedContentHTML) {
                Util.hideLoading();
                retorno = true;
            }
        });

        return retorno;
    }


    // Valida se o Email é Valido
    this.isValidEmailAddress = function(emailAddress) {
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        return pattern.test(emailAddress);
    }

    // Move a Pagina ate a posicao do Ancora informado
    this.MoveToAnchor = function(anchorName) {
        if (anchorName != "") {
            var destination = $("#" + anchorName).offset().top;

            //alert(destination);

            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination }, 1000);
        }
    }

    // Verifica se é IPhone ou IPod
    this.IsIPhone = function() {
        if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
            return true;
        } else {
            return false;
        }
    }

    // Submit o Link atrave do Botao "Aplicar Filtro"
    this.SubmitAplicarFiltro = function() {
        var linktoSubmit = $("#ctl00_content_selectFilterID > .select:last > .filtro").attr("link");
        Util.submitLink($('#filtroURLDefaultID').val() + linktoSubmit);
    }


    this.submitLinkDestaque = function(pIdEmpresa, pIdAnuncio, pIdProduto, pUrlDestino, pExtra) {
        // Cria as Variaveis de Controle
        var authenticationKey = new String();
        var idClickType = new Number();
        var id = new Number();
        var urlAtual = new String();
        var urlDestino = new String();
        var extra = new String();
        // Seta o valor das Variaveis Automaticos
        authenticationKey = this.keyAuth;
        idClickType = 6 // No Destaque (Site)
        urlAtual = window.location;

        // Seta os valores das Variavies recebidas
        urlDestino = pUrlDestino;
        extra = pExtra;

        // Variaveis de Controle do AJAX
        var lData = new String();
        var caminhoWS = new String();

        // Seta os Dados a Enviar
        // Verifica se é um Produto
        if (Number(pIdProduto) != 0) {
            // É um click para Produto
            lData = "authenticationKey=" + authenticationKey + "&pIdClickType=" + idClickType + "&pIdAnuncioProduto=" + pIdProduto + "&pUrlAtual=" + urlAtual + "&pUrlDestino=" + urlDestino + "&pExtra=" + extra;
            // Seta o Caminho do WS
            caminhoWS = window.location.protocol + "//" + window.location.host + "/" + "ws/anuncioProduto.asmx/SaveClick";
        } else if (Number(pIdAnuncio) != 0) {
            // É um click para Anuncio
            lData = "authenticationKey=" + authenticationKey + "&pIdClickType=" + idClickType + "&pIdAnuncio=" + pIdAnuncio + "&pUrlAtual=" + urlAtual + "&pUrlDestino=" + urlDestino + "&pExtra=" + extra;
            // Seta o Caminho do WS
            caminhoWS = window.location.protocol + "//" + window.location.host + "/" + "ws/anuncio.asmx/SaveClick";
        } else {
            // É um click para Anuncio
            lData = "authenticationKey=" + authenticationKey + "&pIdClickType=" + idClickType + "&pIdEmpresa=" + pIdEmpresa + "&pUrlAtual=" + urlAtual + "&pUrlDestino=" + urlDestino + "&pExtra=" + extra;
            // Seta o Caminho do WS
            caminhoWS = window.location.protocol + "//" + window.location.host + "/" + "ws/anuncio.asmx/EmpresaSaveClick";
        }

        // Monta o Ajax
        $.ajax({
            type: "POST",
            url: caminhoWS,
            data: lData,
            dataType: "html",
            beforeSend: function(XMLHttpRequest) {
                Util.showLoading();
            },
            complete: function() {
                Util.hideLoading();
            },
            error: function(errorRequest) {
                alert('Erro' + errorRequest);
                Util.hideLoading();
                // Da o redirect
                setTimeout(function() {
                    top.location = urlDestino;
                }, 20);
            },
            success: function(loadedContentHTML) {
                // Da o redirect
                setTimeout(function() {
                    top.location = urlDestino;
                }, 20);
            }
        });

        return false;
    }
}