// JScript source code
var ajax = {};
ajax.xhr = {};

// XMLHttpRequest »ý¼ºÇÏ±â À§ÇÑ Request Å¬·¡½ºÀÇ »ý¼ºÀÚ ÇÔ¼ö.
// url : ¼­¹ö url
// params : 
// callback : callback ÇÔ¼ö¸í
// method : È£Ãâ¹æ½Ä (GET, POST - Default GET¹æ½Ä)
ajax.xhr.Request = function(url, params, callback, method)
{
    this.url = url;
    this.params = params;
    this.callback = callback;
    this.method = method;
    
    this.send();
}


// ºê¶ó¿ìÀú¿¡ µû¶ó XMLHttpRequest °´Ã¼¸¦ »ý¼ºÇØ ÁÖ´Â ÇÔ¼ö getXMLHttpRequest ¸â¹öÇÔ¼ö¿Í send ¸â¹öÇÔ¼ö Á¤ÀÇ
ajax.xhr.Request.prototype = {   
    getXMLHttpRequest : function() 
    {
        if (window.ActiveXObject) 
        {       
            try 
            {
                return new ActiveXObject("MSXML2.XMLHTTP");
            } 
            catch (e) 
            {
                try 
                {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e1) 
                {
                    return null;
                }
            }
        }
        else if (window.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);
		}

		// Àü¼Û ¹æ½ÄÀÌ POSTÀÌ¸é ¿äÃ» ÀÎÀÚ¸¦ send() ÀÇ ÀÎÀÚ·Î Àü´ÞÇÑ´Ù.
		this.httpRequest.send(httpMethod == 'POST' ? httpParams : null);
	}
}



