﻿function getHTTPObjectHtml() {
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }
    return http_request;
}

var isBusy = false;
var gHttp = getHTTPObjectHtml();

function callAjax(pUrl, pDiv) {
    var lHttp = getHTTPObjectHtml();
    var lUrl = pUrl;
    lUrl = lUrl.replace("+", "%2b");

    if (lUrl.indexOf("?") == -1)
        lUrl += "?garbageTime=" + new Date().getTime();
    else
        lUrl += "&garbageTime=" + new Date().getTime();

    if (isBusy) {
        lHttp.onreadystatechange = function() { }
        lHttp.abort();
    }

    lHttp.open("GET", lUrl, false); // fire off a SYNCHRONOUS response
    lHttp.send(null);
    getHttpResponseText(pDiv, lHttp);
}

function callAjaxBackground(pUrl, pDiv) {
    var lHttp = getHTTPObjectHtml();
    var lUrl = pUrl;
    lUrl = lUrl.replace("+", "%2b");

    if (lUrl.indexOf("?") == -1)
        lUrl += "?garbageTime=" + new Date().getTime();
    else
        lUrl += "&garbageTime=" + new Date().getTime();

    if (isBusy) {
        lHttp.onreadystatechange = function() { }
        lHttp.abort();
    }

    lHttp.open("GET", lUrl, true);
    lHttp.onreadystatechange = function() { getHttpResponseText(pDiv, lHttp); };

    if (window.XMLHttpRequest) { // Mozilla check
        if (!isBusy) { // getting Javascript errors (only in Mozilla) this check prevents error
            isBusy = true;
            lHttp.send(null);
        }
    } else { // just proceed IE does not have issue!
        isBusy = true;
        lHttp.send(null);
    }
}

function callAjaxBatch(pUrl) {
    // dump output into a standard div included in the master page so that we could
    // obtain the output if we needed it for debugging, etc.
    callAjaxBackground(pUrl, 'batchOutput');
}

// use AJAX to submit a URL in the background with no response
function submitBatchBackground(pUrl) {
    var lHttp = getHTTPObjectHtml(); // create the HTTP Object
    lHttp.open("GET", pUrl, true);
    lHttp.send(null);

    return (lHttp);
}

function submitBatch(pUrl) {
    var lHttp = getHTTPObjectHtml(); // create the HTTP Object
    lHttp.open("GET", pUrl, false);
    lHttp.send(null);

    return (lHttp);
}

function callAjaxFunctionBackground(pUrl, pDiv, pFunc) {
    var lHttp = getHTTPObjectHtml();
    var lUrl = pUrl;
    lUrl = lUrl.replace("+", "%2b");

    if (isBusy) {
        lHttp.onreadystatechange = function() { };
        lHttp.abort();
    }

    lHttp.open("GET", lUrl, true);
    lHttp.onreadystatechange = function() { eval(pFunc) };

    if (window.XMLHttpRequest) { // Mozilla check
        if (!isBusy) { // getting Javascript errors (only in Mozilla) this check prevents error
            isBusy = true;
            lHttp.send(null);
        }
    } else { // just proceed IE does not have issue!
        isBusy = true;
        lHttp.send(null);
    }
}

function callAjaxFunction(pUrl, pDiv, pFunc) {
    var lHttp = getHTTPObjectHtml();
    var lUrl = pUrl;
    lUrl = lUrl.replace("+", "%2b");

    if (isBusy) {
        lHttp.onreadystatechange = function() { };
        lHttp.abort();
    }

    lHttp.open("GET", lUrl, false); // fire off a SYNCHRONOUS response
    lHttp.onreadystatechange = function() { eval(pFunc) };

    if (window.XMLHttpRequest) { // Mozilla check
        if (!isBusy) { // getting Javascript errors (only in Mozilla) this check prevents error
            isBusy = true;
            lHttp.send(null);
        }
    } else { // just proceed IE does not have issue!
        isBusy = true;
        lHttp.send(null);
    }
}

var gInternationalSupport = false;

function setInternationalSupport(pValue) {
    gInternationalSupport = pValue;
}

function isInternationalEnabled() {
    return (gInternationalSupport);
}

function getHttpResponseText(pDiv, pHttp) {
    var lHttp = pHttp;

    if (lHttp == null) // just in case!
        lHttp = gHttp;

    if (lHttp.readyState == 4) {
        isBusy = false;

        var status = "";
        try {
            status = lHttp.statusText;
            if (lHttp.status == 200) {
                var htmlDocument = lHttp.responseText;
                if (gAjaxForeground) {
                    setVisible(pDiv, true);
                }

                if (isInternationalEnabled())
                    document.getElementById(pDiv).innerHTML = unescape(htmlDocument);
                else
                    document.getElementById(pDiv).innerHTML = htmlDocument;
            }
        } catch (e) {
            status = "Trouble accessing it";
        }
    } else {
        if (getAjaxLoadingMessage() != '' && getAjaxLoadingMessage() != null) {
            // only display a loading message if it is defined
            setVisible(pDiv, true);
            document.getElementById(pDiv).innerHTML = getAjaxLoadingMessage();
        }
        return;
    }
}



var gAjaxLoadingMessage = '<b>Your request is loading...</b>';
var gAjaxForeground = true;

function doAjaxLoadingMessage(pDiv, pMessage) {
    document.getElementById(pDiv).innerHTML =
          " <div style='padding-left:15px;'>\n"
        + "     <div style='float:left;'>\n"
        + "         <img src='images/ajax-loader.gif' alt='loading...'>\n"
        + "     </div>\n"
        + "     <div style='float:left; padding:10px 0px 0px 10px; font-size:12pt; font-weight:bold;'>\n"
        +           pMessage + "\n"
        + "     </div>\n"
        + " </div>\n"
    ;
}

