﻿Type.registerNamespace('Vehix.Presentation.Inventory')
Vehix.Presentation.Inventory.EndecaInventorySelector = function(panels, radiusDropDownListID, zipCodeTextBoxID, yearDropDownListID, endYearDropDownListID, makeDropDownListID, modelDropDownListID, priceLowTextBoxID, priceHighTextBoxID, colorTextBoxID, mpgCityTextBoxID, mpgHighwayTextBoxID, customPriceLowTextBoxID, customPriceHighTextBoxID, searchingInventoryID, validationGroup) {
    this._controlsPanelIDArray = new Array();
    for (var i = 0; i < panels.length; i++) {
        this._controlsPanelIDArray.push(panels[i]);
    }
    this._radiusDropDownListID = radiusDropDownListID;
    this._zipCodeTextBoxID = zipCodeTextBoxID;
    this._searchingInventoryID = searchingInventoryID
    this._yearDropDownListID = yearDropDownListID;
    this._endYearDropDownListID = endYearDropDownListID;
    this._makeDropDownListID = makeDropDownListID;
    this._modelDropDownListID = modelDropDownListID;
    this._priceHighTextBoxID = priceHighTextBoxID;
    this._priceLowTextBoxID = priceLowTextBoxID;
    this._colorTextBoxID = colorTextBoxID;
    this._mpgCityTextBoxID = mpgCityTextBoxID;
    this._mpgHighwayTextBoxID = mpgHighwayTextBoxID;
    this._customPriceLowTextBoxID = customPriceLowTextBoxID;
    this._customPriceHighTextBoxID = customPriceHighTextBoxID;
    this._validationGroup = validationGroup;

}

