// by Eber
function callback_VerificarCodigo(campo,msg){
	statusCodigo = document.getElementById("sp"+campo);
	idCodigo = document.getElementById(campo);
	statusCodigo.innerHTML = 'Campo requerido!';
	statusCodigo.style.color = "red";
	statusCodigo.style.bgcolor = "#FF3300";
	idCodigo.style.borderColor = "#E00";
	document.getElementById("sp"+campo).innerHTML = msg;
}
function callback_ok(campo){
	statusCodigo = document.getElementById("sp"+campo);
	idCodigo = document.getElementById(campo);
	statusCodigo.innerHTML = "<img src='/_img/ok.gif'>";
	statusCodigo.style.color = "#66CC00";
	idCodigo.style.borderColor = "#66CC00";
}
function verifica_texto(obj) {
	var ret = false;
	if (obj.value.length == 0) {
		ret = true;
	}
	return ret;
}
function validaform(frm){
	var retorno = true;
	for (var i = 0; i < frm.elements.length - 1; i++) {
		if ((frm.elements[i].getAttribute('tipo') == 'texto') && (frm.elements[i].getAttribute('req') == 'true')) {
			if (verifica_texto(frm.elements[i])){
				retorno = false;
				callback_VerificarCodigo(frm.elements[i].getAttribute('name'),frm.elements[i].getAttribute('msg'));
				frm.elements[i].focus();
				break;
			}else{
				retorno = true;
			 	callback_ok(frm.elements[i].getAttribute('name'));
			}
		}else if ((frm.elements[i].getAttribute('tipo') == 'email') && (frm.elements[i].getAttribute('req') == 'true')) {
			if (checa(frm.elements[i])) {
				retorno = false;
				callback_VerificarCodigo(frm.elements[i].getAttribute('name'),frm.elements[i].getAttribute('msg'));
				frm.elements[i].focus();
				break;
			}else{
				retorno = true;
				callback_ok(frm.elements[i].getAttribute('name'));
			}
		}else if ((frm.elements[i].getAttribute('tipo') == 'cnpj') && (frm.elements[i].getAttribute('req') == 'true')) {
			if (isCNPJ(frm.elements[i].value,frm.elements[i].getAttribute('name'))){
				retorno = false;
				callback_VerificarCodigo(frm.elements[i].getAttribute('name'),frm.elements[i].getAttribute('msg'));
				frm.elements[i].focus();
				break;
			}else{
				retorno = true;
				callback_ok(frm.elements[i].getAttribute('name'));
			}
		}
	}
	for (var i = 0; i < frm.elements.length - 1; i++) {
		if(retorno==false){
			break;
		}
	}
	return retorno;
}

