﻿// Validação dos campos de e-mail

function ValidarEnvioPagina()
{
    if(document.getElementById('txtNome').value.isEmpty())
   {
        alert('Por Favor, informe seu nome!');
        document.getElementById('txtNome').focus();
        return false;
   }
   
   if(document.getElementById('txtEmail').value.isEmpty())
   {
        alert('Por Favor, informe seu e-mai!');
        document.getElementById('txtEmail').focus();
        return false;
   }
   else
   {
       if(!document.getElementById('txtEmail').value.isValidEmail())
       {
            alert('E-mail invalido!');
            document.getElementById('txtEmail').focus();
            return false;
       }
   }
   
   if(document.getElementById('txtNomeDest1').value.isEmpty())
   {
        alert('Por Favor, insira o nome do destinatario!');
        document.getElementById('txtNomeDest1').focus();
        return false;
   }
  
   if(document.getElementById('txtEmailDest1').value.isEmpty())
   {
        alert('Por Favor, insira o e-mail do destinatario!');
        document.getElementById('txtEmailDest1').focus();
        return false;
   }
   else
   {
       if(!document.getElementById('txtEmailDest1').value.isValidEmail())
       {
            alert('E-mail invalido!');
            document.getElementById('txtEmailDest1').focus();
            return false;
       }
   }
   
    if(document.getElementById('txtComentario').value.isEmpty())
    {
        alert('Por favor, insira o seu comentario!');
        document.getElementById('txtComentario').focus();
        return false;
    }
   
   if(!document.getElementById('txtNomeDest2').value.isEmpty())
   {
        if(document.getElementById('txtNomeDest2').value.isEmpty())
        {
             alert('Por favor, insira o nome do destinatario!');
             document.getElementById('txtNomeDest2').focus();
             return false;
        }
        if(document.getElementById('txtEmailDest2').value.isEmpty())
        {
            alert('Por favor, insira o e-mail do destinatario!');
            document.getElementById('txtEmailDest2').focus();
            return false;
        }
   }
   
   if(!document.getElementById('txtEmailDest2').value.isEmpty())
   {
        if(!document.getElementById('txtEmailDest2').value.isValidEmail())
        {
             alert('E-mail invalido!');
             document.getElementById('txtEmailDest2').focus();
             return false;
        }
        if(document.getElementById('txtNomeDest2').value.isEmpty())
        {
            alert('Por favor, insira o nome do destinatario!');
            document.getElementById('txtNomeDest2').focus();
            return false;
        }
   }
   
   if(!document.getElementById('txtNomeDest3').value.isEmpty())
   {
        if(document.getElementById('txtNomeDest3').value.isEmpty())
        {
             alert('Por favor, insira o nome do destinatario!');
             document.getElementById('txtNomeDest3').focus();
             return false;
        }
        if(document.getElementById('txtEmailDest3').value.isEmpty())
        {
            alert('Por favor, insira o e-mail do destinatario!');
            document.getElementById('txtEmailDest3').focus();
            return false;
        }
   }
   
   if(!document.getElementById('txtEmailDest3').value.isEmpty())
   {
        if(!document.getElementById('txtEmailDest3').value.isValidEmail())
        {
             alert('E-mail invalido!');
             document.getElementById('txtEmailDest3').focus();
             return false;
        }
        if(document.getElementById('txtNomeDest3').value.isEmpty())
        {
            alert('Por favor, insira o nome do destinatario!');
            document.getElementById('txtNomeDest3').focus();
            return false;
        }
   }  
         
   return;
}

function FecharEmail(){
    alert("E-mail enviado com sucesso!");
    window.close();
}

