function isFieldBlank(theField)
{
  inStr = theField.value;
  inLen = inStr.length;
      
  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1)

    if (ch != " ")
      return (false);   
  }

  theField.Value = "";
  return (true);
}

function isFieldBlankText(theField)
{
  inStr = theField.value;
  inLen = inStr.length;
      
  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1)
    var chcode = inStr.charCodeAt(i);
    if ((chcode == 13) || (chcode==10))
      ch = " ";
    
    if (ch != " ")
      return (false);   
  }

  theField.Value = "";
  return (true);
}

function isNumber(theNumber)
{
  inStr = theNumber.value;
  inLen = inStr.length;

  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1);
  
    if ((ch < "0") || (ch > "9"))
      return (false);
  }
  
  return (true);
}

function isFloat(theFloat)
{
  inStr = theFloat.value;
  inAux = "";
  inLen = inStr.length;
  var cont = 0;

  if (inStr == ".")
    return (false);
    
  if (inStr == ",")
    return (false);
    
  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1)

    if ((ch < "0") || (ch > "9"))
      if ((ch != ".") && (ch != ","))
        return (false);

    if ((ch == ".") || (ch == ","))
      cont++;

    if (cont >= 2)
      return (false);

    if (ch == ",")
      inAux = inAux + "."
    else 
      inAux = inAux + ch;
  }
  
  theFloat.value = inAux;
  return (true);
}

function isFloatNeg(theFloat)
{
  inStr = theFloat.value;
  inAux = "";
  inLen = inStr.length;
  var cont = 0;

  if (inStr == "-")
    return (false);
    
  if (inStr == ".")
    return (false);
    
  if (inStr == ",")
    return (false);
    
  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1)

    if ((ch < "0") || (ch > "9"))
      if ((ch != ".") && (ch != ","))
        if (ch != "-")
          return (false);
    
    if (ch == "-")
      if (i != 0)
        return (false);

    if ((ch == ".") || (ch == ","))
      cont++;

    if (cont >= 2)
      return (false);

    if (ch == ",")
      inAux = inAux + "."
    else 
      inAux = inAux + ch;
  }

  theFloat.value = inAux;
  return (true);
}

function floatFixOld(Val, Decimais)
{
  var Res = "" + Math.round(Val * Math.pow(10, Decimais));
  var Dec = Res.length - Decimais;

  if (Decimais != 0)
    if (Dec < 0)
      OutString = "0.0" + Res;
    else if (Res.substring(0, Dec) == "")
      OutString = "0." + Res.substring(Dec, Res.length);
    else
      OutString = Res.substring(0, Dec) + "." + Res.substring(Dec, Res.length);
  else
    OutString = Res;
		
  return (OutString);
}

function floatFix(Val, Decimais)
{
	var neg = Val < 0;
	if ( neg ) {
	 Val = Val * -1;
	}
  var Res = "" + Math.round(Val * Math.pow(10, Decimais));
  var Dec = Decimais - Res.length;
  var Zer = Res.length - Decimais;
  var StrZero = "0.";

  if (Decimais != 0)
  {
    if (Val < 1)
    {
   	  for (Count=0; Count<=Dec-1; Count+= 1) 
      {	
    	  StrZero += "0";
      }
      if (Res < 1 * Math.pow(10, Decimais))
      {
        OutString = StrZero + Res;
      }
      else
      {
        OutString = Res.substring(0, Zer) + "." + Res.substring(Zer, Res.length);
      }
    }
    else
    {
      OutString = Res.substring(0, Zer) + "." + Res.substring(Zer, Res.length);
    }
  }
  else
  {
      OutString = Res.substring(0, Zer);
  }
  if ( neg ) {
	  OutString = "-" + OutString;
  }
  return (OutString);
}

function isTime(theTime)
{
  var i = 0;
  var qcaracter = 2;
  inStr = theTime.value;
  var ch =  inStr.substring(0,2)

  while ( i < 4)
  {
    i++;
    if (i == 1)  
      if ((ch < "00")  || (ch > "23"))
        return (false)
      else
        ch = inStr.substring(2,3);

    if (i == 2)
      if (ch != ":")
        return (false)
      else
        ch = inStr.substring(3,5);

    if (i == 3) 
      if ((ch < "00")  || (ch > "59"))
        return (false);
  }

  return (true);
}

function isFabricante(theField)
{
  inStr = theField.value;
  inLen = inStr.length;
      
  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1)

    if (ch == "#")
      return (false);   
  }

  return (true);
}

