// need that to hack around special chars like in andré.
jsrsContextProp.useGet = false;


/**
* OnomasticsClient class for jsrs (javascript remote scripting)
* 
* @see        core/html/form/specialfields/Bs_FormFieldFirstname.class.php
* @package    javascript_plugins
* @subpackage onomastics
*/
function OnomasticsClient() {
  
  /**
  * the name that this instance uses. this is a bit unusual, but we need it here.
  * @var string objectName
  */
  this.objectName;
  
  /**
  * the form object (that holds the form fields).
  this.theForm;
  */
	
	/**
	* the url to the php communicator. 
	* 
	* you can use the default if you have set up the /_bsToolbox alias in 
	* your webserver, and if you use a global db object. 
	* if you don't use a global db object then you need to write your 
	* own communicator.php. take the original, instanciate a Bs_MySql, and 
	* use $theOnoServer->setDbByObj().
	* 
	* @access public
	* @var    string communicatorUrl
	* @since  bs4.3
	*/
	this.communicatorUrl = '/_bsToolbox/onomastics/communicator.php';
  
  //string
  this.formName;
  this.firstnameName;
  this.lastnameName;
  this.sexName;
  
  this.firstnameObj;
  this.lastnameObj;
  this.sexObj;
  
  /**
  * cached value of the firstname (for a compare on a callback event).
  */
  this.firstname;
  
  /**
  * cached value of the lastname (for a compare on a callback event).
  */
  this.lastname;
  
  /**
  * cached value of the sex (gender) (for a compare on a callback event).
  */
  this.sex;
	
	/**
	* @access public
	* @var    string errorMessageGenderMale
	*/
	this.errorMessageGenderMale   = "Are you sure that is a male name";
	
	/**
	* @access public
	* @var    string errorMessageGenderFemale
	*/
	this.errorMessageGenderFemale = "Are you sure that is a female name?";
	
	/**
	* @access public
	* @var    string errorMessageMix
	*/
	this.errorMessageMix          = "Are you sure you didn't mix firstname/lastname?";
	
  
  /**
  * inits the object.
  * @param string objectName (name of this object, eg 'myOnoClient'.)
  * @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, formName, firstnameId, lastnameId, sexId) {
    this.objectName    = objectName;
		
		if (typeof(formName) != 'undefined') {
	    this.formName      = formName;
	    this.firstnameName = firstnameId;
	    this.lastnameName  = lastnameId;
	    this.sexName       = sexId;
	    /*
	    this.firstnameObj = document.getElementById(firstnameId);
	    this.lastnameObj  = document.getElementById(lastnameId);
	    this.sexObj       = document.getElementById(sexId);
	    */
	    this.firstnameObj = document.forms[formName].elements[firstnameId];
	    this.lastnameObj  = document.forms[formName].elements[lastnameId];
	    this.sexObj       = document.forms[formName].elements[sexId];
		}
  };
  
  /**
  * starts a query to the server to check if the selected gender 
  * could be wrong. example: richard can't be female, can it?
  * @see this._callbackGender()
  */
  this.queryGender = function() {
    this.firstname = this.firstnameObj.value;
    this.sex       = this._getRadioFieldValue(this.formName, this.sexName);
    if ((this.firstname != '') && (this.sex != '')) {
      var callId    = jsrsCall(this.communicatorUrl, this.objectName + "._callbackGender", "Bs_Om_OnomasticsServer.getGender", this.firstname);
    }
  }
  
  /**
  * is called automatically once the queryGender request to the server returns.
  * @see this.queryGender()
  */
  this._callbackGender = function(value, callId) {
    if (value != 'false') {
      var firstname, sex;
      var firstname = this.firstnameObj.value;
      var sex       = this._getRadioFieldValue(this.formName, this.sexName);

      if (sex == 1) {
        if (value < 0) alert(this.errorMessageGenderMale);
      } else if (sex == 2) {
        if (value > 0) alert(this.errorMessageGenderFemale);
      }
    }
  }
  
  /**
  * 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(this.communicatorUrl, 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(this.errorMessageMix);
    }
  }
  
  /**
  * 
  */
  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;
  }
  
	
  /**
	* @access public
	* @param  int typeID (relation type id)
	* @param  int fromID (name id)
	* @param  int toID   (name id)
	* @return void
  */
  this.addRelation = function(typeID, fromID, toID) {
    var callId = jsrsCall(this.communicatorUrl, this.objectName + "._callbackAddRelation", "Bs_Om_OnomasticsServer.addRelation", typeID, fromID, toID);
  }
  
	
  /**
  * @see this.addRelation()
  */
  this._callbackAddRelation = function(value, callId) {
	}
	
	
  /**
	* @access public
	* @param  int typeID (relation type id)
	* @param  int fromID (name id)
	* @param  int toID   (name id)
	* @return void
  */
  this.deleteRelation = function(typeID, fromID, toID) {
    var callId = jsrsCall(this.communicatorUrl, this.objectName + "._callbackDeleteRelation", "Bs_Om_OnomasticsServer.deleteRelation", typeID, fromID, toID);
  }
  
	
  /**
  * @see this.deleteRelation()
  */
  this._callbackDeleteRelation = function(value, callId) {
	}
	
	
	/**
	* 
	*/
	this.findFirstname = function(name, gender, strict) {
		if ((typeof(name) == 'undefined') || (name == '')) return false;
		
		if (typeof(gender) == 'undefined') gender = 0;
		if (typeof(strict) == 'undefined') strict = false;
    var callId = jsrsCall(this.communicatorUrl, this.objectName + "._callbackFindFirstname", "Bs_Om_OnomasticsServer.findFirstname", name, gender, strict, true);
	}
	
	
  /**
  * @see this.findFirstname()
  */
  this._callbackFindFirstname = function(value, callId) {
		//alert(value);
		for (id in value) {
			alert(id);
		}
	}
	
	
}