function IsDate(obj){
	var pData = obj.value;
	if(pData.length<10 || pData.length>10){
		alert(obj.msg);
		return false;
	}
	var ano = '' + pData.substring(6,10);
	var mes = '' + pData.substring(3,5);
	var dia = '' + pData.substring(0,2);
	if(dia>'31'){
		alert('Data inválida');
		return false;
	}
	if(mes>'12'){
		alert('Data inválida');
		return false;
	}
	if(mes=='02'){
		if(ano%4!=0 && dia>'28'){
			alert('Data Inválida');
			return false;
		}
		else{
			if(dia>'29'){
				alert('Data Inválida');
				return false;
			}
		}
	}
	if(mes<='07'){
		if(mes%2==0 && dia>'30'){
			alert('Data inválida');
			return false;
		}
	}
	else{
		if(mes>'09'){
				if(mes%2!=0 && dia>'30'){
					alert('Data inválida');
					return false;
				}
		}
	}
	return true

}
function isCNPJ(strCNPJ,name) {
	 CNPJ = strCNPJ;
	 erro = new String;
	 if (CNPJ.length < 18) erro += "Preencha corretamente o CNPJ!<br>";
	 if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
	 if (erro.length == 0) erro += "Preencha corretamente o CNPJ!<br>";
	 }
	 //substituir os caracteres que não são números
   if(document.layers && parseInt(navigator.appVersion) == 4){
		   x = CNPJ.substring(0,2);
		   x += CNPJ.substring(3,6);
		   x += CNPJ.substring(7,10);
		   x += CNPJ.substring(11,15);
		   x += CNPJ.substring(16,18);
		   CNPJ = x;
   } else {
		   CNPJ = CNPJ.replace(".","");
		   CNPJ = CNPJ.replace(".","");
		   CNPJ = CNPJ.replace("-","");
		   CNPJ = CNPJ.replace("/","");
   }
   var nonNumbers = /\D/;
   if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! <br>";
   var a = [];
   var b = new Number;
   var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
   for (i=0; i<12; i++){
		   a[i] = CNPJ.charAt(i);
		   b += a[i] * c[i+1];
	}
   if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
   b = 0;
   for (y=0; y<13; y++) {
		   b += (a[y] * c[y]);
   }
   if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
   if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
		   erro +="Dígito verificador com problema!";
   }
   if (erro.length > 0){
	   	   campo = document.getElementById(name);
		   campo.msg = erro;
		   return true;
   } else {
		   return false;
   }
  return true;
}
function verifica_texto(obj) {
	var ret = false;
	if (obj.value.length == 0) {
		alert(obj.msg);
		ret = true;
	}
	return ret;
}
function validaCPF(ncpf) {
	lcpf = ncpf.replace('.', '');
	lcpf = lcpf.replace('.', '');
	lcpf = lcpf.replace('-', '');
	cpf = lcpf;
	valor = true;
	erro = new String;
	if (cpf.length < 11) erro += "São necessários 11 dígitos para verificação do CPF! \n\n";
	var nonNumbers = /\D/;
	if (nonNumbers.test(cpf)) erro += "A verificação de CPF suporta apenas números! \n\n";
	if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
		  erro += "Número de CPF inválido!"
	}
	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 +="Dígito verificador com problema!";
	}
	if (erro.length > 0){
		alert(erro);
		return true;
	}
	return false;
}
function Tecla(e)
{
	if (document.all) // Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers) // Nestcape
		var tecla = e.which;
		if (tecla > 47 && tecla < 58) // numeros de 0 a 9
			return true;
		else
			{
				if (tecla != 8) // backspace
					event.keyCode = 0;
					//return false;
				else
					return true;
			}
}
function checa(nform) {
	if (nform.value == "") {
		alert("Digite seu e-mail.");
		return true;
	} else {
		prim = nform.value.indexOf("@")
		if(prim < 2) {
			alert("Os digitos antes @ devem ser maior ou igual a 2!");
			return true;
		}
		if(nform.value.indexOf("@",prim + 1) != -1) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf(".") < 1) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf(" ") != -1) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf("zipmeil.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf("hotmeil.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf(".@") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf("@.") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf(".com.br.") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf("/") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf("[") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf("]") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf("(") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf(")") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
		if(nform.value.indexOf("..") > 0) {
			alert("O e-mail informado parece não estar correto.");
			return true;
		}
	}
		return false;
}

function formatar(src, mask) {
	var i = src.value.length;
	var saida = mask.substring(i,i+1);
	var ascii = event.keyCode;
	if (saida == "A") {
		if ((ascii >=97) && (ascii <= 122)) { event.keyCode -= 32; }
		else { event.keyCode = 0; }
	} else if (saida == "0") {
		if ((ascii >= 48) && (ascii <= 57)) { return }
		else { event.keyCode = 0 }
	} else if (saida == "#") {
		return;
	} else {
		src.value += saida;
		i += 1
		saida = mask.substring(i,i+1);
		if (saida == "A") {
			if ((ascii >=97) && (ascii <= 122)) { event.keyCode -= 32; }
			else { event.keyCode = 0; }
		} else if (saida == "0") {
			if ((ascii >= 48) && (ascii <= 57)) { return }
			else { event.keyCode = 0 }
		} else { return; }
	}
}

