////////////////////////////////////////
// Validação P/ Campo só Aceitar Número
////////////////////////////////////////
function soNumero(evt){
	try {
		var charCode = (evt.which) ? evt.which : event.keyCode;
		
  		if (charCode > 31 && (charCode < 48 || charCode > 57)){
			return false;
  		}
	}catch(e){
		return true;
	}

   return true;
}

///////////////////////////////////////////////////////////////////
//Não permite a digitação de letras nos campos somente numéricos
//////////////////////////////////////////////////////////////////
function validaNumeros(num) {
	var nums = "0123456789";
	var valor;
	
	for ( var i=0; i<num.value.length; i++ )
	{
		valor=num.value.substring(i, i+1);
		if ( nums.indexOf(valor) == -1 )
		{
			num.value = num.value.substring(0,i);
			break;
		}
	}
}

///////////////////////////////////////////////////////////////////
//Se caracter digitado tiver acentos, cedilha, etc., muda os caracteres
//////////////////////////////////////////////////////////////////
function validaTexto(campo) {	
	var CaracteresInvalidos = new Array("\"","¨","-",";",":","|","\\",
		"à","è","ì","ò","ù","á","é","í","ó","ú","â","ê","î","ô","û",
		"À","È","Ì","Ò","Ù","Á","É","Í","Ó","Ú","Â","Ê","Î","Ô","Û","Ã","Õ",		
		"ã","õ","[","]","{","}","<",">","*","+","$","!","?","#",
		"_","'","=","Ç","ç",
		"@","(",")","´","`","^","~","/");
		
	var CaracteresValidos = new Array(" "," "," "," "," "," "," ",
		"a","e","i","o","u","a","e","i","o","u","a","e","i","o","u",
		"A","E","I","O","U","A","E","I","O","U","A","E","I","O","U","A","O",
		"a","o"," "," "," "," "," "," "," "," "," "," "," "," ",
		" "," "," ","C","c",
		" "," "," "," "," "," "," "," ");
		
	for (i = 0; i < CaracteresInvalidos.length; i++){
		posicao = campo.value.indexOf(CaracteresInvalidos[i]);
		if (posicao != -1){			
			campo.value = campo.value.substring(0,posicao) + CaracteresValidos[i];			
		}	else if( (posicao = campo.value.indexOf("  ")) != -1 ) {
			campo.value = campo.value.substring(0,posicao + 1);			
		}
	}	
}
///////////////////////////////////////////////////////////////////
//Se caracter digitado tiver acentos, cedilha, etc., muda os caracteres
//////////////////////////////////////////////////////////////////
function removeAcentos(campo) {	
	var CaracteresInvalidos = new Array(
			"Á","À","Ä","Â","Ã","É","È","Ë","Ê","Í","Ì","Ï","Î","Ó","Ò","Ö","Ô","Õ","Ú","Ù","Û","Ü","Ç",
			"á","à","ä","â","ã","é","è","ë","ê","í","ì","ï","î","ó","ò","ö","ô","õ","ú","ù","û","ü","ç",
			"'","´","`","¨","^","~");
		
	var CaracteresValidos = new Array(
			"A","A","A","A","A","E","E","E","E","I","I","I","I","O","O","O","O","O","U","U","U","U","C",
			"a","a","a","a","a","e","e","e","e","i","i","i","i","i","ò","ö","ô","õ","ú","ù","û","ü","ç",
			"'","´","`","¨","^","~");
		
	for (i = 0; i < CaracteresInvalidos.length; i++){
		posicao = campo.value.indexOf(CaracteresInvalidos[i]);
		if (posicao != -1){			
			campo.value = campo.value.substring(0,posicao) + CaracteresValidos[i];			
		}	else if( (posicao = campo.value.indexOf("  ")) != -1 ) {
			campo.value = campo.value.substring(0,posicao + 1);			
		}
	}	
}

//////////////////////////////////////////////
//Capturar valor de radio button
//////////////////////////////////////////////
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	
	var radioLength = radioObj.length;
	
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
//////////////////////////////////////////////
//Formatar valor de moeda no input
//////////////////////////////////////////////
function FormataReais(fld, milSep, decSep, e, tam) {
	
	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 || whichCode == 8 ) return true;
	
	key = String.fromCharCode(whichCode);// Valor para o código da Chave
	
	if (strCheck.indexOf(key) == -1) return false; // Chave inválida
	
	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 = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 10) return false;
	
	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;
}

///////////////////////////////////////////////////////////////
//Verifica se o string tem algum dígito que não seja número
///////////////////////////////////////////////////////////////
function EhNumero (Valor) {
var Numeros="1234567890";
for (Cont=0; Cont < Valor.length; Cont++) {
 TempChar = Valor.substring(Cont,Cont+1);
 if    (Numeros.indexOf(TempChar,0) == -1)    return (false);
}
return (true);
}

