// Função de alerta para o usuário
function alertaEmail(objeto) {
    if(objeto.value != '') {
        var controle = verificaEmail(objeto.value);
        if(controle === false) {
            alert('Digite um endereço de e-mail válido!');
            objeto.focus();
            return false;
        } else {
            return true;
        }
    }
}


// Função para validar email
function verificaEmail(mail){
	str = new String(mail);
	var arroba = str.indexOf('@',0);
	if (arroba > 0){ // Tem arroba
		var ponto = str.indexOf('.',arroba);
		if (ponto > 0){ // Tem ponto
			if (str.length > ponto){ // Tem algo depois do ponto
				if ((arroba + 1) != ponto){ // Tem algo entre a arroba e o ponto
					// É um E-mail !!!
					return true;
				}
				return false;
			}
			return false;
		}
		return false;
	}
	return false;
}


// Função para Tratamento Númerico
function numeros(event)
{
    if (event.keyCode) {
        if (event.keyCode < 44 || event.keyCode > 58) {
            return false;
        } else {
            return true;
        }
    } else {
        if (event.which < 44 || event.which > 58) {
            return false;
        } else {
            return true;
        }
    }
}


// Função para Mascara de Formatação
function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode; 
	} else if(document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	}

	sValue = objForm[strField].value;

	// Limpa todos os caracteres de formatação que
	// já estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	fldLen = sValue.length;
	mskLen = sMask.length;

	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;
	
	while (i <= mskLen) {
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
	
		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++;
		} else {
			sCod += sValue.charAt(nCount);
			nCount++;
		}
	
		i++;
	}
	
	objForm[strField].value = sCod;
	
	if (nTecla != 8) { // backspace
		if (sMask.charAt(i-1) == "9") { // apenas números...
			return ((nTecla > 47) && (nTecla < 58)); // números de 0 a 9
		} else { // qualquer caracter...
			return true;
		} 
	} else {
		return true;
	}
}


// Função para validar CPF
function validar_cpf( sender ){
	var conf=0
	var temp
	var temp1
	var cont
	var dg1
	var dg1_1
	var dg2
	var dg2_2
	var sValue
	var digits="0123456789"

	sValue = sender.value;

	// Limpa todos os caracteres de formatação que
	// já estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
      
	for (var i = 0; i < sValue.length; i++) {
		temp1=temp
		temp=sValue.substring(i,i+1)
		
		if (temp==temp1){
			conf=conf+1
		}
    
		if (digits.indexOf(temp)==-1){
			alert("Por Favor, o Campo CPF deve ser preenchido apenas com números!")
      sender.focus()
      return
		}
	}

	if (conf==10){
		alert("Número do CPF Inválido")
		sender.focus()
		return
	}

	cont=0

	for (var i=0;i<sValue.length;i++){
		temp=sValue.substring(i,i+1)
		cont=cont+1
	}

	if (cont!=0){
		if (cont!=11){
			alert("Número do CPF Inválido")
			sender.focus()
			return
		}
    else {
			cont=1
			dg1=0
			dg2=0
			
			for (var i=0;i<sValue.length;i++){
				temp=Math.abs(sValue.substring(i,i+1))
        
				if (cont==1) {
					dg1=dg1+Math.abs(temp*10)
				}
               
				if (cont==2) {
					dg1=dg1+Math.abs(temp*9)
				}

				if (cont==3) {
					dg1=dg1+Math.abs(temp*8)
				}

				if (cont==4) {
					dg1=dg1+Math.abs(temp*7)
				}

				if (cont==5) {
					dg1=dg1+Math.abs(temp*6)
				}

				if (cont==6) {
					dg1=dg1+Math.abs(temp*5)
				}

				if (cont==7) {
					dg1=dg1+Math.abs(temp*4)
				}

				if (cont==8) {
					dg1=dg1+Math.abs(temp*3)
				}

				if (cont==9){
					dg1=dg1+Math.abs(temp*2)
				}

				if (cont==10) {
					dg1_1=temp
				}
					
				if (cont==11) {
					dg2_2=temp
				}
				
				cont=cont+1
			}

			dg1=11-(dg1-((Math.floor(dg1/11))*11))

			if (dg1==10 || dg1==11) {
				dg1=0
			}

			if (dg1!=dg1_1) {
				alert("Número do CPF Inválido")
				sender.focus()
				return
			}
			else {
				cont=1

				for (var i=0;i<sValue.length;i++) {
					temp=Math.abs(sValue.substring(i,i+1))

					if (cont==1){
						dg2=dg2+Math.abs(temp*11)
					}

					if (cont==2){
						dg2=dg2+Math.abs(temp*10)
					}

					if (cont==3){
						dg2=dg2+Math.abs(temp*9)
					}

					if (cont==4){
						dg2=dg2+Math.abs(temp*8)
					}

					if (cont==5){
						dg2=dg2+Math.abs(temp*7)
					}

					if (cont==6){
						dg2=dg2+Math.abs(temp*6)
					}

					if (cont==7){
						dg2=dg2+Math.abs(temp*5)
					}

					if (cont==8){
						dg2=dg2+Math.abs(temp*4)
					}

					if (cont==9){
						dg2=dg2+Math.abs(temp*3)
					}

					if (cont==10){
						 dg2=dg2+Math.abs(temp*2)
					}
					
					cont=cont+1
				}
        
				dg2=11-(dg2-((Math.floor(dg2/11))*11))
        
				if (dg2==10 || dg2==11){
        	dg2=0
				}
        
				if (dg2!=dg2_2) {
					alert("Número do CPF Inválido")
					sender.focus()
					return
				}
			}
		}
	}
}