function isMail(mailField){
  strMail = mailField.value;
  var re = new RegExp;
  re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var arr = re.exec(strMail);
  if (arr == null)
    return(false);
  else
    return(true);
}
function minLen(txtField, minVal){
  strExp = txtField.value;
  l = strExp.length;
  if (l < minVal)
    return(true);
  else
    return(false);
}
function maxLen(txtField, maxVal){
  strExp = txtField.value;
  l = strExp.length;
  if (l > maxVal)
    return(true);
  else
    return(false);
}
function isBlank(txtField){
  if (txtField.value)
    return (false);
  else
    return(true);
}
function isSelectedZero(txtField){
  selected = txtField.selectedIndex;
  if (selected == 0) {
    alert(txtField.msg);
	return(true); }
  else {
    return(false); }
}
function isNumber(txtField){
  numExp = txtField.value;
  if (isNaN(numExp) || (numExp.length == 0)) {
	alert(txtField.msg);
    return (false); }
  else {
    return(true); }
}
function isCPF(txtField){
  var i;
  s = txtField.value;
  var c = s.substr(0,9);
  var dv = s.substr(9,2);
  var d1 = 0;
  for (i = 0; i < 9; i++){
    d1 += c.charAt(i)*(10-i);
  }
  if (d1 == 0) return false;
  d1 = 11 - (d1 % 11);
  if (d1 > 9) d1 = 0;
  if (dv.charAt(0) != d1) return false;
  d1 *= 2;
  for (i = 0; i < 9; i++){
   d1 += c.charAt(i)*(11-i);
 }
  d1 = 11 - (d1 % 11);
  if (d1 > 9) d1 = 0;
  if (dv.charAt(1) != d1) return false;
  return true;
}
/**
 *
 * function mask(_mask, val)
 *
 * _mask = Mascara Exemplo: ##/##/#### ou ###.###.###-##
 * val   = Valor a ser formatado.
 *
 * Formata um valor  para a mascara definida.
 *
 * pedro.leao@ig.com.br 2003/08/16
 */
function mask(_mask, val) {
	var i, mki;
	var aux="";
	for(i=mki=0; i<val.length; i++, mki++) {
		if(_mask.charAt(mki)=='' || _mask.charAt(mki)=='#' || _mask.charAt(i)==val.charAt(i)) {
			aux+=val.charAt(i);
		} else {
			aux+=_mask.charAt(mki)+val.charAt(i);
			mki++;
		}
	}
	return aux;
}

/**
 * function maskEvent(field, _mask, event)
 *
 * field = Objeto que esta enviando o evendo onKeyPress()
 * _mask = Mascara Exemplo: ##/##/#### ou ###.###.###-##
 * event = Evento a ser observado.
 *
 * Formata um valor para a mascara definida conforma o valor vai sendo digitado.
 *
 * pedro.leao@ig.com.br 2003;08/16
 */
function maskEvent(field, _mask, event) {
	var key ='';
	var aux='';
	var len=0;
	var i=0;
	var strCheck = '0123456789';
	var rcode = (window.Event) ? event.which : event.keyCode;

	if(rcode == 13) {
		//Enter
		return true;
	}

	//Get key value from key code
	key=String.fromCharCode(rcode);
	if(strCheck.indexOf(key)==-1 || (field.value.length >= _mask.length)) {
		//Not a valid key
		return false;
	}
	aux=field.value+key;
	//window.alert(aux);
	aux=mask(_mask,aux);
	//window.alert(aux);
	field.value=aux;
	return false;
}
/**
 *
 * function currencyFormat(fld, milSep, decSep, e)
 *
 * fld    = Objeto a ser verficado.
 * milSep = Separador para milhar.
 * decSep = Separador para decimal.
 * e      = Evento.
 *
 * Formata um valor decimal conforme for digitado no box.
 * Criação: Anonima (coletada em http://http://www.scriptbrasil.com/?class=2&secao=javascript&categoria=Formulários&menu=javascript&ini=1
 * Revisao: pedro.leao@ig.com.br	2003/08/16
 */
function currencyFormat(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) {
		return true;  // Enter
	}
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) {
		return false;  // Not a valid key
	}
	len = fld.value.length;
	for(i = 0; i < len; i++) {
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)){
			 break;
		}
	}
	aux = '';
	for(; i < len; i++) {
		if (strCheck.indexOf(fld.value.charAt(i))!=-1){
			aux += fld.value.charAt(i);
		}
	}
	aux += key;
	len = aux.length;
	if (len == 0) {
		fld.value = '';
	} else if (len == 1) {
		fld.value = '0'+ decSep + '0' + aux;
	} else if (len == 2) {
		fld.value = '0'+ decSep + aux;
	} else if (len > 2) {
		aux2 = '';
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
	}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--) {
			fld.value += aux2.charAt(i);
		}
		fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}
