function retorna_dia(paTexto)
{
	var dia;
	var mes;
	var ano;
	var vaTexto=new String(paTexto);
	var vetData;
	
	vetData=vaTexto.split('/');
	dia=vetData[0];
	mes=vetData[1];
	ano=vetData[2];
	
	return dia;
}

function retorna_mes(paTexto)
{
	var dia;
	var mes;
	var ano;
	var vaTexto=new String(paTexto);
	var vetData;
	
	vetData=vaTexto.split('/');
	dia=vetData[0];
	mes=vetData[1];
	ano=vetData[2];
	if ( mes.charAt(0) == '0' ) { mes = mes.charAt(1) }
	return parseInt(mes)-1;
}

function retorna_ano(paTexto)
{
	var dia;
	var mes;
	var ano;
	var vaTexto=new String(paTexto);
	var vetData;
	
	vetData=vaTexto.split('/');
	dia=vetData[0];
	mes=vetData[1];
	ano=vetData[2];
	
	return ano;
}

function desformata( cnpjOucpfFormatado ) {
	var numCPF;
	numCPF = new String (cnpjOucpfFormatado);
	strSaida=new String();
	for( i=0; i<numCPF.length; i++) {
		if( numCPF.charAt(i)<'0' || numCPF.charAt(i)>'9' ) {
		continue;
		} else {
			strSaida = strSaida + numCPF.charAt(i);
		}
	}
	return strSaida;
}

function valida_numero_inteiro(patexto)
{
	var vatexto;
	var tamanho;
	var pos;

	vatexto=new String(patexto);
	pos=0;

	if(vatexto.length==0)
		return false;
	else
		tamanho=vatexto.length;
	
	while(pos<tamanho)
	{
		if((vatexto.substr(pos,1)<'0' || vatexto.substr(pos,1)>'9'))		
		{
			return false;
		}

		pos=pos+1;
	}

	return true;
}

function valida_numero_Z(patexto)
{
	var vatexto;
	var tamanho;
	var pos;

	if (!isNaN(patexto)){
		return false;

	}
	return true;

	vatexto=new String(patexto);
	pos=0;

	if(vatexto.length==0)
		return false;
	else
		tamanho=vatexto.length;
	
	if((vatexto.substr(0,1)=='+' || vatexto.substr(0,1)=='-'))		
	{
		pos = 1;
	}

	while(pos<tamanho)
	{

		if((vatexto.substr(pos,1)<'0' || vatexto.substr(pos,1)>'9'))		
		{
			return false;
		}

		pos=pos+1;
	}

	return true;
}
function valida_numero_decimal(patexto)
{
	var vatexto;
	var tamanho;
	var pos;
	var contDec;
	
	vatexto=new String(patexto);
	pos=0;
	contDec=0;
	
	if(vatexto.length==0)
		return false;
	else
		tamanho=vatexto.length;
	
	while(pos<tamanho)
	{
		if(vatexto.substr(pos,1)==',')
		{
			contDec=contDec+1;
			if(contDec>1)
			{
				return false;
			}
		}
		else
		{
			if((vatexto.substr(pos,1)<'0' || vatexto.substr(pos,1)>'9'))
			{
				return false;
			}
		}
		
		pos=pos+1;
	}

	return true;
}

function valida_telefone(patexto)
{
	var vatexto;
	var tamanho;
	var pos;

	vatexto=new String(patexto);
	pos=0;

	if(vatexto.length==0)
		return false;
	else
		tamanho=vatexto.length;
	
	while(pos<tamanho)
	{
		if((vatexto.substr(pos,1)<'0' || vatexto.substr(pos,1)>'9') && (vatexto.substr(pos,1)!='(') && (vatexto.substr(pos,1)!=')') && (vatexto.substr(pos,1)!='-') && (vatexto.substr(pos,1)!=' '))		
		{
			return false;
		}

		pos=pos+1;
	}

	return true;
}

