//*******************************************************************************************************
//* jsrsCore.js - javascript remote scripting Lib
//*
//* Here's where you start:
//*   1) Place the files in a dir that is reachable by the server e.g. http://www.foo.ch/jsrs/.
//*   2) Include to lib to your web page by adding this *one* line into the
//*      <HEAD> section.
//*         <script type="text/javascript" src="/jsrs/jsrsCore.js"></script>
//*      Note that all other js-scripts needed are loaded automatically
//*   3) The only function to call is
//*           jsrsCall(remoteTarget, jsCallback, remoteFunction, params);
//*      E.g. callId = jsrsCall("/jsrs/myServer.php", showCity, "getCitys", 'USA');
//*
//*      *NOTE*: You may call jsrsCall() with a *variable* amount of parameters!
//*         E.g.  jsrsCall(remoteTarget, callback, remoteFunction, param_1, param_2, param_3, ...)
//*         The parameters will be passed to the remote function in the same order
//*         E.g. remoteFunction(param_1, param_2, param_3, ...)
//*
//*      Make sure to have a jsCallback function with 2 parameters
//*      E.g. function showCity(param, callId) {alert(callId+" "+param);}
//*
//*
//* Parameters of jsrsCall():
//*   remoteTarget  : The href target of the jsrs-server-script. It must be able to
//*                   read out the passed data and respond correctly
//*   jsCallback,   : This is your javascript function that is called on return of
//*                   the remote call.
//*   remoteFunction: Is the remote function you intend to call. It's either a simple
//*                   function name or an method call. Method calls have the structure
//*                   <class name>.<method name> E.g. 'Interface.getData'
//*   params        : This can be any js data (string, number, array, object) even
//*                   cascaded types (e.g. array of objects) as long as there
//*                   no *loop* references!!
//*   <return>      : Is the call-ID. This is returned when calling the callback function
//*                   and may be important when calling multiple functions, as the return
//*                   is asynchron.
//*
//* Parameters to use for the callback function
//*   <1st param> : Is the return value that can be string, number, array, object.
//*   <2ed param> : Is the call-ID (See 'return' above)
//*
//*
//* Advanced Use:  Passing Objects (see also Bugs below).
//*   To identify an object it must have the field wddxSerializationType=[class Name].
//*
//*
//* BUGS:
//*   Only happens when server is written in PHP:
//*    - Date problem:
//*         If you pass a date object ( new Date() ) it will be ignored by a PHP server.
//*      Reason: The data exchange is done uning the WDDX formate. PHP built in WDDX
//*         converter seams to ignore the WDDX tag <dateTime>.
//*      Workaround: Don't pass a Date object; convert it to a string.
//*    - Object problem:
//*         Unfortunately PHP has it's own way to convert Objects to and from WDDX.
//*         The standard way in WDDX is to make a <struc type='className'>.  But PHP
//*         ignores this feature. So object passing is not really suported.
//*      Workaround: Passing Objects from Client -> Server.
//*         Instead pass an Object with a field name 'php_class_name'
//*         E.g. var myObject = new Object();
//*                  myObject['php_class_name'] = 'aPhpClassName';
//*                  myObject['foo'] =  new Array(1,2,3);
//*                      :
//*
//*         Passing Objects from Server->Client. IS not currently suported
//* ------------------------------------------------------------------------------------------------------
//* @Requires NS6.2 and up / IE5.5 and up / Don't know about Opera
//* ------------------------------------------------------------------------------------------------------
//* @Version: 3.0 (10-April-2002)
//* ------------------------------------------------------------------------------------------------------
//* @Author:  Sam   Blum   <sam-at-blueshoes> Collected all the ideas, merged and rewrote them and added doc.
//* @package    javascript_plugins
//* @subpackage jsrs
//**

//------------------------------------------------------------------------------
// Dynamic code unit loading routines
// Idea taken from D. Kadrioski (See http://www.domapi.com/)
//------------------------------------------------------------------------------

// ============================================================================
//  -- Core Functions --
// ============================================================================

// Start by identifying the path to this javascript file (javascript files are also called unit)
var jsrsLibs = new Object();
jsrsLibs.path = _jsrsGetUnitPath('JsrsCore'); // obviously this unit is loaded if this code executes ;)


//------------------------------------------------------------------------------
function jsrsLoadUnit(name, useCompression){
  if('undefined' != typeof(jsrsLibs[name])) return false; // unit was already loaded, nothing to do
  // if we are using compression, add the "_c"
  //name+=(useCompression ? "_c" : "")+".js"; //commented out by andrej. needs a different implementation now.
  // load the script
  // 4debug  alert(jsrsLibs.path+name);
  var fullPath = ((name.substr(0, 1) == '/') || (name.substr(0, 2) == './')) ? name : jsrsLibs.path + name;
	if ((typeof(bs_documentOnloadStarted) != 'undefined') && (bs_documentOnloadStarted)) {
		//have to load using dom
		var html_doc = document.getElementsByTagName('head').item(0);
		var js = document.createElement('script');
		js.setAttribute('language', 'javascript');
		js.setAttribute('type', 'text/javascript');
		js.setAttribute('src', fullPath);
		html_doc.appendChild(js);
	} else {
		//can load source using document.write
	  document.writeln('<script type="text/javascript" src="'+fullPath+'"></script>');
	}
  jsrsLibs[name] = true;
  return true;
};
//------------------------------------------------------------------------------
function _jsrsGetUnitPath(name){ // returns false or the path to the unit
  var r=false;
  var i;
  var re=new RegExp("\/?"+name+"[\._]");
  var tags=document.getElementsByTagName("SCRIPT");
  for(var a=0;a<tags.length;a++){
    i=tags[a].src.search(re);
    if(i>=0) r= (i==0) ? '' : tags[a].src.substr(0,i) +'/';
  }
  return r;
};
//------------------------------------------------------------------------------

//sorry, had to move the wddx and update the url's here. 2002/10/17 --andrej
// Then load the units dynamically

jsrsLoadUnit('/_bsJavascript/core/lang/Bs_Misc.lib.js', false);          // <- Some utils
jsrsLoadUnit('/_bsJavascript/core/util/Bs_XmlParser.class.js', false);   // <- The XML parser
jsrsLoadUnit('/_bsJavascript/core/util/Bs_Wddx.class.js', false);        // <- The WDDX serializer
jsrsLoadUnit('/_bsJavascript/lib/json_c.proto.js', false);        // <- The json serializer
jsrsLoadUnit('JsrsClient.class.js', false); // <- The client gateway

