// Validator.js
function Validator() {
  // Declaração de váriaveis privadas
  var validatingCupom = null;
  var forwardOnFinish = null;
  this.checkForm = function(step) {
    if (step == 1) return ValidatorS1();
    else if (step == 2) return ValidatorS2();
    else if (step == 3) return ValidatorS3();
  }
}

function validateEmail(email) {
  return (/^[A-Za-z0-9]+(([\.\_\-]{1}[A-Za-z0-9]+)+)?@[A-Za-z0-9]+(([\.\_\-]{1}[A-Za-z0-9]+)+)?\.[A-Za-z]{2,4}$/.test(email));
}

function isSequenceRepeated(str,mode) {
  if (mode != null && mode == "+") {
    for (var i = 0; i < str.length; i++) {
	  var s = str.substring(i,i+1);
	  if (s != "." && s != "$" && s != "*" && s != "+" && s != "^" && s != "(" && s != ")" && s != "{" && s != "}" && s != "[" && s != "]" && s != "(" && s == ")" && s != "|") {
	    eval("var reg = /"+s+"{4,}/");
        if (reg.test(str)) return true;
	  }
    }
    return false;
  } else if (mode != null && mode == "^") {
	  var s = str.substring(0,1);
	  if (s != "." && s != "$" && s != "*" && s != "+" && s != "^" && s != "{" && s != "}" && s != "[" && s != "]" && s != "(" && s == ")" && s != "|") {
	    eval("var reg = /^"+s+"{4,}/");
        if (reg.test(str)) return true;
	  }
  
	  return false;
  } else return null;
}

