
// script.aculo.us controls.js v1.6.4, Wed Sep 06 11:30:58 CEST 2006

// Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
//           (c) 2005 Ivan Krstic (http://blogs.law.harvard.edu/ivan)
//           (c) 2005 Jon Tirsen (http://www.tirsen.com)
// Contributors:
//  Richard Livsey
//  Rahul Bhargava
//  Rob Wills
// 
// See scriptaculous.js for full license.

// Autocompleter.Base handles all the autocompletion functionality 
// that's independent of the data source for autocompletion. This
// includes drawing the autocompletion menu, observing keyboard
// and mouse events, and similar.
//
// Specific autocompleters need to provide, at the very least, 
// a getUpdatedChoices function that will be invoked every time
// the text inside the monitored textbox changes. This method 
// should get the text for which to provide autocompletion by
// invoking this.getToken(), NOT by directly accessing
// this.aElement[0].value. This is to allow incremental tokenized
// autocompletion. Specific auto-completion logic (AJAX, etc)
// belongs in getUpdatedChoices.
//
// Tokenized incremental autocompletion is enabled automatically
// when an autocompleter is instantiated with the 'tokens' option
// in the options parameter, e.g.:
// new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' });
// will incrementally autocomplete with a comma as the token.
// Additionally, ',' in the above example can be replaced with
// a token array, e.g. { tokens: [',', '\n'] } which
// enables autocompletion on multiple tokens. This is most 
// useful when one of the tokens is \n (a newline), as it 
// allows smart autocompletion after linebreaks.

//if(typeof Effect == 'undefined')
//  throw("controls.js requires including script.aculo.us' effects.js library");