///////////////////////////////////////////////////////////////////////////////
//Valida CPF
///////////////////////////////////////////////////////////////////////////////
function CPF(cpf){
	var numeros, digitos, soma, i, resultado, digitos_iguais;
	digitos_iguais = 1;
	if (cpf.length < 11)
		return false;
	
	for (i = 0; i < cpf.length - 1; i++) {
		if (cpf.charAt(i) != cpf.charAt(i + 1)) {
			digitos_iguais = 0;
			break;
		}
	}
	
	if (!digitos_iguais) {
		numeros = cpf.substring(0,9);
		digitos = cpf.substring(9);
		soma = 0;
		
		for (i = 10; i > 1; i--)
			soma += numeros.charAt(10 - i) * i;
		
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		
		if (resultado != digitos.charAt(0))
			return false;
		
		numeros = cpf.substring(0,10);
		soma = 0;
		
		for (i = 11; i > 1; i--)
			soma += numeros.charAt(11 - i) * i;
		
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		
		if (resultado != digitos.charAt(1))
			return false;
		
		return true;
	} else {
		return false;
	}
}


///////////////////////////////////////////////////////////////////////////////
//Valida CNPJ
///////////////////////////////////////////////////////////////////////////////
function CNPJ(cnpj){
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
	digitos_iguais = 1;
	
	if (cnpj.length < 14 && cnpj.length < 15)
		return false;
	
	for (i = 0; i < cnpj.length - 1; i++) {
		if (cnpj.charAt(i) != cnpj.charAt(i + 1)) {
			digitos_iguais = 0;
			break;
		}
	}

	if (!digitos_iguais) {
		tamanho = cnpj.length - 2
		numeros = cnpj.substring(0,tamanho);
		digitos = cnpj.substring(tamanho);
		soma = 0;
		pos = tamanho - 7;
		
		for (i = tamanho; i >= 1; i--) {
			soma += numeros.charAt(tamanho - i) * pos--;
			if (pos < 2)
				pos = 9;
		}
		
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

		if (resultado != digitos.charAt(0))
			return false;

		tamanho = tamanho + 1;
		numeros = cnpj.substring(0,tamanho);
		soma = 0;
		pos = tamanho - 7;

		for (i = tamanho; i >= 1; i--) {
			soma += numeros.charAt(tamanho - i) * pos--;
			if (pos < 2)
				pos = 9;
		}

		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		
		if (resultado != digitos.charAt(1))
			return false;
		
		return true;
	} else {
		return false;
	}
}

///////////////////////////////////////////////////////////////////////////////
//Valida Email
///////////////////////////////////////////////////////////////////////////////
function validaEmail(email) {

	prim = email.indexOf("@")
	if(prim < 2) {
		return false;
	}
	if(email.indexOf("@",prim + 1) != -1) {
		return false;
	}
	if(email.indexOf(".") < 1) {			
		return false;
	}
	if(email.indexOf(" ") != -1) {
		return false;
	}
	if(email.indexOf("zipmeil.com") > 0) {
		return false;
	}
	if(email.indexOf("hotmeil.com") > 0) {
		return false;
	}
	if(email.indexOf(".@") > 0) {
		return false;
	}
	if(email.indexOf("@.") > 0) {
		return false;
	}
	if(email.indexOf(".com.br.") > 0) {
		return false;
	}
	if(email.indexOf("/") > 0) {
		return false;
	}
	if(email.indexOf("[") > 0) {
		return false;
	}
	if(email.indexOf("]") > 0) {
		return false;
	}
	if(email.indexOf("(") > 0) {
		return false;
	}
	if(email.indexOf(")") > 0) {
		return false;
	}
	if(email.indexOf("..") > 0) {
		return false;
	}	
	
	return true;
}
///////////////////////////////////////////////////////////////////////////////
//Valida DV do cheque
///////////////////////////////////////////////////////////////////////////////
function Mod11(strTemp) {
	retorno = false;
	//if (strTemp.length > 20) return (false);	   
	var tam = strTemp.length-2	
	var Multiplic = "54329876543298765432" ;
	Multiplic = rightString(Multiplic,tam+1);
	var soma = 0;
	for (Count=0;Count < tam+1;Count++)
		{
		cchar = strTemp.substring(tam-Count,tam-Count+1);
		cpeso = Multiplic.substring(tam-Count,tam-Count+1);
		soma = soma + parseInt(cchar) * parseInt(cpeso);
		}
	resto = soma % 11;
	if (resto == 0) cret = 0;
	
	cret = 11-resto ;
	
	if (cret > 9) cret = cret -10;

	if (cret==strTemp.substring(strTemp.length-1,strTemp.length)){
		retorno = true;
	}else{
		retorno = false;
	}

	return retorno;	   
}
function rightString (InString, num)  {
	OutString=InString.substring (InString.length-num, InString.length);
	return (OutString);
}
function preencheZeros(valor,tamanho) {
	while ( valor.length < tamanho)
		valor = "0" + valor;
	
	return valor;
}

function HighlightRow(Row,Chk) {
	if (Row.className) {
		if (Row.className.indexOf('Unr') == -1) {
			if (Chk && Row.className.indexOf('_s') == -1 ) {
				Row.className += '_s';
			} else if (Chk) {
				Row.className = Row.className;
			} else {
				Row.className = (Row.className.indexOf('_s') > 0 ? Row.className.substr(0, (Row.className.length - 2)) : Row.className);
			}
		}
	}
}