function ValidatorS1() {
  $("lblcupom").style.textDecoration = "none";
  $("lbltipoDom").style.textDecoration = "none";
  $("lbldominio").style.textDecoration = "none";
  $("lbldadosPlanos").style.textDecoration = "none";
  $("lblplano").style.textDecoration = "none";
  $("lbldiaVenc").style.textDecoration = "none";
  $("lblperiodo").style.textDecoration = "none";
  if ((!$("cupom").value.isWhite()) && (!assine.cupom.confirmado)) {
    if (assine.currentStep != 1) assine.showStep(1);
    blinkField("lbl","cupom","");
    $("cupom").focus();
    $("cupom").select();
    return false;
  }
  if ($("tipoDomN").checked == false && $("tipoDomT").checked == false) {
    if (assine.currentStep != 1) assine.showStep(1);
    blinkField("lbl","tipoDom","Escolha um tipo de domínio.");
    $("tipoDomN").focus();
    return false;
  }
  if (($("dominio").value.isWhite())) {
    if (assine.currentStep != 1) assine.showStep(1);
    blinkField("lbl","dominio","Informe um domínio.");
    $("dominio").focus();
    return false;
  }
  if (isSequenceRepeated($("dominio").value,"^")) {
    if (assine.currentStep != 1) assine.showStep(1);
    blinkField("lbl","dominio","O domínio parece ser inválido.\nSe estiver correto, entre em contato com nossa equipe técnica.");
    $("dominio").focus();
    $("dominio").select();
    return false;
  }
  if ($("extensao").value == "") {
    if (assine.currentStep != 1) assine.showStep(1);
    blinkField("lbl","dominio","Selecione uma extensão válida para seu domínio.");
    $("extensao").focus();
    return false;
  }
  var v1 = new Array(".agr.br",".am.br",".art.br",".com.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",".net.br",".org.br",".psi.br",".rec.br",".srv.br",".tmp.br",".tur.br",".tv.br",".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"); // NAC
  var v2 = new Array(".com",".net",".org",".us",".biz",".info",".tv",".ws",".cc"); // INT
  var dom = $("dominio").value.toLowerCase();
  if (v1.haveItem($("extensao").value)) {
    if ($("dominio").value.length < 2 || $("dominio").value.length > 26) {
      if (assine.currentStep != 1) assine.showStep(1);
      blinkField("lbl","dominio","Seu domínio deve conter entre 2 e 26 caracteres");
      $("dominio").focus();
      $("dominio").select();
      return false;
    }
    
    if ($("extensao").value != ".nom.br") {      
	  if (/^\-/.test(dom) || /(\-\-)/.test(dom) || /[^a-z0-9\-\à\á\â\ã\é\ê\í\ó\ô\õ\ú\ü\ç\.]/.test(dom) || /\-$/.test(dom)) {
	    if (assine.currentStep != 1) assine.showStep(1);
	    blinkField("lbl","dominio","O domínio deve conter somente letras, números, hífens e (àáâãéêíóôõúüç) para ser válido.\nNão pode começar ou terminar com hífen.\nSem duplo hífen (--).");
	    $("dominio").focus();
	    $("dominio").select();
	    return false;
      }

      if (!(/[^\d]+/.test(dom))) {
	    if (assine.currentStep != 1) assine.showStep(1);
	    blinkField("lbl","dominio","O domínio não pode conter somente números");
	    $("dominio").focus();
	    $("dominio").select();
	    return false;
      }
    } else {
	  if (/^\-/.test(dom) || /(\-\-)/.test(dom) || /[^a-z0-9\-\à\á\â\ã\é\ê\í\ó\ô\õ\ú\ü\ç\.]/.test(dom) || /\-$/.test(dom)) {
	    if (assine.currentStep != 1) assine.showStep(1);
	    blinkField("lbl","dominio","O domínio deve conter somente letras, números, hífens, ponto e (àáâãéêíóôõúüç) para ser válido.\nNão pode começar ou terminar com hífen.\nSem duplo hífen (--).");
	    $("dominio").focus();
	    $("dominio").select();
	    return false;
      }
      if (!(/[a-z\d\-\à\á\â\ã\é\ê\í\ó\ô\õ\ú\ü\ç]+\.[a-z0-9\-\à\á\â\ã\é\ê\í\ó\ô\õ\ú\ü\ç]+/.test(dom))) {
        if (assine.currentStep != 1) assine.showStep(1);
        blinkField("lbl","dominio","Domínios nominais devem estar da seguinte formatação:\n nome1.nome2.nom.br");
        $("dominio").focus();
        $("dominio").select();
        return false;      	
      }
      if (dom.split(".")[0].length < 2 || dom.split(".")[1].length < 2 || dom.split(".")[0].length > 26 || dom.split(".")[1].length > 26) {
        if (assine.currentStep != 1) assine.showStep(1);
        blinkField("lbl","dominio","Os dois termos do domínio nominal devem ter no mínimo 2 caracteres e no máximo 26");
        $("dominio").focus();
        $("dominio").select();
        return false;      	
      }
      if (/\.[a-z\d\-\à\á\â\ã\é\ê\í\ó\ô\õ\ú\ü\ç]+\.|\.\./.test(dom)) {
        if (assine.currentStep != 1) assine.showStep(1);
        blinkField("lbl","dominio","Domínios nominais não podem conter mais de dois pontos.");
        $("dominio").focus();
        $("dominio").select();
        return false;      	
      }
      if ( (!(/[^\d]+[^\.]+[^a-z\d\-\à\á\â\ã\é\ê\í\ó\ô\õ\ú\ü\ç]+/).test(dom)) || (!(/[^a-z\d\-\à\á\â\ã\é\ê\í\ó\ô\õ\ú\ü\ç]+[^\.]+[^\d]+/).test(dom)) ) {
        if (assine.currentStep != 1) assine.showStep(1);
        blinkField("lbl","dominio","O nenhum dois termos do domínio nominal podem conter somente números.");
        $("dominio").focus();
        $("dominio").select();
        return false;      	
      }
    }
  } else {
    if ($("dominio").value.length < 2 || $("dominio").value.length > 56) {
      if (assine.currentStep != 1) assine.showStep(1);
      blinkField("lbl","dominio","Seu domínio deve conter entre 2 e 56 caracteres");
      $("dominio").focus();
      $("dominio").select();
      return false;
    }
    if (!(/[^\d]+/.test(dom))) {
      if (assine.currentStep != 1) assine.showStep(1);
      blinkField("lbl","dominio","O domínio não pode conter somente números.");
      $("dominio").focus();
      $("dominio").select();
      return false;
    }
	if (!(/[^\d]+/.test(dom)) || (/^\-/.test(dom) || /(\-\-)/.test(dom) || /[^a-z0-9\-\à\á\â\ã\é\ê\í\ó\ô\õ\ú\ü\ç\.]/.test(dom) || /\-$/.test(dom))) {
	  if (assine.currentStep != 1) assine.showStep(1);
	  blinkField("lbl","dominio","O domínio deve conter somente letras, números, hífens e (àáâãéêíóôõúüç) para ser válido.\nNão pode começar ou terminar com hífen.\nSem duplo hífen (--).");
	  $("dominio").focus();
	  $("dominio").select();
	  return false;
    }
  }
  if ($("plano").value == "") {
    if (assine.currentStep != 1) assine.showStep(1);
    $("lbldadosPlanos").style.textDecoration = "blink";
    blinkField("lbl","plano","Selecione um plano.");
    $("plano").focus();
    return false;
  }
  if ($("diaVenc").value == "") {
    if (assine.currentStep != 1) assine.showStep(1);
    $("lbldadosPlanos").style.textDecoration = "blink";
    blinkField("lbl","diaVenc","Selecione um dia para o vencimento das faturas.");
    $("diaVenc").focus();
    return false;
  }
  if ($("periodo").value == "") {
    if (assine.currentStep != 1) assine.showStep(1);
    $("lbldadosPlanos").style.textDecoration = "blink";
    blinkField("lbl","periodo","Escolha a periodicidade.");
    $("periodo").focus();
    return false;
  }
  return true;  
}

