//Common request object for the whole page
//All AJAX RPC calls go through here
var http = createRequestObject();

//Method to create the request object
//Simply determines which browser the user is 
//using and creates the correct object
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

//Method to send AJAX RPC requests to the server
//Note that all requests go through the same servlet (/RPC as the uri)
//The method to execute is encoded as the action parameter
//and all other parameters for the request are encoded 
//in the HTTP GET request.
//
//The onreadystatechange assignment assigns the function below as the
//handler for all responses from the server
function sndReq(action, params) {
	//alert("Action: " + action +"\nParams: "+params);
	var url = 'rpc.do?action='+action;
	if(params != null && params != '') {
		url += '&'+params;
	}
    http.open('get', url);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

//Method to handle responses from the server
function handleResponse() {
	//Wait for state 4, so that we know that the response is complete
    if(http.readyState == 4){
    	//Make sure the response was successful (HTTP response 200) 
    	if(http.status == 200 || http.status == 302) {
    		//Get the contents of the response
	        var response = http.responseText;

        	//try{
		      	eval(response);  
		    //}catch(e) {
//		    	alert("AJAX RPC error: "+e.description);
		    //}		
		}else {
			//Notify the user of an error . . . this needs to be better
//			alert("AJAX RPC error: "+http.status+"-"+http.statusText);
		}
    }
}

setInterval("pingServer()", 1740000);  // in miliseconds (29 minutes)
function pingServer() {
	//alert('ping');
	sndReq('ping', '');
}