var DealerSearch = new Class({
	options: {
		minZipLength: 5,
		maxZipLength: 5,
		checkNumericZip: 1
	},
	/**
	 * Initializes the dealer search.
	 * @param {Object} dealerSubmit				submit button for the search
	 * @param {Object} dealerSearchForm			form for the dealer search 
	 * @param {String} zip						value of the zip field
	 * @param {String} searchType				value of the type selection
	 * @param {String} radius					value of the radius field
	 * @param {String} dealerResultContainer	id of div that will contain the result div
	 * @param {String} resultContainerClass		class for the result div
	 * @param {String} targetUrl				URL to call by ajax
	 * @param {String} loadingMessage			message to be displayed while loading data
	 * @param {String} zipErrorMessage			error message for invalid zip
	 * @param {String} ajaxErrorMessage			error message for failed ajax call
	 */
	initialize: function(dealerSubmit, dealerSearchForm, zip, searchType, radius, dealerResultContainer, resultContainerClass, targetUrl, loadingMessage, zipErrorMessage, ajaxErrorMessage, options, dealerFile) {
		this.setOptions(options);
		// set class members
		this.dealerSubmit = dealerSubmit;
		this.zip = zip;
		this.searchType = searchType;
		this.radius = radius;
		this.dealerResultContainer = dealerResultContainer;
		this.resultContainerClass = resultContainerClass;
		this.targetUrl = targetUrl;
		this.loadingMessage = loadingMessage;
		this.zipErrorMessage = zipErrorMessage;
		this.ajaxErrorMessage = ajaxErrorMessage;
		this.dealerFile = dealerFile;
		
		// add event handler		
		dealerSubmit.addEvent('click', this.prepareSearch.bind(this));
		dealerSearchForm.addEvent('submit', this.prepareSearch.bind(this));
	},
	
	/**
	 * Prepares the search.
	 * @param {Object} e
	 */
	prepareSearch: function(e) {
		// stop submit
		new Event(e).stop();
		
		// get the values
		var zip = $(this.zip).value;
		var radius = $(this.radius).value;
		var searchType = $(this.searchType).value;
		
		// check zip field
		if (!this.isValidZip(zip)) {
			alert(this.zipErrorMessage);
		} else {
			// empty base container
			$(this.dealerResultContainer).empty();
			
			// create result container
			this.resultContainer = new Element('div').injectInside(this.dealerResultContainer);
			this.resultContainer.empty().addClass('ajax-loading');
			
			// add info text
			this.resultContainerInfo = new Element('div').injectInside(this.resultContainer);
			this.resultContainerInfo.setText(this.loadingMessage);

			// build the url			
			var url = this.targetUrl + '?' + 'zip=' + encodeURI(zip) + '&type=' + encodeURI(searchType) + '&radius=' + encodeURI(radius);
			
			if (this.dealerFile != '') {
				url = url + '&dfile=' + encodeURI(this.dealerFile);
			}
			
			// make the ajax call
			new Ajax(url, {
				'method': 'get',
				'update': $(this.dealerResultContainer),
				'onFailure': this.handleError.bind(this)
			}).request();
		}
	},
	
	/**
	 * Checks if the given zip is valid.
	 * @param {String} zip
	 */
	isValidZip: function(zip) {
		var result = false;
		//zip.length != this.options.ziplength || !this.isNumeric(zip)
		
		if (zip.length >= this.options.minZipLength && zip.length <= this.options.maxZipLength) {
			if (this.options.checkNumericZip == 1 && !this.isNumeric(zip)) {
				result = false;
			} else {
				result = true;
			}
		}
		return result;
	},
	
	/**
	 * Handles the error case by displaying an error message.
	 */
	handleError: function() {
		$(this.dealerResultContainer).empty();
		var errorDiv = new Element('div').injectInside(this.dealerResultContainer);
		errorDiv.setText(this.ajaxErrorMessage);
	},
	
	/**
	 * Checks if the given possible string represents a number.
	 * @param {Object} text	text to check
	 */
	isNumeric: function(text) {
		var validChars = "0123456789";
		var isNumber = true;
		var character;
		for(i = 0; i < text.length && isNumber == true; i++) { 
			character = text.charAt(i);
			if(validChars.indexOf(character) == -1) {
				isNumber = false;
			}
		}
		return isNumber;
	}
});
DealerSearch.implement(new Options);