function ValidatorS2() {
  $("lblidentificacao").style.textDecoration = "none";
  $("lblreplegal").style.textDecoration = "none";
  $("lbllocalizacao").style.textDecoration = "none";
  $("lblcontato").style.textDecoration = "none";
  $("lblrazao").style.textDecoration = "none";
  $("lbldocumento").style.textDecoration = "none";
  $("lblrg").style.textDecoration = "none";
  $("lblrepnome").style.textDecoration = "none";
  $("lblrepcpf").style.textDecoration = "none";
  $("lblreprg").style.textDecoration = "none";
  $("lblendereco").style.textDecoration = "none";
  $("lblnumero").style.textDecoration = "none";
  $("lblbairro").style.textDecoration = "none";
  $("lblcidade").style.textDecoration = "none";
  $("lblestado").style.textDecoration = "none";
  $("lblcep").style.textDecoration = "none";
  $("lbltelefone").style.textDecoration = "none";
  $("lblresponsavel").style.textDecoration = "none";
  $("lblaniversario").style.textDecoration = "none";
  $("lblmsn").style.textDecoration = "none";
  $("lblemail").style.textDecoration = "none";
  $("lblemail2").style.textDecoration = "none";
  $("lblemail3").style.textDecoration = "none";
  $("lblemail4").style.textDecoration = "none";
  $("lblobs").style.textDecoration = "none";
  var campos = new Array("razao","endereco","bairro","cidade","responsavel");
  for (var i = 0; i < campos.length; i++) {
    if (isSequenceRepeated($(campos[i]).value,"+")) {
      if (assine.currentStep != 2) assine.showStep(2);
      blinkField("lbl",campos[i],"O campo " + campos[i] + " parece ser inválido.\nSe estiver correto, entre em contato com nossa equipe técnica.");
      $(campos[i]).focus();
      $(campos[i]).select();
      return false;
    }
  }
  var isPF = (assine.clientType == 2 || (assine.clientType == 0 && $("pessoaF").checked));
  if (($("razao").value.isWhite())) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","razao",(isPF?"O nome não pode ser vazio.":"A razão social não pode ser vazia."));
    blinkField("lbl","identificacao","");
    $("razao").focus();
	$("razao").select();
    return false;
  }
  if (($("documento").value.isWhite())) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","documento","O campo " + (isPF?"CPF":"CNPJ") + " não pode ser vazio.");
    blinkField("lbl","identificacao","");
    $("documento").focus();
	$("documento").select();
    return false;
  }
  if (/[^0-9]+/.test($("documento").value)) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","documento","O campo " + (isPF?"CPF":"CNPJ") + " deve conter somente números.");
    blinkField("lbl","identificacao","");
    $("documento").focus();
    $("documento").select();
    return false;
  }
  if (!new Document().isValidDoc($("documento").value)) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","documento","O campo " + (isPF?"CPF":"CNPJ") + " não é válido.");
    blinkField("lbl","identificacao","");
    $("documento").focus();
    $("documento").select();
    return false;
  }
  if (assine.clientType == 2) {
	  if (($("rg").value.isWhite())) {
	    if (assine.currentStep != 2) assine.showStep(2);
	    blinkField("lbl","rg","O campo RG não pode ser vazio.");
	    blinkField("lbl","identificacao","");
	    $("rg").focus();
		$("rg").select();
	    return false;
	  }
	  if (/[^0-9A-Za-z]+/.test($("rg").value)) {
	    if (assine.currentStep != 2) assine.showStep(2);
	    blinkField("lbl","rg","O campo RG deve conter somente números e letras.");
	    blinkField("lbl","identificacao","");
	    $("rg").focus();
		$("rg").select();
	    return false;
	  }
  } else if (assine.clientType == 1) {
	  if (($("repnome").value.isWhite())) {
	    if (assine.currentStep != 2) assine.showStep(2);
	    blinkField("lbl","repnome","O campo nome do representante não pode ser vazio.");
		blinkField("lbl","identificacao","");
	    blinkField("lbl","replegal","");
	    $("repnome").focus();
		$("repnome").select();
	    return false;
	  }

	  if (($("repcpf").value.isWhite())) {
	    if (assine.currentStep != 2) assine.showStep(2);
	    blinkField("lbl","repcpf","O campo CPF do representante não pode ser vazio.");
		blinkField("lbl","identificacao","");
	    blinkField("lbl","replegal","");
	    $("repcpf").focus();
		$("repcpf").select();
	    return false;
	  }

	  if (/[^0-9]+/.test($("repcpf").value)) {
	    if (assine.currentStep != 2) assine.showStep(2);
	    blinkField("lbl","repcpf","O campo CPF do representante deve conter somente números e letras.");
		blinkField("lbl","identificacao","");
	    blinkField("lbl","replegal","");
	    $("repcpf").focus();
		$("repcpf").select();
	    return false;
	  }

	  if (!new Document().validateCpf($("repcpf").value)) {
	    if (assine.currentStep != 2) assine.showStep(2);
	    blinkField("lbl","repcpf","O campo CPF do representante não é válido.");
	    blinkField("lbl","identificacao","");
		blinkField("lbl","replegal","");
	    $("repcpf").focus();
	    $("repcpf").select();
	    return false;
	  }
	  if (($("reprg").value.isWhite())) {
	    if (assine.currentStep != 2) assine.showStep(2);
	    blinkField("lbl","reprg","O campo RG do representante não pode ser vazio.");
	    blinkField("lbl","identificacao","");
		blinkField("lbl","replegal","");
	    $("reprg").focus();
		$("reprg").select();
	    return false;
	  }

	  if (/[^0-9A-Za-z]+/.test($("reprg").value)) {
	    if (assine.currentStep != 2) assine.showStep(2);
	    blinkField("lbl","reprg","O campo RG do representante deve conter somente números e letras.");
	    blinkField("lbl","identificacao","");
		blinkField("lbl","replegal","");
	    $("reprg").focus();
		$("reprg").select();
	    return false;
	  }
  }

  if ($("endereco").value.isWhite()) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","endereco","O endereço não pode ser vazio.");
    blinkField("lbl","localizacao","");
    $("endereco").focus();
    return false;
  }

  if ($("numero").value.isWhite()) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","numero","O número não pode ser vazio.");
    blinkField("lbl","localizacao","");
    $("numero").focus();
    return false;
  }

  if ($("bairro").value.isWhite()) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","bairro","O bairro não pode ser vazio.");
    blinkField("lbl","localizacao","");
    $("bairro").focus();
    return false;
  }

  if ($("cidade").value.isWhite()) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","cidade","A cidade não pode ser vazia.");
    blinkField("lbl","localizacao","");
    $("cidade").focus();
    return false;
  }
  
  if ($("estado").value == "") {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","estado","O estado não pode ser vazio.");
    blinkField("lbl","localizacao","");
    $("estado").focus();
    return false;
  }
  if ($("cep").value.isWhite()) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","cep","O cep não pode ser vazio.");
    blinkField("lbl","localizacao","");
    $("cep").focus();
    return false;
  }

  if (/[^0-9]+/.test($("cep").value)) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","cep","O cep deve conter somente números.");
    blinkField("lbl","localizacao","");
    $("cep").focus();
    $("cep").select();
    return false;
  }

  if ($("cep").value.length != 8) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","cep","O cep deve conter 8 caracteres.");
    blinkField("lbl","localizacao","");
    $("cep").focus();
    $("cep").select();
    return false;
  }

  if ($("responsavel").value.isWhite()) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","responsavel","O responsavel não pode ser vazio.");
    blinkField("lbl","contato","");
    $("responsavel").focus();
    return false;
  }

