// Programação: Fabrício Brandão
// Contato: fabricio@eesign.net
//
// Última modificação: nova função > limparstring
// Data: 14/10/2005 15:58

//onmouseover="TrocaCor(this,'#EEEEEE')"
//function TrocaCor(this,cor){
//	this.bgcolor = cor
//}

function MO(e){
	if (!e)
		var e=window.event;
	var S=e.srcElement;
	while (S.tagName!="TD"){
		S=S.parentElement;
	}
	S.className="T";
}
function MU(e){
	if (!e)
		var e=window.event;
	var S=e.srcElement;
	while (S.tagName!="TD"){
		S=S.parentElement;
	}
	S.className="P";
}

//Não permite digitar espaços no inicio do campo
function tiraEspacos(campo,teclapres)	{
	var	tecla =	teclapres.keyCode;
	
	if (tecla == 32){
		if (document.all[campo].value.length <= 1)
			document.all[campo].value = '';
	}
}

function setRestantes(text, restante, LimiteMax){ 
	if (text.value.length > LimiteMax)
		text.value = text.value.substring(0, LimiteMax);
	else {
		restante.value = LimiteMax - text.value.length;
	}
}

function soNumeros(str)	{
	var	resposta = true;
	for	(var i = 0;	i <	str.length;	i++)
		{ if (str.substring(i,i+1) < "0" ||	str.substring(i,i+1) > "9")
			{ resposta=false;
			  break;
			}
		}
	return resposta;
}

function vVazio(str){
	if (str == "")
		return true;
	else
		return false;
}

function vNumeroValido(str){
	if ((str == "") || (isNaN(str)))
		return false;
	else
		return true;
}

function vData(val){
	var	mes, dia, ano;
	var	retval = false;
	if (val!="") {
		if (val.length != 10 ||	val.substring(2,3)!="/"	|| val.substring(5,6)!="/")	{ }
		else
		{ mes =	val.substring(3,5);
		if (soNumeros(mes) && parseInt(mes,10) > 0 && parseInt(mes,10) <= 12)
			{
			mes	= parseInt(mes,10);
			ano	= val.substring(6,10);
			if (soNumeros(ano) && parseInt(ano,10) >= 1500)
				{
				if ((parseInt(ano,10) %	4) == 0) {
				bissexto = 1;
					}
				else {
				bissexto = 0;
					}
				dia	= val.substring(0,2);
				if (soNumeros(dia))
					{
				dia	= parseInt(dia,10);
				if (((mes==1 ||	mes==3 || mes==5 ||	mes==7 || mes==8 ||	mes==10	|| mes==12)	&& (dia	>= 1 &&	dia	<= 31))	||
					((mes==4 ||	mes==6 || mes==9 ||	mes==11) &&	(dia >=	1 && dia <=	30)) ||
					(mes==2	&& dia >= 1	&& dia <= (28 +	bissexto)))	{
					retval=true;
					}
					}
				}
			}
		}
	}
	else {
		retval = true;
	}
	return retval;
}

// Formato:	DD/MM/YYYY
function fData(campo,teclapres)	{
	var	tecla =	teclapres.keyCode;

//	pData(campo,teclapres);
	vr = document.form[campo].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( " ", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ":", "" );

	tam	= vr.length	+ 1;
	if ( tecla != 9	&& tecla !=	8 && tecla != 46 &&	tecla != 37	&& tecla !=	39 && tecla	!= 35 && tecla != 36 &&	tecla != 16){
		if ( tam > 2 &&	tam	< 5	)
			document.form[campo].value = vr.substr(	0, tam - 2	) +	'/'	+ vr.substr( tam - 2, tam );
		if ( tam >=	5 && tam <=	10 )
			document.form[campo].value = vr.substr(	0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr(	4, 4 );
	}
}

function fMoeda(campo,tammax,teclapres) {
var tecla = teclapres.keyCode;
vr = event.srcElement.value;
  vr = vr.replace("/","" );
  vr = vr.replace( "/","" ); 
  vr = vr.replace( ",", "" ); 
  vr = vr.replace( ".", "" ); 
  vr = vr.replace( ".", "" ); 
  vr = vr.replace( ".", "" ); 
  vr = vr.replace( ".", "" ); 
  vr = vr.replace( "-", "" ); 
  vr = vr.replace( "-", "" ); 
  vr = vr.replace( "-", "" ); 
  vr = vr.replace( "-", "" ); 
  vr = vr.replace( "-", "" ); 
  tam = vr.length;
 	if (tam < tammax && tecla != 8){
		tam = vr.length + 1 ; 
	} 
 	if (tecla == 8 ){
		tam = tam - 1 ; 
	} 
 	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){ 
 	 	if ( tam <= 2 ){
			event.srcElement.value = vr ; 
		} 
  		if (tam > 2) { 
			event.srcElement.value = vr.substr( 0, tam - 2 ) + '.' + vr.substr( tam - 2, tam ); 
		} 
	}
}

