var areaXML;
areaXML = new ActiveXObject("MSXML2.DOMDocument");
areaXML.async = false;
areaXML.load("/branchInfo.xml");

var list = areaXML.getElementsByTagName("list");
var depth1 = areaXML.getElementsByTagName("depth1");
var depth2 = areaXML.getElementsByTagName("depth2");

var listElement;
var depth1Element;
var depth2Element;

var debugEngine = false;
var engine;

function Engine()
{
    this.xmlhttp = false;
    this.type    = null;
    this.src     = null;
    this.param   = null;
    this.exec    = null;
    this.ifrcnt  = 0;
    this.iswait  = false; //µ¿½Ã¿¡ µÎ°³°¡ È£ÃâµÇ¾ú´Ù¸é
    this.isxml   = false;

    /*
     * AJAX ±âº» Å¬·¡½º ÇÔ¼ö
     * type : GET, AGET, POST, IPOST, IGET
     *        GET : µ¿±â½Ä GET                (ºñÃßÃµ)
              AGET : ºñµ¿±â½Ä GETÇü        (ÃßÃµ)
              POST : ºñµ¿±â½Ä POSTÇü        (ÃßÃµ)
             IPOST : µ¿±â½Ä IFRAME Çü    (ºñÃßÃµ)
             IGET : µ¿±â½Ä GETÇü            (ºñÃßÃµ)
     * src : URL
     * param : POST¹æ½Ä¿¡¼­ »ç¿ëµÊ
     * exec : ¸®ÅÏÇÒ ÇÔ¼ö¸í
     */
    this.execute = function(type, src, param, exec, isxml)
    {
        //alert('type : '+type+' src : '+ src+' param : '+param + 'exec : '+ exec + ' isxml : ' + isxml);
        this.type = type;
        this.src = src;
        this.param = param;
        this.exec = exec;
        this.getXmlHttpRequest();
        this.iswait = true;
        this.isxml = isxml;
                
        try    
        {            
            return this._execute();
        }
        catch (e)
        {            
            this.errorHandle('µ¥ÀÌÅÍ¿¡ Á¢±ÙÇÒ ¼ö ¾ø½À´Ï´Ù.');            
        }        
    }


    this.getXmlHttpRequest = function()
    {            
        if(this.iswait == true)
        {
            setTimeout("engine.getXmlHttpRequest();",200);
            return;
        }
        else
        {            
            if(window.XMLHttpRequest)            
            {
                this.xmlhttp = new XMLHttpRequest();
            }
            else if (window.ActiveXObject && !(navigator.userAgent.indexOf('Mac') >= 0 && navigator.userAgent.indexOf("MSIE") >= 0))
            {
                this.xmlhttp = this._ActiveXObject(["Microsoft.XMLHTTP","Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP"]);
            }
        }
    }


    this._ActiveXObject = function (axarray)
    {
        var returnValue; 
        
        for (var i = 0; i < axarray.length; i++)
        {
            try
            {                
                returnValue = new ActiveXObject(axarray[i]);
                break;
            }
            catch (ex)
            {
            }
        }

        return returnValue;
    }



    // GET, AGET, POST Çü½Ä
    this._execute = function()
    {            

        if(this.type == 'GET' || this.type == 'AGET')
        {
            this.xmlhttp.open("GET",this.src, this.type == 'GET' ? false : true); 
        }
        else if(this.type == 'POST')
        {
            this.xmlhttp.open("POST",this.src, true);     
        }
        else if(this.type == 'IPOST' || this.type == 'IGET')
        {            
            this._executeIFR();
            return;
        }        


        this.xmlhttp.setRequestHeader("Content-type:", this.type == 'POST' ? "application/x-www-form-urlencoded" : "text/xml"); 
        this.xmlhttp.setRequestHeader("Cache-Control:", "no-cache"); 
        this.xmlhttp.setRequestHeader("Pragma:", "no-cache"); 
        this.xmlhttp.setRequestHeader("Referer:", this.src);
        //alert(this.bxml);
        if(this.type == 'AGET' || this.type == 'POST') // ºñµ¿±â½Ä GET, POST
        {
        	if(this.isxml == true) {            
	            this.xmlhttp.onreadystatechange = function()
	            {                
	                if(engine.xmlhttp.readyState == 4) 
	                {                     
	                    if(engine.xmlhttp.status == 200) 
	                    {                            
	                        var result;
	                        
	                        
                        	result = engine.xmlhttp.responseXML;
                   
	                        engine.debugPrint(result);                        
	                        if(engine.exec) eval(engine.exec +'(result);');
	                        engine.iswait = false;
	                    }
	                }
	            }
	        } else {
	        	this.xmlhttp.onreadystatechange = function()
	            {                
	                if(engine.xmlhttp.readyState == 4) 
	                {                     
	                    if(engine.xmlhttp.status == 200) 
	                    {                            
	                        var result;
	                        
	                        result = engine.xmlhttp.responseText;  
                  
	                        engine.debugPrint(result);                        
	                        if(engine.exec) eval(engine.exec +'(result);');
	                        engine.iswait = false;
	                    }
	                }
	            }
	        }
            
            if(this.type == 'AGET')             this.xmlhttp.send(null);
            else if(this.type == 'POST')        this.xmlhttp.send("message="+encodeURIComponent(this.param));
        }
        
        else if(this.type == 'GET') // µ¿±â½Ä GET
        {
            this.xmlhttp.send(null); 

            if (this.xmlhttp.status == 200) 
            {    
                this.debugPrint();
                this.iswait = false;
                return ( this.xmlhttp.responseText );
            }
        }
    }


    // IFRAME Çü½ÄÀÇ POST //
    this._executeIFR = function()
    {
        var idname = 'ul-ifr-no'+this.ifrcnt
        var xmlDiv = null;        var xmlIfr = null;    var xmlForm = null; var formInput = null;

        xmlDiv = document.createElement('div');
        if(debugEngine)
            xmlDiv.innerHTML = "<iframe frameborder='0' width='800' height='200' id='" + idname + "' name='" + idname + "'></iframe>";
        else
            xmlDiv.innerHTML = "<iframe frameborder='0' width='0' height='0' id='" + idname + "' name='" + idname + "'></iframe>";
        document.body.appendChild(xmlDiv);
    
		xmlIfr = document.getElementById(idname);
		xmlIfr.setAttribute('style', 'width:0px; height:0px; border:0px;');        
        
        if(this.type == 'IPOST')
        {            
            xmlForm = document.createElement('form');
            xmlForm.setAttribute('id', 'ul-form');
            xmlForm.setAttribute('action', this.src);
            xmlForm.setAttribute('target', idname);
            xmlForm.target = idname;
            xmlForm.setAttribute('method', 'post');

            formInput = document.createElement('input');
            formInput.setAttribute('type', 'hidden');
            formInput.setAttribute('name', 'info');
            formInput.setAttribute('value', this.param);
            xmlForm.appendChild(formInput);

            document.body.appendChild(xmlForm);
            xmlForm.submit();
        }
        else if(this.type == 'IGET')
        {
            xmlIfr.src = this.src;    
            document.body.appendChild(xmlIfr);
        }
        this.ifrcnt++;
    }


    // ¿¡·¯ÇÚµé·¯
    this.errorHandle = function(code)
    {
        alert(code);
    }


    // Debug //
    this.debugPrint = function(value)
    {
        if(debugEngine == 'div')
        {
        }
        else if(debugEngine == 'alert')
            alert(value);
        else
            return;
    }

}

