function evento_ascii(e) {
	var tecla = 0
	var keynum
	if(window.event) keynum = e.keyCode
	else if(e.which) keynum = e.which
	return keynum
}

function evento(e)
{
	if(typeof(e)=='undefined')var e=window.event;
    var source=e.target?e.target:e.srcElement;
    if(source.nodeType==3)source=source.parentNode;
	return source;
}
function soNumeros()
{
	tecla_ascii = evento_ascii(e)
	if(tecla_ascii == 8 || tecla_ascii == 13 || tecla_ascii == undefined) return true
	if(tecla_ascii >= 48 && tecla_ascii <=57) return true
	return false
}
function masc(e,obj,mascara) {
	var val = ""
	tecla = evento_ascii(e)
		str   = obj.value
		len   = str.length
		if(tecla == 8 || tecla == 13 || tecla == undefined) return true
		
		if(len < (mascara.length) && (tecla >= 48 && tecla <=57))
		{
			str2  = ""
			digito = /^\d$/
			for (i = 0; i < len ; i ++ ) {
				if (digito.test(str.charAt(i))) str2 += str.charAt(i)
			}
			str   = str2
			len   = str.length
			str2  = ""
			for (i = 0 , j = 0 ; j < len && i < (mascara.length-1); i ++ ) {
				if (mascara.charAt(i) == "#") {
					str2 += str.charAt(j)
					j++
				} else {
					str2 += mascara.charAt(i)
				}
			}
			obj.value = str2
			return true
		}else return false
}	

function masc_moeda(e) 
{
	var val = ""
	tecla_ascii = evento_ascii(e)
	
		tecla   = evento(e)
		len   	= tecla.value.length
		if(tecla_ascii == 8 || tecla_ascii == 13 || tecla_ascii == undefined) return true
		if(tecla_ascii >= 48 && tecla_ascii <=57)
		{
			str2  = ""
			digito = /^\d$/
			
			for (i = 0; i < len ; i ++ ) {
				if (digito.test(tecla.value.charAt(i))) str2 += tecla.value.charAt(i)
			}
			
			str   = str2
			len   = str.length
			str2  = ""
			cont = 0
			flag = 1
			for (i = len ; i >= 0 ; i-- ) {
				if(cont % 2 == 0 && flag && cont != 0 )
				{
					flag = 0
					cont = 0
					str2 =  str.charAt(i)+ " ," + str2 
				}
				else
				{
					if( cont % 3 == 0 && cont != 0 )
						str2 =  str.charAt(i)+ "." + str2 
					else
						str2 = str.charAt(i) + str2	
				}
				cont++
			}
		
			tecla.value = str2;
			return true
		}else return false
}

function validaCPF(op,msg) {
		cpf   = document.getElementById(op).value
		len   = cpf.length
		cpf2  = ""
		digito = /^\d$/
		for (i = 0; i < len ; i ++ ) {
			if (digito.test(cpf.charAt(i))) cpf2 += cpf.charAt(i)
		}
		cpf = cpf2

		//cpf = document.getElementById(op).value
		valor = true
		erro = new String
		if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! <br>" 
		var nonNumbers = /\D/;
		if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! <br>"	
		if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
			  erro += "Numero de CPF invalido!"
		}
		var a = []
		var b = new Number
		var c = 11
		for (i=0; i<11; i++){
			a[i] = cpf.charAt(i)
			if (i < 9) b += (a[i] *  --c)
		}
		if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
		b = 0
		c = 11
		for (y=0; y<10; y++) b += (a[y] *  c--)
		if ((x = b % 11) < 2) { a[10] = 0 } else { a[10] = 11-x }
		if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
			erro +="Digito verificador com problema!"
		}
		if (erro.length > 0){
			document.getElementById(msg).innerHTML = erro			
			document.getElementById(msg).style.visibility = 'visible';
			return false
		}
		return true
	}
	
	

