function IsIgual(pvntParan1, pvntParan2){
  if(pvntParan1 == pvntParan2){
    return true;
  }
  return false;
}


function ValidaCPF(numero){
  dig_1 = 0;
  dig_2 = 0;
  controle_1 = 10;
  controle_2 = 11;
  lsucesso = 1;

  if ((numero.length != 12) ){
    alert("CPF inválido!");
    return false;
  }
  else {
    for (i=0 ; i < 9 ; i++){
      dig_1 = dig_1 + parseInt(numero.substring(i, i+1) * controle_1);
      controle_1 = controle_1 - 1;
    }

    resto = dig_1 % 11;
    dig_1 = 11 - resto;

    if ((resto == 0) || (resto == 1))
    dig_1 = 0;

    for ( i=0 ; i < 9 ; i++){
      dig_2 = dig_2 + parseInt(numero.substring(i, i + 1) * controle_2);
      controle_2 = controle_2 - 1;
    }

    dig_2 = dig_2 + 2 * dig_1;
    resto = dig_2 % 11;
    dig_2 = 11 - resto;

    if ((resto == 0) || (resto == 1))
    dig_2 = 0;

    dig_ver = (dig_1 * 10) + dig_2;

    if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))){
      alert("CPF inválido!");
      return false;
    }
  }
  return true;
} 



function iscgccpf(number, strTpPessoa) {

   var digit, cgc, i, j, b, s, numbers, replicated, tipo;
   numbers = '0123456789';
   replicated= '';
   digit = '';
   var tipo = ''   

   // Tira Pontos, Barras e Hífens do CGC //

   for (i=number.length-1; i>=0; i--) {
      if (numbers.indexOf(number.substr(i, 1)) < 0) 
         number = number.substr(0, i).concat(number.substr(i+1, number.length-i));
   }

   // Cria número de cgc/cpf repetido para teste de validade //

   for (i=0; i<number.length-2; i++)
      replicated = replicated.concat(number.substr(0, 1));
   // Testa se é cpf ou cgc //
   
   if (strTpPessoa == 'F'){
    tipo = 'CPF'
    if (number.length != 11){
      alert('O CPF deve ter no mínimo de 11 caracteres.');
      return false;
    }
   }
   else{
    tipo = 'CNPJ'
    if (number.length != 14){
      alert('O CNPJ deve ter no mínimo de 14 caracteres.');
      return false;
    }
   }
   
   
   if (number.substr(0, number.length-2) == replicated) {
      alert('Número inconsistente');
      return false;       
   }
   // Calcula Dígito //
   else {      
      cgc = number.substr(0, number.length-2);

      while (digit.length <2) {
         s = 0;
         b = 2;
         for (i=cgc.length-1; i>=0; i--) {
             s = s + b * cgc.substr(i, 1); 
             b = b + 1;
             if (number.length == 14 && b > 9)
                b = 2;
         }
         s = 11 - (s % 11);
         if (s > 9) s = 0;
         digit = digit.concat(s);
         cgc = cgc.concat(s);
      }

      // Verifica retorno //
      if (digit==number.substr(number.length-2, 2))
         return true;
      else {
         alert(tipo + ' inválido.');
         return false;
      }   
   }
}

//Nome: MoveNextField
//Parâmetros: input -- objeto que dispara o evento portando pode ser "this"
//Objetivo: mover o usuário para o proximo campo quando ele atingir o max length
//isso isso é controlado pelo tabIndex
function MoveNextField(input, e, len)
{
       
	var isNN    = (navigator.appName.indexOf("Netscape") != -1);
	
	if (e == null && !isNN) e = event;
	if (len == null) len = input.maxLength;	
	
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter  = (isNN) ? [0,8,9,16] : [0,8,9,16,17,18,37,38,39,40,46];
	var x;

	if((input.value.length >= len) && (!containsElement(filter,keyCode)))
	{
		input.value = input.value.slice(0, len);
		if((input.form[(getIndex(input)+1) % input.form.length].disabled)== false)
		{
			input.form[(getIndex(input)+1) % input.form.length].focus();
			input.form[(getIndex(input)+1) % input.form.length].select();
		}
		else
		{
			if((input.form[(getIndex(input)+2) % input.form.length].disabled)== false)
			{
				input.form[(getIndex(input)+2) % input.form.length].focus();
				input.form[(getIndex(input)+2) % input.form.length].select();
			}
			else
			{
				input.form[(getIndex(input)+4) % input.form.length].focus();
				input.form[(getIndex(input)+4) % input.form.length].select();
			}
		}
	}
	
	function containsElement(arr, ele)
	{
		var index;
		
		for(index = 0; index < arr.length; index++)
			if(arr[index] == ele)
				return true;

		return false;
	}

	function getIndex(input)
	{
		var index = -1, i = 0, found = false;
		while ((i < input.form.length) && (index == -1))
		if (input.form[i] == input)
			index = i;
		else
			i++;
	
		return index;
	}
	return true;
}