////////////////////////////////////////
//Validar Campo do Telefone sem o DDD
////////////////////////////////////////
function validaTelefoneSemDDD(campo, nomeCampo) {
	if ( campo.value == null || campo.value == "" ) {
		erro('O campo "' + nomeCampo + '" é obrigatório.');
		campo.focus();
		return false;
	} else if ( campo.value.length < 7 ) {
		erro('O campo "' + nomeCampo + '" está incorreto.');
		campo.focus();
		return false;
	} else if ( !EhNumero(campo.value) ) {
		erro('O campo "' + nomeCampo + '" só pode conter números.');
		campo.focus();
		return false;
	}
	return true;
}

function validaDDD(campo, nomeCampo) {
	if ( campo.value == null || campo.value == "" ) {
		erro('O "DDD" do campo "' + nomeCampo + '" é obrigatório.');
		campo.focus();
		return false;
	} else if ( campo.value.length < 2 ) {
		erro('O "DDD" do campo "' + nomeCampo + '" está incorreto.');
		campo.focus();
		return false;
	} else if ( !EhNumero(campo.value) ) {
		erro('O "DDD" do campo "' + nomeCampo + '" só pode conter números.');
		campo.focus();
		return false;
	}
	return true;
}

function validaTelefone(campo, nomeCampo) {
	try {
		var telefone = campo.value;
		telefone = telefone.substr(1, 2) + telefone.substr(5, 4)
				+ telefone.substr(10, 4);
	} catch(e) {
		erro('O campo "' + nomeCampo + '" está incorreto.');
		campo.focus();
		return false;
	}
	
	if ( telefone == null || telefone == "" ) {
		erro('O campo "' + nomeCampo + '" é obrigatório.');
		campo.focus();
		return false;
	} else if ( telefone.length < 9 ) {
		erro('O campo "' + nomeCampo + '" está incorreto.');
		campo.focus();
		return false;
	} else if ( !EhNumero(telefone) ) {
		erro('O campo "' + nomeCampo + '" só pode conter números.');
		campo.focus();
		return false;
	}	
	return true;
}
////////////////////////////////////////
//Validar Data
////////////////////////////////////////
function ValidaData(digData) {
	var bissexto = 0;
	var data = digData;
	var tam = data.length;
	if (tam == 10) {
		var dia = data.substr(0, 2)
		var mes = data.substr(3, 2)
		var ano = data.substr(6, 4)
		if ((ano > 1900) && (ano < 2100)) {
			switch (mes) {
			case '01':
			case '03':
			case '05':
			case '07':
			case '08':
			case '10':
			case '12':
				if (dia <= 31) {
					return true;
				}
				break

			case '04':
			case '06':
			case '09':
			case '11':
				if (dia <= 30) {
					return true;
				}
				break
			case '02':
				/* Validando ano Bissexto / fevereiro / dia */
				if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) {
					bissexto = 1;
				}
				if ((bissexto == 1) && (dia <= 29)) {
					return true;
				}
				if ((bissexto != 1) && (dia <= 28)) {
					return true;
				}
				break
			}
		}
	}
	return false;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
}
String.prototype.replaceAll = function(de, para) {
	var str = this;
	var pos = str.indexOf(de);
	while (pos > -1) {
		str = str.replace(de, para);
		pos = str.indexOf(de)
	}
	return (str)
};

function validaFormDuvida(form) {
	if ( form.nome.value == null || form.nome.value == "" ) {
		erro('O campo "Nome" é obrigatório.');
		form.nome.focus();
		return false;
	} else if ( form.nome.value.indexOf(" ") == -1 ) {
		erro('Favor digitar pelo menos um sobrenome.');
		form.nome.focus();
		return false;
	}
	if ( form.email.value == null || form.email.value == "" ) {
		erro('O campo "E-mail" é obrigatório.');
		form.email.focus();
		return false;
	} else if ( !validaEmail(form.email.value) ) {
		erro('"E-mail" inválido.');
		form.email.focus();
		return false;
	}
	if ( form.assunto.value == null || form.assunto.value == "" ) {
		erro('O campo "Assunto" é obrigatório.');
		form.assunto.focus();
		return false;
	}
	if ( form.mensagem.value == null || form.mensagem.value == "" ) {
		erro('O campo "Mensagem" é obrigatório.');
		form.mensagem.focus();
		return false;
	}
	// Validações OK
	fechaMensagemAlerta();
	enviaDuvida(form);
	return true;
}

function enviaDuvida(form) {
	var pagina = "/enviaDuvida";
	pagina += "?nome=" +		form.nome.value;
	pagina += "&email=" +		form.email.value;
	pagina += "&mensagem=" +	form.mensagem.value;
	pagina += "&assunto=" +		form.assunto.value;
	
	bloqueiaTela();
	carregando();
	redireciona(pagina);
}