var Autocompleter = {}
Autocompleter.Base = function() {};
Autocompleter.Base.prototype = {
 // baseInitialize: function(table, element_pk, element_id, element_f1, element_f2, element_f3, element_f4, element_f5, width_id, width_f1, width_f2, width_f3, width_f4, width_f5, update, options) {
 baseInitialize: function(table, element_pk, options) {
    this.table	     = table;
    this.pk	     = $(element_pk);
    this.selectedPk = null;
    this.isLoaded = false;
    if(this.setOptions)
      this.setOptions(options);
    else
      this.options = options || {};
     
    this.aElement    = new Array();
    this.aWidth = new Array();

    var xElement = Element.nextSiblings(this.pk);
    this.updateWidth = 0;
    var i = 0;
    var j = 0;
    //2010-06-25 we never use pre-load anymore
    this.options.preLoad = 0;

    j = xElement.length-2
	
    for (i = 0; i < j; i++)
    	{
	this.aElement[i] = xElement[i];

	this.aWidth[i] = parseInt(this.aElement[i].style.width.replace('px', ''))
	this.updateWidth = this.updateWidth + this.aWidth[i];
	//2010-06-25 we don't lock Enumerators anymore by default
	//if (this.aElement[i].attributes.getNamedItem("pobtype").value == 'E' ||this.options.disabled==1)
	if (this.options.disabled==1)
	{
		disableField(this.aElement[i].id);
	}
	
    	}
        if ((this.options.comboboxStyle == 1 || this.options.lookUpButton == 1) && (1==1))
    	{
    		if (this.options.comboboxStyle == 1)
    			{
    			//var str = '<input style="POSITION: relative;LEFT: -17px;TOP: 2px;" type="image" name="BM_LOO" src="/PW_RES/combobox.gif" id="'+this.pk.id +'.BM_LOO" onclick="if (!window.opera) return false; else disableReturnSubmit();">'
    			var str = '<img style="POSITION: relative;LEFT: -17px;TOP: 2px;" name="BM_LOO" src="/PW_RES/combobox.gif" id="'+this.pk.id +'.BM_LOO"> '
    			if (this.options.preLoad == 1)
    				{
    				this.aElement[i-1].readOnly = true;
    				}
    			
    			if (this.options.disabled!=1)
			{
				Event.observe(this.aElement[i-1], "click", this.cbClick.bindAsEventListener(this));
			}
    				
    			
    			}
    		else
    			//var str = '<input style="POSITION: relative;LEFT: 0px;TOP: 1px;" type="image" name="BM_LOO" src="/PW_RES/lookup.gif" id="'+this.pk.id +'.BM_LOO"> onclick="return false"'
    			var str = '<img style="POSITION: relative;LEFT: 0px;TOP: 1px;" name="BM_LOO" src="/PW_RES/lookup.gif" id="'+this.pk.id +'.BM_LOO">'
    			
    		var par = this.aElement[i-1].parentNode;
    		while (par.tagName != 'TD' && par != null)
    		{
    			par = par.parentNode;
    		}
    		//Work Around for e.g. Nat. ID
    		if (par.width != '' && par.width -17 <= this.aWidth[i-1])
    		{
    			this.aElement[i-1].style.width = par.width -17

    		}
		new Insertion.After(this.aElement[i-1], str);
		this.bm_loo = $(this.pk.id +'.BM_LOO')
		if (this.options.disabled!=1)
			{
			Event.observe(this.bm_loo, "click", this.cbClick.bindAsEventListener(this));
			}
		Event.observe(this.bm_loo, "focus", this.onfocus.bindAsEventListener(this));
		Event.observe(this.bm_loo, "blur", this.onBlur.bindAsEventListener(this));
	
	}
   
    this.update = $('AJAX_UPDATE')
    if (this.update == null)
    	this.update      = xElement[i];
    
    this.returnHTML = true;
    this.autoSetting = false;
    if ((this.pk.value != '') && (this.pk.value != '0')) 
    	{
    	this.setValid();
	}
    else	
    	{
    	this.isvalid = 0;
    	}
    if (this.options.useDiagnose==1 || this.options.autoDiagnose==1)
    
    	{
            //var str = '<input type="image" align="top" name="BM_LOO" src="/PW_RES/pbdia.gif" id="'+this.pk.id +'.BM_DIA">'	
            var str = '<img style="POSITION: relative;LEFT: 0px;TOP: 5px;" name="BM_DIA" src="/PW_RES/pbdia.gif" id="'+this.pk.id +'.BM_DIA"> '
            
            if (this.bm_loo)
            	new Insertion.After(this.bm_loo, str);
            else
    	    	new Insertion.After(this.aElement[i-1], str);
	    this.diagnose = $(this.pk.id +'.BM_DIA')
	    Event.observe(this.diagnose, "click", this.diagnoseClick.bindAsEventListener(this));
	    Element.hide(this.diagnose)
	    if ((this.options.autoDiagnose == null) || (this.options.autoDiagnose != 1))
	    	this.getDiagnoseMemo();
	}
	
    //
    var str = '<input style="display:none" type="button" name="BM_LOO" id="'+this.pk.id +'.BM_SET">'	
    new Insertion.After(this.aElement[i-1], str);
    this.bm_set = $(this.pk.id +'.BM_SET')
    Event.observe(this.bm_set, "click", this.setValue.bindAsEventListener(this));
    //

    this.hasFocus    = false; 
    this.forceFocus  = false; 
    this.changed     = false; 
    this.active      = false; 
    this.index       = 0;     
    this.entryCount  = 0;
    
    this.options.autoSelect = true;
    this.options.autoSelect    = this.options.autoSelect || true;
    
    this.selectFirst = false;

    this.options.frequency    = this.options.frequency || 0.4;
    this.options.minChars     = this.options.minChars || 1;
    this.options.onShow       = this.options.onShow || 
      function(element, update, width){ 
        if(!update.style.position || update.style.position=='absolute') {
          update.style.position = 'absolute';
          Position.clone(element, update, {
            setHeight: false, 
            offsetTop: element.offsetHeight
          });
        }
        Effect.Appear(update,{duration:0.15});
        //update.style.width = width;
	if ($('AJAX_UPDATE') != null)
        	update.style.width = width;
        else
        	update.style.width = '80%';
      };
    this.options.onHide = this.options.onHide || 
      function(element, update){ new Effect.Fade(update,{duration:0.15}) };
    this.options.afterUpdateElement = this.options.afterUpdateElement || 
      function(autoCompleter){ 
      	//Default 
      };

    this.observer = null;
    
    this.pk.focus =   this.setIdFocus;
    this.pk.select = this.selectId;
    
    //var att = document.createAttribute('forceValidRefto');
    //att.value = 'false';
    //this.pk.setAttributeNode (att);
    //delete att
    this.forceValidRefto = false;
    //
    
    
    
    var att = document.createAttribute('showlist');
    att.value = 'false';
    this.pk.setAttributeNode (att);
    delete att
    

    
    
    
    //this.pk.forceValidRefto = false;
    //this.pk.showlist = false;

    Element.hide(this.update);
    
    for (i = 0; i < this.aElement.length; i++)
    	{
       	this.aElement[i].setAttribute('autocomplete','off');
       	Event.observe(this.aElement[i], "focus", this.onfocus.bindAsEventListener(this));
       	if (this.options.disabled!=1)
       		{
	       	Event.observe(this.aElement[i], "blur", this.onBlur.bindAsEventListener(this));
	    	Event.observe(this.aElement[i], "keypress", this.onKeyPress.bindAsEventListener(this));
	    	//Event.observe(this.aElement[i], "focus", this.onfocus.bindAsEventListener(this));
	        
	        Event.observe(this.aElement[i], "cut", this.onCutPaste.bindAsEventListener(this));
	        Event.observe(this.aElement[i], "paste", this.onCutPaste.bindAsEventListener(this));
		}
		
	}
	
	Event.observe(this.pk, "click", this.setValue.bindAsEventListener(this));
	//OPERA
	if(window.document.forms[0] && window.opera && gOperaPressReturn == null) {
		gOperaPressReturn = false;
		Event.observe(window.document.forms[0], 'submit', operaOnSubmit.bindAsEventListener(this));
		}	
	//
   
  },
 
  
  onCutPaste: function()
  {
  if (this.isvalid ==1)
	{
	this.clearRefto();
	}
  this.changed = true;		
  if(this.observer) clearTimeout(this.observer);
  this.observer = 
        	setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000);
},
  
  diagnoseClick: function()
  {
    RefMemToAddText(this.diagnoseMemo)	
  },
  
  cbClick: function()
  {
    this.hasFocus    = true; 
    $(this.pk.id).focus();	
    setfocusctrl(this.pk.id);
    this.selectedPk = this.pk.value;
    this.getUpdatedChoices();
  },
  
  setIdFocus: function()
  {
  	
  	this.nextSibling.focus();
  },
  
  
  setValue: function(event)
  {
	//if (this.pk.forceValidRefto == true)
	//if (this.pk.attributes.getNamedItem("forceValidRefto").value == 'true')
  	//	this.autoSetting = false;
  	//else
  	//	this.autoSetting = true;
  	if (this.forceValidRefto == true)
  		this.autoSetting = false;
  	else
  		this.autoSetting = true;
  	//
  	
  	//used by setreftoresult
	if (this.pk.attributes.getNamedItem("forceValidRefto") != null)
		{
  		if (this.pk.attributes.getNamedItem("forceValidRefto").value == 'true')
  			{
  			this.autoSetting = false;
  			this.forceValidRefto = true;
  			this.pk.attributes.getNamedItem("forceValidRefto").value == 'false';
  			}
  		}
  	

  	//if (this.pk.showlist == true) 
  	if ((this.pk.attributes.getNamedItem("showlist").value == 'true') || (this.pk.attributes.getNamedItem("showlist").value == 'list'))
  	{
  		this.hasFocus    = true; 
  		this.pk.value = '';
  		this.forceFocus    = true; 
		this.getUpdatedChoices();
		if (this.pk.attributes.getNamedItem("showlist").value == 'list')
			{
			this.autoSetting = false;	
			this.pk.attributes.getNamedItem("showlist").value = 'false';
			if (event != null)
				Event.stop(event);
			return false;
			}
		else
			this.pk.attributes.getNamedItem("showlist").value = 'false';
			
	
  	}
  	if ((this.pk.value == '0')||(this.pk.value == ''))
  		{
  		this.clearRefto();
  		}
	else
  		this.getUpdatedChoices();
  	if (event != null)
         Event.stop(event);
  },
  
   showList: function()
  {	
  	
  	{
	this.hasFocus    = true; 
  	this.pk.value = '';
  	this.forceFocus    = true; 
	this.getUpdatedChoices();
}
  },
