﻿
function imprimir_perfil(perfil) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('imprimir.aspx?pP=" + perfil + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=700,height=568');");
}

function replaceAll(string, token, newtoken) {
    while (string.indexOf(token) != -1) {
        string = string.replace(token, newtoken);
    }
    return string;
}

function abrirLink(campo, tipo, id) {
    if (tipo == "email") {
        window.location = "mailto:" + document.getElementById(campo).innerHTML;
        $("#" + id).attr("href", "mailto:" + document.getElementById(campo).innerHTML).click();
    } else if (tipo == "website") {
        url = document.getElementById(campo).innerHTML;
        if (url.indexOf("http://") == "-1") { url = "http://" + url; }
        $("#" + id).attr("href", url).attr("target", "_blank").click();
    }
}

function changeInputType(
  oldElm, // a reference to the input element
  iType, // value of the type property: 'text' or 'password'
  iValue, // the default value, set to 'password' in the demo
  blankValue, // true if the value should be empty, false otherwise
  noFocus) {  // set to true if the element should not be given focus
    if (!oldElm || !oldElm.parentNode || (iType.length < 4) ||
    !document.getElementById || !document.createElement) return;
    var newElm = document.createElement('input');
    newElm.type = iType;
    if (oldElm.name) newElm.name = oldElm.name;
    if (oldElm.id) newElm.id = oldElm.id;
    if (oldElm.className) newElm.className = oldElm.className;
    if (oldElm.style.width) newElm.style.width = oldElm.style.width;
    if (oldElm.size) newElm.size = oldElm.size;
    if (oldElm.tabIndex) newElm.tabIndex = oldElm.tabIndex;
    if (oldElm.accessKey) newElm.accessKey = oldElm.accessKey;
    newElm.onfocus = function() {
        return function() {
            if (this.hasFocus) return;
            var newElm = changeInputType(this, 'password', iValue,
      (this.value.toLowerCase() == iValue.toLowerCase()) ? true : false);
            if (newElm) newElm.hasFocus = true;
        }
    } ();
    newElm.onblur = function() {
        return function() {
            if (this.hasFocus)
                if (this.value == '' || (this.value.toLowerCase() == iValue.toLowerCase())) {
                changeInputType(this, 'text', iValue, false, true);
            }
        }
    } ();
    // hasFocus is to prevent a loop where onfocus is triggered over and over again
    newElm.hasFocus = false;
    oldElm.parentNode.replaceChild(newElm, oldElm);
    if (!blankValue) newElm.value = iValue;
    if (!noFocus || typeof (noFocus) == 'undefined') {
        window.tempElm = newElm;
        setTimeout("tempElm.hasFocus=true;tempElm.focus();", 1);
    }
    return newElm;
}

function configurar_nome(oElem, iValue, blankValue) {
    if (oElem.value == iValue || oElem.value == '') {
        oElem.value = (blankValue) ? '' : iValue;
    }
}

function configurar_nome_senha() {
    if (document.getElementById("t_acesso_senha").value == 'Senha') {
        changeInputType(document.getElementById("t_acesso_senha"), 'password', 'Senha', true, false)
    }
}

function checkMail(mail) {
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if (typeof (mail) == "string") {
        if (er.test(mail)) { return true; }
    }
    else if (typeof (mail) == "object") {
        if (er.test(mail.value)) { return true; }
    }
    else {
        return false;
    }
}

function validar_newsletter() {
    if (document.getElementById("t_newsletter_nome").value == 'Nome' || document.getElementById("t_newsletter_nome").value == '') {
        alert("O campo nome é obrigatório!");
        document.getElementById("t_newsletter_nome").focus();
        return false;
    }
    if (document.getElementById("t_newsletter_email").value == 'E-mail' || document.getElementById("t_newsletter_email").value == '') {
        alert("O campo e-mail é obrigatório!");
        document.getElementById("t_newsletter_email").focus();
        return false;
    }
    if (!checkMail(document.getElementById("t_newsletter_email").value)) {
        alert("E-mail inválido!");
        document.getElementById("t_newsletter_email").focus();
        return false;
    }
    return true;
}

