var MyAjax = Class.create();
MyAjax.prototype = {
  initialize: function(form_id) {
    this.form_id = form_id;
    var edit_form = $(form_id);
    var ff=navigator.userAgent.indexOf("Firefox")!=-1
    if(edit_form) {
      this.form_action = ff?(edit_form.action.split("/")[0]+"//"+edit_form.action.split("/")[2]+root+"/"+edit_form.action.split("/")[3]):(root+edit_form.action);
      this.edit_form = edit_form;
    }
  },

  /*sendForm: function(callback) {
    this.edit_form.isSuccess = true;
    if(typeof callback == 'function')
      this.callback = callback;
    var f_completed = this.completed.bind(this);

    var myAjax = new Ajax.Request(
      this.form_action,
      {
        parameters: Form.serialize(this.edit_form),
        onLoading: function() {
          //if(top.frames("top")) {
              //var obj_loading = top.frames("top").bottom_frame.document.getElementById("loading");
             // obj_loading.style.display = "block";
          //}
        },
        onLoaded: function() {
          //if(top.frames("top")) {
            //  var obj_loading = top.frames("top").bottom_frame.document.getElementById("loading")
             // obj_loading.style.display = "none";
          //}
        },
        onComplete: f_completed
      }
    );
  },*/
  sendForm: function(callback, rendered_id) {
    this.edit_form.isSuccess = true;
    if(typeof callback == 'function')
      this.callback = callback;
    if($(rendered_id))
      this.rendered_node = $(rendered_id);

    var f_completed = this.completed.bind(this);
    var myAjax = new Ajax.Request(
      this.form_action,
      {
        parameters: Form.serialize(this.edit_form),
        onLoading: function() {
          if(top) {
            var obj_loading = top.frames("top").document.getElementById("loading");
            obj_loading.style.display = "";
          }
        },
        onLoaded: function() {
          if(top) {
            var obj_loading = top.frames("top").document.getElementById("loading");
            obj_loading.style.display = "none";
          }
        },
        onComplete: f_completed
      }
    );
  },

  send: function(url, pars, callback) {
    var myAjax = new Ajax.Request(
      url,
      {
        method: 'post',
        parameters: pars,
        onComplete: callback
      }
    );
  },

  completed: function(originalRequest) {
    var contentType = originalRequest.getResponseHeader("Content-Type");
    if(contentType.indexOf("text/html") > -1 && this.rendered_node) {
      this.rendered_node.innerHTML = originalRequest.responseText;
    }
    if(contentType.indexOf("text/xml") > -1) {
      var xmlParser = new XMLParser(originalRequest.responseXML);
      var node = xmlParser.list("message");
	      var msg = xmlParser.get(node[0], "msg");
	      var msgType = xmlParser.get(node[0], "msgType");
	      var stack = xmlParser.get(node[0], "callStack");
	      var errorCode = xmlParser.get(node[0], "errorCode");

	      if(msgType == "SYSERR") {
	        var msgObj = new Object();
	        msgObj.text = msg;
	        msgObj.stack = stack;
	        msgObj.type = msgType;
	        this.edit_form.isSuccess = false;
	        window.showModalDialog(root+"/jsp/comm/error.jsp", msgObj, "dialogWidth:350px;dialogHeight:235px;scroll:no;status:no");
	        return;
	      } else {
	        if(msg.trim() != "")
	          alert(msg);
	        if(msgType == "BIZERR")
	          this.edit_form.isSuccess = false;
	        if(errorCode == "MSG_0001") {
	          top.location.replace(root+"/index.jsp");
	          return;
	        }
	      }
    }
    if(this.callback) {
      this.callback(originalRequest);
    }
  }
}