//Valida e-mail
function evaluateEmail(obj) {
	var re = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{ 1,3}){3}\])$/
	
    if (re.exec(obj.value) != null)
    {
		return true;
    }
    else
    {
		return false;           
	}
} 

//Valida um CPF (apenas números)
function evaluateCPF(cpf) {
	valor = true;
	erro = new String;
	if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n"; 
	var nonNumbers = /\D/;
	if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";	
	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){
		//alert(erro);
		return false;
	}
	return true;
}

//Limpa Todos os campos de um formulário
function clearForm(objForm) {
	for(i=0;i<=(objForm.length - 1);i++){
		vType = (objForm.item(i).name).substring(0,3)
		switch(vType){
			case "txt":
				(objForm.item(i)).value = ""
			case "chk":
				(objForm.item(i)).checked = false
			case "lst":
				(objForm.item(i)).selectedIndex = 0
		}
	}
}

//Aplica a mascara desejada a um campo texto
function maskFormat(mask,obj){
	if(obj.value != ""){
		sObj = obj.value.toString()
		nString = ""
		for(i=0;i<(sObj.length);i++){
			if(!isNaN(sObj.charAt(i)) && (sObj.charAt(i) != " ")) {
				nString = nString + sObj.charAt(i)
			}
		}
		j = 0
		k = 0
		mString = ""
		while(j < nString.length && k < mask.length){
			if(mask.charAt(k).toUpperCase() == "N"){
				mString = mString + nString.charAt(j)
				j = j+1
			} else {
				mString = mString + mask.charAt(k)
			}
			k = k+1
		}
		obj.value = mString
	}
}

//Mostra a dica do campo em um objeto SPAN de nome info
function msg_onfocus(msg) {
	info.innerHTML = msg
}

//Mostra o tamanho da caixa e texto ou textarea em um objeto SPAN passado em objCounter
function counter(obj, objCounter,maxLength){
	if((obj.value).length > maxLength){
		obj.value = (obj.value).substring(0,maxLength)
	}
	objCounter.innerText = "(" + (obj.value).length + "/" + maxLength + ")"
}

function validateFloat(Obj) {
	if (isNaN(Obj.value)) {
		val = Obj.value.toString()
		newval = ''
		for (i=0;i<=val.length;i++) {
			if (!isNaN(val.substring(i,i+1)) || val.substring(i,i+1) == '.' || val.substring(i,i+1) == ',') {
				if (val.substring(i,i+1) == ',') {
					newval = newval + '.'
				} else {
					newval = newval + val.substring(i,i+1)
				}
			}
		}
		Obj.value = newval
		Obj.focus()
	}
}
function verificaCpf(cpf) {
  if (cpf.length == 11) {
  	var cpf1,cpf2,controle,contini,contfim,ii,soma,i,digito;
  	cpf1 = cpf;
  	cpf2 = cpf.substring(cpf.length-2,cpf.length);
  	controle = "";
  	contini = 2;
  	contfim = 10;

  	for (ii=1;ii<=2;ii++){
  	  soma=0;
  		for(i=contini;i<=contfim;i++)
  			soma = soma + ((cpf1.substring(i-ii-1,i-ii) * (contfim + 1 + ii - i)));
  		if (ii==2)
  			soma = soma + (2 * digito);
  		digito = (soma * 10)%11;
  		if (digito==10)
  		  digito=0;
  		controle = controle + digito;
  		contini = 3;
  		contfim = 11;
    }
  	if (controle!=cpf2){
  	 	return false;
  	}else{
  		return true;
  	}
  }
  if ((cpf.length < 11) && (cpf.length > 0)){
  	return false;    
  }     
  return false;
}

