//   
//Ext.ToolTip.prototype.show = function ()
//{
//	this.showAt();
//};
   
   
ASC.apply(Page, {
	
	yearList:null,
	startYears:null,
	endYears:null,
	
	init: function ()
	{
		this.initYears();
		
   		this.zipCodeValidator = new ASC.Chrysler.ZipValidator({ajax:this.ajax});
		ASC.apply(this.zipCodeValidator,{
			handleZipValidation: function (response, ioArgs)
			{			
				var isValidZipCode=(response.data=='true');
//				var isValidYearRange=Page.validateYearRange();		
				Page.validateFormat('lblZipCode','zipCode_ASC', !isValidZipCode);			
				var bRes = true;
//				if(isValidZipCode && isValidYearRange)	{
				if(isValidZipCode)	{
					var bRes = true;
				} else {
					var bRes = false;
				}
	
				var elMsg = ASC.getEl('msgZipCode');
				if(bRes){
					var zipCode = ASC.getEl("zipCode_ASC").getValue();
					ASC.Chrysler.Cookies.setZipCode(zipCode);
					elMsg.setDisplayed('none');
					ASC.getEl('yearStart').setValue(ASC.getEl('yearStartSel').getValue());
					ASC.getEl('yearEnd').setValue(ASC.getEl('yearEndSel').getValue());
					document.vehiclesearch_form.action="vehicleResults.do";
					document.vehiclesearch_form.onsubmit="";
					document.vehiclesearch_form.submit();
				}else{
					elMsg.setDisplayed('block');			
				}
			},
			handleOrigZipValidation: function (response, ioArgs)
			{
				if(this.validate()){			
					ASC.getEl('yearStart').setValue(ASC.getEl('yearStartSel').getValue());
					ASC.getEl('yearEnd').setValue(ASC.getEl('yearEndSel').getValue());
					document.vehiclesearch_form.action="vehicleResults.do";
					document.vehiclesearch_form.onsubmit="";
					document.vehiclesearch_form.submit();
				}
			}
		});
   		
   		
		var tt1 = new Ext.ToolTip({
		    target: 'carFax',
		    html: '<div>'+this.carFax +'</div>',
		    shadow: false,
		    mouseOffset: [-300,10],
		    dismissDelay: 0 
		});		
		this.setupDefault();
	},

	    showAt : function(xy){
        this.lastActive = new Date();
        this.clearTimers();
        Ext.ToolTip.superclass.showAt.call(this, xy);
        if(this.dismissDelay && this.autoHide !== false){
            this.dismissTimer = this.hide.defer(this.dismissDelay, this);
        }
    },
	
	setupDefault: function()
	{
		if (this.zipCode!='')
		ASC.getEl('zipCode_ASC').setValue(this.zipCode);
		if (this.maxMileage!='')
		ASC.getEl('maxMileage').setValue(this.maxMileage);
		if (this.modelDescription!='')
		ASC.getEl('modelDescription').setValue(this.modelDescription);
		if (this.yearStart!='')
		ASC.getEl('yearStart').setValue(this.yearStart);
		if (this.yearEnd!='')
		ASC.getEl('yearEnd').setValue(this.yearEnd);
		if (this.zipDistance!='')
		ASC.getEl('zipDistance').setValue(this.zipDistance);
		if (this.maxPrice!='')
		ASC.getEl('maxPrice').setValue(this.maxPrice);

		//alert(this.zipCode);
	},
	onEnterForZip:function(evt,id)
	{
		var isie = (document.all) ? true : false; 
		if(isie) evt = window.event;
		var key;
		key = isie?event.keyCode:evt.which;
		
		if(key==13)
			Page.submitForm(false,id);
	},
	stopEvent: function(e) {				
		  if(Ext.isIE) {					  
		    window.event.cancelBubble = true;
		    window.event.returnValue = false;
		  } else {
		    //e.preventDefault();
		    e.stopPropagation();
		  }
	},
	submitForm: function (){
		this.zipCodeValidator.validateZip('zipCode_ASC');
	},
	
	validate: function ()
	{
		//Zip Code must pass Zip Code validation defined in 2.2.02
		//alert("sdfs");
		var bRes = true;
		if(this.validateZipCode() && this.validateYearRange())
		{
			var bRes = true;
		}
		else
		{
			var bRes = false;
		}

		var elMsg = ASC.getEl('msgZipCode');
		if(bRes){
			elMsg.setDisplayed('none');
		}else{
			elMsg.setDisplayed('block');			
		}	
		//Year Range must pass validation defined in 2.2.06
		return bRes;
	},	
	validateZipCode: function()
	{
		var zipCode = ASC.getEl('zipCode_ASC');
		return this.validateFormat('lblZipCode','zipCode_ASC', !ASC.util.isZip(zipCode.getValue()));		
	},
	validateYearRange: function()
	{
		var yearEnd = ASC.getEl('yearEnd');
		var yearStart = ASC.getEl('yearStart');		
		return this.validateFormat('lblYearRange', 'yearEnd', (yearStart.getValue() > yearEnd.getValue()));		
	},
	
	validateFormat: function ( label, field, isInvalid)
	{
		var elLabel = ASC.getEl(label);
		var elField = ASC.getEl(field);
		
		if (isInvalid) {
			ASC.util.addClass(elLabel,"errorText");
			elField.dom.focus();
			return false;
		} else { 
			ASC.util.removeClass(elLabel,"errorText");
			return true;
		}
	},
	
	findADealer: function(){
		window.open( ASC.cfg.getContextPath() + "/fad/findDealers.do", "_parent");
	},
	
	dropDownOnChange: function(operation, yearListType){
		if( yearListType == "yearStartSel" ){
			var selIndex = ASC.getEl('yearStartSel').getValue();
			this.endYears.update(this.yearList, selIndex);
			this.startYears.setCurrentSelected(selIndex);
		}else if( yearListType == "yearEndSel" ){
			var selIndex = ASC.getEl('yearEndSel').getValue();
			this.startYears.update(this.yearList, selIndex);
			this.endYears.setCurrentSelected(selIndex);
		}
		this.dropDownMetrics('content',yearListType);
	}
});

ASC.onReady(Page.init, Page);
