// Document.js

function Document() {

  this.returnDocType = function(objeto) {
    var v1 = new Array(".agr.br",".am.br",".art.br",".coop.br",".edu.br",".esp.br",".etc.br",".far.br",".fm.br",".g12.br",".gov.br",".imb.br",".ind.br",".inf.br",".mil.br",".org.br",".psi.br",".rec.br",".srv.br",".tmp.br",".tur.br",".tv.br"); // CNPJ
    var v2 = new Array(".com",".net",".org",".us",".biz",".info",".tv",".ws",".cc",".com.br",".net.br"); // AMBOS
    var v3 = new Array(".nom.br",".adm.br",".adv.br",".arq.br",".ato.br",".bio.br",".bmd.br",".can.br",".cim.br",".cng.br",".cnt.br",".ecn.br",".eng.br",".eti.br",".fnd.br",".fst.br",".fst.br",".ggf.br",".jor.br",".lel.br",".mat.br",".med.br",".mus.br",".not.br",".ntr.br",".odo.br",".ppg.br",".pro.br",".psc.br",".qsl.br",".slg.br",".trd.br",".vet.br",".zlg.br",".blog.br",".flog.br",".vlog.br",".wiki.br"); // CPF
	
    if (!isVoid(objeto.value)) {
      if (v2.haveItem(objeto.value)) return 0; // Ambos
      else if (v1.haveItem(objeto.value)) return 1; // P. Juridica
      else if (v3.haveItem(objeto.value)) return 2; // P. Física
      else return 0; // Ambos
    }
  }
  
  this.showPessoaType = function(cT) {
	$("divRepLegal").style.display = "none";
	if (cT == 0) {
	  $("pessoaTipo").style.display = "block";
	  $("pessoaF").checked = true;
	  $("pessoaJ").checked = false;
	  showDocType(2);
	  if ($("documento").value.length == 14) {
		$("pessoaF").checked = false;
		$("pessoaJ").checked = true;
		showDocType(1);
	  }
	} else {
		$("pessoaTipo").style.display = "none";
		showDocType(cT);
	}
  }
  
  this.isValidDoc = function(documento) {
    if (assine.clientType == 1 && documento.length == 14)      return this.validateCnpj(documento);
    else if (assine.clientType == 2 && documento.length == 11) return this.validateCpf(documento);
    else if (assine.clientType == 0) {
      if ($("pessoaF").checked && documento.length == 11)      return this.validateCpf(documento);
      else if ($("pessoaJ").checked && documento.length == 14) return this.validateCnpj(documento);
      else return false;
    }
  }
  
  this.validateCpf = function(cpf) {
    var i;
    var c = cpf.substr(0,9);
    var dv = cpf.substr(9,2);
    var d1 = 0;
    
    for (i = 0; i < 9; i++) d1 += c.charAt(i)*(10-i);
    if (d1 == 0) return false;
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1) return false;
    d1 *= 2;
    for (i = 0; i < 9; i++) d1 += c.charAt(i)*(11-i);
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(1) != d1)  return false;
    return true;
  }
  
  this.validateCnpj = function(cnpj) {
    var i;
    var c = cnpj.substr(0,12);
    var dv = cnpj.substr(12,2);
    var d1 = 0;
    
    for (i = 0; i < 12; i++) d1 += c.charAt(11-i)*(2+(i % 8));
    if (d1 == 0) return false;
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1)  return false;
    d1 *= 2;
    for (i = 0; i < 12; i++) d1 += c.charAt(11-i)*(2+((i+1) % 8));
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(1) != d1)  return false;
    return true;
  }
  
}

function showDocType(type) {	
	if (type == 1) {
      $("lblrazao").innerHTML = "Raz&atilde;o Social:*";
      $("lbldocumento").innerHTML = "CNPJ:*";
      $("documento").maxLength = 14;
      $("tipDoc").style.display = "";
	  $("pessoalFRG").style.display = "none";
	  $("divRepLegal").style.display = "block";
	  $("pessoaF").checked = false;
	  $("pessoaJ").checked = true;
	} else if (type == 2) {
      $("lblrazao").innerHTML = "Nome:*";
      $("lbldocumento").innerHTML = "CPF:*";
      $("documento").maxLength = 11;
      $("tipDoc").style.display = "none";
	  $("pessoalFRG").style.display = "block";
	  $("divRepLegal").style.display = "none";
	  $("pessoaF").checked = true;
	  $("pessoaJ").checked = false;
	}
}

function showDocTypeClear() {
	var objList = new Array("razao","documento","rg","repnome","repcpf","reprg");
	for (var i = 0; i < objList.length; i++) $(objList[i]).value = "";
}