﻿function ajax(url, callback) {

    var that = this;

    var xmlhttp = new xmlhttpobj();
    this.responseText = "";
    this.url = url;
    this.errorMessage = "";
    this.success = true;

    xmlhttp.onreadystatechange = function () { processcallback(callback); };

    function xmlhttpobj() {
        var tempobj;
        try {
            if (window.XMLHttpRequest) { tempobj = new XMLHttpRequest(); }
            else { tempobj = new ActiveXObject("Microsoft.XMLHTTP"); }
        } catch (e) {
            return (null);
        }
        return (tempobj);
    }

    function processcallback(usercallbackfunction) {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            that.responseText = xmlhttp.responseText;
            that.success = true;
            eval(usercallbackfunction);
        }
        else {
            that.errorMessage = "Error. Request could not be completed.";
            that.responseText = null;
            that.success = false;
        }
    }

    this.get = function () {
        xmlhttp.open("GET", that.url, false);
        xmlhttp.send();
    }

    this.post = function (parameterString) {
        // clean up the parameter string
        if (parameterString == "" || parameterString == NaN || parameterString == null) {
            that.errorMessage = "POST requires a parameter string";
            that.success = false;
            return false;
        }

        if ("?" != left(parameterString, 1)) {
            parameterString = "?" + parameterString;
        }

        xmlhttp.open("POST", that.url + encodeURI(parameterString), false);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send(parameterString);
    }
}
