﻿function jgoRequest(TYP, URL, PARAMs, XSL, DIV, SUCCESS, FAILURE) {
    var XHR;
    jsoMessage(DIV, "Images/ajaxLoader.gif", "資料載入中........");

    if (window.XMLHttpRequest) {
        XHR = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        try {
            XHR = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (err_101) {
            try {
                XHR = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (err_102) {
            alert(err_102);
            return;
             }
        }
    }

    try {
        XHR.open('POST', URL);
        if (TYP == "html") {
            PARAMs += "&urlxsl=" + XSL;
            XHR.onreadystatechange = function() { jgoHtml(XHR, DIV) };
        }
        else {            
            XHR.onreadystatechange = function() { jgoReceive(XHR, XSL, DIV, SUCCESS, FAILURE) };
        }        
        
        XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        XHR.send(PARAMs);
    }
    catch (err) {
        jsoMessage(DIV, "../Images/003.gif", "無法開啟連線! (" + err + ")");
    }
}

function jgoHtml(XHR, DIV) {
    if ((XHR.readyState == 4) && (XHR.status == 200)) {
        var DTL = document.getElementById(DIV);
        DTL.innerHTML = XHR.responseText;

//        document.getElementById("HTML").value += "<!-- ===== " + DIV + " ========================================== -->\r\n"
//        document.getElementById("HTML").value += DTL.innerHTML;
//        document.getElementById("HTML").value += "\r\n";
//        document.getElementById("HTML").value += "<!-- ***** " + DIV + " ****************************************** -->\r\n"
    }
}

function jgoReceive(XHR, XSL, DIV, SUCCESS, FAILURE) {
    switch (XHR.readyState) {
        case 0:
            window.status = "0: Begin..." + "(" + DIV + ")";
            break;
        case 1:
            window.status = "1: Initialize..." + "(" + DIV + ")";
            break;
        case 2:
            window.status = "2: Send..." + "(" + DIV + ")";
            break;
        case 3:
            window.status = "3: Received..." + "(" + DIV + ")";
            break;
        case 4:
            window.status = "4: Finished" + "(" + DIV + ")";
            if (XHR.status == 200) {
                jgoResponse(XHR, XSL, DIV, SUCCESS, FAILURE)
                jsoCompleted(SUCCESS);
                window.status = "Completed." + "(" + DIV + ")";
            }
            else {
                jsoCompleted(FAILURE);
                window.status = "Error: " + XHR.status + "(" + DIV + ")";
            }
            break;
        default:
            window.status = XHR.readyState + "(" + DIV + ")";
            break;
    }
}

function jgoResponse(XHR, XSL, DIV, SUCCESS, FAILURE) {
    try {
        var DTL = document.getElementById(DIV);
        if (XHR.responseXML.xml == '') {
            DTL.innerHTML = XHR.responseText;
        }
        else
        {
            try {
                var xslDoc = document.implementation.createDocument("", "", null);
                xslDoc.async = false;
                xslDoc.load(XSL);
                var xsltProcessor = new XSLTProcessor();
                xsltProcessor.importStylesheet(xslDoc);
                DTL.innerHTML = "";
                DTL.appendChild(xsltProcessor.transformToFragment(XHR.responseXML, document));
            }
            catch (err_201) {
                DTL.setAttribute("XSLT", new ActiveXObject("MSXML.DOMDocument"));
                DTL.XSLT.async = false;
                DTL.XSLT.load(XSL);
                try {
                    DTL.innerHTML = XHR.responseXML.transformNode(DTL.XSLT);
                }
                catch (err_202) {
                    alert("err_202: " + DIV + ", " + err_202);
                }
            }
        }
    }
    catch (err) {
        window.status = DIV + ": " + err;
    }

    try {
        var RUNFUN = document.getElementById("RUNFUN");
        if (RUNFUN != null) {
            if (RUNFUN.value == "") return;
            eval(RUNFUN.value);
            RUNFUN.value = "";
            RUNFUN.removeNode();
        }
    }
    catch (err) {
        window.status = "RUNFUN: " + err;
    }

    try {
        var DEBUG = document.getElementById("DEBUG");
        if (DEBUG != null) {
            if (DEBUG.style.display != "none") {
                document.getElementById("XSL").value = XSL;
                document.getElementById("DIV").value = DIV;
                document.getElementById("XML").value = XHR.responseText;
                document.getElementById("HTML").value = DTL.innerHTML;
            }
        }
    }
    catch (err) {
        window.status = "DEBUG: " + err;
    }
}

function jsoCompleted(SCRIPT) {
    if (SCRIPT == null) return;
    if (SCRIPT == "") return;
    try {
        eval(SCRIPT);
    }
    catch (err) {
        window.status = err;
    }
}

function jsoMessage(DIV, IMG, TXT) {
    var txt = "<table width='100%'><tr><td align='right'><img width='32' height='32' src='" + IMG + "'/></td><td valign='baseline'><h3>" + TXT + "</h3></td></tr></table>";
    var DTL = document.getElementById(DIV);
    DTL.innerHTML = txt;
}