﻿var req;
var CurrentDIV;

function Initialize() {

    try {
        req = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (oc) {
            req = null;
        }
    }

    if (!req && typeof XMLHttpRequest != "undefined") {
        req = new XMLHttpRequest();
    }
}

function SendQuery(key, MyDiv, url, pleaseWait) {    
    if (key == null || key.length == 0) {
        document.getElementById(MyDiv).innerHTML = "";
        HideDiv(MyDiv);
        HideDiv(pleaseWait);
        return;
    }
    ShowDiv(pleaseWait);
    CurrentDIV = MyDiv;
    Initialize();
    var url = url + "&k=" + key + "&MyDiv=" + MyDiv;    
    if (req != null) {
        req.onreadystatechange = Process;
        req.open("GET", url, true);
        req.send(null);
    }
}


//checks is status was good when calling url for data lookup

function Process() {
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            if (req.responseText == "")
                HideDiv(CurrentDIV);
            else {
                //alert(req.responseText);
                ShowDiv(CurrentDIV);
                document.getElementById(CurrentDIV).innerHTML = req.responseText;
                HideDiv('spnPleaseWait');
            }
        }
        else {
            document.getElementById(CurrentDIV).innerHTML =
                           "There was a problem retrieving data:<br>" + req.statusText;
        }
    }
    
}


//Show Div Section in html
function ShowDiv(divid) {
    if (document.layers) document.layers[divid].display = "inline";
    else document.getElementById(divid).style.display = "inline";
}


//Hide Div Section in html
function HideDiv(divid) {
    if (document.layers) document.layers[divid].display = "none";
    else document.getElementById(divid).style.display = "none";
}


//Load selected value into textbox, and hide div
function SetTextbox(TextboxID, data, MyDiv) {
    document.getElementById(TextboxID).value = data;
    if (MyDiv.length > 0) {
        HideDiv(MyDiv);
    }
}