function retornaElemento(obj) {
    return document.getElementById(obj);
}

/*function $(){
 try {
 for(var i=0, elements=[], args=$.arguments; i<args.length;
 elements.push( ( (document.getElementById(args[i]) || (document.all && document.all[args[i]])) || document[args[i]] ) || null ), i++);
 return elements.length == 1 ? elements[0] : elements;
 } catch(e){
 new Error("Um erro ocorreu. Por favor, relate através do e-mail atendimento@personare.com.br");
 }

 }*/

function AbreEdicaoFormulario(X, Y, nomeCampo, OrigemCadastro) {
    var PerfilID = eval('document.InsereProduto.' + nomeCampo + '[document.InsereProduto.' + nomeCampo + '.selectedIndex].value');
    if (PerfilID == 0) {
        alert('Você precisa selecionar a pessoa que deseja editar. Caso deseje cadastrar uma nova pessoa, clique em "Incluir nova pessoa"');
        return;
    }
    document.location.href = 'ctl.php?mdl=Usuario&cmd=TelaEditaPerfilAstrologico&PerfilCod=' + PerfilID + '&OrigemCadastro=' + OrigemCadastro;
}

function AbrePopPerfil(a, X, Y) {
    janela2 = window.open(a, 'popAdicionarPerfil', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=' + X + ',height=' + Y + ',top=0,left=0,maximized');
}

function AbreEdicao(X, Y, PerfilID) {
    janela2 = window.open('ctl.php?mdl=Usuario&cmd=TelaEditaPerfilAstrologico&PerfilID=' + PerfilID, 'popEdicaoPefil', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=' + X + ',height=' + Y + ',top=0,left=0,maximized');
}

function mudaFundo(ID, strClassName) {
    document.getElementById('esquerda' + ID).className = strClassName;
    document.getElementById('direita' + ID).className = strClassName;
}

function AbreEdicaoMeuPerfil(X, Y, PerfilCod, OrigemCadastro) {
    var Endereco = 'ctl.php?MeuPerfil=1&mdl=Usuario&cmd=TelaEditaPerfilAstrologico&PerfilCod=' + PerfilCod + '&OrigemCadastro=' + OrigemCadastro;
    document.location.href = Endereco;
}

function AbreApaga(X, Y, PerfilID) {
    if (confirm("Deseja realmente apagar esse perfil do seu cadastro? \nClique em OK para apagar ou em Cancel para cancelar esta ação.")) {
        janela2 = window.open('ctl.php?mdl=Usuario&cmd=ApagaPerfilAstrologico&PerfilID=' + PerfilID, 'popEdicaoPefil', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=' + X + ',height=' + Y + ',top=0,left=0,maximized');
    }
}

function AbrePop(a, X, Y, scroll) {
    if (typeof(scroll) == 'undefined') {
        scroll = 'yes';
    }

    janela2 = window.open(a, 'popAdicionarPerfil', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + scroll + ',resizable=yes,copyhistory=no,width=' + X + ',height=' + Y + ',top=0,left=0,maximized');
}

function AbrePopSemScroll(a, X, Y) {
    janela2 = window.open(a, 'popAdicionarPerfil', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=' + X + ',height=' + Y + ',top=0,left=0,maximized');
}

function TestaCampoText(formularioAux, campoAux, msgErroAux) {
    var valor = valorCampoText(formularioAux, campoAux);

    if (valor != '') {
        return '';
    }
    else {
        return msgErroAux + '\n';
    }
}

function valorCampoText(formularioAux, campoAux) {
    formElement = 'document.' + formularioAux + '.' + campoAux + '.value';

    valor = eval(formElement);

    return valor;
}

function valorCampoSelect(formularioAux, campoAux) {
    formElement = 'document.' + formularioAux + '.' + campoAux + '[document.' + formularioAux + '.' + campoAux + '.selectedIndex].value'

    valor = eval(formElement);

    return valor
}

function valorCampoRadio(formularioAux, campoAux) {
    var formAux = jQuery(document).find('form[name='+formularioAux+']');
    if (formAux.length === 0) {
        formAux = jQuery(document).find('#'+formularioAux);
    }

    if (formAux.length === 0) {
        return 0;
    }

    var radio = formAux.find('input[name='+campoAux+']');

    if (radio.length === 0) {
        formAux.find('#'+campoAux);
    }

    if (radio.length === 0) {
        return 0;
    }

    j = radio.length; //alert(j)
    for (i = 0; i < j; i++) {
        if (radio[i].checked) var valor = radio[i].value;
    }
    return valor;
}

function TestaCampoCheck(formularioAux, campoAux, msgErroAux) {

    formElement = 'document.' + formularioAux + '.' + campoAux + '.checked'

    valor = eval(formElement);

    if (valor) {
        return '';
    }
    else {
        return msgErroAux;
    }
}


function TestaCampoSelect(formularioAux, campoAux, msgErroAux) {

    formElement = 'document.' + formularioAux + '.' + campoAux + '[document.' + formularioAux + '.' + campoAux + '.selectedIndex].value'

    valor = eval(formElement);

    if (valor != '') {
        return '';
    }
    else {
        return msgErroAux + '\n';
    }
}

function TestaCampoSelectMultiple(formularioAux, campoAux, msgErroAux) {

    tamanhoSelect = eval('document.' + formularioAux + '.' + campoAux + ' .length')

    valor = false
    for (i = 0; i < tamanhoSelect; i++) {
        if (!valor) {
            valor = eval('document.' + formularioAux + '.ddCidade.options[' + i + '].selected')
        }
    }

    if (valor) {
        return '';
    }
    else {
        return msgErroAux + '\n';
    }
}

function validaCPF(cpf) {

    var i;
    var soma = soma2 = 0;

    if (!(cpf.match(/^[0-9]{3,3}[.]{0,1}[0-9]{3,3}[.]{0,1}[0-9]{3,3}[-]{0,1}[0-9]{2,2}$/))) {
        return false;
    }

    cpf = retiraCaracter(retiraCaracter(cpf, '.'), '-');

    for (i = 0; i < 10; i++) {
        if (i < 9) {
            soma += cpf.charAt(i) * (10 - i);
        }
        soma2 += cpf.charAt(i) * (11 - i);
    }

    if (((soma % 11) < 2 ? 0 : 11 - (soma % 11 )) != cpf.charAt(9)) {
        return false;
    }

    return (((soma2 % 11 ) < 2 ? 0 : 11 - (soma2 % 11 )) != cpf.charAt(10)) ? false : true;
}

function retiraCaracter(string, caracter) {
    var i = 0;
    var final = '';
    while (i < string.length) {
        if (string.charAt(i) == caracter) {
            final += string.substr(0, i);
            string = string.substr(i + 1, string.length - (i + 1));
            i = 0;
        }
        else {
            i++;
        }
    }
    return final + string;
}

function ExibeJanela(url) {
    frame = '<iframe id="janela" name="janela" src="' + url + '" marginheight="0" marginwidth="0" frameborder="0" scrolling="no"></iframe>'

    retornaElemento('JanelaCentral').innerHTML = frame;
    retornaElemento('JanelaCentral').style.display = 'block';

    //retornaElemento('JanelaCentral').style.top = getY()+ 100 + "px"

}

function FecharJanela() {
    retornaElemento('JanelaCentral').style.display = 'none';
    retornaElemento('JanelaCentral').innerHTML = '';
}

/**
 *
 * @access public
 * @return void
 **/
function RedimensionaJanela(divName) {
    sizeY = retornaElemento(divName).offsetHeight;
    sizeX = retornaElemento(divName).offsetWidth;


    top.retornaElemento('janela').style.height = sizeY + "px";
    top.retornaElemento('janela').style.width = sizeX + "px";

    top.retornaElemento('JanelaCentral').style.left = "50%";
    top.retornaElemento('JanelaCentral').style.marginLeft = sizeX / -2 + "px";

    top.retornaElemento('JanelaCentral').style.top = "50%";
    top.retornaElemento('JanelaCentral').style.marginTop = ((sizeY / -2) + top.getY()) + "px";

}


/**
 *
 * @access public
 * @return void
 **/
function getY() {

    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;
    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }
    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }
    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;
    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    return y;
}

function centralizaDiv(Id) {
    document.getElementById(Id).style.height = getY + 200;
    document.getElementById(Id).style.display = 'block';
}

//mostra campo select
function showSelect(tipo) {

    for (i = 0; i <= document.getElementsByTagName('select').length - 1; i++) {
        document.getElementsByTagName('select')[i].style.display = (tipo) ? "block" : "none";
    }
}
// ** /

//validação de emails
function ValidaEmail(email) {
    prim = email.indexOf("@");

    if (prim < 2) {
        return false;
    }
    else {
        return true;
    }
}
// ** /

//validação de teclas
function validateKey(evt) {
    //tecla CTRL
    if (evt.keyCode == 17) {
        return false;
    }
    return true;
}
// ** /

//redimensionamento de iframe
var redimensiona = false;
var docLocation = document.location.href;
function verificaRedimensionaIframe() {
    if (docLocation.indexOf("bolsa") > -1) {
        intervalIframe = window.setInterval(redimensionaIframe, 500);
    }

    if (redimensiona == true) {

    }
}
function redimensionaIframe() {
    parent.document.getElementById('personare').style.height = document.documentElement['offsetHeight'] + 'px';
}
;

verificaRedimensionaIframe();
// ** /

var linkClicked
linkClicked = false

function Link(url) {

    if (linkClicked == false) {
        document.location.href = url
    }

    linkClicked = true

    setTimeout('releaseLink()', 20000);

}

function releaseLink() {
    linkClicked = false
}

function alerta(frase) {
    if (frase != '')
        alert(frase)
}

function printYear() {
    var Hoje = new Date();

    if (Hoje.getYear() < 1900)
        document.write(1900 + Hoje.getYear());
    else
        document.write(Hoje.getYear());
}


function showPopup(ID, altura, largura) {
    var w = screen.width;
    var h = screen.height;

    //divide a resoluÃ§Ã£o por 2, obtendo o centro do monitor
    var meio_w = w / 2;
    var meio_h = h / 2;

    //diminui o valor da metade da resoluÃ§Ã£o pelo tamanho da janela, fazendo com q ela fique centralizada
    var altura2 = altura / 2;
    var largura2 = largura / 2;
    var meio1 = meio_h - altura2 + getScrollPos().y;
    var meio2 = meio_w - largura2;

    var tE = document.getElementById(ID);

    redimensionaFundo();

    tE.style.top = meio1 + 'px';
    tE.style.left = meio2 + 'px';
    tE.style.display = 'block';
}
function redimensionaFundo() {
    document.getElementById('fundo_pop_carrinho').style.height = document.body.scrollHeight + "px";
    document.getElementById('fundo_pop_carrinho').style.width = document.body.scrollWidth + "px";
    document.getElementById('fundo_pop_carrinho').style.display = 'block';
}

function gotoSectionID(ID) {
    var _goto = 'ctl.php?mdl=Cartao&cmd=';
    if (ID) {
        _goto += 'TelaExibeSecao&SecaoID=' + ID;
    } else {
        _goto += 'TelaHomeCartao';
    }

    window.location.href = _goto;
    return void(0);
}
var Campaign = {
    'WomanLanding' : {

        'closeFixed': function(ID) {

            SetCookie('WLFC', 1, 0);

            hideDiv(ID);

            divSpace = parent.parent.document.getElementById("format_148x143");

            divSpace.innerHTML = "";

        }

    }

}

/**
 * Funcao para validar o formulario de indicacao no deprecated
 * @param String form - nome do formulario
 * */
function checaEnviaIndicacaoProduto() {

    var erro = "";

    if (!valorCampoText('urlForm', 'txNomeRemetente')) {
        erro += "Faltou incluir o nome do remetente. \n";
    }

    if (!valorCampoText('urlForm', 'txEmailRemetente')) {
        erro += "Faltou incluir o e-mail do remetente. \n";
    }

    if (!valorCampoText('urlForm', 'txNomeDestinatario1')) {
        erro += "Faltou incluir o nome do destinatário. \n";
    }

    if (!valorCampoText('urlForm', 'txEmailDestinatario1')) {
        erro += "Faltou incluir o e-mail do destinatário. \n";
    }

    if (!valorCampoText('urlForm', 'taComentario')) {
        erro += "Faltou incluir a mensagem. \n";
    }

    if (erro == "") {

        document.urlForm.action = '/ctl.php';
        document.urlForm.submit();

    } else {
        alert(erro);
    }

}