Vehix.Presentation.Inventory.EndecaInventorySelector.prototype =
{
	get_radius: function() {
		return Vehix.Presentation.tryGetSelectedValue($get(this._radiusDropDownListID));
	},
	get_zip: function() {
		return $get(this._zipCodeTextBoxID).value;
	},
	get_year: function() {
		return Vehix.Presentation.tryGetSelectedValue($get(this._yearDropDownListID));
	},
	get_endYear: function() {
		return Vehix.Presentation.tryGetSelectedValue($get(this._endYearDropDownListID));
	},
	get_make: function() {
		return Vehix.Presentation.tryGetSelectedValue($get(this._makeDropDownListID));
	},
	get_model: function() {
		return Vehix.Presentation.tryGetSelectedValue($get(this._modelDropDownListID));
	},
	get_priceHigh: function() {
		var result = null;
		result = parseInt($get(this._priceHighTextBoxID).value);
		var low = parseInt($get(this._priceLowTextBoxID).value);
		if (result < low) {
			result = low;
		}
		if (result <= 0 || result == NaN) {
			result = 1;
		}
		return result;
	},
	get_priceLow: function() {
		var result = null;
		result = parseInt($get(this._priceLowTextBoxID).value);
		var high = parseInt($get(this._priceHighTextBoxID).value);
		if (result > high) {
			result = high;
		}
		if (result <= 0 || result == NaN) {
			result = 1;
		}
		return result;
	},
	get_customPriceHigh: function() {
		var result = null;
		result = parseInt($get(this._customPriceHighTextBoxID).value);
		var low = parseInt($get(this._customPriceLowTextBoxID).value);
		if (result < low) {
			result = low;
		}
		if (result <= 0 || result == NaN) {
			result = 1;
		}
		return result;
	},
	get_customPriceLow: function() {
		var result = null;
		result = parseInt($get(this._customPriceLowTextBoxID).value);
		var high = parseInt($get(this._customPriceHighTextBoxID).value);
		if (result > high) {
			result = high;
		}
		if (result <= 0 || result == NaN) {
			result = 1;
		}
		return result;
	},
	get_color: function() {
		return $get(this._colorTextBoxID).value;
	},
	get_mpgCity: function() {
		var result = null;
		result = parseInt($get(this._mpgCityTextBoxID).value);
		if (result < 0 || result == NaN) {
			result = 0;
		}
		return result;
	},
	get_mpgHighway: function() {
		var result = null;
		result = parseInt($get(this._mpgHighwayTextBoxID).value);
		if (result < 0 || result == NaN) {
			result = 0;
		}
		return result;
	},
	_hideAllPanels: function() {
		for (var i = 0; i < this._controlsPanelIDArray.length; i++) {
			$get(this._controlsPanelIDArray[i]).style.display = "none";
		}
	},
	showPanel: function(oid) {
		this._hideAllPanels();
		$get(oid).style.display = "block";
		if (refreshAds) {
			setTimeout(refreshAds, 1);
		}
		if (s) {
			s.t();
		}
	},
	yearRangeIsValid: function() {
		var isValid = true;
		var beginYearSelected = this.get_year() != null && this.get_year() != '' && this.get_year().indexOf('All') == -1;
		var endYearSelected = this.get_endYear() != null && this.get_endYear() != '' && this.get_endYear().indexOf('All') == -1;
		if ((beginYearSelected || endYearSelected) && beginYearSelected != endYearSelected) {
			var beginYearElement = $get(this._yearDropDownListID);
			var endYearElement = $get(this._endYearDropDownListID);
			alert("-- \"" + beginYearElement[beginYearElement.selectedIndex].text + " to " + endYearElement[endYearElement.selectedIndex].text + "\" is not a valid year range.");
			isValid = false;
		}
		return isValid;
	},
	navigate: function(sender) {
		var result = false;
		if (!sender.clicked) {
			if (Page_ClientValidate(this._validationGroup)) {
				this.showHourGlass();
				this.showSearchProgress();
				sender.clicked = true;
				var queryString = new Vehix.Presentation.QueryString(sender.href);
				queryString.add('zip', this.get_zip())
				queryString.add('radius', this.get_radius());
				sender.href = queryString.toString(Vehix.Presentation.QueryString.stripQuery(sender.href));
				result = true;
			}
			else {
				if (refreshAds) {
					setTimeout(refreshAds);
				}
				if (s) {
					s.t();
				}
			}
		}
		return result;
	},
	navigateWithAdvancedSelections: function(sender) {
		var result = false;
		if (!sender.clicked) {
			if (Page_ClientValidate(this._validationGroup) && this.yearRangeIsValid()) {
				this.showHourGlass();
				this.showSearchProgress();
				sender.clicked = true;
				var queryString = new Vehix.Presentation.QueryString(sender.href);
				queryString.add('zip', this.get_zip())
				queryString.add('radius', this.get_radius());
				queryString.addIf(this.get_year() != null && this.get_year() != '' && this.get_year().indexOf("All") == -1, 'yearHigh', this.get_year());
				queryString.addIf(this.get_endYear() != null && this.get_endYear() != '' && this.get_endYear().indexOf("All") == -1, 'yearLow', this.get_endYear());
				queryString.addIf(this.get_make() != null && this.get_make() != '', 'make', this.get_make());
				queryString.addIf(this.get_model() != null && this.get_model() != '', 'smodel', this.get_model());
				queryString.addIf(this.get_priceLow() != null && this.get_priceLow() > 0, 'priceLow', this.get_priceLow());
				queryString.addIf(this.get_priceHigh() != null && this.get_priceHigh() > 0, 'priceHigh', this.get_priceHigh());
				if (this.get_color() != null && this.get_color() != '') {
					queryString.add('Ntk', 'Inventory Color')
					queryString.add('Ntt', this.get_color());
					queryString.add('Ntx', 'mode+matchany');
				}
				queryString.addIf(this.get_mpgCity() != null && this.get_mpgCity() > 0, 'mpgCityLow', this.get_mpgCity());
				queryString.addIf(this.get_mpgHighway() != null && this.get_mpgHighway() > 0, 'mpgHighwayLow', this.get_mpgHighway());
				sender.href = queryString.toString(Vehix.Presentation.QueryString.stripQuery(sender.href));
				result = true;
			}
			else {
				if (refreshAds) {
					setTimeout(refreshAds);
				}
				if (s) {
					s.t();
				}
			}
		}
		return result;
	},
	navigateWithCustomPrice: function(sender) {
		var result = false;
		if (!sender.clicked) {
			if (Page_ClientValidate(this._validationGroup)) {
				if (this.get_customPriceLow() != null && this.get_customPriceLow() >= 0 && this.get_customPriceHigh() != null && this.get_customPriceHigh() >= 0 && this.get_customPriceHigh() != this.get_customPriceLow()) {
					this.showHourGlass();
					this.showSearchProgress();
					sender.clicked = true;
					var queryString = new Vehix.Presentation.QueryString(sender.href);
					queryString.add('zip', this.get_zip())
					queryString.add('radius', this.get_radius());
					queryString.add('priceLow', this.get_customPriceLow());
					queryString.add('priceHigh', this.get_customPriceHigh());
					sender.href = queryString.toString(Vehix.Presentation.QueryString.stripQuery(sender.href));
					result = true;
				}
				else {
					result = false;

					if (this.get_customPriceHigh() == this.get_customPriceLow() && this.get_customPriceLow() != null) {
						alert('-- A valid price range is required.');
					}
					else {
						alert('-- Both price fields are required.');
					}
				}
			}
			else {
				if (refreshAds) {
					setTimeout(refreshAds);
				}
				if (s) {
					s.t();
				}
			}
		}
		return result;
	},
	showHourGlass: function() {
		if (document && document.styleSheets) {
			var stylesheet = document.styleSheets[document.styleSheets.length - 1];
			if (stylesheet.addRule) {
				stylesheet.addRule('*', 'cursor:wait !important', 0);
			}
			else if (stylesheet.insertRule) {
				stylesheet.insertRule('* { cursor:wait !important }', 0);
			}
		}
	},
	showSearchProgress: function() {
		this._hideAllPanels();
		setTimeout('$get(\'' + this._searchingInventoryID + '\').style.display=\'\';', 1);
		setTimeout('$get(\'' + this._radiusDropDownListID + '\').style.display=\'none\';', 1);
	}
}
Vehix.Presentation.Inventory.EndecaInventorySelector.registerClass('Vehix.Presentation.Inventory.EndecaInventorySelector', Sys.Component);

