/*
 * Ext JS Library 2.2.1
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */


function createCustomGoalSearchBar() {
	var ds = new Ext.data.Store({
      proxy: new Ext.data.HttpProxy({
          url: '/suggestContractType.php'
      }),
      reader: new Ext.data.JsonReader({
          root: 'contractTypes',
          totalProperty: 'totalCount'
      }, [
          {name: 'contract_id', mapping: 'contractId'},
          {name: 'title', mapping: 'goalHighlighted'},
          {name: 'titleClean', mapping: 'contractTitle'}
      ])                               
  });

  // Custom rendering Template
  var resultTpl = new Ext.XTemplate(
      '<tpl for=".">',
        '<div class="search-item suggestedGoal" style="overflow:hidden;z-index:10;">',
            '<span class="listAutoComplete type_{contract_id}">{title}</span>',
        '</div>',
      '</tpl>'
  );
  
  Ext.goalSearch = new Ext.form.ComboBox({
      store: ds,
      //triggerAction: 'all',
      minChars: 3,
      triggerAction: 'query',
      displayField:'title',
      typeAhead: false,
      loadingText: 'Searching...',
      //width: 375,
      pageSize:0,
      tpl: resultTpl,
      applyTo: 'customContractSearch',
      itemSelector: 'div.search-item',
      hideTrigger: true,
      onSelect: function(record){
          customContractSearchSelected(record.data)
      },
      onEmptyResults: function(){
          this.collapse();
          //customContractSearchNoResultsFound();
      }
  });
}

function customContractSearchFocus(el) {
	if (el.value == $j(el).attr('defval')) {
		el.value = '';
		$j(el).removeClass('default');
	}
}

function customContractSearchBlur(el) {
	if (el.value == '') {
		el.value = $j(el).attr('defval');
		$j(el).addClass('default');
	}
}

function customContractSearchSelected(data) {
	location.href = '/members/commitment-create.php?step=form&type=' + data.contract_id + '&customTitle=' + escape(data.titleClean);
}

function customContractSearchNoResultsFound() 
{
  document.getElementById('searchForGoalsMessage').style.color = "#FF0000";
  document.getElementById('searchForGoalsMessage').innerHTML = "No results found, please try again.";
  setTimeout("showNormalSeachGoalsMessage()",3000);
}

function startCommitmentClick() {
	var titleFld = $("customContractSearch");
	
	if ($('predefinedGoal') && $('predefinedGoal').visible()) {
		if ($('predefinedGoal').value) {
			location.href = '/members/commitment-create.php?step=form&type=' + escape($('predefinedGoal').value);
		}
		else {
			displayAlert(get_constant('JS_PLEASE_CHOOSE_YOUR_GOAL'));
		}
	}
	else {
		customGoalStart();
	}
}

function customGoalStart() {
	var titleFld = $("customContractSearch");
	if (titleFld.value.length && titleFld.value != titleFld.readAttribute('defval')) {
		location.href = '/members/commitment-create.php?step=form&customTitle=' + escape(titleFld.value);
	}
	else {
		location.href = '/members/commitment-create.php?step=form';
	}
}

function customGoalGoClick() {
	customGoalStart();
}
