function deixaNumeros(str)
{
	var strClean = "";
	var ch;

	if (str == null)
		return null;
	
	var tam = str.length;

	for (var cont = 0; cont < tam; cont++)
	{
		ch = str.charAt(cont);

		if (ch > '9' || ch < '0')
			continue;

		strClean += ch;
	}

	return strClean;
}

function validaCPF(str)
{
	var strC = deixaNumeros(str),
		tmn,
		s1 = 0,
		s2 = 0,
		dv1,
		dv2;

	if (strC == null)
		return false;

	tmn = strC.length;

	if (tmn < 11)
		return false;


	if (strC.match(/^(([0]+)|([1]+)|([2]+)|([3]+)|([4]+)|([5]+)|([6]+)|([7]+)|([8]+)|([9]+))$/))
		return false;


	for (var i = 0; i < tmn -2; i++)
	{
		s1 += (tmn -1 -i) * parseInt(strC.charAt(i));
		s2 += (tmn -i) * parseInt(strC.charAt(i));
	}
	
	s1 %= 11;
	dv1 = (s1 == 0 || s1 == 1)?0:11-s1;

	s2 += dv1 * 2;
	s2 %= 11;
	dv2 = (s2 == 0 || s2 == 1)?0:11-s2;

	try
	{
		if (apresentaVerificador)
			window.alert(dv1+''+dv2);
	}
	catch(e)
	{}

	return (dv1 == parseInt(strC.charAt(tmn -2)) && dv2 == parseInt(strC.charAt(tmn -1)))?true:false;
}

function numbersonly(myfield, e, dec) {
	var key;
	var keychar;
	if (window.event) {
		key = window.event.keyCode;
	} else if (e) {
		key = e.which;
	} else {
		return true;
	}

	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27)) {
		return true;
	// Control-V
	} else if ((key == 118 || key == 86) && e.ctrlKey) {
		return false;
	// numbers
	} else if ((("0123456789").indexOf(keychar) > -1)) {
		return true;
	// decimal point jump
	} else if (dec && (keychar == ".")) {
		myfield.form.elements[dec].focus();
		return false;
	} else {
		return false;
	}
}

function validcharsonly(myfield, e, dec) {
	var key;
	var keychar;
	if (window.event) {
		key = window.event.keyCode;
	} else if (e) {
		key = e.which;
	} else {
		return true;
	}

	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27)) {
		return true;
	// Control-V
	} else if ((key == 118 || key == 86) && e.ctrlKey) {
		return false;
	// Caracteres v�lidos
	} else if (((" 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").indexOf(keychar) > -1)) {
		return true;
	} else {
		return false;
	}
}

function Vazio(input, text)
{
   if(input.value.length==0 || input.value == '')
   {
      alert(text);
      input.focus();
      return true;
   }
}

function Trim(str){
  
  return str.replace(/^\s+|\s+$/g,"");
}

function formataCPF(cpf, e){
  if(numbersonly(cpf, e))
    formataCampo(cpf, "000.000.000-00",e);
   else
    return false;
}

function formataCEP(cep,e){
  if(numbersonly(cep, e))
    formataCampo(cep, "00.000-000",e);
   else
    return false;
  

}

//formata de forma generica os campos
function formataCampo(campo, Mascara, evento) {
    var boleanoMascara;
    
    var Digitato = evento.keyCode;
    exp = /\-|\.|\/|\(|\)| /g
    campoSoNumeros = campo.value.toString().replace( exp, "" );
  
    var posicaoCampo = 0;    
    var NovoValorCampo="";
    var TamanhoMascara = campoSoNumeros.length;;
    
    if (Digitato != 8) { // backspace
        for(i=0; i<= TamanhoMascara; i++) {
            boleanoMascara  = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                || (Mascara.charAt(i) == "/"))
            boleanoMascara  = boleanoMascara || ((Mascara.charAt(i) == "(")
                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " "))
            if (boleanoMascara) {
                NovoValorCampo += Mascara.charAt(i);
                  TamanhoMascara++;
            }else {
                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo);
                posicaoCampo++;
              }           
          }    
        campo.value = NovoValorCampo;
          return true;
    }else {
        return true;
    }
}

function pularcampo(campo,alvo,maxlength){
    if (document.getElementById(campo).value.length==maxlength){
        document.getElementById(alvo).focus();
        }
}

function isNUMBER(what){
	if(isNUMB(what.value) != 1){
	  window.alert("Neste campo só são permitidos números!");
	  what.value="";
	  return false;
	}
	return true;
}

function isNUMB(c)
{
	if((cx=c.indexOf(","))!=-1)
	{
		c = c.substring(0,cx)+"."+c.substring(cx+1);
	}
	if((parseFloat(c) / c != 1))
	{
		if(parseFloat(c) * c == 0)
		{
			return(1);
		}
		else
		{
			return(0);
		}
	}
	else
	{
		return(1);
	}
}

//Critica Cadastro
function ValidaCadastroCliente(){

	if ((document.cadastrotorpedocliente.ddd.value == "")
	|| (document.cadastrotorpedocliente.meuoi.value == "")
	|| (document.cadastrotorpedocliente.dia.value == "")
	|| (document.cadastrotorpedocliente.mes.value == "")
	|| (document.cadastrotorpedocliente.cpf.value == "")
	|| (document.cadastrotorpedocliente.ano.value == "")
	)
	{
		window.alert("Todos os campos devem estar preenchidos!");
		return false;
	}
	
	if ((validaCPF(document.cadastrotorpedocliente.cpf.value) == 0) && (ValidaCNPJ(document.cadastrotorpedocliente.cpf.value) == 0))
	{
		window.alert("O n??mero do CPF/CNPJ informado n??o ?? v??lido. Informe o seu CPF/CNPJ utilizando somente d??gitos.");
		return false;
	}

	return true;
}