var ajax = {};
ajax.xhr = {};

ajax.xhr.Request = function(url, params, callback, method) {
  this.url = url;
  this.params = params;
  this.callback = callback;
  this.method = method;
  this.send();
}

ajax.xhr.Request.prototype = {
  //ºê¶ó¿ìÀú¿¡ µû¶ó XMLHttpRequest °´Ã¼¸¦ »ý¼ºÇØ ÁÖ´Â ÇÔ¼ö
  getXMLHttpRequest: function() {
    if (window.ActiveXObject) {
      //IE¿¡¼­ XMLHttpRequest °´Ã¼ ±¸ÇÏ±â 
      try {
        return new ActiveXObject("MSXML2.XMLHTTP");
      } catch (e) {
        try {
          return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e1) {
          return null;
        }
      }
    } else if (window.XMLHttpRequest) {
      //IE¸¦ Á¦¿ÜÇÑ ÆÄÀÌ¾î Æø½º, ¿ÀÆä¶ó¿Í °°Àº ºê¶ó¿ìÀú¿¡¼­ XMLHttpRequest °´Ã¼¸¦ ±¸ÇÑ´Ù.
      return new XMLHttpRequest;
    }
    
    return null;
  },

//XMLHttpRequest¸¦ »ç¿ëÇØ¼­ ÁöÁ¤µÈ URL·Î ÁöÁ¤µÈ ¿äÃ» ÀÎÀÚ¸¦ 
//Àü¼Û¹æ½Ä(GET/POST)À¸·Î  À¥ ¼­¹ö¿¡ ¿äÃ»À» Àü¼ÛÇÑ´Ù.
//¼­¹öÀÇ ÀÀ´ä °á°ú´Â callback À¸·Î ÁöÁ¤µÈ ÄÝ¹éÇÔ¼ö¸¦ È£ÃâÇÑ´Ù.
  send : function () {
    this.httpRequest = this.getXMLHttpRequest();
    //Àü¼Û¹æ½ÄÀÌ »ý·«µÈ °æ¿ì ±âº»À¸·Î  GET ¹æ½ÄÀ¸·Î ¼³Á¤ÇÑ´Ù.
    var httpMethod = this.method ? this.method : 'GET';
    
    // Àü¼Û ¹æ¹ýÀÌ GET/POSTÀÌ¿Ü´Â  ¹«Á¶°Ç GET ¹æ½ÄÀ¸·Î ¼³Á¤ÇÑ´Ù.
    if (httpMethod != 'GET' && httpMethod != 'POST') {
      httpMethod = 'GET';
    }
    
    //¿äÃ» ÀÎÀÚÀÇ ±âº»°ªÀ» ¼³Á¤ÇÑ´Ù.
    var httpParams = "";
    if (this.params != null && this.params != '') {
      for (var key in this.params) {
        if (httpParams == "") {
          httpParams=key+'='+encodeURIComponent(this.params[key]);
        } else {
          httpParams+='&'+key+'='+encodeURIComponent(this.params[key]);
        }
      }
    }
      
    var httpUrl = this.url;
    //Àü¼Û ¹æ¹ýÀÌ GET ¹æ¹ýÀÌ¸é¼­ ¿äÃ»ÀÎÀÚ°¡ Á¸ÀçÇÒ °æ¿ì URLµÚ¿¡ 
    //¿äÃ»ÀÎÀÚ¸¦ Ãß°¡ÇÑ´Ù.
    if (httpMethod == 'GET' && httpParams != "") {
      httpUrl = httpUrl + "?" + httpParams;
    }
    
    //Àü¼Û ¹æ¹ý°ú URLÀ» ¼³Á¤ÇÑ´Ù.
    this.httpRequest.open(httpMethod, httpUrl, true);
    
    //Àü¼Û ¹æ¹ýÀÌ POSTÀÌ¸é Àü¼ÛÇÒ ÄËÅÙÃ÷ÀÇ Å¸ÀÔÀ» ÁöÁ¤ÇÑ´Ù.
    if (httpMethod == 'POST') {
      this.httpRequest.setRequestHeader('Content-Type', 
                               'application/x-www-form-urlencoded');
    }
    
    //readyState ¼Ó¼ºÀÌ º¯°æµÉ¶§¸¶´Ù È£ÃâµÉ ÄÝ¹éÇÔ¼ö¸¦ ÁöÁ¤ÇÑ´Ù.
    var localThis = this;
    this.httpRequest.onreadystatechange = function () {
      //»ç¿ëÀÚ°¡ Á¤ÀÇÇÑ ÄÝ¹éÇÔ¼ö¸¦ È£ÃâÇÑ´Ù.
      localThis.callback(localThis.httpRequest);

    }

//  this.httpRequest.onreadystatechange = this.callback;
//  callback() ÇÔ¼ö ¾È¿¡¼­ 
//  httpRequest¿¡´ëÇÑ Á¤º¸¸¦ ¾òÀ» ¼ö ¾ø´Ù.
  
    
//  this.httpRequest.onreadystatechange = function () {
//    this.callback(this.httpRequest);
//  }
  
  
    
    //Àü¼Û¹æ¹ýÀÌ POSTÀÌ¸é ¿äÃ»ÀÎÀÚ¸¦ send()ÀÇ ÀÎÀÚ·Î Àü´ÞÇÑ´Ù.
    this.httpRequest.send(httpMethod == 'POST' ? httpParams : null);
  
  }
}