function valida_cpf(pcpf)
{
	var val;
	var soma;
	var resto;
	var dig;
	
	if(pcpf.length != 11)
	{
		return false;
	}


	for (i=0;i<pcpf.length;i++)
	{
		val = pcpf.charAt(i);
		if((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4")&&(val!="5")&&(val!="6")&&(val!="7")&&(val!="8"))
		{
			return false;
		}
	}


	soma = 0;
	for (i=0;i<=8;i++)
	{
		val = eval(pcpf.charAt(i));
		soma = soma + (val*(i+1));
	}

	resto = soma % 11;
	if(resto>9)
	{
		dig = resto -10;
	}
	else
	{
		dig = resto;
	}

    if(dig != eval(pcpf.charAt(9)))
	{
		return false;
	}
	else   // valida o segundo digito
	{
		soma = 0;
		for (i=0;i<=7;i++)
		{
			val = eval(pcpf.charAt(i+1));
			soma = soma + (val*(i+1));
		}

		soma = soma + (dig * 9);
		resto = soma % 11;
		if(resto>9)
		{
			dig = resto -10;
		}
		else
		{
			dig = resto;
		}
		
		if(dig != eval(pcpf.charAt(10)))
		{
			return false;
		}
	}

	return true;
}


function valida_cgc(pcgc) {
	// verifica o tamanho
 	if (pcgc.length != 14) {
		sim=false
  		//alert ("Tamanho Invalido de CGC")
 	 	return false;
 	}
 	else {
	sim=true
	}
	if (sim )  // verifica se e numero
	{
  		for (i=0;((i<=(pcgc.length-1))&& sim); i++) {
   			val = pcgc.charAt(i)
       		// alert (val)
	   		if ((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4")&&(val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {
				sim=false
				return false;
			}
   		}
   		if (sim)  // se for numero continua
   		{
		    m2 = 2
		    soma1 = 0
		    soma2 = 0
		    for (i=11;i>=0;i--)	{
    			val = eval(pcgc.charAt(i))
       			// alert ("Valor do Val: "+val)
     			m1 = m2
  				if (m2<9) { 
					m2 = m2+1
				}
  				else {
					m2 = 2
				}
  				soma1 = soma1 + (val * m1)
  				soma2 = soma2 + (val * m2)
    		} // fim do for de soma
			soma1 = soma1 % 11
  			if (soma1 < 2) {
				d1 = 0
			}
  			else { 
				d1 = 11- soma1
			}
			soma2 = (soma2 + (2 * d1)) % 11
  			if (soma2 < 2) { 
				d2 = 0
			}
   			else { 
				d2 = 11- soma2
			}
        	// alert (d1)
       		// alert (d2)
    		if ((d1==pcgc.charAt(12)) && (d2==pcgc.charAt(13))) { 
				//alert("Valor Valido de CCG")
				return true;
			}
   			else {
				//alert("Valor invalido de CCG")
				return false;
			}
		}
 	}
}


//------------------------------------------
// checagem de campo valor com casa decimal
//------------------------------------------
function valida_valor(str)
{
   var t = str.length // tamanho da string
   var k = 0 ;
   for (k =0;k<t;k++)
   {
    var oneChar = str.substr(k,1)
    if ((oneChar < "0" || oneChar > "9") && (oneChar != ".") && (oneChar != ",")  && (oneChar != " "))
      { 
        return false
      }
   }
   
   return true;
}  

function FormatDouble(psDouble, piDecimalPlaces) {
  var i = 0;
  var iDecimalPlaces = 2;

  if (typeof(piDecimalPlaces) != "undefined") {
      iDecimalPlaces = piDecimalPlaces;
  }

  // verifica se e´ um numero valido
  var dDouble = ToDouble(psDouble);
  if (isNaN(dDouble)) 
      return "";
  
  var iSinal;
  if (dDouble >= 0)
      iSinal = 1;
  else
      iSinal = -1;
  dDouble = iSinal * dDouble; // salva o sinal e forca o numero para positivo

  // formata a parte decimal
  var iInt = Math.floor(dDouble);
  var dDecimal = dDouble - iInt;
  var sDecimal = "";

  for (i = 0; i < iDecimalPlaces; i++) {
      dDecimal = dDecimal * 10;
  }
  var iDecimal = Math.round(dDecimal);
  if (iDecimal == 100) {
      iDecimal = 0;
      iInt = iInt + 1;
  }
  sDecimal = iDecimal.toString();
  while (sDecimal.length < iDecimalPlaces) {
      sDecimal = "0" + sDecimal;
  }

  // formata a parte inteira
  var sIntClean = iInt.toString();
  var sIntFormatado = "";
  var iLenInt = sIntClean.length;
  for (i = 0; i < iLenInt; i++) {
      sIntFormatado = sIntClean.substr(iLenInt - i - 1, 1) + sIntFormatado;
      if (i < (iLenInt - 1)) {
          if (((i + 1) % 3) == 0) {
              sIntFormatado = "." + sIntFormatado;
          }
      }
  }

  sIntFormatado = sIntFormatado + "," + sDecimal;
  if (iSinal < 0) {
      sIntFormatado = "-" + sIntFormatado;
  }
  return sIntFormatado;
}
function ToDouble(psDouble) {
  var iLen = psDouble.length;
  var iLastSep = -1;
  var i;
  var s = "";
  var sDouble = "";
  var bSinal = false;
  var iSinal = 1;

  for (i = 0; i < iLen; i++) {
      s = psDouble.substr(i, 1);
      switch (s) {
      case ".":
      case ",":
          iLastSep = sDouble.length;
          break;
      case "-":
          if (bSinal) {
              return NaN;
          } else {
              bSinal = true;
              iSinal = -1;
          }
          break;
      case "+":
          if (bSinal) {
              return NaN;
          } else {
              bSinal = true;
              iSinal = 1;
          }
          break;
      case "0":
      case "1":
      case "2":
      case "3":
      case "4":
      case "5":
      case "6":
      case "7":
      case "8":
      case "9":
          sDouble = sDouble + s;
          break;
      default:
          return NaN;
          break;
      }
  }

  if (iLastSep >= 0) {
      sDouble = sDouble.substr(0, iLastSep) + "." + sDouble.substr(iLastSep);
  }
  return iSinal * (parseFloat(sDouble));
}


function ValidaMinutos( Campo ) {
	if (  valida_numero_inteiro(eval('document.frmPrincipal.' + Campo + '.value'))  ) {
		if ( eval('document.frmPrincipal.' + Campo + '.value') != 0 ) {
			if ( eval('document.frmPrincipal.' + Campo + '.value') > 59 ) {
				alert( 'Os minutos não podem ser maior que 59 ' );
				eval( 'document.frmPrincipal.' + Campo + '.focus();' );
				return false;
			} else
				return true;
		} else
			return true;
	} else {
		alert( 'Os minutos devem ser números inteiros' );
		eval( 'document.frmPrincipal.' + Campo + '.focus();' );	
	}
	return false;
}

//Verifica se "Cod" já está incluso em uma ListBox = "Item"
function InList( Cod , Item ) {
	for ( i = 0 ; i < parseInt( eval( 'document.frmPrincipal.' + Item + '.options.length' ) ) ; i++ ) {
		
		if ( Cod == eval( 'document.frmPrincipal.' + Item + '.options[i].value' )  ) {
			return true;
		}
	}
	return false;
}
