

//-------------------- mTextUpdate.js

TextUpdate = Class.create();

TextUpdate.prototype = {

   initialize: function(anIdPrefix, url, query, options) {
   		
   	this.groupIdPrefix = anIdPrefix;
      this.id          = anIdPrefix + options.updateSuffix;
      var browser = navigator.userAgent.toLowerCase();
      this.isIE        = browser.indexOf("msie") != -1;
      this.isOpera     = browser.indexOf("opera")!= -1;
      this.textInput   = $(this.id);
      this.setOptions(options);
      
      this.initAjax(url);
      this.lastRequestString = query;
      
      this.values = $A(this.options.valueArray);
      this.values.textUpdate = this;

      if (this.textInput) {
    	  this.normalStyleValue = getStyle(this.textInput.id, this.options.errorStyle);
      }
      
      this.executeUpdateBehavior();
   },
   
   initAjax: function(url) {
      ajaxEngine.registerRequest( this.id + '_request', url );
      ajaxEngine.registerAjaxObject( this.id + '_updater', this );
   },

   setOptions: function(options) {
      this.options = {
         errorStyle			 : 'color',
         errorStyleValue	 : '#CC0000',
         udpateSuffix		 :	'updateSuffix',
         valueArray         : new Array(),
         errorSuffix			 : 'errorSuffix'
      }
      
      Object.extend(this.options, options || {});
   },

   executeUpdateBehavior: function() {
   
   	this.values.each(this.clearElement.bind(this));
   		if (this.lastRequestString.length > 0) {
   			this.callRicoAjaxEngine();
   		} else {     	
     		this.setElement( this.groupIdPrefix + this.options.errorSuffix, ''); 
     		this.callRicoAjaxEngine();
    	}
   },
   
   callRicoAjaxEngine: function() {
      var callParms = [];
      callParms.push( this.id + '_request');
      callParms.push( 'id='             + this.id);
      callParms.push( 'query='          + this.lastRequestString);
     
      var additionalParms = this.options.requestParameters || [];
      for( var i=0 ; i < additionalParms.length ; i++ )
         callParms.push(additionalParms[i]);

      ajaxEngine.sendRequest.apply( ajaxEngine, callParms );
   },
   
   setElement: function(elementId, value) {
   	
   	var valueInput = $( elementId );
   	
   	if (valueInput) {
   		if (valueInput.type == 'text' || valueInput.type == 'hidden') {
   			valueInput.value  = value;
        } else if (valueInput.type == 'textarea') {
        	valueInput.value  = value;
        	valueInput.innerHTML  = value;
        } else {
        	valueInput.innerHTML  = value;
        }
      }
   },
   
    updateElement: function(valueSuffix, valueIndex) {  	  	
   	var element = this.entries[0].getElementsByTagName(valueSuffix)[0];  	
   	if (element) {
   		var value = this.getElementContent(this.entries[0].getElementsByTagName(valueSuffix)[0]);
   		this.setElement( this.groupIdPrefix + valueSuffix, value);	
   	}
   },
   
   clearElement: function(valueSuffix) {  
   	this.setElement( this.groupIdPrefix + valueSuffix, '');
   },

   ajaxUpdate: function( ajaxResponse ) {
 
     	var errorOutput = $( this.groupIdPrefix + this.options.errorSuffix );
    
      var entries = ajaxResponse.getElementsByTagName('entry');
 
      if (entries.length > 0) {
  
  			var strErrorText  = this.getElementContent(entries[0].getElementsByTagName('errorText')[0]);
 		
  			if (strErrorText) {		 
 				errorOutput.value = strErrorText;  
 				setStyle(errorOutput.id, this.options.errorStyle, this.options.errorStyleValue); 	
  			} else {

  				if (errorOutput) {
  					errorOutput.value = '';
  					setStyle(errorOutput.id, this.options.errorStyle, this.normalStyleValue); 
  				}
  				
  				this.entries = entries;
      		this.values.each(this.updateElement.bind(this));
  				this.entries = null;

  			}
             
      }     
   },

   getElementContent: function(element) {
   		
   		if (element.firstChild) {
   			return element.firstChild.data;
    	} else {
 	  		return null;		
    	}
   }

};

