
function _GatewayAPI(){
this.build="203";
this.items=[];
this.opera=(new RegExp("opera(|/)[56]","i")).test(navigator.userAgent);
this.opera5=(new RegExp("opera(|/)5","i")).test(navigator.userAgent);
this.supported=(document.layers||document.all||document.getElementById);
}
GatewayAPI=new _GatewayAPI();

_GatewayAPI.prototype.register=function(o){
var i=this.items.length;
this.items[i]=o;
return "GatewayAPI.items['"+i+"']";
}

function Gateway(u,_d){
this.errors=[];
this.cache={};
if(!u)this.throwError("No server gateway was specified.",true);
if(!GatewayAPI.supported)this.throwError("Your browser does not meet the minimum requirements. \nPortions of the page have been disabled and therefore \nthe page may not work as expected.",true);

this.disabled=false;
this.url=u;
this.useWddx=(!!window.WddxSerializer);
this.enableCache=(GatewayAPI.opera5)? false:true;
this.iframeSrc=null;

this.mode=(!!_d && _d==true)? "debug":"release";
this.html="";
this.sent=null;
this.received=null;
this.counter=0;
this.status="idle";
this.multithreaded=true;
this.delay=1;this.timeout=60;this.statusReset=3;this.statusdelay=100;
this.statusID=null;
this.delayID=null;
this.timeoutID=null;
this.statusResetID=null;
this.method="get";

this.idGateway="idGatewayAPI_"+GatewayAPI.items.length;
this.idForm="idGatewayAPI_Form_"+GatewayAPI.items.length;

this.id=GatewayAPI.register(this);
}
Gateway.prototype.isWddxEnabled=function(){
return(!!window.WddxSerializer && this.useWddx);
}
Gateway.prototype.getUrl=function(){
var uuid=((this.url.indexOf("?")==-1)? "?":"&")+"uuid="+(new Date().getTime()+""+Math.floor(1000 * Math.random()));
return this.url+uuid;
}

Gateway.prototype.onReceive=function(){};


Gateway.prototype.onTimeout=function(){
this.throwError("The current request has timed out. Please \ntry your request again.");
}

Gateway.prototype.create=function(){
this.width=(this.mode=="release")? "1":"400";
this.height=(this.mode=="release")? "1":"200";
this.visibility=(this.mode=="release")?((document.layers)? "hide":"hidden"):((document.layers)? "show":"visible");
this.bgcolor=(this.mode=="release")? "##ffffff":"##cccccc";

if(this.disabled)return false;

this.createCSS();
this.createFrame();
this.createForm();
document.write(this.html);
}

Gateway.prototype.throwError=function(error,_disable){
var disable=(typeof _disable=="boolean")? _disable:false;
this.errors[this.errors.length++]=error;
if(this.status=="sending")this.receivePacket(null,false);
if(disable)this.disabled=true;
alert(error);
}

Gateway.prototype.createCSS=function(){
this.html+="<style type=\"text\/css\">\n";
this.html+="##"+this.idGateway+"{position:absolute;width:"+this.width+"px;height:"+this.height+"px;clip:rect(0px "+this.width+"px "+this.height+"px 0px);visibility:"+this.visibility+";background:"+this.bgcolor+";}\n";
this.html+="</style>\n";
}

Gateway.prototype.createForm=function(){
this.html+="<form name=\""+this.idForm+"\" action=\""+this.url+"\" target=\""+this.idGateway+"\" method=\"post\" style=\"width:0px;height:0px;margin:0px 0px 0px 0px;\">\n";
this.html+="<input type=\"Hidden\" name=\"packet\" value=\"\"></form>\n";
}

