//////////////////////////////////////////////////////////////////////////
//      _
//    <' )_,  Copyright RealDecoy
//    (    )          2007
//   ~~~~~~~~
// This file is subject to a license agreement and cannot be used
// or sold without without the written approval of RealDecoy Inc.
// Information about licensing can be found on the RealDecoy website
// at www.realdecoy.com.
//
//////////////////////////////////////////////////////////////////////////

/*

	try
	{
	  var _o = { onsuccess: function(){console.log("got it") } };
	  var _a = com.realdecoy.ajax.create( "http://localhost/index.html", _o, "" );
	      _a.start();
	}
	catch(err)
	{
	  console.log(err);
	}
	console.log(_a);

*/

if( typeof rd_jsversion != "undefined" )
{
  var _reload_location = "/";

  if( (rd_jsversion > 1000) && ((window == window.parent) || ((typeof(window.parent.rd_jsversion) != "undefined") && (window.parent.rd_jsversion <= 1000))) )
  {
    alert( "Bilingual message declaring JS version in cache is out of date. Please clear cache before immediately." );
    window.location.replace( _reload_location );
  }
}

Array.prototype.each = function()
{
  if( !arguments.length ) return;

  var _fn = arguments[0];

  for( var i = 0, imax = this.length ; i < imax ; ++i )
  {
  	arguments[0] = this[i];
  	_fn.apply( this, arguments );
  }
 
  arguments[0] = null;
};

browser =
{
  ie: function()
  {
    var agent   = navigator.userAgent.toLowerCase();

	if( !agent || typeof(agent) == "undefined" ) return false;

	if( ( agent.indexOf( "msie" ) != -1 ) && ( agt.indexOf( "opera" ) == -1 ) ) return true;

    return false;
  },

  gecko: function()
  {
    var agent = navigator.userAgent.toLowerCase();

	if( !agent || typeof(agent) == "undefiend" ) return false;

    if( ( agent.indexOf( "mozilla" ) != -1 ) && ( agent.indexOf( "spoofer" ) == -1 ) && ( agent.indexOf( "compatible") == -1 ) ) return true; 
	if( agent.indexOf( "gecko" ) != -1 ) return true;
	if( agent.indexOf( "firebird" ) != -1 ) return true;
	if( agent.indexOf( "firefox" ) != -1 ) return true;

    return false;
  },

  safari: function()
  {
	var agent = navigator.userAgent.toLowerCase();
	
	if( !agent || typeof(agent) == "undefined" ) return false;

	if( agent.indexOf( "safari" ) != -1 ) return true;

	return false;
  },

  opera: function()
  {
	var agent = navigator.userAgent.toLowerCase();

	if( !agent || typeof(agent) == "undefined" ) return false;

    if( agent.indexOf( "opera" ) != -1 ) return true;

	return false;
  }
};

com = {};
com.realdecoy = {};

com.realdecoy.pass = function()
{
 var args = Array.prototype.slice.apply( arguments, [2] );
 var obj = arguments[0];
 var fn = arguments[1];
 return function() { return fn.apply( obj, args ); };
};

com.realdecoy.bind = function( obj, fn ) { return function() { return fn.apply( obj, arguments ); }; };
com.realdecoy.copy = function(a, b) { if(!b) b = {}; if(a) for(var c in a) {var d = a[c]; b[c] = d;} return b; };
com.realdecoy.ajax = 
{
   onsuccess: null,
   onerror: null,
   ontimeout: null,
   onrschange: null,
   timeout: null,
   acct: null,
   pw: null,
   verb: 'POST',

  _postd: null,
  _url: null,
  _timeout: null,
  _xhrobj: null,
  _inuse: false,
  _thandle: null,

  create: function( url, opts, postd )
  {
  	var	_ret = com.realdecoy.copy( opts, com.realdecoy.copy( com.realdecoy.ajax, new Object() ) );
  		 	_ret._url = url;

  	if( postd ) _ret._postd = postd;

 	return _ret;
  },

  start: function()
  {
    this.cancel();

    if( ! this._xhrobj )
    {
      if( !browser.ie() )
      {
        try
        {
          netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
        }
        catch (e)
        {
          if( this.onerror )
          {
            window.setTimeout( this.onerror.apply( this, []), 20 );
          }
        }
      }

      if( window.XMLHttpRequest ) { try { this._xhrobj = new XMLHttpRequest(); }  catch(err)  { this._xhrobj = null; } }

      if( window.ActiveXObject )
      {
        try { this._xhrobj = new ActiveXObject("Msxml2.XMLHTTP"); } catch(err) { _xhrobj = null; }
        try { this._xhrobj = new ActiveXObject("Microsoft.XMLHTTP"); } catch(err) { _xhrobj = null; }
      }

       if( !this._xhrobj ) return false;
    }

    if( this.timeout ){ this._thandle = window.setTimeout( com.realdecoy.pass( this, this.ontimeout, this ), this.timeout ); }

    try
    {
      if( this.acct || this.pw ) this._xhrobj.open( this.verb, this._url, true, this.acct, this.pw );
      else this._xhrobj.open( this.verb, this._url, true );
    }
    catch(err)
    {
      if( this.onerror ) window.setTimeout( com.realdecoy.pass( this, this.onerror, this ), 20 );
      return false;
    }

    if( this.verb == "POST" ) { try { this._xhrobj.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' ); } catch(err) {} }
    
    this._xhrobj.onreadystatechange = com.realdecoy.bind( this, this.rschange );
    var xhrsendret = this._xhrobj.send( this._postd || "" );

    return true;
  },

  cancel: function()
  {
    if( this._inuse && this._xhrobj ) { this._xhrobj.onreadystatechange = null; this._xhrobj.abort(); this._xhrobj = null; }

    if( this._thandle != null ) window.clearTimeout( this._thandle );

    this._inuse = false;
    this._thandle = null;
  },

  rschange: function()
  {
    var xhr = this._xhrobj;

    if( this.onrschange ) window.setTimeout( com.realdecoy.pass( this, this.onrschange, this ), 10 );

    if( this._xhrobj.readyState == 4 )
    {
      this._inuse = false;
      if( this._thandle != null ) { window.clearTimeout( this._thandle ); this._thandle = null; }

      var status;

      try
      {
        status = typeof(xhr.status) != "undefined" ? xhr.status : 0;
        if( browser.safari() ) status = 200;
      }
      catch(err) { status = 0; }

      if( (status >= 200) && (status < 400) ) { if( this.onsuccess ) window.setTimeout( com.realdecoy.pass( this, this.onsuccess, this ), 20 ); }
      else if( this.onerror ) window.setTimeout( this.onerror.apply(this, [] ), 20 );

      if( browser.ie() ) window.setTimeout( function(){ xhr.onreadystatechange=function(){ return null;}; }, 0 );
      else window.setTimeout( function(){xhr.onreadystatechange= null;}, 0 );
    }
  },

  dtor: function()
  {
    if( this._xhrobj ) this._xhrobj.onreadystatechange = null;
  
    this.cancel();

    this.onsuccess = null;
    this.onerror = null;
    this.ontimeout = null;
    this.onrschange = null;
  }
};