// Cupom.js

function Cupom() {
  
  // Declaração de váriaveis privadas
  this.value      = null;
  this.type       = null;
  this.amount     = null;
  this.frequency  = null;
  this.id         = null;
  this.confirmado = false;
  this.validating = false;
  this.lastValue  = null;
  
  this.validate = function (obj) {
  	if (obj.value != this.id) {
      this.validating = true;
      $("eCupom").style.display = "";
      $("eCupom").innerHTML = "<span style=\"color: #006666;\">Validando cupom...</span>";
      $("periodo").disabled = false;

      this.lastValue = obj.value;
      this.id = null;
      this.confirmado = false;
      getCupom(obj.value.toLowerCase());
  	}
  }
  
  this.processValidate = function (resp) {

    var rs = new ResultSet();
    if (rs.mount(resp,"ajax-response")) {

      if (rs.next()) {
      	
        this.validating = false;
        $("eCupom").style.display = "";
        $("periodo").disabled = false;
        
        if (rs.getValue(0) != "true") {
          $("eCupom").innerHTML = "<span style=\"color: red; text-decoration: blink\">Cupom inexistente!</span> <strong>(Insira um cupom válido ou deixe o campo vazio)</strong>";
          this.cleanCupom();
          assine.forwardWaited(2);
          return false;
        }

        if (rs.getValue(1) == "true") {
          $("eCupom").innerHTML = "<span style=\"color: red; text-decoration: blink\">Cupom vencido!</span> <strong>(Insira um cupom válido ou deixe o campo vazio)</strong>";
          this.cleanCupom();
          assine.forwardWaited(2);
          return false;
        }

        if (rs.getValue(2) != "true") {
          $("eCupom").innerHTML = "<span style=\"color: red; text-decoration: blink\">Cupom inativo!</span> <strong>(Insira um cupom válido ou deixe o campo vazio)</strong>";
          this.cleanCupom();
          assine.forwardWaited(2);
          return false;
        }  

        var periodos = {
          m: "mensais",
          t: "trimestrais",
          s: "semestrais",
          a: "anuais"
        };
        
        $("eCupom").innerHTML = "<span style=\"color: green\">Código aceito!</span> Este cupom promocional lhe dá o desconto de <span style=\"color: green\">" + (rs.getValue(4)=="v"?"R$":"") + rs.getValue(3) + (rs.getValue(4)=="p"?"%":"") + "</span> em <span style=\"color: red\">" + rs.getValue(5) + " fatura" + (rs.getValue(5)>1?"s":"") + "</span>." + (!rs.getValue(6).isWhite()?" Entretanto, ele só é válido para faturas <span style=\"color: red\">" + periodos[rs.getValue(6)] + "</span>.":"");
        this.value = rs.getValue(3);
        this.type = rs.getValue(4);
        this.amount = rs.getValue(5);
        this.frequency = rs.getValue(6);
        this.id = this.lastValue;
        this.confirmado = true;

		if (!rs.getValue(6).isWhite()) {
          for (i = 0; i < $("periodo").length; i++) {
            if ($("periodo").options[i].value == rs.getValue(6)) {
              $("periodo").options[i].selected = true;
              $("periodo").disabled = "disabled";
            }
          }
		}

        assine.forwardWaited(2);
            
      }
  
    } else alert("ResultSet erro: " + rs.getErro());
    rs.close();
    
    this.validating = false;
  }
  
  this.cleanCupom = function() {
    this.value = "";
    this.type = "";
    this.amount = "";
    this.frequency = "";
    this.id = "";
    this.confirmado = false;
  }
  
  this.cupomFieldChanged = function() {
  	if ($("cupom").value == "" || $("cupom").value != assine.cupom.lastValue) { 
  		$('eCupom').style.display = "none"; 
  		$('periodo').disabled = false; 
  	}
  }
}