//class servcom
var gAjaxTimeoutMilliseconds=5000;
function Servcom(onResponse){
this.myStat=null;
this.onResponse=onResponse;
this.onFail=null;
}//end function

Servcom.prototype.setOnFail=function Servcom_setOnFail(onFail){
this.onFail=onFail;
}//end function

Servcom.prototype.setRequestTimeout=function Servcom_setRequestTimeout(milliseconds){
gAjaxTimeoutMilliseconds=milliseconds;
}//end function

Servcom.prototype.init=function Servcom_init(q_stat){
if(q_stat){
	this.myStat=q_stat;
}//end if
if(gServcom){
}else{
	alert("Global object gServcom has not been defined.");
}//end if
}//end function

Servcom.prototype.request=function Servcom_request(q_url,q_data){
if(q_url>""){
	if(gServcom.myStat){
		gServcom.myStat.reg("request");
	}//end if
	this.lastrequest=q_url;
	this.myAjax = new Ajax.Request(q_url,{method: 'post', parameters: q_data, onComplete:Servcom_response,onFailure:Servcom_failure});
	return true;
}else{
	return false;
}//end if
}//end function

Servcom.prototype.arrayRequest=function Servcom_request(q_url,q_array){
var q_data="ARRAY="+escape(array2tab(q_array));
this.request(q_url,q_data);
}//end function

function ledblink(color){
if(div("blink")){
	if(color){
		div("blink").style.backgroundColor=color;
	}else{
		if(isGrey(div("blink").style.backgroundColor)){
			div("blink").style.backgroundColor="#00ff00";
		}else{
			div("blink").style.backgroundColor="#c0c0c0";
		}//end if
	}//end if
}//end if
}//end function

function isGrey(colorcode){
return contains((colorcode).toLowerCase(),"c0c0c0")||contains((colorcode).toLowerCase(),"192, 192, 192");
}//end function

function Servcom_response(originalRequest){
if(gServcom.myStat){gServcom.myStat.reg("response");}//end if
if(div("blink")){ledblink();}//end if
this.last=originalRequest.responseText;
gServcom.onResponse(originalRequest.responseText,originalRequest);
}//end function

function Servcom_failure(originalRequest){
if(gServcom.myStat){gServcom.myStat.reg("failure");}//end if
if(div("blink")){ledblink("red");}//end if
try{
	if(typeof(originalRequest)!="undefined"){
		gServcom.error=originalRequest.responseText;
	}else{
		gServcom.error="failure and originalRequest undefined";
	}//end if
	if(this.onFail){
		this.onFail(gServcom.error);
	}//end if
}catch(errObj){
	if(gServcom.error){
		gServcom.error="Servcom_failure:"+errObj.message;
		logga(gServcom.error);
		if(gServcom.myStat){
			gServcom.myStat.reg("failure");
		}else{
			logga("gServcom.myStat is null");
		}//end if
	}else{
		logga("gServcom.error is undefined");
	}//end if
}//end catch
}//end function

function Servcom_timeout(){
Servcom_failure();
}//end function

function Ajax_callInProgress (xmlhttp) {
switch (xmlhttp.readyState) {
	case 1: case 2: case 3:
	return true;
	// Case 4 and 0
	default:
	return false;
}//end switch
}//end function

// Register global responders that will occur on all AJAX requests
Ajax.Responders.register({
onCreate: function(request) {
request['timeoutId'] = window.setTimeout(function(){
// If we have hit the timeout and the AJAX request is active, abort it and let the user know
if(Ajax_callInProgress(request.transport)) {
	request.transport.abort();
	Servcom_timeout();
	// Run the onFailure method if we set one up when creating the AJAX object
	if (request.options['onFailure']) {
		request.options['onFailure'](request.transport, request.json);
	}//end if
}//end if
}//end function
,gAjaxTimeoutMilliseconds);
},
onComplete: function(request){
// Clear the timeout, the request completed ok
window.clearTimeout(request['timeoutId']);
}//end function
});