selectId: function(pthis)
  {
  	this.value = '';
  	//this.showlist = true;
  	
  	this.attributes.getNamedItem("showlist").value = 'true';
  	var bmSet = gEBI(this.id+'.BM_SET')
	if (bmSet != null)
		{
		bmSet.click();
		}
 	
  },  

  show: function() {
    if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.aElement[0], this.update, this.updateWidth);
    if(!this.iefix && 
      (navigator.appVersion.indexOf('MSIE')>0) &&
      (navigator.userAgent.indexOf('Opera')<0) &&
      (Element.getStyle(this.update, 'position')=='absolute')) {
      new Insertion.After(this.update, 
       '<iframe id="' + this.update.id + '_iefix" '+
       'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
       'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
      this.iefix = $(this.update.id+'_iefix');
    }
    if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50);
  },
  
  fixIEOverlapping: function() {
    Position.clone(this.update, this.iefix, {setTop:(!this.update.style.height)});
    this.iefix.style.zIndex = 1;
    this.update.style.zIndex = 2;
    Element.show(this.iefix);
  },

  hide: function() {
    this.stopIndicator();
    if(Element.getStyle(this.update, 'display')!='none') this.options.onHide(this.aElement[0], this.update);
    if(this.iefix) Element.hide(this.iefix);
  },

  startIndicator: function() {
    if(this.options.indicator) Element.show(this.options.indicator);
  },

  stopIndicator: function() {
    if(this.options.indicator) Element.hide(this.options.indicator);
  },