function VerificaMoney(QtdeCasasEsq, QtdeCasasDir, SepMilhar, SepDecimal, oCampo, oEvent)
{
    var zeros = "00000000000000000";
    var tecla_codigo;
    var tecla_char;
    var valor = oCampo.value;
    var valor_aux;
    
    if (oEvent.keyCode)
        tecla_codigo = oEvent.keyCode;
    else if (oEvent.which)
        tecla_codigo = oEvent.which;
    
    tecla_char = String.fromCharCode(tecla_codigo);
    
//    if (!OnlyNumbers(oEvent) && tecla_codigo != 8 && tecla_codigo != 46)
//        return false;
    if (!ValidarNumero2(oCampo))
        return false;
    
    oCampo.style.textAlign = 'right';
    valor = oCampo.value.toString();

    if (QtdeCasasDir == '' || isNaN(QtdeCasasDir) || parseInt(QtdeCasasDir,10) < 0)
        QtdeCasasDir = 0;
    
    if (QtdeCasasDir == 0)
        SepDecimal = '';
    
    if (isNaN(QtdeCasasEsq) || QtdeCasasEsq < 0)
        QtdeCasasEsq = oCampo.maxLength - QtdeCasasDir;
    
    if (SepDecimal == SepMilhar && SepDecimal != '')
        SepMilhar = '';
    
    if ((QtdeCasasEsq + QtdeCasasDir) > 16)
    {
        alert('A função não aceita mais que 16 digitos')
        return;
    } 
    
    //46 é a tecla Delete
    if (valor == '' || tecla_codigo == 46)
    {
        valor = '0';
    }
    else
    {
        if (SepMilhar != '')
        {
            while (valor.indexOf(SepMilhar) != -1)
            {
                valor = valor.replace(SepMilhar, '');
            }
        }
        
        if (SepDecimal != '')
            valor = valor.replace(SepDecimal, '');
    }
    
    //8 é a tecla Backspace
    if (tecla_codigo == 8)
    {
        valor = valor.substr(0, valor.length - 1);
        return;
    }
    
    if (valor.length == 0)
        valor = '0';
    
    valor_aux = parseInt(valor + tecla_char, 10).toString();
    
    //Verificando se o número é menor que 0 (decimal, por exemplo: 0,01)
    if (valor_aux.length <= QtdeCasasDir)
        valor_aux = zeros.substr(0, QtdeCasasDir - (valor_aux.length - 1)) + valor_aux;
    
    if (QtdeCasasEsq < valor_aux.length - QtdeCasasDir)
        return false;
    
    var j = 0;
    valor = '';
    for (var i = valor_aux.length - QtdeCasasDir-1; i >= 0; i--)
    {
        if (j == 3)
        {
            valor = SepMilhar + valor;
            j = 0;
        }
        valor = valor_aux.charAt(i) + valor;
        j++;
    }
    valor += SepDecimal + valor_aux.substr(valor_aux.length - QtdeCasasDir, QtdeCasasDir);
    
    oCampo.value = '';
    for (var i = 0; i < valor.length; i++)
    {
        oCampo.value += valor.charAt(i);
    }
    
    return false;
}

function ChamarVerificaMoney(QtdeCasasEsq, QtdeCasasDir, SepMilhar, SepDecimal, oCampo, oEvent)
{
    if (window.event && window.opera == null)
        VerificaMoney(QtdeCasasEsq, QtdeCasasDir, SepMilhar, SepDecimal, oCampo, oEvent);
    
    return;
}

function OnlyNumbers(oEvent)
{
    var oRegExp = /[0-9]/;
    var tecla_codigo;
    var tecla_char;
    
    if (oEvent.keyCode)
        tecla_codigo = oEvent.keyCode;
    else if (oEvent.which)
        tecla_codigo = oEvent.which;
    
    tecla_char = String.fromCharCode(tecla_codigo);
    
    //Permite algumas teclas
    if (tecla_codigo == 8 || tecla_codigo == 9 || (tecla_codigo == 35 && oEvent.shiftKey) || (tecla_codigo == 36 && oEvent.shiftKey) || (tecla_codigo == 37 && (oEvent.shiftKey || tecla_char != '%')) || (tecla_codigo == 39 && tecla_char != "'") || (tecla_codigo == 46 && tecla_char != '.'))
        return true;
    else 
        return !oRegExp.test(tecla_char);
}

function ValidarNumero2(sender)
{
	sender.onkeypress=function(e)
	{
		var e=window.event || e;
		var keyunicode=e.charCode || e.keyCode;
		return (keyunicode>=48 && keyunicode<=57 || keyunicode==8 || keyunicode==9 || keyunicode==44 || keyunicode==37 || keyunicode==39);
	};
}
