/*********************************************************
 * Hamt ajax module

 사용법

 aj = new ajax('aj');
 aj2 = new ajax('aj');


 aj.loadXMLDoc('url');
 aj2.loadXMLDoc('url2');

 onload 될때 2개 이상을 동시에 ajax로 채울때 개체별로 명령하면 둘중 하나 씹히는 현상을 없앨 수 있다
 */
var Ajax = function(objname)
{
	this.objname = objname;
	this.req;
	this.debug_mode = false;

	this.loadXMLDoc = function(url) {

		this.req = false;
		if (window.XMLHttpRequest) {
			try {
				this.req = new XMLHttpRequest();
			}
			catch(e) {
				this.req = false;
			}

		} else if (window.ActiveXObject) {
			try {
				this.req = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) {
				try {
					this.req = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e) {
					this.req = false;
				}
			}

		}
		if (this.req) {
			var str = ''; str += 'function(){\n';
			str += ' if('+this.objname+'.req.readyState == 4){\n';
			str += '  if('+this.debug_mode+'){\n';
			str += '    document.write('+this.objname+'.req.responseText);\n';
			str += '  }\n';
			str += '  eval('+this.objname+'.req.responseText);\n';
			//          str += '  alert('+ this.objname +'.req.responseText);';
			str += ' }\n';
			str += '}';

			eval(this.objname+ '.getdata = ' + str);
			this.req.onreadystatechange = this.getdata;

			this.req.open("GET", url, true);
			this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=EUC-KR");
			this.req.send("");
		}
	}
		this.loadXMLDocPost = function(url, queryString) {
			this.req = false;
			if (window.XMLHttpRequest) {
				try {
					this.req = new XMLHttpRequest();
				}
				catch(e) {
					this.req = false;
				}
			}
			else if (window.ActiveXObject) {
				try {
					this.req = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e) {
					try {
						this.req = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e) {
						this.req = false;
					}
				}
			}
			if (this.req) {
				this.req.open("POST", url, true);
				var str = ''; str += 'function(){\n';
				str += ' if('+this.objname+'.req.readyState == 4){\n';
				str += '  if('+this.debug_mode+'){\n';
				str += '    document.write('+this.objname+'.req.responseText);\n';
				str += '  }\n';
				str += '  eval('+this.objname+'.req.responseText);\n';
				//          str += '  alert('+ this.objname +'.req.responseText);';
				str += ' }\n';
				str += '}';

				eval(this.objname+ '.getdata = ' + str);
				this.req.onreadystatechange = this.getdata;
				this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; Charset=utf-8");
				this.req.send(queryString);
			}
		}
	}




// *********************************************************
// AJAX
// *********************************************************

var READY_STATE_UNINITIALIZED = 0;
var READY_STATE_LOADING = 1;
var READY_STATE_LOADED = 2;
var READY_STATE_INTERACTIVE = 3;
var READY_STATE_COMPLATE = 4;
var req = null;

function initXMLHTTPRequest() {
    var xRequest = null;

    if (window.XMLHttpRequest) {
        xRequest = new XMLHttpRequest();
    }
    else if (typeof ActiveXObject != "undefined") {
        try {
            xRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (err) {
            try {
                xRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (err) {
            }
        }
    }

    return xRequest;
}

function sendRequest(URL, params, HttpMethod) {
    if (!HttpMethod) {
        HttpMethod = "POST";
    }
    
    req = initXMLHTTPRequest();

    if (req) {
        req.open(HttpMethod, URL, false);       // (true:비동기 false:동기)
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        req.send(params);

        if (req.readyState == READY_STATE_COMPLATE) {
            if (req.status == 200) {
                return req.responseText;
            }
        }
    }
}
