function addCommas(nStr) {
	nStr += '';
	x = nStr.split(',');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) x1 = x1.replace(rgx, '$1' + '.' + '$2');
	return x1 + x2;
}

function rand(l,u) { return Math.floor((Math.random() * (u-l+1))+l); }

//function isEmail(mail) {
//	var er = RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
//	return (er.test(mail) == false) ? false : true;
//}

//valida email
function isEmail(email){
    exp = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/
    return (exp.test(email)) ? true : false;
}

//valida CPF
function isCPF(cpf){
    exp = /^[0-9]{3}\.[0-9]{3}\.[0-9]{3}-[0-9]{2}$/
    return (exp.test(cpf)) ? true : false;
}

function isCPF2(cpf){
    exp = /^[0-9]{3}[0-9]{3}[0-9]{3}[0-9]{2}$/
    return (exp.test(cpf)) ? true : false;
}

function valida_cpf(cpf)
      {
      var numeros, digitos, soma, i, resultado, digitos_iguais;
      digitos_iguais = 1;
      if (cpf.length < 11)
            return false;
      for (i = 0; i < cpf.length - 1; i++)
            if (cpf.charAt(i) != cpf.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            numeros = cpf.substring(0,9);
            digitos = cpf.substring(9);
            soma = 0;
            for (i = 10; i > 1; i--)
                  soma += numeros.charAt(10 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            numeros = cpf.substring(0,10);
            soma = 0;
            for (i = 11; i > 1; i--)
                  soma += numeros.charAt(11 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
      else
            return false;
      }





//valida CEP
function isCEP(cep){
    exp = /^[0-9]{5}-[0-9]{3}$/
    return (exp.test(cep)) ? true : false;
}

function CEPMask (id, e) {
	if (window.event) e = event; //IE
	if (e.keyCode==8) return 8;
	
	var value = $('#'+id).val();

	//if (value.length > 5 && value.substr(5, 1) != '-') value =  value.substr(0, 5) + '-' + value.substr(5);
	value = insertChar(value, 5, '-');
	value = value.substr(0, 9);
	$('#'+id).val( value );
}

function CPFMask (id, e) {
	if (window.event) e = event; //IE
	if (e.keyCode==8) return 8;
	
	var value = $('#'+id).val();

	value = insertChar(value, 3, '.');
	value = insertChar(value, 7, '.');
	value = insertChar(value, 11, '-');

	value = value.substr(0, 14);

	$('#'+id).val( value );
}

function valida_cnpj(cnpj)
      {
      var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
      digitos_iguais = 1;
      if (cnpj.length < 14 && cnpj.length < 15)
            return false;
      for (i = 0; i < cnpj.length - 1; i++)
            if (cnpj.charAt(i) != cnpj.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            tamanho = cnpj.length - 2
            numeros = cnpj.substring(0,tamanho);
            digitos = cnpj.substring(tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            tamanho = tamanho + 1;
            numeros = cnpj.substring(0,tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
      else
            return false;
    }



function insertChar(value, position, char) {
	if (value.length > position && value.substr(position, 1) != char) value =  value.substr(0, position) + char + value.substr(position);
	return value;
}

function onlyNumbers (e) {
    var tecla = (window.event) ? event.keyCode : e.which;

	if (tecla > 47 && tecla < 58) return true;
    return (tecla != 8) ? false : true;
}


function alertaErro(mensagem)
{
        if(mensagem.length>0){
        mensagem = "<strong>Ocorreram os seguintes erros:</strong><br>" +mensagem;
        csscody.error(mensagem);
        return false;
        }
    

}

function alertaMsg(mensagem)
{
        if(mensagem.length>0){
        mensagem = "<strong>Mensagem Hotel dos Carros</strong><br>" +mensagem;
        csscody.alert(mensagem);
        return false;
        }


}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}


