var LlForm = function(obj) {
  this.setup(obj);
}

LlForm.prototype = {
  setup: function(pars) {
    this.type = pars.type || 'msg';
    this.msg = pars.msg;
    this.w = pars.w;
    this.show = pars.show || '0';
    this.btn_cancel = pars.btn_cancel;
    this.btn_submit = pars.btn_submit;
    this.method = pars.a || "post";
    this.form = pars.form;
    this.action = pars.action;
    this.success = pars.success;
    this.loading = pars.loading;
    this.submit = pars.submit || null; // submit function
  },
  
  popup: function() {
    var THIS = this;
    alert({
      msg: this.msg,
      w: this.w,
      show: this.show,
      btn_cancel: this.btn_cancel
    });
    
    document.getElementById(this.btn_submit).onclick = function() {
      if(THIS.submit != null) {
        if(THIS.submit() == false) return;
      }
      var xmlHttp = new XMLHttpRequest();
      xmlHttp.open(THIS.method, THIS.action, true);
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState == 4) {
          responseText = xmlHttp.responseText;
          THIS.success(responseText);
        } else {
          THIS.loading();
        }
      }
      xmlHttp.send($('#' + THIS.form).serialize());   
    };
  }
}
