/*
Believe it or not, but this is all the code needed for
a DSO to be replaced by an XML data island.
    
    
* To make the change, replace your DSO object (called CCICDSO ) with the following tag.
<xml id="CCICDSO" sql=""></xml>
    
* Include this script file, and where you find the VBScript code: -
CCICDSO.Refresh
        
replace it with 
DSORefresh CCICDSO
    
JavaScript is similar. The difference is
DSORefresh(CCICDSO);
    
    
* Also change "CCICDSO.Recordset.EOF" to "DSOEOF(CCICDSO)".
    
    
*/

function GetMSXmlHttp() {
    var xmlHttp = null;
    var clsids = ["Microsoft.XMLHTTP.1", "Msxml2.XMLHTTP.6.0",
                     "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0",
                     "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP.2.6",
                     "Microsoft.XMLHTTP.1.0", "Microsoft.XMLHTTP"];
    for (var i = 0; i < clsids.length && xmlHttp == null; i++) {
        xmlHttp = CreateXmlHttp(clsids[i]);
    }
    return xmlHttp;
}

function CreateXmlHttp(clsid) {
    var xmlHttp = null;
    try {
        xmlHttp = new ActiveXObject(clsid);
        lastclsid = clsid;
        return xmlHttp;
    }
    catch (e) { }
}

function DSORefresh(XMLObj) {
    var objHTTP = GetMSXmlHttp();
    var strResponse = "";

    objHTTP.Open("POST", "functions.asp?Query=QRY", false);
    objHTTP.Send(XMLObj.sql);
    if ((objHTTP.readyState == 4) || (objHTTP.readyState == 'complete')) {
        // Get rid of any XML rubbish in the response.
        strResponse = objHTTP.responseText.replace(' xmlns:sql="urn:schemas-microsoft-com:xml-sql"', '');
        // Get rid of Norton pop-up protection code inserted into the response.
        strResponse = strResponse.replace('\x00A<script language="JavaScript">\x00A<!--\x00A\x00Afunction SymError()\x00A{\x00A  return true;\x00A}\x00A\x00Awindow.onerror = SymError;\x00A\x00Avar SymRealWinOpen = window.open;\x00A\x00Afunction SymWinOpen(url, name, attributes)\x00A{\x00A  return (new Object());\x00A}\x00A\x00Awindow.open = SymWinOpen;\x00A\x00A//-->\x00A</script>\x00A\x00A', '');
        strResponse = strResponse.replace('<script language="JavaScript">\x00A<!--\x00Avar SymRealOnLoad;\x00Avar SymRealOnUnload;\x00A\x00Afunction SymOnUnload()\x00A{\x00A  window.open = SymWinOpen;\x00A  if(SymRealOnUnload != null)\x00A     SymRealOnUnload();\x00A}\x00A\x00Afunction SymOnLoad()\x00A{\x00A  if(SymRealOnLoad != null)\x00A     SymRealOnLoad();\x00A  window.open = SymRealWinOpen;\x00A  SymRealOnUnload = window.onunload;\x00A  window.onunload = SymOnUnload;\x00A}\x00A\x00ASymRealOnLoad = window.onload;\x00Awindow.onload = SymOnLoad;\x00A\x00A//-->\x00A</script>\x00A', '');
        // get rid of Norton pop-up protection code inserted into the response.
        strResponse = strResponse.replace('<script language="JavaScript">\x00A<!--\x00A\x00Afunction SymError()\x00A{\x00A  return true;\x00A}\x00A\x00Awindow.onerror = SymError;\x00A\x00A//-->\x00A</script>', '');

        if (!XMLObj.loadXML(strResponse)) {
            if ((strResponse == "<?xml version=\"1.0\"?>") || (strResponse == "")) {
                // Empty resultset, just return true.
                return true;
            } else {
                try {
                    eval(strResponse);
                } catch (err) {
                    alert("Error code: " + XMLObj.parseError.errorCode + "\nError reason: " + XMLObj.parseError.reason + "\nError line: " + XMLObj.parseError.line);
                    alert('The server returned an invalid response to a data request from this page. The response was:\n' + strResponse);
                }
                XMLObj.loadXML('');
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
} // end DataRefresh

function DSOEOF(XMLObj) {
    if (XMLObj == null) {
        return true;
    } else if (XMLObj.recordset.state == 0) {
        return true;
    } else if (XMLObj.recordset.EOF) {
        return true;
    } else {
        return false;
    }
} // end DSOEOF

function populateSelect(poDSO, poSelect, psQuery, psValueFieldName, psDescFieldName, pbIncl) {
    var oOption = null;
    var bRetVal = false;

    //		alert(psQuery);
    poDSO.sql = psQuery;

    DSORefresh(poDSO);
    // clear out the old items.
    while (poSelect.options.length > 0)
        poSelect.remove(0);

    if (pbIncl) {
        oOption = document.createElement('OPTION');
        oOption.text = '<Click to select>';
        oOption.value = 'Null';
        poSelect.add(oOption);
    }

    // populate the new ones.
    while (!DSOEOF(poDSO)) {
        bRetVal = true;
        try {
            oOption = document.createElement('OPTION');
            oOption.text = poDSO.recordset(psDescFieldName);
            oOption.value = '\'' + poDSO.recordset(psValueFieldName) + '\'';
            poSelect.add(oOption);
        } catch (e) {
            alert('Error loading combo box:\n\n' + e.message);
        }
        try {
            poDSO.recordset.moveNext();
        } catch (e) {
            alert('Error getting next combo box row:\n\n' + e.message);
            return false;
        }
    }

    return bRetVal
}