// Formato:	MM/YYYY
function fData2(campo,teclapres) {
	var	tecla =	teclapres.keyCode;
	if (tecla != 9 && tecla	!= 16 && tecla != 35 &&	tecla != 36	&& tecla !=	37 && tecla	!= 39) {
		pData(campo,teclapres);
		vr = document.all(campo).value;

		for	(var i = vr.length;	i >	0; i--)	{
			if (vr.substring(i-1,i)	< "0" || vr.substring(i-1,i) > "9")
			  vr = vr.replace( vr.substring(i-1,i),	"" );
		}

		tam	= vr.length	+ 1;
		if ( tecla != 9	&& tecla !=	8 && tecla != 46 &&	tecla != 37	&& tecla !=	39 && tecla	!= 35 && tecla != 36) {
			if ( tam > 2 &&	tam	< 5	)
				document.all(campo).value =	vr.substr( 0, tam -	2  ) + '/' + vr.substr(	tam	- 2, tam );
		}
	}
}

// Tira	todas as letras	do campo
function tiraLetra(campo,teclapres)	{
	vr = document.form[campo].value;
	for	(var i = vr.length;	i >	0; i--){
	  if ((vr.substring(i-1,i) >= "a" && vr.substring(i-1,i) <=	"z") ||	(vr.substring(i-1,i) >=	"A"	&& vr.substring(i-1,i) <= "Z"))
		  vr = vr.replace( vr.substring(i-1,i),	"" );
	}
	document.form[campo].value = vr;
}

// Permite só números
function pData(campo, teclapres) {
	vr = document.all(campo).value;
	for	(var i = vr.length;	i >	0; i--){
	  if ((vr.substring(i-1,i) < "0" ||	vr.substring(i-1,i)	> "9") && (vr.substring(i-1,i) != "/"))
		  vr = vr.replace( vr.substring(i-1,i),	"" );
	}
	document.all(campo).value =	vr;
}

function proximoCampo(campoAtual, key, prox) {
	// Caso	seja TAB ou	SHIFT desconsiderar
	if ((campoAtual.value.length ==	campoAtual.maxLength) && (key.keyCode != 9)	&& (key.keyCode	!= 16))	{
		prox.focus();
	}
}

function proximoCampo2(key)	{
	var	campoAtual = document.activeElement;
	// Caso	seja TAB ou	SHIFT desconsiderar
	// Setas de	direção, HOME, END,	INSERT e DELETE
	if (campoAtual.isTextEdit) {
		if ((campoAtual.value.length ==	campoAtual.maxLength) && (key.keyCode != 9)	&& (key.keyCode	!= 16) &&
			  ((key.keyCode	< 35) || (key.keyCode >	40)) &&	(key.keyCode !=	45)	&& (key.keyCode	!= 46))	{
			i =	0;
			var	fim	= false;
			while ((document.form.elements[i] != null) && (!fim)) {
				if (document.form.elements[i] == campoAtual) {
					if (campoAtual.tabIndex	!= 0) {
						while ((document.form.elements[i] != null) && (!fim)) {
							if (document.form.elements[i].tabIndex == (campoAtual.tabIndex+1)) {
								if (document.form.elements[i].isTextEdit){
									if ((document.form.elements[i].style.visibility	== "visible") || (document.form.elements[i].style.display == "")) {
										document.form.elements[i].select();
									}
								}
								else
									if ((document.form.elements[i].style.visibility	== "visible") || (document.form.elements[i].style.display == "")) {
										document.form.elements[i].focus();
								}
							}
							i++;
						}
					}
					else {
						i++;
						while (!fim) {						
							if (document.form.elements[i] != null) {
								if ((document.form.elements[i].isTextEdit) && (document.form.elements[i].type != 'hidden')) {
									if ((document.form.elements[i].style.display == "") && (document.form.elements[i].parentElement.style.display == "") &&	(document.form.elements[i].parentElement.parentElement.style.display == "")) {
										if (!document.form.elements[i].disabled) {
											document.form.elements[i].focus();
											fim = true;
										}
									}									
								}								
							}
							else {
								fim = true;
							}
							i++;
						}
					}
				}
				i++;
			}
		}
	}
}