// Função utilizada para setar todas as aplicações iguais no carrinho de compras.
function SetAplicacao(TheForm, theVal){
  var i = 1;
  alert("A Aplicação de todos os itens do carrinho será alterada automaticamente.");
  eval("var combo = document." + TheForm + ".aplicacao1;");
  while (combo){
    combo.value = theVal;
    i++;
    eval("var combo = document." + TheForm + ".aplicacao" + i + ";");
  }
}

/* Criada por Ze Luis 
   Exclui espacos em branco do fim e do inicio de uma string */
function spaceTrim(theString)
{
  var strAux = theString;
  var strLen = strAux.length;
  var saida  = false;
  var chAux  = ""; 
  
  while (! saida)  
  {
    chAux = strAux.substring(strLen-1,strLen);
    if (chAux != " ") 
      saida = true
    else
    {
      strAux = strAux.substring(0,strLen-1);
      strLen = strAux.length;
    }
  }
  
  saida = false;  
  
  while (! saida)  
  {
    chAux = strAux.substring(0,1);
    if (chAux != " ") 
      saida = true
    else
    {
      strAux = strAux.substring(1,strLen);
      strLen = strAux.length;
    }
  }
  
  return(strAux);
}


// Data 05/12/00 verifica os valores e  inibe numeros com 3 casas decimais
// para ser usada com o evento onblur

function FloatThis(theNumber, Casas)
{
  inStr = theNumber.value;
  inAux = "";
  inLen = inStr.length;
  var cont   = 0;
  var milhar = -1; //casas decimais 
  
  if (inStr == ".")
  {
    return ('');
  }
  if (inStr == ",")
  {
    return ('');
  }
  
  if (inStr == "")
  {
    return ('');
  }
  
  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1);
    
    // verifica a quantidade de casas decimais
    if ( milhar >= 0)
    {
    milhar = milhar + 1;
    }
    
    if ((ch < "0") || (ch > "9"))
      if ((ch != ".") && (ch != ","))
      {
        theNumber.focus();  
        return ('');
      }

    if ((ch == ".") || (ch == ","))
    {
      cont++;
      milhar = 0;
    }

    if (cont >= 2)
    {
      alert("Número inválido.\n\nUtilize apenas um \".\" ou \",\" como separador decimal.\n Não utilize separador de milhar. ");
      theNumber.focus();  
      return ('');
    }

    if (ch == ",")
      inAux = inAux + "."
    else 
      inAux = inAux + ch;
  }
  if ( milhar == 3)
  {
    theNumber.focus();  
    alert("Números no formato \"9,999\" ou \"9.999\" não são aceitos.\n Para digitar milhares não use separador, o formato é \"9999\".");
    return('');
  }
  return (floatFix(inAux, Casas));
}

// Data 10/04/01 verifica os valores e  inibe numeros com 3 casas decimais
// para ser usada com o evento onblur

function FloatThisNeg(theNumber, Casas)
{
  inStr = theNumber.value;
  inAux = "";
  inLen = inStr.length;
  var cont   = 0;
  var milhar = -1; //casas decimais 
  var neg    = ''
  
  neg = inStr.substring(0,1);

  if (neg == '-')
  {
    inStr = inStr.substring(1,inLen)
    inLen = inLen - 1 ;
  }
  else
  {
    neg = '';
  }

  if (inStr == ".")
  {
    return ('');
  }
  if (inStr == ",")
  {
    return ('');
  }
  
  if (inStr == "")
  {
    return ('');
  }
  
  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1);

    // verifica a quantidade de casas decimais
    if ( milhar >= 0)
    {
    milhar = milhar + 1;
    }
    
    if ((ch < "0") || (ch > "9"))
      if ((ch != ".") && (ch != ","))
      {
        theNumber.focus();  
        return ('');
      }

    if ((ch == ".") || (ch == ","))
    {
      cont++;
      milhar = 0;
    }

    if (cont >= 2)
    {
      alert("Número inválido.\n\nUtilize apenas um \".\" ou \",\" como separador decimal.\n Não utilize separador de milhar. ");
      theNumber.focus();  
      return ('');
    }

    if (ch == ",")
      inAux = inAux + "."
    else 
      inAux = inAux + ch;
  }
  if ( milhar == 3)
  {
    theNumber.focus();  
    alert("Números no formato \"9,999\" ou \"9.999\" não são aceitos.\n Para digitar milhares não use separador, o formato é \"9999\".");
    return('');
  }
  var retorno = floatFix(inAux, Casas);
  retorno = neg + retorno
  return (retorno);
}

