
var xmlActionURL = "";
var xmlReturnFunction = "processAjax()";
var xmlErrorFunction = "";
var xmlProcessId = 0;
var xmlHttp;
var xmlDoc;

function initializeURL(url) {
        xmlActionURL = url;
}

function initializeReturnFunction(func) {
        xmlReturnFunction = func;
}

function initializeErrorFunction(errfunc) {
        xmlErrorFunction = errfunc;
}

function createXMLHttpRequest() {
        if (window.ActiveXObject) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } else if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
        }
}

function doStartRequest(processid) {
        if (xmlProcessId != processid) {
                return;
        }
        createXMLHttpRequest();
        xmlHttp.onreadystatechange = callback;
        xmlHttp.open("GET", xmlActionURL, true);
        xmlHttp.send(null);
        // alert("" + processid + " ¹øÂ° ¿äÃ»Ã³¸®");
}

function startRequest() {
        // ´õºíÅ¬¸¯ or <select> ¿¡¼­ÀÇ ´Ù·®ÀÇ ¿äÃ»¿¡ ´ëÇÑ Ã³¸®
        // 200ms ÀÇ Å¸ÀÌ¸Ó¸¦ µÎ°í ÇØ´ç½Ã°£µ¿¾È ¿äÃ»ÀÌ ¾øÀ»¶§ ½ÇÁ¦ ¿äÃ» ½ÃÀÛ
        if (xmlProcessId >= 10000) {
                xmlProcessId = 1;
        } else {
                xmlProcessId = xmlProcessId + 1;
        }
        setTimeout("doStartRequest(" + xmlProcessId + ")", 200);
}

function callback() {
        if(xmlHttp.readyState == 4) {
                if(xmlHttp.status == 200) {
                        // Á¤»óÀûÀÎ µ¥ÀÌÅ¸ ¹ÝÈ¯
                        // ÀüÃ¼(TXT) : xmlHttp.responseText
                        if (window.ActiveXObject) {
                                // XML ·Î º¯È¯ÇÑ´Ù.
                                // ÅØ½ºÆ® ¾ÕºÎºÐ¿¡¼­ "<" ÀÌÀü ¹®ÀÚµéÀ» Á¦°ÅÇÑ´Ù.(°ø¹é¹®ÀÚ Á¦°Å¿ë,  ÀÌ·¸°Ô ¾ÈÇÏ¸é º¯È¯ÀÌ ¾ÈµÈ´Ù --)
                                xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
                                var rawXML = xmlHttp.responseText;
                                var filteredML;

                                var index = 0;
                                for (var i = 0; i < rawXML.length; i++) {
                                        if (rawXML.charAt(i) == "<") {
                                                index = i;
                                                break;
                                        }
                                }

                                filteredML = rawXML.substring(index);
                                xmlDoc.loadXML(filteredML);
                        } else if (window.XMLHttpRequest) {
                                xmlDoc = xmlHttp.responseXML;
                        }

                        eval(xmlReturnFunction);
                } else if (xmlHttp.status == 204){
                        // µ¥ÀÌÅÍ°¡ Á¸ÀçÇÏÁö ¾ÊÀ» °æ¿ì
                        if (xmlErrorFunction == "") {
                                alert("µ¥ÀÌÅ¸°¡ Á¸ÀçÇÏÁö ¾Ê½À´Ï´Ù.(CODE : " + xmlHttp.status + ")");
                        } else {
                                eval(xmlErrorFunction);
                        }
                } else if (xmlHttp.status == 500){
                        // ¿¡·¯¹ß»ý½Ã
                        if (xmlErrorFunction == "") {
                                alert("µ¥ÀÌÅ¸ ¼ö½ÅÁß ¿¡·¯°¡ ¹ß»ýÇÏ¿´½À´Ï´Ù.(CODE : " + xmlHttp.status + ")");
                        } else {
                                eval(xmlErrorFunction);
                        }
                }
        }
}

function onProcessAjax(div) {
		
		var TargetDiv = document.getElementById(div);
		TargetDiv.innerHTML = xmlHttp.responseText;
}

function onErrorProcessAjax() {
        alert("ERROR : " + xmlHttp.status);
}
function onErrorProcessAjax3() {
        alert("ÀÚµîµî·Ï¹æÁö°¡ Àß¸øÀÔ·ÂµÇ¾ú½À´Ï´Ù");
}
function onErrorProcessAjax2() {
        alert("ºñ¹Ð¹øÈ£°¡ Àß¸øÀÔ·ÂµÇ¾ú½À´Ï´Ù");
}
