// need that to hack around special chars like in andré.
jsrsContextProp.useGet = false;


/**
* Bs_InstantHelpClient class for jsrs (javascript remote scripting)
* 
* dependencies: /_bsJavascript/plugins/jsrs/JsrsCore.lib.js
* 
* @author     andrej at arn dot li
* @copyright  blueshoes.org
* @package    javascript_plugins
* @subpackage instanthelp
*/
function Bs_InstantHelpClient() {
  
  /**
  * the name that this instance uses. this is a bit unusual, but we need it here.
  * @var string objectName
  */
  this.objectName = '';
  
  /**
  * vector with the elements
  * dictName, strKey, lang, output
  */
  this._issuedCalls = new Array();
  
	/**
	* the global language to use. calls to getText() use that as default.
	* @access public
	* @var    string language
	*/
  this.language = '';
  
  /**
  * if the help text should be treated as plain text or html.
  * one of 'text' or 'html' (default).
  * @access public
  * @var    string textMode
  */
  this.textMode = 'html';
  
  /**
  * if you don't want to use the global db as defined in the global.conf 
  * then you can specify a name here. make sure that the settings for this 
  * db connection are set in the global.conf, even if the connection is 
  * not open.
  * 
  * example: $APP['db']['mysql_life']... (so dbSettingName is mysql_life)
  * 
  * @access public
  * @var    string dbSettingName
  */
  this.dbSettingName = '';
  
  /**
  * the file on the server that will be called.
  * it may not have a query string. if you need one, recode some things here.
  * @access public
  * @var    string serverUrl
  */
  this.serverUrl = '/_bsToolbox/instanthelp/communicator.php';
  
	/**
	* the name of the server class to which we are issueing the getText() calls.
	* @access public
	* @var    string phpClassName
	*/
	this.phpClassName = 'Bs_Ih_InstantHelp';
  
  /**
  * inits the object.
  * @param string objectName (name of this object, eg 'ih'.)
  * @param string formName (name of the form)
  * @param string firstnameId (id of the firstname field)
  * @param string lastnameId (id of the lastname field)
  * @param string sexId (id of the sex (gender) field)
  */
  this.init = function(objectName) {
    this.objectName    = objectName;
  };
  
  
  /**
	* starts a 'fetch text' request.
	* 
	* param output: 
	* 2 options. 
	*   1) The element id in the html code where the help text gets automatically assigned to. 
	*   2) it can be a function name that gets called. Example: "alert()" (with brackets, no params).
	* 
	* @access public
  * @param  string dictName      (The dictionary name)
  * @param  string strKey        (The "name" of the help text)
  * @param  string lang          (The language like "en". if empty string or not specified then the default this.language will be used.)
  * @param  string output        (where to send the output to. read above.)
	* @return void
  */
  this.getText = function(dictName, strKey, lang, output) {
		if ((typeof(lang) == 'undefined') || (lang == '')) lang = this.language;
    var serverUrl = this.serverUrl;
    if (typeof(this.dbSettingName)) {
      serverUrl += '?dbSettingName=' + this.dbSettingName;
    }
    var callId    = jsrsCall(serverUrl, this.objectName + "._callbackGetText", this.phpClassName+".getText", strKey, lang, dictName);
    this._issuedCalls[callId] = new Array(dictName, strKey, lang, output);
  }
  
  /**
  * is called automatically once the getText request to the server returns.
  * @see this.getText()
  */
  this._callbackGetText = function(value, callId) {
    if ((value != 'false') && (this._issuedCalls[callId])) {
      if (this._issuedCalls[callId][3].indexOf("()") == -1) {
        if (this.textMode == 'text') {
          //todo: innerText not supported by moz, use innerHTML but convert!
          //document.getElementById(this._issuedCalls[callId][3]).innerText = value;
          document.getElementById(this._issuedCalls[callId][3]).innerHTML = value;
        } else { //'html'
          document.getElementById(this._issuedCalls[callId][3]).innerHTML = value;
        }
      } else {
        var t = this._issuedCalls[callId][3];
        //this is actually an escape-for-js. a function would be nice. but do we want to include 
        //another file? well js can't do that, what a pain. and if js would have a way to call 
        //a function that's named in a string, like php's $$function thing, we would not need 
        //an eval and all this shit. crap. 
        try {
          value += ''; //to string converstion, stupid js. it's needed here. cause we got an 'object'.
          //alert(value);
          //alert(typeof(value));
          //if (!val.replace) alert('no replace');
          value = value.replace(/\"/g,'\\"'); //escape doublequote. it would break the code otherwise.
          value = value.replace(/\r/g,'\\\r');
          value = value.replace(/\n/g,'\\\n');
        } catch (e) {
          /*
          alert(value);
          alert(e);
          alert('catch'); //4debug
          */
        }
        //alert(t); //4debug
        t = t.substr(0, t.length -2) + "(\"" + value + "\")";
        //maybe add try/catch here. 
        eval(t);
      }
    }
  }
  
  /**
  * starts a query to the server to check if the user mixed first/lastname.
  * @see this._callbackMix()
  */
  this.queryMix = function() {
    this.firstname = this.firstnameObj.value;
    this.lastname  = this.lastnameObj.value;
    if ((this.firstname != '') && (this.lastname != '')) {
      var callId    = jsrsCall("OnoServer.php", this.objectName + "._callbackMix", "Bs_Om_OnomasticsServer.isOrderOk", this.firstname, this.lastname); 
    }
  }
  
  /**
  * is called automatically once the queryMix request to the server returns.
  * @see this.queryMix()
  */
  this._callbackMix = function(value, callId) {
    if ((value == 0) || (value == '0')) {
      alert("are you sure you didn't mix firstname/lastname?");
    }
  }
  
  /**
  * 
  */
  this._getRadioFieldValue = function(formName, fieldName) {
    for (counter=0; counter<document.forms[formName].elements[fieldName].length; counter++) {
      if (document.forms[formName].elements[fieldName][counter].checked) return document.forms[formName].elements[fieldName][counter].value;
    }
    return false;
  }
  
}