function IntThis(theNumber)
{
  inStr = theNumber.value;
  inAux = "";
  inLen = inStr.length;
  
  if (inStr == ".")
  {
    return ('');
  }

  if (inStr == ",")
  {
    return ('');
  }
  
  if (inStr == "")
  {
    return ('');
  }
  
  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1);
    
    if ((ch < "0") || (ch > "9"))
      {
        alert("Formato para o número inválido.\nUtilize apenas números.\nNão utilize separador de milhar.");
        theNumber.focus();
        return ('');
      }
    else
      {
      inAux = inAux + ch
      }
  }

  return (inAux);
}

// ZL - Para pegar o value de um combo
function SelectedCombo(Objeto)
{
  return Objeto[Objeto.selectedIndex].value
}

// ZL - Para pegar o value de um radio
function CheckedValue(Objeto)
{
  var LenAux = Objeto.length;
  
  for ( i = 0; i < LenAux; i ++)
  {
    if (Objeto[i].checked)
      return Objeto[i].value;
  }
  return ""
}

// Procura o Ch1 e troca por Ch2 na string de entrada
function Replace(theString, Ch1, Ch2)
{
  var strAux = theString;
  var strLen = strAux.length;
  var saida  = "";
  var chAux  = ""; 
  var P = 0;

  while (P < strLen)
  {
    chAux = strAux.substring(P, P + 1);

    if (chAux == Ch1) 
      saida = saida + Ch2
    else
      saida = saida + chAux;
 
    P++;
  }

  return(saida);
}

function AnoBissexto(Ano)
{
  var DifAno = (Ano/4) - Math.ceil(Ano/4)
  if (DifAno != 0)
    return (false);

  return (true);
}

function ValidaData(CampoData)
{
  inStr = CampoData.value;

  var ok = true;
  var ch =  inStr.substring(0,2)
  var i = 0;
  AnoBiss = (AnoBissexto(inStr.substring(6,10)))
  
  while ( i < 6)
  {
    i++;

    if (i == 1)
    {
      Dia = ch;

      if (isNaN(Dia) || ((Dia < 1) || (Dia > 31)))
        ok = false
      else
        ch = inStr.substring(2,3);
    }

    if (i == 2)
      if (ch != "/")
        ok = false
      else
        ch = inStr.substring(3,5);

    if (i == 3)
    {
      Mes = ch;

      if (isNaN(Mes) || ((Mes < 1) || (Mes > 12)))
        ok = false
      else

        if (((! AnoBiss) && Mes == 2 && Dia > 28) || ((AnoBiss) && Mes == 2 && Dia > 29) || (Dia > 30 && (Mes == 4 || Mes == 6 || Mes == 9 || Mes == 11))) 
          ok = false
        else
          ch = inStr.substring(5,6);
    }

    if (i == 4)
      if (ch != "/")
        ok = false
      else
        ch = inStr.substring(6,10);

    if (i == 5)
    {
      Ano = ch;

      if (isNaN(Ano) || Ano < 1990)
        ok = false;
    }
  }
  return (ok);
}

function ComparaData(CampoDataInicio, CampoDataFim)
{
  Dia = CampoDataInicio.value.substring(0,2)
  Mes = CampoDataInicio.value.substring(3,5)
  Ano = CampoDataInicio.value.substring(6,10)
  var DataInicioConvert = Ano + Mes + Dia

  Dia = CampoDataFim.value.substring(0,2)
  Mes = CampoDataFim.value.substring(3,5)
  Ano = CampoDataFim.value.substring(6,10)
  var DataFimConvert = Ano + Mes + Dia

  if (DataFimConvert < DataInicioConvert)
  {
    return (false);
  }
  return (true);
}

function ComparaDatas(CampoDataInicio, CampoDataFim){
	Dia = CampoDataInicio.substring(0,2)
	Mes = CampoDataInicio.substring(3,5)
	Ano = CampoDataInicio.substring(6,10)
	var DataInicioConvert = Ano + Mes + Dia

	Dia = CampoDataFim.substring(0,2)
	Mes = CampoDataFim.substring(3,5)
	Ano = CampoDataFim.substring(6,10)
	var DataFimConvert = Ano + Mes + Dia

	if (DataFimConvert <= DataInicioConvert) {
		return (false);
	}
	return (true);
}