Gateway.prototype.createFrame=function(){
var sSrc=(typeof this.iframeSrc=="string")? "src=\""+this.iframeSrc+"\" ":(GatewayAPI.opera)? "src=\"opera:about\" ":"";
if(document.layers)this.html+="<ilayer name=\""+this.idGateway+"\" id=\""+this.idGateway+"\" width=\""+this.width+"\" height=\""+this.height+"\" bgcolor=\""+this.bgcolor+"\" visibility=\""+this.visibility+"\"></ilayer>\n";
else this.html+="<iframe "+sSrc+"width=\""+this.width+"\" height=\""+this.height+"\" name=\""+this.idGateway+"\" id=\""+this.idGateway+"\" frameBorder=\"1\" frameSpacing=\"0\" marginWidth=\"0\" marginHeight=\"0\"></iframe>\n";
}

Gateway.prototype.serverTimeout=function(id){
if(this.status=="sending" && this.counter==id){
this.status="timedout";
clearInterval(this.statusID);
window.status="";
this.timeoutID=null;
this.onTimeout();
}
}

Gateway.prototype.resetStatus=function(){
this.status="idle";
this.statusResetID=null;
}

Gateway.prototype.receivePacket=function(packet,_bRunEvent){
if(this.disabled||this.status=="timedout")return false;
var b=(typeof _bRunEvent !="boolean")? true:_b;

clearInterval(this.statusID);
window.status="";

this.received=packet;

if(this.cacheResults==true){
this.cache[this.packetString]=packet;
this.cacheResults=null;
}

if(b)this.onReceive();

clearInterval(this.statusID);
this.statusID=null;
this.status="received";

this.statusResetID=setTimeout(this.id+".resetStatus();",this.statusReset * 1000);
}

Gateway.prototype.clearCache=function()
{this.cache={};
}

Gateway.prototype.sendPacket=function(packet,_bUseCache){
if(this.disabled)return false;
var bUseCache=(typeof _bUseCache=="boolean")? _bUseCache:true;

if(!this.multithreaded && this.status=="sending")return false;
if(this.delayID !=null)clearTimeout(this.delayID);
if(this.statusResetID !=null)clearTimeout(this.statusResetID);

this.sent=packet;


this.delayID=setTimeout(this.id+".serializeAndSend("+String(bUseCache)+")",this.delay);
}

Gateway.prototype.serializeAndSend=function(_bUseCache){
if(this.disabled)return false;

this.counter++;
this.delayID=null;this.received=null;this.status="sending";
window.status="Sending.";
if(this.statusID !=null)clearInterval(this.statusID);
this.statusID=setInterval("window.status=window.status+'.'",this.statusdelay);
this.timeoutID=setTimeout(this.id+".serverTimeout("+this.counter+")",this.timeout * 1000);

switch(this.method){
case "post":

case "get":
this.methodGet(this.sent,_bUseCache);
break;
}

}
Gateway.prototype.formatPacket=function(packet){
if(this.isWddxEnabled()){
var oWddx=new WddxSerializer();
return "&wddx="+escape(oWddx.serialize(packet));
}else{
if(typeof packet=="string")return "&data="+packet;
else if(typeof packet=="object"){
var p=[];
for(var k in packet)p[p.length]=k+"="+escape(packet[k]);
return "&"+p.join("&");
}
}
}
Gateway.prototype.methodPost=function(packet,_bUseCache){
return alert("The post method is currently unsupported. Netscape v4.0 does not support posting to a layer.");
if(this.disabled)return false;
oForm=document[this.idForm];

oForm.submit();
}
Gateway.prototype.methodGet=function(packet,_bUseCache){
var sPacket=this.formatPacket(packet);
var sUrl=this.getUrl()+sPacket;

if(this.enableCache &&(_bUseCache==true)&& !!this.cache[sPacket])return this.receivePacket(this.cache[sPacket]);

this.cacheResults=(this.enableCache &&(_bUseCache==true));
this.packetString=sPacket;

if(!!document.getElementById && !!document.getElementById(this.idGateway).contentDocument){
document.getElementById(this.idGateway).contentDocument.location.replace(sUrl);

}else if(GatewayAPI.opera){
document.getElementById(this.idGateway).location.replace(sUrl);

}else if(document.all){
document[this.idGateway].location.replace(sUrl);

}else if(document.layers){
document[this.idGateway].src=sUrl;
}
} 