// O mesmo que split em	VB
// Retorna um array	de string dividida pelo	caracter passado
function parseString(str, caracter)	{
	var	temp = new Array(),	i =	0;
	while (str != "") {
		if (str.indexOf(caracter) >	-1)	{
			temp[i]	= str.substring(0,str.indexOf(caracter));
			str	= str.substring(str.indexOf(caracter)+caracter.length, str.length);
		}
		else {
			temp[i]	= str;
			str	= "";
		}
		i++;
	}
	return temp;
}

function v(x) {
	if (((document.activeElement.className == "Informacao")	|| (document.activeElement.className ==	"RelatorioObs")) &&	(x.keyCode == 8)) {
		x.keyCode =	0;
	}
}

function mudarValor(obj, str) {
	document.all(obj).value	= str;
}

function mudarValorSpan(obj, str) {
	document.all(obj).innerHTML	= str;
}

var	focoOK = false;
function darFoco(obj) {
	if ((!focoOK) && (document.all(obj)	!= null)) {
		document.all(obj).focus();
		focoOK = true;
	}
}

// Permite só números positivos
function vNumero() {
	/* 8 = backspace
	 * 45 =	insert
	 * 46 =	delete
	 * 37 =	seta esquerda
	 * 39 =	seta direita
	 * 16 =	shift
	 * 109 e 189 = menos
	 * 35 e	36 = home e	end
	 */
	if (!(((event.keyCode >= 96) &&	(event.keyCode <= 105))	||
		  ((event.keyCode >= 48) &&	(event.keyCode <= 57 ))	||
		  (event.keyCode ==	8) || (event.keyCode ==	46)	||
		  (event.keyCode ==	37)	|| (event.keyCode == 39) ||
		  (event.keyCode ==	9) || (event.keyCode ==	45)	||
		  (event.keyCode ==	35)	|| (event.keyCode == 36) ||
		  (event.keyCode ==	16)	|| (event.keyCode == 13))) {
		//event.keyCode	= 0;
		event.returnValue =	'';
	}
}

// Permite só números positivos
function vReal() {
	/* 8 = backspace
	 * 45 =	insert
	 * 46 =	delete
	 * 37 =	seta esquerda
	 * 39 =	seta direita
	 * 16 =	shift
	 * 190 e 194 = pontos
	 * 109 e 189 = menos
	 * 35 e	36 = home e	end
	 */
//	 alert(event.keyCode)
	if (!(((event.keyCode >= 96) &&	(event.keyCode <= 105))	||
		  ((event.keyCode >= 48) &&	(event.keyCode <= 57 ))	||
		  (event.keyCode ==	8) || (event.keyCode ==	46)	||
		  (event.keyCode ==	37)	|| (event.keyCode == 39) ||
		  (event.keyCode ==	9) || (event.keyCode ==	45)	||
		  (event.keyCode ==	35)	|| (event.keyCode == 36) ||
		  (event.keyCode ==	190)	|| (event.keyCode == 194) ||		  
		  (event.keyCode ==	16)	|| (event.keyCode == 13))) {
		//event.keyCode	= 0;
		event.returnValue =	'';
	}
}

// val - string	contendo o valor e Tipo	númerico, onde:
// 1. preencher	com	espaço em branco à direita
// 2. preencher	com	zeros à	esquerda (só aceita	números)
// 3. repetir val até o	tamanho	especificado ***********
function toFormat(val, tamanho,	tipo) {
	var	comp = "";
	val	= String(val);
	if (tipo ==	2) val = toNumeros(val);
	for	(var i = 1;	i <= (tamanho -	val.length); i++) {
		switch (tipo) {
			case 1:	comp +=	"&nbsp;";
				break;
			case 2:	comp +=	"0";
				break;
			case 3:
				if (val	== " ")
					comp +=	"&nbsp;";
				else
					comp +=	val;
				break;
		}
	}
	if (tipo ==	1)
		comp = val + comp;
	else
		if (tipo ==	4) { //	Formata	data
			comp = val;
		}
		else
			comp +=	val;
	return comp;
}

function toNumeros(val)	{
	var	temp = "";
	val	= String(val);
	for	(var i = 0;	i <	val.length;	i++) {
	 if	(val.substring(i,i+1) >= "0" &&	val.substring(i,i+1) <=	"9")
		temp +=	val.substring(i,i+1);
	}
	//if (temp != "")	temp = parseInt(temp);
	return temp;
}
function removeNumeros(val)	{
	var	temp = "";
	for	(var i = 0;	i <	val.length;	i++) {
	 if	(!(val.substring(i,i+1)	>= "0" && val.substring(i,i+1) <= "9"))
		temp +=	val.substring(i,i+1);
	}
	//if (temp != "")	temp = parseInt(temp);
	return temp;
}
function criarArray(n) {
	var	temp = new Array();
	for	(var i = 0;	i <	n; i++)	{ temp[i] =	null; }
	return temp;
}

