Ext.form.Action.Submit.prototype.run = Ext.form.Action.Submit.prototype.run.createInterceptor(function() {
  this.form.items.each(function(item) {
    if (item.el.getValue() == item.emptyText) {
      item.el.dom.value = '';
    }
    });
 });
Ext.form.Action.Submit.prototype.run = Ext.form.Action.Submit.prototype.run.createSequence(function() {
  this.form.items.each(function(item) {
    if (item.el.getValue() == '' && item.emptyText) {
      item.el.dom.value = item.emptyText;
    }
    });
 });


Ext.onReady(function(){
	var buttons = $$('input[type=submit]');
	for(var i = 0; i < buttons.length; i++)
	{
		var dh = Ext.DomHelper;
		dh.insertAfter(buttons[i],'<span id="button'+i+'_container"></span>');
		//$(buttons[i]).insert({after: '<span id="button'+i+'_container"></span>'});
		new Ext.Button({style:'display:inline;',renderTo:'button'+i+'_container',id:'button'+i,text:buttons[i].value, handler: function(){$(this.getId()+'_submit').click()}});
		buttons[i].setStyle("position:absolute;top:0;left:-3000px;display:block;width:10px;");
		buttons[i].id = 'button'+i+'_submit';
	}
	
	var textboxes = $$('input[type=text], input[type=password]');
	for(var i = 0; i < textboxes.length; i++)
	{
		new Ext.form.TextField({applyTo:textboxes[i]});
	}
	
	var selects = $$('select');
	for(var i = 0; i < selects.length; i++)
	{
		var select = selects[i].onchange;
		
		var formSubmitOnEnter = true;
		
		if(selects[i].hasClassName('enter_no_submit'))
		{
			formSubmitOnEnter = false;
		}
		var combo = new Ext.form.ComboBox({transform:selects[i],triggerAction: 'all', editable:false,forceSelection:true,cls:selects[i].className,tabIndex:selects[i].getAttribute('tabindex')});
		combo.on('select', select);
		if(!formSubmitOnEnter)
		{
			combo.on('beforeselect', function(el,record,i){
				this.setValue(record.data[this.valueField || this.displayField]);
	    	this.collapse();
				return false;
			});
		}
	}
});

Ext.app.SearchField = Ext.extend(Ext.form.TwinTriggerField, {
    initComponent : function(){
        Ext.app.SearchField.superclass.initComponent.call(this);
        this.on('specialkey', function(f, e){
            if(e.getKey() == e.ENTER){
                //this.onTrigger2Click();
            }
        }, this);
    },

    validationEvent:false,
    validateOnBlur:false,
    trigger1Class:'x-form-clear-trigger',
    trigger2Class:'x-form-search-trigger',
    hideTrigger1:true,
    width:200,
		emptyText: 'Quick Search',
    hasSearch : false,
    paramName : 'query',

    onTrigger1Click : function(){
        if(this.hasSearch){
            this.el.dom.value = '';
						searchField.setRawValue(this.getRawValue());
            $('search_form').submit();
        }
    },

    onTrigger2Click : function(){
        var v = this.getRawValue();
        if(v.length < 1){
            this.onTrigger1Click();
            return;
        }
				searchField.setRawValue(this.getRawValue());
				$('search_form').submit();
    }
});

function setAllCheckBoxes(FormName, FieldName, CheckValue)
{
  if(!document.forms[FormName])
    return;
  var objCheckBoxes = document.forms[FormName].elements[FieldName];
  if(!objCheckBoxes)
    return;
  var countCheckBoxes = objCheckBoxes.length;
  if(!countCheckBoxes)                                                                                                                                                                    
    objCheckBoxes.checked = CheckValue;
  else {
    // set the check value for all check boxes
    for(var i = 0; i < countCheckBoxes; i++)
      objCheckBoxes[i].checked = CheckValue;
  }
}

function submitForm(FormName, Url)
{
  if(!document.forms[FormName])
    return;
  document.forms[FormName].action = Url;
  document.forms[FormName].submit();
}

Ext.msg = new function(){
    var msgCt;

    function createBox(t, s){
				s = s || '';
        return ['<div class="msg">',
                '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
                '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', t, '</h3>', s, '</div></div></div>',
                '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
                '</div>'].join('');
    }
	  this.create = function(title, format, after){
				after = after || function(){};
        if(!msgCt){
            msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
        }
        msgCt.alignTo(document, 't-t');
        var s = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
        var m = Ext.DomHelper.append(msgCt, {html:createBox(title, s)}, true);
        m.slideIn('t').pause(2).ghost("t", {remove:true,callback:after});
    }
};
function MBA() {
}

MBA.prototype.init = function() {
  $$('a.add_to_list').each(function(node) {
    // Only process when anchor tag
    if(node.href == 'javascript:{}')
    {
      node.listId = node.title;
      node.title = 'Add to List';
      node.observe('click', mba.addToList.bindAsEventListener(MBA));
    }
  });
}

var win = null;
MBA.prototype.addToList = function(event) {
  var element = Event.element(event);
  win = new Ext.Window({
			title: 'Add to Lists',
      width:500,
			autoLoad:
				{
					url: '/list/popup/person_id/' + element.listId,
					callback: function(){
						new Ext.Button({
							style:'display:inline;',
							renderTo:'add_to_list_submit_b',
							text: 'Save Person to Lists', 
							handler: function()
							{
								$('add_to_list_submit').click();
							}
						});
					}
				}
   });
	win.show(this);
  /*var listPopupNode = $('listPopup');
  
  if (listPopupNode) {
    listPopupNode.innerHTML = '';
    listPopupNode.style.top = Event.pointerY(event) + 'px';
    listPopupNode.style.left = Event.pointerX(event) + 'px';
    //listPopupNode.style.display = 'none';
  } else {
    listPopupNode = document.createElement('DIV');
    if (!listPopupNode) {
      return;
    }

    listPopupNode.id = 'listPopup';
    listPopupNode.style.top = Event.pointerY(event) + 'px';
    listPopupNode.style.left = Event.pointerX(event) + 'px';
    //listPopupNode.style.display = 'none';
    document.body.appendChild(listPopupNode);
  }

  //Effect.Appear('listPopup');
  new Ajax.Updater('listPopup', '/list/popup/person_id/' + element.listId, {asynchronous:true, evalScripts:true});

  //Event.stop(event);
*/
}

var mba = new MBA();

Event.observe(window, 'load', mba.init, false);
//mba.init();