function validar_cadastro() {
    if (document.getElementById("t_nome").value == '') {
        alert("O campo nome é obrigatório!");
        document.getElementById("t_nome").focus();
        return false;
    }
    if (document.getElementById("t_email").value == '') {
        alert("O campo e-mail é obrigatório!");
        document.getElementById("t_email").focus();
        return false;
    }
    if (!checkMail(document.getElementById("t_email").value)) {
        alert("E-mail inválido!");
        document.getElementById("t_email").focus();
        return false;
    }
    if (document.getElementById("t_cemail").value == '') {
        alert("O campo confirmar e-mail é obrigatório!");
        document.getElementById("t_cemail").focus();
        return false;
    }
    if (!checkMail(document.getElementById("t_cemail").value)) {
        alert("E-mail inválido!");
        document.getElementById("t_cemail").focus();
        return false;
    }
    if (document.getElementById("t_email").value != document.getElementById("t_cemail").value) {
        alert("Você digitou dois emails diferentes!");
        document.getElementById("t_email").focus();
        return false;
    }
    if (document.getElementById("t_senha").value == '') {
        alert("O campo senha é obrigatório!");
        document.getElementById("t_senha").focus();
        return false;
    }
    if (document.getElementById("t_csenha").value == '') {
        alert("O campo confirmar senha é obrigatório!");
        document.getElementById("t_csenha").focus();
        return false;
    }
    if (document.getElementById("t_senha").value != document.getElementById("t_csenha").value) {
        alert("Você digitou duas senhas diferentes!");
        document.getElementById("t_senha").focus();
        return false;
    }
    return true;
}

function validar_login(tipo) {
    if (tipo == "home") {
        if (document.getElementById("t_acesso_login").value == '' || document.getElementById("t_acesso_login").value == 'E-mail') {
            alert("O campo e-mail é obrigatório!");
            document.getElementById("t_acesso_login").focus();
            return false;
        }
        if (!checkMail(document.getElementById("t_acesso_login").value)) {
            alert("E-mail inválido!");
            document.getElementById("t_acesso_login").focus();
            return false;
        }
        if (document.getElementById("t_acesso_senha").value == '' || document.getElementById("t_acesso_senha").value == 'Senha') {
            alert("O campo senha é obrigatório!");
            document.getElementById("t_acesso_senha").focus();
            return false;
        }
    } else {
        if (document.getElementById("t_acesso_login").value == '') {
            alert("O campo e-mail é obrigatório!");
            document.getElementById("t_acesso_login").focus();
            return false;
        }
        if (!checkMail(document.getElementById("t_acesso_login").value)) {
            alert("E-mail inválido!");
            document.getElementById("t_acesso_login").focus();
            return false;
        }
        if (document.getElementById("t_acesso_senha").value == '') {
            alert("O campo senha é obrigatório!");
            document.getElementById("t_acesso_senha").focus();
            return false;
        }
    }
    return true;
}

