function ajax()
{
    try {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch(ex) {
            try {
                return new XMLHttpRequest();
            } catch(exc) {
                alert("Esse browser não tem recursos para uso do AJAX");
                return false;
            }
        }
    }
}
ajax1 = new ajax;

function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, prec = decimals;
 
    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };
 
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
 
    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
 
    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;
 
    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
 
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }
 
    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}

function calcula_valor(valor,a,tipo,minimo) {
	
	valor_agora = document.getElementById('valor').value;
	valor_agora = valor_agora.replace(',','.');
	valor_agora = parseFloat(valor_agora), 2, ',', '.';
	valor_agora = valor_agora;
	
	if (tipo == 'un') { 
	valor = valor*document.getElementById('qtd').value; 
	} else { 
	valor = valor;
	}
	
	minimo = minimo;
	if (valor >= minimo) { 
	// nao faz nada
	} else { 
	valor = minimo;
	}
	
	
	
	if (a.checked == true) {
		/*document.getElementById('mostrar_valor').innerHTML = "R$"+number_format(parseFloat(document.getElementById('valor').value) + parseFloat(valor), 2, ',', '.');				
		document.getElementById('valor').value = parseFloat(document.getElementById('valor').value) + parseFloat(valor); */
		
		valor_novo = valor_agora + valor;
		valor_novo = valor_novo;
 		document.getElementById('valor').value = valor_novo;
		document.getElementById('mostrar_valor').innerHTML = "R$"+number_format(parseFloat(valor_agora) + parseFloat(valor), 2, ',', '.');				

	} else {
		valor_novo = valor_agora - valor;
		valor_novo = valor_novo;
 		document.getElementById('valor').value = valor_novo;
		document.getElementById('mostrar_valor').innerHTML = "R$"+number_format(parseFloat(valor_agora) - parseFloat(valor), 2, ',', '.');				
	}
}

function marcartudo(){
	var objeto;
	
	objeto = document.getElementById("marcar") ;
	indice = document.getElementById("indice").value ;
	
	if (objeto.checked == true){
		for (i=1;i<=indice;i++){
			document.getElementById("checkbox"+i).checked = true;
		}		
	} else {
		for (i=1;i<=indice;i++){
			document.getElementById("checkbox"+i).checked = false;
		}	
	}	
}

function somente_numero(campo){   
	var digits="0123456789"   
	var campo_temp   
	for (var i=0;i<campo.value.length;i++){   
		campo_temp=campo.value.substring(i,i+1)   
		if (digits.indexOf(campo_temp)==-1){   
			campo.value = campo.value.substring(0,i);   
		}   
	}   
}

function Validar(theCPF)
{
  
  if (theCPF.value == "")
  {
    alert("Campo inválido. É necessário informar o CPF ou CNPJ");
    
	
	document.getElementById("cpfcnpj").value = "";
	
    return (false);
  }
  if (((theCPF.value.length == 11) && (theCPF.value == 11111111111) || (theCPF.value == 22222222222) || (theCPF.value == 33333333333) || (theCPF.value == 44444444444) || (theCPF.value == 55555555555) || (theCPF.value == 66666666666) || (theCPF.value == 77777777777) || (theCPF.value == 88888888888) || (theCPF.value == 99999999999) || (theCPF.value == 00000000000)))
  {
    alert("CPF inválido.");
    document.getElementById("cpfcnpj").value = "";
    return (false);
  }


  if (!((theCPF.value.length == 11) || (theCPF.value.length == 14)))
  {
    alert("CPF inválido.");
    document.getElementById("cpfcnpj").value = "";
    return (false);
  }

  var checkOK = "0123456789";
  var checkStr = theCPF.value;
  var allValid = true;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Favor preencher somente com dígitos o campo CPF/CNPJ.");
    document.getElementById("cpfcnpj").value = "";
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseFloat(allNum);
  if (chkVal != "" && !(prsVal > "0"))
  {
    alert("CPF zerado !");
    document.getElementById("cpfcnpj").value = "";
    return (false);
  }

if (theCPF.value.length == 11)
{
  var tot = 0;

  for (i = 2;  i <= 10;  i++)
    tot += i * parseInt(checkStr.charAt(10 - i));

  if ((tot * 10 % 11 % 10) != parseInt(checkStr.charAt(9)))
  {
    alert("CPF inválido.");
    document.getElementById("cpfcnpj").value = "";
    return (false);
  }
  
  tot = 0;
  
  for (i = 2;  i <= 11;  i++)
    tot += i * parseInt(checkStr.charAt(11 - i));

  if ((tot * 10 % 11 % 10) != parseInt(checkStr.charAt(10)))
  {
    alert("CPF inválido.");
    document.getElementById("cpfcnpj").value = "";
    return (false);
  }
}
else
{
  var tot  = 0;
  var peso = 2;
  
  for (i = 0;  i <= 11;  i++)
  {
    tot += peso * parseInt(checkStr.charAt(11 - i));
    peso++;
    if (peso == 10)
    {
        peso = 2;
    }
  }

  if ((tot * 10 % 11 % 10) != parseInt(checkStr.charAt(12)))
  {
    alert("CPF inválido.");
    document.getElementById("cpfcnpj").value = "";
    return (false);
  }
  
  tot  = 0;
  peso = 2;
  
  for (i = 0;  i <= 12;  i++)
  {
    tot += peso * parseInt(checkStr.charAt(12 - i));
    peso++;
    if (peso == 10)
    {
        peso = 2;
    }
  }

  if ((tot * 10 % 11 % 10) != parseInt(checkStr.charAt(13)))
  {
    alert("CPF inválido.");
    document.getElementById("cpfcnpj").value = "";
    return (false);
  }
}
  return(true);
}
