
// DocumentTools.js

// Para utilizar estas funcoes, incluir StringTools.js


function validateCPF(cpf) {
  if (!stringIsDigit(cpf)) return false;

  var i, ref, soma, len = cpf.length, ch = cpf.charAt(0);
  if (len != 11) return false;

  for (i = 1; i < len && ch == cpf.charAt(i); i++);
  if (i == len) return false;

  for (ref = len - 2; ref < len; ref++) {
    soma = 0;

    for (i = 0; i < ref; i++) {
      soma += (charToDigit(cpf.charAt(i)) * (ref - i + 1));
    }

    soma %= 11;
    soma = (soma <= 1 ? 0 : 11 - soma);

    if (soma != charToDigit(cpf.charAt(ref))) return false;
  }

  return true;
}


function validateCNPJ(cnpj) {
  if (!stringIsDigit(cnpj)) return false;

  var i, ref, soma, len = cnpj.length; base = "6543298765432";
  if (len != 14) return false;

  for (ref = len - 2; ref < len; ref++) {
    soma = 0;

    for (i = 0; i < ref; i++) {
      soma += (charToDigit(cnpj.charAt(i)) * charToDigit(base.charAt(i + len - ref - 1)));
    }

    soma %= 11;
    soma = (soma <= 1 ? 0 : 11 - soma);

    if (soma != charToDigit(cnpj.charAt(ref))) return false;
  }

  return true;
}

function applyCPFMask(cpf) {
  return applyMask("###.###.###-##", cpf, "#");
}

function applyCNPJMask(cnpj) {
  return applyMask("##.###.###/####-##", cnpj, "#");
}

function clearForm(frm) {
	for (var i=0; i < frm.elements.length; i++) {
		if (frm.elements[i].type != "submit" && frm.elements[i].type != "button") {
			frm.elements[i].value = "";
		}
	}
	return true;
}