//  if ($("aniversario").value.isWhite() || $("aniversario").value == "  /  /    ") {
//    if (assine.currentStep != 2) assine.showStep(2);
//    blinkField("lbl","aniversario","O aniversário não pode ser vazio.");
//    blinkField("lbl","contato","");
//    $("aniversario").focus();
//    return false;
//  }

//  if ($("aniversario").value.replace(/\/|\s/g,"").length != 8) {
//    if (assine.currentStep != 2) assine.showStep(2);
//    blinkField("lbl","aniversario","O aniversário deve conter 8 digitos.");
//    blinkField("lbl","contato","");
//    $("aniversario").focus();
//    return false;
//  }

//  if (!(/^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/).test($("aniversario").value)) {
//    if (assine.currentStep != 2) assine.showStep(2);
//    blinkField("lbl","aniversario","O aniversário não é uma data válida.");
//    blinkField("lbl","contato","");
//    $("aniversario").focus();
//    return false;
//  }

  if ($("ddd").value.isWhite()) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","telefone","O DDD não pode ser vazio.");
    blinkField("lbl","contato","");
    $("ddd").focus();
    return false;
  }

  if (/[^0-9]+/.test($("ddd").value)) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","telefone","O DDD deve conter somente números.");
    blinkField("lbl","contato","");
    $("ddd").focus();
    $("ddd").select();
    return false;
  }

  if ($("ddd").value.length != 2) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","telefone","O DDD deve conter 2 caracteres.");
    blinkField("lbl","contato","");
    $("ddd").focus();
    $("ddd").select();
    return false;
  }

  if ($("telefone").value.isWhite()) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","telefone","O telefone não pode ser vazio.");
    blinkField("lbl","contato","");
    $("telefone").focus();
    return false;
  }

 if (/[^0-9]+/.test($("telefone").value)) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","telefone","O telefone deve conter somente números.");
    blinkField("lbl","contato","");
    $("telefone").focus();
    $("telefone").select();
    return false;
  }

  if ($("telefone").value.length != 8) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","telefone","O telefone deve conter 8 caracteres.");
    blinkField("lbl","contato","");
    $("telefone").focus();
    $("telefone").select();
    return false;
  }

  if (!$("msn").value.isWhite() && !validateEmail($("msn").value)) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","msn","Endereço de Msn inválido.");
    blinkField("lbl","contato","");
    $("msn").focus();
    return false;
  }

  if ($("email").value.isWhite()) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","email","O email não pode ser vazio.");
    blinkField("lbl","contato","");
    $("email").focus();
    return false;
  }

  if (!validateEmail($("email").value)) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","email","Endereço de email inválido.");
    blinkField("lbl","contato","");
    $("email").focus();
    return false;
  }

  if ($("email2").value.isWhite()) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","email2","O outro email não pode ser vazio.");
    blinkField("lbl","contato","");
    $("email2").focus();
    return false;
  }

  if ($("email2").value == $("email").value) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","email2","O outro email não pode ser igual ao primeiro email.");
    blinkField("lbl","contato","");
    $("email2").focus();
    return false;
  }

  if (!validateEmail($("email2").value)) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","email2","Endereço de email inválido.");
    blinkField("lbl","contato","");
    $("email2").focus();
    return false;
  }

  if ($("email3").value.isWhite()) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","email3","O email cobrança não pode ser vazio.");
    blinkField("lbl","contato","");
    $("email3").focus();
    return false;
  }

  if (!validateEmail($("email3").value)) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","email3","Endereço de email inválido.");
    blinkField("lbl","contato","");
    $("email3").focus();
    return false;
  }

  if ($("email4").value.isWhite()) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","email4","O outro email cobrança não pode ser vazio.");
    blinkField("lbl","contato","");
    $("email4").focus();
    return false;
  }

  if ($("email4").value == $("email3").value) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","email4","O outro email cobrança não pode ser igual ao primeiro email de cobrança.");
    blinkField("lbl","contato","");
    $("email4").focus();
    return false;
  }

  if (!validateEmail($("email4").value)) {
    if (assine.currentStep != 2) assine.showStep(2);
    blinkField("lbl","email4","Endereço de email inválido.");
    blinkField("lbl","contato","");
    $("email4").focus();
    return false;
  }

  return true;
}

function ValidatorS3() {
  $("lblcontrato").style.textDecoration = "none";
  if (!$("aceito").checked) {
    if (assine.currentStep != 3) assine.showStep(3);
    blinkField("lbl","contrato","Você deve ter lido e aceitado os termos do contrato para continuar.");
    $("aceito").focus();
    return false;
  }
  return true;
}
