/*
This is the javascript object that will handle zip code validations
*/	
ASC.namespace('ASC.Chrysler.ZipValidation');

ASC.Chrysler.ZipValidator=ASC.extend(Ext.util.Observable,{
	ajax:null,
	handleZipValidation:function(response, ioArgs){
		/*
			This is the default behavior when receiving validateZipCode ajax response
			override for each implementation
		*/		
	},
	handleOrigZipValidation:function(response, ioArgs){
		/*
			This is the default behavior when validateZipCode is not available or 
			a Failed response is received override for each implementation
		*/
	},
	constructor: function (config)
	{
		this.ajax=config.ajax;
		ASC.Ajax.ZipCodeCheckResponse = ASC.extend(ASC.Ajax.Response, {
			_failure: function (eRes, ioArgs) {
					var me = this;
					this.fireEvent('failure', {
						error: { 
							errorData: me.errors,										
							toString: function () {
								return this.errorData.join('\n');
							}
						},
						result: eRes        
					}, ioArgs);
			}
		});
		
		this.ajax.registerRequest("validateZipCode", ASC.cfg.getContextPath() + '/util/checkValidZipCode.ajax');
	    this.ajax.registerAjaxObject('isValidZipCode', new ASC.Ajax.ZipCodeCheckResponse({ 
	       events: {
					success: {
						fn: this._handleZipValidation,
						scope: this
					},
					failure: {
						fn: this._origZipValidation,
						scope: this
					}
				}				
	    })); 
	},
	
	_handleZipValidation: function (response, ioArgs)
	{
		this.handleZipValidation(response, ioArgs);
	},
	_handleOrigZipValidation: function (response, ioArgs)
	{
		this.handleOrigZipValidation(response, ioArgs);
	},

	
	validateZip:function(zipCodeElementId){
		var zipCode=ASC.getEl(zipCodeElementId).getValue();
		this.ajax.sendRequest('validateZipCode', {
		params: {
    			zipCode:zipCode,  
    			elementId:zipCodeElementId
			}
		});
	},
	
	isNumeric:function(controlId,locale){
	    var valid = ASC.util.isNumeric(ASC.getEl(controlId,locale).getValue());	

	    if(!valid){
	    	ASC.getEl(controlId).focus();
	    }
	    return valid;
	}
	
		
});