function isFone(theFone)
{
  inStr = theFone.value;
  inLen = inStr.length;

  var ContAbr = 0;
  var ContFec = 0;
  var PosAbr  = 0;
  var PosFec  = 0;

  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1);
  
    if ((ch != "0") && (ch != "1") && (ch != "2") && (ch != "3") && (ch != "4") && (ch != "5") && (ch != "6") && (ch != "7") && (ch != "8") && (ch != "9") && (ch != "(") && (ch != ")") && (ch != " "))
      return (false);

    if (ch == "(")
    {
      ContAbr = ContAbr + 1;
      PosAbr  = i;
    }
    
    if (ch == ")")
    {
      ContFec = ContFec + 1;
      PosFec  = i;
    }
  }
  
  if (ContAbr > 0 || ContFec > 0)
  {
    if (ContAbr > 1 || ContFec > 1)
      return (false);

    if (ContAbr != ContFec)
      return (false);
  }

  if (PosAbr > PosFec)
    return (false);

  if ((PosFec + 1 )== inLen)
    return (false);

  return (true);
}

function isText(theText)
{
  inStr = theText.value;
  inLen = inStr.length;

  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1);

    if (! ((ch == " ") || 
          ((ch >= "0") && (ch <= "9")) || 
          ((ch >= "A") && (ch <= "Z")) || 
          ((ch >= "a") && (ch <= "z"))))
      return (false);
  }
  
  return (true);
}

function ValidaFloat(theNumber, Casas)
{
	var cont   = 0;
	var milhar = -1; //casas decimais 

	inStr = theNumber.value;
	inAux = "";
	inLen = inStr.length;
  
	if (inStr == ".")
	{
		return ('');
	}
	if (inStr == ",")
	{
		return ('');
	}
  
	if (inStr == "")
	{
		return ('');
	}
  
	for(var i = 0; i < inLen; i++)
	{
		var ch = inStr.substring(i, i+1);
    
		// verifica a quantidade de casas decimais
		if ( milhar >= 0)
		{
			milhar = milhar + 1;
			
			if ( milhar > Casas)
			{
				alert("Atenção! O número máximo de casas decimais é " + Casas + ". O número digitado será truncado.");
				theNumber.focus();  
				return inAux;
			}
		}
    
		if ((ch < "0") || (ch > "9"))
			if ((ch != ".") && (ch != ","))
			{
				theNumber.focus();  
				return ('');
			}

		if ((ch == ".") || (ch == ","))
		{
			cont++;
			milhar = 0;
		}

		if (cont >= 2)
		{
			alert("Número inválido.\n\nUtilize apenas um \".\" ou \",\" como separador decimal.\n Não utilize separador de milhar. ");
			theNumber.focus();  
			return ('');
		}

		if (ch == ",")
			inAux = inAux + "."
		else 
			inAux = inAux + ch;
	}
	
	return inAux;
}

//	Funcao para formatar um valor como string de moeda. 
//	Entrada:
//		Val - valor a ser formatado
//		Decimal - caracter a ser utilizado como separador de casas decimais
//		Milhar  - caracter a ser utilizado como separador de milhar
function FormataMoedaReal(Val, Decimal, Milhar)
{
	if( !Decimal )
		Decimal = ",";
	if( !Milhar )
		Milhar = ".";

	//	Associar default para Decimal e Milhar.
	
	strRetorno = "";

	//	Fixar numero de casas decimais (2 casas).
	Val = floatFix(Val, 2);

	//	Procurar caracter '.' na string.
	if( Val.indexOf(".") >= 0)
	{
		//	Obter parte inteira para colocar caracteres de milhar.
		strRetorno = Val.substr(0,Val.length-3);
		
		var iTamanho = strRetorno.length;
		
		//	Loop para colocar separadores de milhar necessários.
		while( iTamanho > 3 )
		{
			strRetorno = strRetorno.substr(0,iTamanho-3) + Milhar + strRetorno.substr(iTamanho-3);
			
			//	Atualiza tamamho da parte da string ainda nao tratada.
			iTamanho -= 3;
		}
		
		//	Concatenar parte decimal.
		strRetorno = strRetorno + Decimal + Val.substr(Val.length-2);
	}

	return strRetorno;
}

// Para AJAX (XMLHTTPRequest)

function newAjaxObject() {
	var A;
	try {
		A=new ActiveXObject('Msxml2.XMLHTTP');
	} catch (e) {
		try {
			A=new ActiveXObject('Microsoft.XMLHTTP');
		} catch (oc) {
			A=null;
		}
	}
	if(!A && typeof XMLHttpRequest != 'undefined') A = new XMLHttpRequest();
	return A;
}

function openWindow(url, name) {
	popupWin = window.open(url,name,"width=320,height=450,top=0,resizable=no,scrollbars=no");
}