//Nome: CalQuant
//Parâmetros: obj -- objeto que dispara o evento portando pode ser "this";
//            Acao -- Informar 'I' para aumentar ou D Diminuir.
//Objetivo: Modificar a quantidade do campo informado.
function CalQuant(obj,Acao)
{
	if (Acao == 'I')
	{
		if (parseInt(obj.value) < 999)
		    obj.value = parseInt(obj.value) + 1;
	}
	else if (Acao = 'D')
	{
		if (parseInt(obj.value) > 1)
		    obj.value = parseInt(obj.value) - 1; 
	}
}


//Nome: CalQuant com submit
//Parâmetros: obj -- objeto que dispara o evento portando pode ser "this";
//            Acao -- Informar 'I' para aumentar ou D Diminuir.
//Objetivo: Modificar a quantidade do campo informado.
function CalQuantSub(obj,Acao,formulario)
{
	if (Acao == 'I')
	{
		if (parseInt(obj.value) < 999)
		    obj.value = parseInt(obj.value) + 1;
		    
	}
	else if (Acao = 'D')
	{
		if (parseInt(obj.value) > 1)
		    obj.value = parseInt(obj.value) - 1; 
		    
	}
	var meuRel = "window.document." + formulario + ".submit();" 
      eval(meuRel);
}
//Nome: CalQuant com submit
//Parâmetros: obj -- objeto que dispara o evento portando pode ser "this";
//            Acao -- Informar 'I' para aumentar ou D Diminuir.
//Objetivo: Modificar a quantidade do campo informado.
function CalQuantSub1(obj,Acao,formulario)
{
	if (Acao == 'B')
	{
	if (parseInt(obj.value) > 1)
		    obj.value = parseInt(obj.value); 
		    
	}
	var meuRel = "window.document." + formulario + ".submit();" 
      eval(meuRel);
}

function validaOrdem(obj, saction)
{
    window.document.frmOrdena.action = saction;
    window.document.frmOrdena.submit();
}

function VerQuants(pcampoprefix, purlAction) {
    var midx, mhid, mqty, mqtyalt;
    
    midx = 0;
    mqtyalt = 0;
    
    do{
        try{
           mhid = window.document.Produtos[pcampoprefix + '_hid_' + midx.toString()].value;
           mqty = window.document.Produtos[pcampoprefix + '_default_' + midx.toString()].value;
        }
        catch(e){
           mhid = null;
           mqty = null;
        }

        if(mhid != null && mqty != null){
            mhid = parseInt(mhid);
            mqty = parseInt(mqty);
            
            if(mhid != mqty){
               mqtyalt = 1;
               alert('Atenção! A quantidade de um ou mais produtos foi alterada. Clique sobre o botão atualizar valores.');
               break;
            };
        };
        
        midx += 1;
    }while((mhid != null || mqty != null) && mqtyalt == 0);
    
    if(mqtyalt == 0){
        window.location = purlAction;
    }
}
function EnableKey(intKeyCodeValue,strRange,strOthers)
{
   /*
   Esta função impede que o usuário digite um valor que não é esperado para o campo.
   
   intKeyCodeValue = event *** o primeiro parâmetro desta função deve ser a palavra chave event,
                               para que possamos identificar qual tecla foi pressionada.
                               
   strRange  *** strRange são os valores das chaves que estão habilitadas. Esta variável deve conter dois números
                 inteiros representando o código ASCII da tecla, separados pelo sinal de ':'(dois pontos).
                 Serão habilitadas todas as teclas que forem maiores ou iguais ao primeiro valor(valor antes dos
                 dois pontos) e menores ou iguais ao segundo valor (valor após os dois pontos).
                 Caso o segundo valor seja maior que o primeiro, os valores serão trocados.
   
   strOthers *** strOthers é dividido por ponto-e-vírgulas(;). Os valores ASCII contidos em strOthers são excessões e quando
                 pressionados serão exibidos mesmo que não esteja no escopo do parâmetro strRange.
                 OBS***PASSE PARA strOthers OS CARACTERS ASCII!!!
   */
   
   strRange = strRange.split(":");
   var intFirst = strRange[0];
   var intEnd   = strRange[1];
   var intAux;
   
   if(strOthers.length>0)
   {
      strOthers = strOthers.split(";")
   }
   if(!(isNaN(intFirst) && isNaN(intEnd)))
   {
      if(intEnd < intFirst)
      {
         intAux   = intFirst;
         intFirst = intEnd;
         intEnd   = intAux;
      }
      if((intKeyCodeValue.keyCode < intFirst) || (intKeyCodeValue.keyCode > intEnd))
      {
         if(strOthers.length < 1)
         {
            intKeyCodeValue.keyCode = false;
         }
         
         intAux=0;
         for(var i=0; i<strOthers.length; i++)
         {
            if(!(isNaN(strOthers[i])))
            {
               if(intKeyCodeValue.keyCode == strOthers[i])
               {
                  intAux += 1;
               }
            }
         }
         if(intAux==0)
         {
            intKeyCodeValue.keyCode = false;
         }
      }
   }
}