onfocus: function(event) {
	this.hasFocus    = true; 
	setfocusctrl(this.pk.id);
},
  

  onKeyPress: function(event) {
    if(this.active)
      {
      switch(event.keyCode) {
       //case Event.KEY_TAB:
       case Event.KEY_RETURN:
       	//OPERA
         disableReturnSubmit();
         this.selectEntry();
         Event.stop(event);
         
       case Event.KEY_ESC:
         this.hide();
         this.active = false;
         Event.stop(event);
         return;
       case Event.KEY_LEFT:
       case Event.KEY_RIGHT:
         return;
       case Event.KEY_UP:
         this.markPrevious();
         this.render();
         if(navigator.appVersion.indexOf('AppleWebKit')>0) Event.stop(event);
         return;
       case Event.KEY_DOWN:
         this.markNext();
         this.render();
         if(navigator.appVersion.indexOf('AppleWebKit')>0) Event.stop(event);
         return;
	}
	}
     else 
     	var keyCode = event.keyCode;
     	if (event.srcElement)
     		var osrcElement = event.srcElement
     	else if (event.target)
     		var osrcElement = event.target.nodeName;
     	
     	if (event.keyCode==Event.KEY_DOWN)
     		this.cbClick();
     		
     	else if ((event.keyCode==Event.KEY_RETURN)){
     		//OPERA
     		disableReturnSubmit();
     		
     		if ((event.shiftKey))
     			{
     			this.getUpdatedChoices();
     		}
     		else
     			{
     			if (osrcElement == this.aElement[0])
     				{
     				this.selectFirst = true;
     				}
     			this.getUpdatedChoices();
     			}
     		
     		Event.stop(event);
     	}
     	if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN || 
         	(navigator.appVersion.indexOf('AppleWebKit') > 0 && event.keyCode == 0)) {return};
     	{
     	
     	
     	
     	if ((changeKey(event) == true) && osrcElement.readOnly != true)
     		{
		if (this.isvalid ==1)
			{
			this.clearRefto();
			}
		this.changed = true;		
		if(this.observer) clearTimeout(this.observer);
      		this.observer = 
        	setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000);
     		}
     	if ((event.keyCode==Event.KEY_BACKSPACE)&&(osrcElement.readOnly == true))
     		{
     		event.returnValue = false;
		event.cancelBubble = true;
		return false;
     		}
    }
    this.hasFocus = true;
  },

  activate: function() {
    this.changed = false;
    this.hasFocus = true;
    this.getUpdatedChoices();
  },

  onHover: function(event) {
    if (this.active==true)
   	{
	    var element = Event.findElement(event, 'TR');
	    if(this.index != element.autocompleteIndex) 
	    {
	        this.index = element.autocompleteIndex;
	        this.render();
	    }
	    Event.stop(event);
	}
  },
  
  onClick: function(event) {
    var element = Event.findElement(event, 'TR');
    this.index = element.autocompleteIndex;
    this.selectEntry();
    this.hide();
  },
  
  onBlur: function(event) {
    // needed to make click events working
    if (this.active)
    	{
    	if (this.pk.value =='')
    		{
    		this.setInvalid();	
    		}	
    	}
    setTimeout(this.hide.bind(this), 250);
    this.hasFocus = false;
    this.active = false;     
    
  }, 
  
  render: function() {
    if ((this.hasFocus == false) && (this.forceFocus==true) && (this.entryCount > 1))
    	{
    	this.pk.focus();
    	this.hasFocus = true;
    	this.forceFocus = false;
	}
    if(this.entryCount > 0) {
      for (var i = 0; i < this.entryCount; i++)
        this.index==i ? 
          Element.addClassName(this.getEntry(i),"lookUpSelected") : 
          Element.removeClassName(this.getEntry(i),"lookUpSelected");
      //Fix for I.E. when e.g. ROO_EXTRA is styled with class.
      if ($('content'))
          $('content').className = $('content').className;  
      if(this.hasFocus ) { 
        this.show();
        this.active = true;
      }
else
{
	this.updateElement  (this.getEntry(0))	
}
    } else {
      this.active = false;
      this.hide();
    }
  },
  
  markPrevious: function() {
    if(this.index > 0) this.index--
      else this.index = this.entryCount-1;
    this.getEntry(this.index).scrollIntoView(true);
  },
  
  markNext: function() {
    if(this.index < this.entryCount-1) this.index++
      else this.index = 0;
    this.getEntry(this.index).scrollIntoView(false);
  },
  
  getEntry: function(index) {
    return this.update.firstChild.firstChild.childNodes[index];
  },
  
  getCurrentEntry: function() {
    return this.getEntry(this.index);
  },
  
  selectEntry: function() {
    this.active = false;
    this.updateElement(this.getCurrentEntry());
  },

  updateElement: function(selectedElement) {
    if (this.options.updateElement) {
      this.options.updateElement(selectedElement);
      return;
    }
    var value = '';
    value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal');
    var selectedChildNodes = selectedElement.childNodes;
    for (var i = 0; i < selectedChildNodes.length; i++)
    {
    	var newvalue;
        //if (selectedChildNodes[i].firstChild != null)
    	if ((selectedChildNodes[i].firstChild != null) && (selectedChildNodes[i].firstChild.nodeValue != null))
    		newvalue = selectedChildNodes[i].firstChild.nodeValue;
    	else
    		newvalue = '';

	if (i==0) 
		this.pk.value = newvalue;
	else 
		this.aElement[i-1].value = newvalue;
    }
    this.setValid();
    if (this.autoSetting == false)
    	{
    	if ((this.options.validRefto==1) ||(this.options.validRefto==3) ) 
    		{
    		this.userValidRefto();
		}
	}
	
    else
    	{
    	if (this.options.useDiagnose==1 || this.options.autoDiagnose==1)
    		{
    		this.getDiagnoseMemo();	
    		}
    		
    	this.autoSetting = false;
	}
     
     if (this.options.validRefto==2)
	{
    	try {
    		GUIUserValidRefto(this);
    		} 
    	catch (e) { }
	}
    
    if (this.options.afterUpdateElement)
    	//This could be some valid refto??
      this.options.afterUpdateElement(this);
      
    if (this.options.jsEvent)
    	{
    	eval(this.options.jsEvent);
	} 

  },

  updateChoices: function(choices) {
    if(!this.changed ) {

      this.update.innerHTML = choices;
      Element.cleanWhitespace(this.update);
      Element.cleanWhitespace(this.update.firstChild);
      this.index = 0;
      if(this.update.firstChild.firstChild && this.update.firstChild.firstChild.childNodes) {

	this.entryCount = 
          this.update.firstChild.firstChild.childNodes.length;          
        for (var i = 0; i < this.entryCount; i++) {
          var entry = this.getEntry(i);
          if (this.selectedPk != null)
          	{
	        if (entry.firstChild != null)
	          	{
	    		var newvalue = entry.firstChild.firstChild.nodeValue;
	    		if (newvalue == this.selectedPk)
	    			this.index = i;
	    		}
	    	}
          entry.autocompleteIndex = i;
          this.addObservers(entry);
        }
      } else { 
        this.entryCount = 0;
      }
      this.selectedPk = null;
      this.stopIndicator();
      if(this.entryCount==1 && this.options.autoSelect) {
        this.selectEntry();
        this.hide();
      } else 
      {
      	if (this.selectFirst)
      		{
      		this.selectFirst = false;
      		var entry = this.getEntry(0);
      		if (entry.firstChild.nextSibling.firstChild.nodeValue != null)
      		if (entry.firstChild.nextSibling.firstChild.nodeValue.toUpperCase()==this.aElement[0].value.toUpperCase())
      			{
      				this.hasFocus = false;
      				this.hide();
      				this.selectEntry();
     			}
      		}
        this.render();
      }
     if (this.entryCount<1)
	{
		this.setInvalid();
	}
    }
  },

  addObservers: function(element) {
    Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this));
    Event.observe(element, "click", this.onClick.bindAsEventListener(this));
  },

  onObserverEvent: function() {
    this.changed = false;   
    
    if(this.getToken().length>=this.options.minChars) {
      this.startIndicator();
       this.getUpdatedChoices();
    } else {
      this.active = false;
      this.hide();
    }
  },

  getToken: function() {
     var ret = '';
     for (var i = 0; i < this.aElement.length; i++)
    	{
       	ret = ret + this.aElement[i].value;
	} 
    return /\n/.test(ret) ? '' : ret;
  }
}

