Ext.namespace('RealEstate');
Ext.namespace('RealEstate.Rates');

    
RealEstate.Rates.Tabs = function(config){
	Ext.apply(this, config);
	this.initTabs();
};
RealEstate.Rates.Tabs.prototype = {  
	initTabs : function()
	{   
	    var aMortgageTab = Ext.get('re-tab-purchase');
	        aMortgageTab.on('click',function()
	                    {
	                        aMortgageTab.radioClass('re-tab-strip-active');
	                        this.purpose.dom.value='Purchase';
	                        var bSubmit=Ext.select('a.btnSubmit').elements[0]; 
	                        if(typeof(bSubmit)!='undefined'){
	                           location.href = bSubmit.href;
                             } 
	                    }
	                   ,this);
	
	    var aHomeEquityTab = Ext.get('re-tab-homeequity');
	        aHomeEquityTab.on('click',function()
	                    {
	                        aHomeEquityTab.radioClass('re-tab-strip-active');
	                        this.purpose.dom.value='Home Equity';
	                        var bSubmit=Ext.select('a.btnSubmit').elements[0]; 
	                        if(typeof(bSubmit)!='undefined'){
	                           location.href = bSubmit.href;
                             } 
	                    }
	                   ,this);
	    
	    var aRefinanceTab = Ext.get('re-tab-refinance');
	        aRefinanceTab.on('click',function()
	                   {
	                        aRefinanceTab.radioClass('re-tab-strip-active');
	                        this.purpose.dom.value='Refinance';
	                        var bSubmit=Ext.select('a.btnSubmit').elements[0]; 
	                        if(typeof(bSubmit)!='undefined'){
	                           location.href = bSubmit.href;
                             }
                       }
	                   ,this);
	    
	    Ext.get('re-tab-' + this.purpose.dom.value.replace(" ", "").toLowerCase()).radioClass('re-tab-strip-active');
	}
};
Ext.onReady(function(){    
     if (Ext.get(sCityControlId)) //make sure it is on the page
     {
         // simple array store

         var store = new Ext.data.SimpleStore({
              fields: ['name', 'count', 'type','id'],
              pruneModifiedRecords:true,
              proxy : new Ext.data.HttpProxy({url: '/handlers/autoCompleteProxy.ashx'})    

          });

          store.on('beforeload', function() {
            store.baseParams = {'query':Ext.get(sCityControlId).getValue()}                 
          });     
          
          var item_template = new Ext.XTemplate(
            '<tpl for="."><div class="x-combo-list-item"><span style="float:left">{name}</span></div></tpl>'
          );
          item_template.compile();
          
          var combo = new Ext.form.ComboBox({
                store: store,
                displayField:'name',
                valueField:'name',
                tpl: item_template,     
                typeAhead: false,
                loadingText: 'Searching...',
                mode: 'remote',
                forceSelection:false,
                hideTrigger:true, 
                triggerAction: 'all',
                width:170,
                emptyText:'\'City, ST\', or Zip',
                applyTo: sCityControlId,
                selectOnFocus:true,
                minChars:2,
                queryDelay:10,
                listeners:{
                    specialkey:function(f,o)
                    {
                        if(o.getKey()==13)
                        {
                            b = Ext.select('a.btnSubmit').elements[0];
                            eval(b.href);
                        }
                    }
                } 
            });
     } 
     if (Ext.get(sAmountControlId)) //make sure it is on the page 
     {
        new Ext.form.TextField({
            enableKeyEvents:true,
            allowBlank:false,
            regex:/[0-9]/,
            minLength:5,
            invalidText: 'Please enter numbers only',
            regexText: 'Please enter numbers only',
            applyTo:Ext.get(sAmountControlId)
        }); 
     }
    this.purposeElem =  Ext.get(sPurposeControlId);
    this.initialPurpose = purposeElem.dom.value;
    this.tabs = new RealEstate.Rates.Tabs({purpose: this.purposeElem});
});
 
function OnPurposeChanged(control)
{
    var typeElem = Ext.get(sTypeControlId);
    if (typeof(typeElem) != 'undefined')
    {
        var elems = Ext.select("div.noFilter");
        if (elems)
        {
            if (control.value != this.initialPurpose)
            { elems.hide();}
            else
            {elems.show();}
        }
        elems = Ext.select("div.noHomeEq");
        if (control.value == "Home Equity")
        {
            typeElem.dom.disabled=true;
            if (elems)
            { elems.hide();}
        }
        else
        {
            typeElem.dom.disabled=false;
            elems = Ext.select("div.Filter");
            if (elems)
            { elems.show();}
        }
        
    }
}

function ShowPop(id, trackUrl) 
{   var elem = Ext.get(id);
    if (typeof(elem) != 'undefined')
    {
        elem.show();
    }
    if (trackUrl && trackUrl.length >0)
    {
        var newImg = new Image();
        newImg.src = trackUrl;
    }
} 

function HidePop(id) 
{   var elem = Ext.get(id);
    if (typeof(elem) != 'undefined')
    {
        elem.hide();
    }

} 
