function isFalse(campo) {
    if (document.getElementById(campo).value == '' || document.getElementById(campo).value == null) {
            return true;
    } else {
            return false;
    }
}

function defineEstado(id_est) {
    document.getElementById('id_cidade').disabled = true;
    ajax('cidade', 'php/cidades.php?&id=' + id_est);
}

function valorRadio(campo) {
    for(i = 0; i < document.getElementById(campo).length; i++){
            if (document.getElementById(campo)[i].checked) break;
            return document.getElementById(campo)[i].value;
    }
}

function innerDiv(div, texto) {
    if (texto == null) texto = '';
    document.getElementById(div).innerHTML = texto;
}

function focusDiv(div, texto) {
    document.getElementById(div).focus();
}

function innerElement(obj, texto) {
    document.getElementById(obj).value = texto;
}

function hideDiv(div){
    document.getElementById(div).style.display = 'none';
}

function showDiv(div){
    document.getElementById(div).style.display = 'block';
}

function showPopup(URL, nome, w, h) {
    var myWin = window.open(URL, 'pp_' + nome, 'width=' + w + ', height=' + h + ', top=' + ((screen.height / 2) - (h / 2)) + ', left=' + ((screen.width / 2) - (w / 2)) + ', toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=0'); 
    myWin.focus();
}

function showTab(div) {
    if (document.getElementById('cont_descricao')) { hideDiv('cont_descricao'); }
    if (document.getElementById('cont_porque_consumir')) { hideDiv('cont_porque_consumir'); }
    if (document.getElementById('cont_sugestao_uso')) { hideDiv('cont_sugestao_uso'); }
    if (document.getElementById('cont_modo_preparo')) { hideDiv('cont_modo_preparo'); }
    if (document.getElementById('cont_dicas')) { hideDiv('cont_dicas'); }
    if (document.getElementById('cont_indicacao')) { hideDiv('cont_indicacao'); }
    if (document.getElementById('cont_ingredientes')) { hideDiv('cont_ingredientes'); }
    showDiv(div);
}

function buscarCep() {
    showDiv('retorno_frete');
    innerDiv('retorno_frete', 'Aguarde, consultando...');
    ajax('retorno_frete','correios.php?cep=' + document.getElementById('cep').value + '&peso=' + document.getElementById('peso').value);
}

function abrirJanela(div, path) {
    showDiv('lightbox');
    innerDiv(div, 'Aguarde, carregando...');

    ajax(div, path);
    showDiv(div);
}

function fecharJanela(div) {
    hideDiv('lightbox');
    hideDiv(div);
    innerDiv(div, '');
}

function login(d) {
if (document.getElementById('email').value == '') {
    alert('Informe seu e-mail de cadastro!');
    focusDiv('email');
} else if (document.getElementById('senha').value == '') {
    alert('Informe sua senha!');
    focusDiv('senha');
} else {
    innerDiv('retorno_login', 'Aguarde, confirmando dados...');
    ajax('retorno_login','login.php?d=' + d + '&e=' + document.getElementById('email').value + '&s=' + Base64.encode(document.getElementById('senha').value));
}
}

function logoff() {
    ajax('retorno_user','logoff.php');
}

function comprar(id) {
    innerDiv('carrinho','<div id="car_transp" style="font-size:14px; color:#FFF; text-align:center;">Adicionando produto, aguarde...</div>');
    ajax('carrinho','comprar.php?id=' + id);
}

function erroProd(v) {
    if (v == 1) { 
            alert('ATENÇÃO\nExistem produtos em seu carrinho fora de estoque.\n\nRemova os produtos que exibem um alerta com texto \nvermelho para poder prosseguir e concluir seu pedido.');
    } else if (v == 2) {
            alert('ATENÇÃO\nNão é possível prosseguir pois não existem produtos em seu carrinho.');
    }
}

function enviarOrc() {
    if (isFalse('nome')) { alert('Por favor, informe seu nome!'); focusDiv('nome'); }
    else if (isFalse('email')) { alert('Por favor, informe seu e-mail!'); focusDiv('email'); }
    else if (isFalse('email_conf')) { alert('Por favor, confirme seu e-mail!'); focusDiv('email_conf'); }
    else if (document.getElementById('email').value != document.getElementById('email_conf').value) { alert('Os e-mails não são iguais, por favor corrija!'); focusDiv('email'); }
    else if (isFalse('cep')) { alert('Por favor, informe um CEP válido!'); focusDiv('cep'); }
    else if (isFalse('ddd')) { alert('Por favor, informe o DDD!'); focusDiv('ddd'); }
    else if (isFalse('telefone')) { alert('Por favor, informe ao menos um número de telefone!'); focusDiv('telefone'); }
    else {
            document.formOrcamento.submit();	
    }
}


function ConfAddEnd() {
    if (document.formulario.titulo_end.value == '') {
        document.formulario.titulo_end.focus();
        alert('Preencha o campo Título!');
    } else if (document.formulario.endereco.value == '') {
        document.formulario.endereco.focus();
        alert('Preencha o campo Endereço!');
    } else if (document.formulario.numero.value == '') {
        document.formulario.numero.focus();
        alert('Preencha o campo Número!');
    } else if (document.formulario.cep.value == '') {
        document.formulario.cep.focus();
        alert('Preencha o campo CEP!');
    } else if (document.formulario.bairro.value == '') {
        document.formulario.bairro.focus();
        alert('Preencha o campo Bairro!');
    } else if (document.formulario.id_estado.value == '0') {
        document.formulario.id_estado.focus();
        alert('Escolha o Estado!');
    } else if (document.formulario.id_cidade.value == '0') {
        document.formulario.id_cidade.focus();
        alert('Escolha a Cidade!');
    } else {
        document.formulario.submit();
    }
}