function vCPF(str) {
	if (isNaN(str))	{
		return false;
	}
	else{
		if (str.length == 11){
			if (checa_cpf(str)){
				return true;
			}else{
				return false;
			}
		}
		else {
			if (str.length == 0){
				return false;
			}else{
				return false;
			}
		}
	}
}

function vCNPJ(str)	{
	if (isNaN(str))	{
		return false;
	}
	else{
		if (str.length == 14){
			if (testaCgc(str)){
				return true;
			}else{
				return false;
			}
		}
		else {
			if ((str.length	> 0) &&	(str.length	< 14)){
				return false;
			}
		}
	  return true;
	}
}

function testaCgc(campoCgc)
{
	// teste se	o CGC tem 11 numeros repetidos (iguais)
	if (temRepeticao(campoCgc,13)) {
		return false;
	}


	  var num1 = new initArray(14);
	  if(campoCgc == null)
		{
			return false;
		}
		if(campoCgc.length != 14)
		{
			return false;
		}
			for	(var i = 0 ; i < 14	; i++)
				{
			   num1[i] = campoCgc.substring(i, i+1);
				}

		digito13 = calculaDigito(13, num1);
		digito14 = calculaDigito(14, num1);
		if (num1[12]==(digito13) &&	num1[13]==(digito14)){
			return true;  }
	  else {
			return false;  }

}

function initArray() {
	this.length	= initArray.arguments.length;
	for (var i = 0 ; i < 14 ; i++) {
		this[i]	= "	";
	}
}

function calculaDigito(	cgc_limite,	 num) {
	 cgc_soma =	0;
	 cgc_ind = 1;
	 cgc_peso =	cgc_limite - 7 - cgc_ind;
	 while(cgc_ind < cgc_limite)
	 {
		 cgc_soma += num[cgc_ind - 1] *	cgc_peso;
		 cgc_ind++;
		 if(cgc_peso ==	2)
			  cgc_peso = 9;
		 else
			  cgc_peso--;
	 }
	 cgc_resto = mod(cgc_soma, 11);
	 if(cgc_resto == 0 || cgc_resto	== 1)
		   {cgc_digito = 0;}
	 else
		   {cgc_digito = 11	- cgc_resto;}
	 return	cgc_digito;
}

function mod(ini, fim)
{
	 t = ini % fim;
	 return	t;
}
function temRepeticao(str, num)
{
 var cont =	0;
 var num = parseInt(num);
  for (i = 0; i	<= str.length -	1; i++)	{
	cont = 0;
	for	(j = i + 1;	j <= str.length	- 1; j++) {
	  if (str.substr(i,1) == str.substr(j,1))
		cont++;
	  else
		break;
	}
	if (cont >=	num)
	  break;
  }
  return (cont >= num);
}

function checa_cpf (numcpf) {
	// teste se	o cpf tem 11 numeros repetidos (iguais)
	if (temRepeticao(numcpf,10)) {
	  return false;
	}
	x =	0;
	soma = 0;
	dig1 = 0;
	dig2 = 0;
	texto =	"";
	numcpf1="";
	len	= numcpf.length; x = len -1;	
	for	(var i=0; i	<= len - 3;	i++) {
		y =	numcpf.substring(i,i+1);
		soma = soma	+ (	y *	x);
		x =	x -	1;
		texto =	texto +	y;
	}
	dig1 = 11 -	(soma %	11);
	if (dig1 ==	10)	dig1=0 ;
	if (dig1 ==	11)	dig1=0 ;
	numcpf1	= numcpf.substring(0,len - 2) +	dig1 ;
	x =	11;	soma=0;
	for	(var i=0; i	<= len - 2;	i++) {
		soma = soma	+ (numcpf1.substring(i,i+1)	* x);
		x =	x -	1;
	}
	dig2= 11 - (soma % 11);
	if (dig2 ==	10)	dig2=0;
	if (dig2 ==	11)	dig2=0;
	if ((dig1 +	"" + dig2) == numcpf.substring(len,len-2)) {
		return true;
	}
	falso =	"F";
	return false;
}

function vEmail(val) {
	if ((val.indexOf('www')	== -1) && (val.indexOf(' ')	== -1) && (val.indexOf('@')	> -1) && (val.indexOf('.') > -1))
		return true;
	else
		return false;
}
// Faz limpeza de strings. Utilizada pela função de validação de campos obrigatorios
function limparstring(s){
	while (s.search(" ")!=-1){
		s=s.replace(" ","");
	}
	return(s);
}

