//global variables
var ajaxRequest;

//requestType is either post or get
//URL is the path to the resource on server(mostly a servlet)
//the asyn type is omitted as it is always true in ajax
//response handler is the name of the function that will run when response comes
//request body is the for query string that is sent with post request

function autoAjax(requestType,URL,responseHandler,requestBody){
    
    //first get the request object
    ajaxRequest = getRequestObject();
    ajaxRequest.open(requestType,URL,true);
    ajaxRequest.onreadystatechange=responseHandler;
    if(requestType == "POST" || requestType == "post"){
    //the following line is mandatory of you are posting info
    ajaxRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    ajaxRequest.send(requestBody);
    }
    else{
       ajaxRequest.send(null);
    }
}

//***************************************************************************/

function getRequestObject(){
    var request = null;
    
    try{ request = new ActiveXObject("Msxml2.XMLHTTP"); }catch(newObjectNotAvailable){
        try{ request = new ActiveXObject("Microsoft.XMLHTTP"); } catch(UsingNonMicrosoftBrowser){
            try{ request = new XMLHttpRequest();} catch(AjaxWillNotWork){
                window.alert("No Request Object available\nAjax will not work with this browser");
            }
        }
    }
    return request;
    
}//end function getRequestObject()

//***************************************************************************/