function setAjaxLoadingMessage(pMessage) {
    gAjaxLoadingMessage = pMessage;
}

function getAjaxLoadingMessage() {
    return (gAjaxLoadingMessage);
}

function setAjaxForeground() {
    gAjaxForeground = true;
}

function setAjaxBackground() {
    gAjaxForeground = false;
}

////////////////////////////////////////////////////////////////////////////////
/// XML HTTP OBJECT
////////////////////////////////////////////////////////////////////////////////
function getHTTPObject() {
    //var xmlhttp;	
    //if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    //	try {
    //		xmlhttp = new XMLHttpRequest();
    //		xmlhttp.overrideMimeType("text/xml");
    //	} catch (e) {
    //		xmlhttp = false;
    //	}
    //}
    //return xmlhttp;

    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }
    return http_request;
}

var http = getHTTPObject(); // create the HTTP Object for 

function updateHttpResponse() {
    if (http.readyState == 4) {
        // Use the XML DOM to unpack the city and state data 
        var xmlDocument = http.responseXML;
        document.getElementById('familiarLinks').innerHTML = ' ';
        document.getElementById('currentLinks').innerHTML = ' ';
        for (var m = 0; m < xmlDocument.getElementsByTagName('link').length; m++) {
            var rowid = xmlDocument.getElementsByTagName('rowid').item(m).firstChild.data;
            var url = xmlDocument.getElementsByTagName('url').item(m).firstChild.data;
            document.getElementById('familiarLinks').innerHTML += "<a href='javascript:void(0)' onClick=\"newExternalWindow('http://" + url + "');\" class=CenterLink >" + url + "</a><br />";
            document.getElementById('currentLinks').innerHTML += "<a href='javascript:void(0)' onClick=\"javascript:document.addLink.linkRowid.value='" + rowid + "';deleteFamLinks();\" class=CenterLink >" + url + "</a><br />";
        }
        if (gLanguage == 'SPANISH') {
            if (xmlDocument.getElementsByTagName('link').length == 0) {
                document.getElementById('currentLinks').innerHTML = 'No links';
            }
        } else {
            if (xmlDocument.getElementsByTagName('link').length == 0) {
                document.getElementById('familiarLinks').innerHTML = "<a href='javascript:void(0)' onClick=\"newExternalWindow('http://cstools.asme.org/holdersearch/ ');\" class=CenterLink >Locate ASME Stamp Holder</a><br />";
                document.getElementById('currentLinks').innerHTML = 'No links';
            } else {
                document.getElementById('familiarLinks').innerHTML += "<a href='javascript:void(0)' onClick=\"newExternalWindow('http://cstools.asme.org/holdersearch/ ');\" class=CenterLink >Locate ASME Stamp Holder</a><br />";
            }
        }
        isWorking = false;
    }
}

var isWorking = false;

function submitAjaxForm(pForm, pUrl, pDiv) {
    if (isBusy) {
        gHttp.onreadystatechange = function() { }
        gHttp.abort();
    }

    var lParms = "?method=ajax"
            , lElement = "";

    for (var i = 0; i < pForm.elements.length; ++i) {
        // loop through all form elements and assemble a Url
        lElement = pForm.elements[i];

        if (lElement.type == "checkbox") {
            if (lElement.checked) {
                lParms += "&" + lElement.name + "=Y";
            } else {
                lParms += "&" + lElement.name + "=N";
            }
        } else if (lElement.type == "radio") {
            if (lElement.checked) {
                lParms += "&" + lElement.name + "=" + lElement.value;
            }
        } else if (lElement.tagName == "SELECT") {
            if (lElement.selectedIndex > -1) {
                // only add this item to the parms list if something has been selected
                lParms += "&" + lElement.name + "=" + lElement.options[lElement.selectedIndex].value;
            }
        } else {
            lParms += "&" + lElement.name + "=" + encodeURI(lElement.value);
        }
    }

    gHttp.open("POST", pUrl + lParms, false);
    gHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    gHttp.onreadystatechange = function() { getHttpResponseText(pDiv); };

    if (window.XMLHttpRequest) { // Mozilla check
        if (!isBusy) { // getting Javascript errors (only in Mozilla) this check prevents error
            isBusy = true;
            gHttp.send(null);
        }
    } else { // just proceed IE does not have issue!
        isBusy = true;
        gHttp.send(null);
    }
}

function buildAjaxForm(pForm, pUrl, pDiv) {
    var lReturn = "";
    if (isBusy) {
        gHttp.onreadystatechange = function() { }
        gHttp.abort();
    }

    var lParms = "?method=ajax"
            , lElement = "";

    for (var i = 0; i < pForm.elements.length; ++i) {
        // loop through all form elements and assemble a Url
        lElement = pForm.elements[i];

        if (lElement.type == "checkbox") {
            if (lElement.checked) {
                lParms += "&" + lElement.name + "=Y";
            } else {
                lParms += "&" + lElement.name + "=N";
            }
        } else if (lElement.type == "radio") {
            if (lElement.checked) {
                lParms += "&" + lElement.name + "=" + lElement.value;
            }
        } else if (lElement.tagName == "SELECT") {
            lParms += "&" + lElement.name + "=" + lElement.options[lElement.selectedIndex].value;
        } else {
            lParms += "&" + lElement.name + "=" + encodeURIComponent(lElement.value);
        }
    }

    lReturn = pUrl + lParms;

    return (lReturn);
}