function valida_edicao_dados() {
    if (document.getElementById("t_nome_perfil").value == '') {
        alert("O campo NOME é obrigatório!");
        document.getElementById("t_nome_perfil").focus();
        return false;
    }

    if (document.getElementById("t_email_perfil").value == '') {
            alert("O campo E-MAIL é obrigatório!");
            document.getElementById("t_email_perfil").focus();
            return false;
        }
        if (!checkMail(document.getElementById("t_email_perfil").value)) {
            alert("E-mail inválido!");
            document.getElementById("t_email_perfil").focus();
            return false;
        }

        if (document.getElementById("t_senha_perfil").value == '') {
            alert("O campo SENHA é obrigatório!");
            document.getElementById("t_senha_perfil").focus();
            return false;
        }

        if (document.getElementById("t_nascimento_perfil").value == '') {
        alert("O campo DATA DE NASCIMENTO é obrigatório!");
        document.getElementById("t_nascimento_perfil").focus();
        return false;
    }

    if (document.getElementById("t_sexo_perfil").value == '') {
        alert("O campo SEXO é obrigatório!");
        document.getElementById("t_sexo_perfil").focus();
        return false;
    }

    if (document.getElementById("t_celular_perfil").value == '') {
        alert("O campo CELULAR é obrigatório!");
        document.getElementById("t_celular_perfil").focus();
        return false;
    }

    if (document.getElementById("t_bairro_perfil").value == '0') {
        alert("O campo BAIRRO é obrigatório!");
        document.getElementById("t_bairro_perfil").focus();
        return false;
    }

    if (document.getElementById("t_resumo_perfil").value == '') {
        alert("O campo RESUMO PROFISSIONAL é obrigatório!");
        document.getElementById("t_resumo_perfil").focus();
        return false;
    }
	else{
		if (document.getElementById("t_resumo_perfil").value.length < 20) {
        alert("O RESUMO PROFISSIONAL deve conter ao mínino 20 caracteres!");
        document.getElementById("t_resumo_perfil").focus();
        return false;
    	}
	}
}

function validar_contato() {
    if (document.getElementById("t_nome").value == '') {
        alert("O campo nome é obrigatório!");
        document.getElementById("t_nome").focus();
        return false;
    }    
    if (document.getElementById("t_cadastrado").value == 'Sim' && document.getElementById("t_nome_usuario").value == '') {
        alert("O campo nome do usuário é obrigatório!");
        document.getElementById("t_nome_usuario").focus();
        return false;
    }
    if (document.getElementById("t_email").value == '') {
        alert("O campo e-mail é obrigatório!");
        document.getElementById("t_email").focus();
        return false;
    }
    if (!checkMail(document.getElementById("t_email").value)) {
        alert("E-mail inválido!");
        document.getElementById("t_email").focus();
        return false;
    }
    if (document.getElementById("t_mensagem").value == '') {
        alert("O campo mensagem é obrigatório!");
        document.getElementById("t_mensagem").focus();
        return false;
    }
    return true;
}

function validar_perfil_contato() {
    if (document.getElementById("t_nome").value == '') {
        alert("O campo nome é obrigatório!");
        document.getElementById("t_nome").focus();
        return false;
    }
    if (document.getElementById("t_email").value == '') {
        alert("O campo e-mail é obrigatório!");
        document.getElementById("t_email").focus();
        return false;
    }
    if (!checkMail(document.getElementById("t_email").value)) {
        alert("E-mail inválido!");
        document.getElementById("t_email").focus();
        return false;
    }
    if (document.getElementById("t_mensagem").value == '') {
        alert("O campo mensagem é obrigatório!");
        document.getElementById("t_mensagem").focus();
        return false;
    }
    return true;
}

function validar_perfil_amigo() {
    if (document.getElementById("t_nome").value == '') {
        alert("O campo nome é obrigatório!");
        document.getElementById("t_nome").focus();
        return false;
    }
    if (document.getElementById("t_email").value == '') {
        alert("O campo e-mail é obrigatório!");
        document.getElementById("t_email").focus();
        return false;
    }
    if (!checkMail(document.getElementById("t_email").value)) {
        alert("E-mail inválido!");
        document.getElementById("t_email").focus();
        return false;
    }
    if (document.getElementById("t_nome_amigo").value == '') {
        alert("O campo nome do amigo é obrigatório!");
        document.getElementById("t_nome_amigo").focus();
        return false;
    }
    if (document.getElementById("t_email_amigo").value == '') {
        alert("O campo e-mail do amigo é obrigatório!");
        document.getElementById("t_email_amigo").focus();
        return false;
    }
    if (!checkMail(document.getElementById("t_email_amigo").value)) {
        alert("E-mail do amigo inválido!");
        document.getElementById("t_email_amigo").focus();
        return false;
    }
    if (document.getElementById("t_mensagem").value == '') {
        alert("O campo mensagem é obrigatório!");
        document.getElementById("t_mensagem").focus();
        return false;
    }
    return true;
}