Ajax.Autocompleter = Class.create();

Object.extend(Object.extend(Ajax.Autocompleter.prototype, Autocompleter.Base.prototype), {
  initialize: function(table, element_pk, url, options ) {
    this.url                   = url;
    this.baseInitialize(table, element_pk, options);
    this.options.asynchronous  = true;
    this.options.defaultParams = this.options.parameters || null;
    
    
  },

  getUpdatedChoices: function() {
//alert('Get UPD')  	
if ($('ISSUBMITTING'))
    	if ($('ISSUBMITTING').value == '1') return;  	
  	
    if (this.options.preLoad == 0 || this.isLoaded == false)
    	{
	this.options.onComplete    = this.onComplete.bind(this);
	this.options.method = 'get';
	var crit =	this.buildSearchCrit();
	crit = encodeURIComponent(crit);
	if (this.options.setSqlWhere == 1)
	{   
		var data1 = makedata1xml();
		this.options.requestHeaders  = ['DATA1', data1]
		var surl = this.url+'ajax_lookup.lookbycr?'+crit;
	}
	else
	{
		var surl = this.url+'ajax_lookup.lookbycr?'+crit
	}
	new Ajax.Request(surl, this.options);
	}
    else
    	{
    		this.onComplete(this.responseText);
    	}
  },

  onComplete: function(request) {
    if (this.options.preLoad == 1)
    	{
    		if (this.isLoaded == false)
    			{
    			this.isLoaded = true;
    			this.responseText = request.responseText;
    			}
    		this.updateChoices(this.responseText);
	}
    else
    	this.updateChoices(request.responseText);
    },
  
  setInvalid: function() {
  	for (var i = 0; i < this.aElement.length; i++)
    	{
         Element.addClassName(this.aElement[i],"lookUpInvalid") ; 
	}	
         //Fix for I.E. when e.g. ROO_EXTRA is styled with class.
         if ($('content'))
         	$('content').className = $('content').className;	
  
},

clearRefto: function() {
	
	this.isvalid =0;
	this.pk.value = '';

	for (var i = 1; i < this.aElement.length; i++)
		{
		//2010-06-25 we don't lock Enumerators anymore by default
		//if (this.aElement[i].attributes.getNamedItem("pobtype").value != 'E' && this.options.disabled!=1)
		if (this.options.disabled!=1)
			{
			clearEnableField (this.aElement[i].id);
			}
		else
			{

			this.aElement[i].value = '';
			}
		
		}	
	if (this.options.useDiagnose==1)
		if (Element.visible(this.diagnose)==true)
			{
			Element.hide(this.diagnose);
			var i = this.aElement.length-1;
			var width = parseInt(this.aElement[i].style.width.replace('px', ''))
			width = width +21;
			this.aElement[i].style.width = width;
					
			}
	if (this.autoSetting == false)
		{

		if ((this.options.validRefto==1) ||(this.options.validRefto==3) ) 
   			{
   			this.userValidRefto();
			}
		else if (this.options.validRefto==2)
			{
			try {
		    		GUIUserValidRefto(this);
		    		} 
		    	catch (e) { }
			}
			
		}
	else
		{

		if (this.options.disabled !=1)
			clearEnableField (this.aElement[0].id);	
		else
			this.aElement[0].value ='';
			
		this.autoSetting = false;
	}
  
},

setValid: function() {
this.isvalid = 1;
for (var i = 0; i < this.aElement.length; i++)
    	{
         	Element.removeClassName(this.aElement[i],"lookUpInvalid") ; 
         	if (i>0)
         	{
         		disableField (this.aElement[i].id);
	 	}	
	 }
//Fix for I.E. when e.g. ROO_EXTRA is styled with class.
if ($('content'))
	$('content').className = $('content').className;  	
  
},
  
  buildSearchCrit: function() {

  //dom = new ActiveXObject("Microsoft.XMLDOM");
  dom = pobDom();
  dom.preserveWhiteSpace = true;
  var root = dom.createElement(this.pk.id);
  dom.appendChild (root)

var att = dom.createAttribute('TABLE');
att.value = this.table;
root.setAttributeNode (att);
delete att	  
  if ((this.pk.value !='') && (this.pk.value !='0') && this.selectedPk == null)
  	{
  	var att = dom.createAttribute('ID');
	att.value = this.pk.value;
  	root.setAttributeNode (att);
  	delete att
	}


	var att = dom.createAttribute('HTML');
	att.value = true;
  	root.setAttributeNode (att);
  	delete att
  	if (this.options.xWhere)
  		{
  		var att = dom.createAttribute('XWHERE');
		att.value = this.options.xWhere
  		root.setAttributeNode (att);
  		delete att
  		
  		}
  	
  	for (var i = 0; i < this.aElement.length; i++)
		{
		var col = dom.createElement(this.aElement[i].id.replace(this.pk.id, '').substr(1));
		
		var att = dom.createAttribute('VALUE');
	        if (this.selectedPk == null || this.selectedPk == '')
	        {
	        	if (UnicodeUsed)
	        	      att.value = UTF8.encode(this.aElement[i].value);
			else
				att.value = this.aElement[i].value;
			
		}
		else
			att.value = '';
  		col.setAttributeNode (att);
  		delete att;
  		
  		var att = dom.createAttribute('COLNO');
		att.value = i;
  		col.setAttributeNode (att);
  		delete att
  		
  		var att = dom.createAttribute('TYPE');
		//att.value = this.aElement[i].pobtype;
		att.value = this.aElement[i].attributes.getNamedItem("pobtype").value;
  		col.setAttributeNode (att);
  		delete att
  		
  		if (this.aElement[i].attributes.getNamedItem("sql"))
  			{

  			var att = dom.createAttribute('SQL');
			att.value = this.aElement[i].attributes.getNamedItem("sql").value;
  			col.setAttributeNode (att);
  			delete att
  			}
  		
  		var att = dom.createAttribute('W');
		att.value = this.aWidth[i];
  		col.setAttributeNode (att);
  		delete att
  		
  		if (this.aElement[i].attributes.getNamedItem("pobtype").value == 'E')
  			{
			var att = dom.createAttribute('ENUM');
			att.value = this.aElement[i].pobenum;
			col.setAttributeNode (att);
			delete att

  			}
		root.appendChild(col);  		
		delete col;

  		}

  var xml2 = serialize (dom)
  delete table
  delete root
  delete dom
  return xml2
},

  buildResultList: function(xml) {

var dom;
var list = '<ul style="list-style: none; margin: 4; padding: 0;">';
dom = parse(xml);
var root = dom.firstChild;
for (var j = 0; j < root.childNodes.length; j++)
	{
	list = list + '<li>';
	row = root.childNodes[j];
	var attributes = row.attributes;
	for (var i = 0; i < attributes.length; i++)
		{

		if (i == 0)
			list = list + '<span style="display: none">' + attributes.getNamedItem("ID").value + '</span>';
		else 
			{
			list = list + '<span valign="top" style="width: '+this.aWidth[i-1]+'px;">' + attributes.getNamedItem(this.aElement[i-1].id.replace(this.pk.id, '').substr(1)).value + '</span>';
			}
		}
	list = list + '</li>';
	}

list = list + '</ul>';
return list
},

userValidRefto: function()
{
if ($('ISSUBMITTING'))
    	if ($('ISSUBMITTING').value == '1') return;
   this.options.onComplete    = this.onCompleteValidRefto.bind(this);

    //entry = encodeURIComponent(this.options.paramName) + '=' + 
    //  encodeURIComponent(this.getToken());
    //this.options.parameters = this.options.callback ?
    //  this.options.callback(this.aElement[0], entry) : entry;

    //if(this.options.defaultParams) 
    //  this.options.parameters += '&' + this.options.defaultParams;
    this.options.method = 'get';
    if (this.options.useDiagnose == 1 || this.options.autoDiagnose==1)
        var crit =	'<COLUMN DIA="1"><' +this.pk.id+' COLUMNVALUE="'+this.pk.value+'" /></COLUMN>'
    else
    	var crit =	'<COLUMN><' +this.pk.id+' COLUMNVALUE="'+this.pk.value+'" /></COLUMN>'
    crit = encodeURIComponent(crit);
    var data1 = makedata1xml();
    this.options.requestHeaders  = ['DATA1', data1];
    var surl = this.url+'ajax_lookup.valrefto?'+crit;
    new Ajax.Request(surl, this.options);
	
},

onCompleteValidRefto: function(request) {
var dom;
dom = parse(request.responseText);
var root = dom.firstChild;
var node = root.firstChild;

while (node != null)
{
	if (node.nodeName == this.pk.id)
		{
		var attributes = node.attributes;
		for (var i = 0; i < attributes.length; i++)
			{
			if (attributes[i].name == 'DIAGNOSEMEMO')
				{
				this.diagnoseMemo = attributes[i].value;
				this.showDiagnose();
				}
			else if (attributes[i].name == 'AUTOHIST')
				{
				LB_RES.xwhere = attributes[i].value;
				LB_RES.options.headeronly = 0;
				LB_RES.getView();
				gEBI('CURRENTTAB').value = 'WCAS000R'
				displayTab(gEBI('TAB'+gEBI('CURRENTTAB').value), gEBI('CURRENTTAB').value);
				}
			}
		}			
	else 
		{
		var attributes = node.attributes;
		for (var i = 0; i < attributes.length; i++)
			{
			if (gEBI(attributes[i].name))
				{
				if (gEBI(attributes[i].name).type == 'checkbox')
					{
					if (attributes[i].value == '1')
						{
						gEBI(attributes[i].name).checked = true;
						}
					else
						gEBI(attributes[i].name).checked = false;
					
					}
				else
					gEBI(attributes[i].name).value = (attributes[i].value);
//var a = gEBI(attributes[i].name);
//debugger;
//alert(gEBI(attributes[i].name));
				var bmSet = gEBI(attributes[i].name+'.BM_SET')
				if (bmSet != null)
					{
					bmSet.click();
					}
				}
			}  	
		}
	node = node.nextSibling;
}
	
if (this.options.validRefto==3)
	{
	//NO Error if GUIUserValidRefto does not exist 
	try {
    		GUIUserValidRefto(this);
    		} 
    	catch (e) { }
	}  
  },
showDiagnose: function() {  
	if (this.options.useDiagnose == true)
	{
		if (Element.visible(this.diagnose)==false)
			{
			Element.show(this.diagnose);
			var i = this.aElement.length-1;
			var width = parseInt(this.aElement[i].style.width.replace('px', ''))
			width = width -21;
			this.aElement[i].style.width = width;
			}
	}
	else
	{
		this.diagnose.click();
		this.diagnoseMemo = '';
	}
	
},

getDiagnoseMemo: function()
{
if ($('ISSUBMITTING'))
    	if ($('ISSUBMITTING').value == '1') return;	
   this.options.onComplete    = this.onCompleteGetDiagnoseMemo.bind(this);
    this.options.method = 'get';
    var crit =	'<' +this.table+' ID="'+this.pk.value+'"></'+this.table+'>'
    crit = encodeURIComponent(crit);
    var surl = this.url+'ajax_lookup.getdiag?'+crit
    //We dont care about theese
    Ajax.activeRequestCount--;
    new Ajax.Request(surl, this.options);	
	
},
onCompleteGetDiagnoseMemo: function(request) {
//We dont care about theese
Ajax.activeRequestCount++;	
var dom;
dom = parse(request.responseText);
var root = dom.firstChild;
var attributes = root.attributes;
for (var i = 0; i < attributes.length; i++)
	{
	if (attributes[i].name == 'DIAGNOSEMEMO')
		{
		this.diagnoseMemo = attributes[i].value;
		this.showDiagnose();
		}
	}  	
}

});

