//RTE URL - The URL to the RTE without the querystring(s)
var RTEURL = "http://fbto-re.speed-trap.nl/realtimeserver/jsoquery";
var RTESECURL = "https://fbto-rt.speed-trap.nl/realtimeserver/jsoquery";

var loc = document.location+""; 
if (loc.indexOf("https:") == 0)
{ 
  RTEURL=RTESECURL;
}

var RTECSA = "fbtoCSA";

//RTEHandler
var RTEHandler=
{

  name:"RTEHandler",
  onload:null,
  onstatus:null,
  onerror:null,
  retries:5,
  retryTimeout:1000,

  request:null,
  busy:false,
  timer:null,
  response:null,
  rteurl:null,
  tryno:0,

  submit:function(url)
  {
    if (url) 
    {
      this.rteurl = url;
      this.tryno = this.retries; 
    }
    var session = this.getSessionID();
    var key = this.getKey();
    if (this.request) this.request.abort();
    if (this.timer) window.clearTimeout(this.timer);
    if ((session)&&(key))
    {
      var url = this.rteurl+"?session="+session+"&key="+key+"&handler=RTEHandler.handleResponse";
      try
      {
      	var script = document.createElement("SCRIPT");
      	script.setAttribute("type","text/javascript");
      	script.setAttribute("src",url);
      	document.body.appendChild(script);
        this.handleStatus("Sent : "+url);
      }
      catch(e)
      {
        this.handleError("Failed to apply script",e);
      }
    }
    else
    {
      if (this.tryno-->0)
      {
        if (!session) this.handleStatus("No Session - retry in "+this.retryTimeout+" ms, retry "+(this.tryno+1));
        if (!key) this.handleStatus("No Key - retry in "+this.retryTimeout+" ms, retry "+(this.tryno+1));
        this.timer = window.setTimeout(this.resubmitHandler, this.retryTimeout);
      }
      else
      {
        this.handleError("Timeout");
      }
    }        
  },

  resubmitHandler:function()
  {
    RTEHandler.submit();
  },

  handleStatus:function(msg)
  {
    if (this.onstatus)
    { 
    this.onstatus(this,msg);
    }else{
    }
  },

  handleResponse:function(rte)
  {
  	try
  	{
    	if (window.handleRTEResponse)
    	{
      		window.handleRTEResponse(rte);
    	}
  	}
  	catch(e){}
  },

  handleError:function(msg,e,extra)
  {
    if (this.onerror)
    {
      if (e)
      {
        if (e.description) msg+=' : '+e.description;
        else if (e.message) msg+=' : '+e.message;
        else msg+=' : '+e;
      }
      if (extra)
      {
        msg+="\r\n"+extra;
      }
      //InstrumentedBrowser.log(InstrumentedBrowserID,"RTE Error : "+msg);
      this.onerror(this,msg);
    } 
  },
  
  getSessionID:function()
  {
    var c = document.cookie+"";
    var re = /(.*)usy46gabsosd\=(\w+)__(\d+)_(\d+)(.*)/;
    var rr = c.match(re);
    if (rr!=null)
    {
      if (rr[3]) return rr[3];
    }
    return null;
  },
  
  getKey:function()
  {
    var c = document.cookie+"";
    var re = new RegExp("(.*)"+RTECSA+"key\\=(((\\w)||(\\d))+)(.*)","");
    var rr = c.match(re);
    if (rr!=null)
    {
      if (rr[2]) return rr[2];
    }
    return null;
  }
  
  
}




