function Ajax(){

    this.xmlhttp            = false;
    this.parameters         = {};
    this.callbackfunction   = null;
    this.responsetype       = "TEXT";
    this.onSend             = null; // called when request sent
    this.onReturn           = null; // called when request returned
    this.async              = true; // asynchrone call?
    this.returnsyncresult   = false; // whether to return non-async call result

    this.addParameter = function(name, value){
        this.parameters[name] = escape(value);
    }

    /** 
     * Associative array name value pairs
     */    
    this.addParameterArray = function(array) {
        var name;

        for (name in array) {
            this.parameters(i) = array[name];
        }
    }
     
    this.setResponseType = function(responsetype) {
        if (responsetype != "TEXT" && 
            responsetype != "XML") {
            return;
        }
        this.responsetype = responsetype;
    }

    this.getQueryString = function(){

        var params = "";

        for (i in this.parameters) {
            if (params != "") params+= "&";
            params+= i + "=" + this.parameters[i];
        }

        return params;
    }

    this.sendRequest = function(url, requestMethod, callback) {

        if (requestMethod != "GET" && 
            requestMethod != "POST") {
            return;            
        }

        var params = this.getQueryString();


        if (!this.xmlhttp) {
            if (window.XMLHttpRequest) {
                try {
                    this.xmlhttp = new XMLHttpRequest();
                } 
                catch(e) {
                    this.xmlhttp = false;             
                }
            }
            else if(window.ActiveXObject) {
                try {
                    
                    this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e) {
                    try {
                        this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch(e){
                        this.xmlhttp = false;
                    }
                }
            }
        }

        if (this.xmlhttp) {

            if (requestMethod == "GET") {
                url+= "?" + params;
            }

            //local variables needed for the callback to work 
            var responsetype = this.responsetype; 
            var xmlhttp      = this.xmlhttp;
            var onReturn     = this.onReturn;

            if (Ajax.debug) {
                Ajax.log+= url + "\n";
                Ajax.params = params + "\n";
            }
            try {
                xmlhttp.open(requestMethod, url, this.async);
            } 
            catch (e) {
                if (Ajax.suppressErrors) return;
                
                alert("Ajax xmlhttp.open error"+
                      "\nerror:"+e.message+
                      "\nmethod:"+requestMethod+
                      "\nurl:"+url+
                      "\nsync:"+this.async+
                      "\nparams:"+params);
                return;
            }

            if (this.async) {            
                xmlhttp.onreadystatechange = function(){
                    if (xmlhttp.readyState==4) {
                        if (xmlhttp.status == 200) {    
                            var content = responsetype == "TEXT" ? 
                                xmlhttp.responseText : 
                                xmlhttp.responseXML;
    
                            if (callback != null) callback(content);
                            if (onReturn != null) onReturn();
                        } 
                        else {
                            if (!Ajax.suppressErrors) {
                                alert("Error retrieving the XML data from: "+ url +"\n" + 
                                      xmlhttp.statusText);
                            }
                        }                    
                    }
                }            
            }

            if (requestMethod == "POST") {
                xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xmlhttp.send(params);                		
            }
            else {
                xmlhttp.send(null);
            }

            if (!this.async) {
                var content = responsetype == "TEXT" ? 
                        xmlhttp.responseText : 
                        xmlhttp.responseXML;

                if (this.returnsyncresult) {
                    return content;
                }
                else {                                    
                    if (callback != null) callback(content);
                }
            }

            if (this.onSend != null) this.onSend();            
        }       
    }
}

/**
 * Ajax static properties, methods
 */
Ajax.debug  = false;
Ajax.result	= '';
Ajax.log    = '';
Ajax.params = '';
Ajax.suppressErrors = false;

Ajax.evalResult = function(result){
    if (Ajax.debug) Ajax.result = result;

    try{
        eval(result);
    }
    catch(e) {
        if (!Ajax.suppressErrors){
            evalError("Ajax.evalResult:\n" + e.name + ":" + e.message, result);
        }
    }
}

function evalError(err, result){
    
     var errorw = window.open("",'errorWin','scrollbars=yes,resizable=yes,width=800,height=600');     
     errorw.focus();
     errorw.document.write("Error: " + err + "\n" + result);
     errorw.document.close();
}


