function createMBPSelector(ProductID, ModelYears, CarMakes, CarModels, SubModels) {
	var objSelector = new VehicleSelector(ModelYears, CarMakes, CarModels, SubModels);

	objSelector.ProductID = ProductID;
		
	objSelector.populateMakes = function(ModelYear) {
		this.addLock();
		jsrsExecute("/ThirdParty/VehicleSelection/MBPVehicleLib.asp", this._cbPopulateMakes, "getMakes", Array(this.ProductID, ModelYear));
	};
	objSelector.populateModels = function(ModelYear, CarMakeID) {
		this.addLock();
		jsrsExecute("/ThirdParty/VehicleSelection/MBPVehicleLib.asp", this._cbPopulateModels, "getModels", Array(this.ProductID, ModelYear, CarMakeID));
	};
	objSelector.populateSubModels = function(ModelYear, CarMakeID, CarModelID) {
		this.addLock();
		jsrsExecute("/ThirdParty/VehicleSelection/MBPVehicleLib.asp", this._cbPopulateSubModels, "getSubModels", Array(this.ProductID, ModelYear, CarModelID));
	};
	
	return objSelector;
}
function createGAPSelector(QuoteID, ModelYears, CarMakes, CarModels, SubModels) {
	var objSelector = new VehicleSelector(ModelYears, CarMakes, CarModels, SubModels);

	objSelector.QuoteID = QuoteID;

	objSelector.populateMakes = function(ModelYear) {
		this.addLock();
		jsrsExecute("/ThirdParty/VehicleSelection/GAPVehicleLib.asp", this._cbPopulateMakes, "getMakes", Array(this.QuoteID, ModelYear));
    };
	objSelector.populateModels = function(ModelYear, CarMakeID) {
		this.addLock();
		jsrsExecute("/ThirdParty/VehicleSelection/GAPVehicleLib.asp", this._cbPopulateModels, "getModels", Array(this.QuoteID, ModelYear, CarMakeID));
	};
	objSelector.populateSubModels = function(ModelYear, CarMakeID, CarModelID) {
		this.addLock();
		jsrsExecute("/ThirdParty/VehicleSelection/GAPVehicleLib.asp", this._cbPopulateSubModels, "getStyles", Array(this.QuoteID, ModelYear, CarMakeID, CarModelID));
	};

	return objSelector;
}
function VehicleSelector(ModelYears, CarMakes, CarModels, SubModels) {
	var objThis = this;

	this.ModelYears = ModelYears;
	this.CarMakes = CarMakes;
	this.CarModels = CarModels;
	this.SubModels = SubModels;
	this.Locks = 0;

	this._changeYear = function (e) {
		objThis.changeYear();
	};
	this._changeMake = function (e) {
		objThis.changeMake();
	};
	this._changeModel = function (e) {
		objThis.changeModel();
	};
	this._cbPopulateMakes = function (List) {
		objThis.cbPopulateMakes(List);
	};
	this._cbPopulateModels = function (List) {
		objThis.cbPopulateModels(List);
	};
	this._cbPopulateSubModels = function (List) {
		objThis.cbPopulateSubModels(List);
	};

	if (typeof ModelYears.addEventListener != "undefined") {
		ModelYears.addEventListener("change", this._changeYear, false);
		CarMakes.addEventListener("change", this._changeMake, false);
		CarModels.addEventListener("change", this._changeModel, false);
	}
	else if (typeof ModelYears.attachEvent != "undefined") {
		ModelYears.attachEvent("onchange", this._changeYear);
		CarMakes.attachEvent("onchange", this._changeMake);
		CarModels.attachEvent("onchange", this._changeModel);
	}
}

/*****************************************************************************
 * Functions to populate the select boxes...                                 *
 *****************************************************************************/

VehicleSelector.prototype.changeYear = function() {
	var strYear = this.ModelYears.options[this.ModelYears.selectedIndex].value;

	if (strYear != '0' && strYear != '')
		this.populateMakes(strYear);
	else {
		loadOptions(this.CarMakes, null, 0);
		this.changeMake();
	}
}
VehicleSelector.prototype.changeMake = function() {
	var strYear = this.ModelYears.options[this.ModelYears.selectedIndex].value;
	var strCarMakeID = this.CarMakes.options[this.CarMakes.selectedIndex].value;
	
	if (strCarMakeID != '0' && strCarMakeID != '')
		this.populateModels(strYear, strCarMakeID);
	else {
		loadOptions(this.CarModels, null, 0);
		this.changeModel();
	}
}
VehicleSelector.prototype.changeModel = function() {
	var strYear = this.ModelYears.options[this.ModelYears.selectedIndex].value;
	var strCarMakeID = this.CarMakes.options[this.CarMakes.selectedIndex].value;
	var strCarModelID = this.CarModels.options[this.CarModels.selectedIndex].value;
	
	if (strCarModelID != '0' && strCarModelID != '')
		this.populateSubModels(strYear, strCarMakeID, strCarModelID);
	else
		loadOptions(this.SubModels, null, 0);
}

/*****************************************************************************
 * Functions to communicate with the Remote Scripts...                       *
 *****************************************************************************/
 
VehicleSelector.prototype.populateMakes = function(ModelYear) {}
VehicleSelector.prototype.populateModels = function(ModelYear, CarMakeID) {}
VehicleSelector.prototype.populateSubModels = function(ModelYear, CarMakeID, CarModelID) {}

/*****************************************************************************
 * Callback functions to respond to the data returned by the remote scripts  *
 *****************************************************************************/

VehicleSelector.prototype.cbPopulateMakes = function(List){
	var strMakeID = this.CarMakes.options[this.CarMakes.selectedIndex].value;

	loadOptions(this.CarMakes, List, strMakeID);

	this.changeMake();
	this.releaseLock();
} 
VehicleSelector.prototype.cbPopulateModels = function(List) {
	var strCarModelID = this.CarModels.options[this.CarModels.selectedIndex].value;

	loadOptions(this.CarModels, List, strCarModelID);

	this.changeModel();
	this.releaseLock();
}
VehicleSelector.prototype.cbPopulateSubModels = function(List) {
	var strSubModelID = this.SubModels.options[this.SubModels.selectedIndex].value;

	loadOptions(this.SubModels, List, strSubModelID);

	this.releaseLock();
}

/*****************************************************************************
 * Other functions...                                                        *
 *****************************************************************************/

VehicleSelector.prototype.addLock = function() {
	if (this.Locks == 0) this.setDisabled(true);
	this.Locks += 1;
}
VehicleSelector.prototype.releaseLock = function() {
	this.Locks -= 1;
	if (this.Locks == 0) this.setDisabled(false);
}
VehicleSelector.prototype.setDisabled = function(Disabled) {
	this.ModelYears.disabled = Disabled;
	this.CarMakes.disabled = Disabled;
	this.CarModels.disabled = Disabled;
	this.SubModels.disabled = Disabled;
}


function loadOptions(SelectBox, List, SelectedOption) {
	var strValue, strText;
	
	SelectBox.length = 0;
	
	if (List == null) {
		SelectBox.options[0] = new Option(" - Select -", "0", true, true);
		return;
	}
	
	var aryOptions = jsrsArrayFromString(List,"|");

	if (aryOptions.length == 1) {
		strValue = jsrsArrayFromString(aryOptions[0],"~")[0];
		strText = jsrsArrayFromString(aryOptions[0],"~")[1];
		SelectBox.options[0] = new Option(strText, strValue, true, true);
	} else {
		SelectBox.options[0] = new Option(" - Select -", "0", true, true);

		for (var i = 0; i < aryOptions.length; i++) {
			strValue = jsrsArrayFromString(aryOptions[i],"~")[0];
			strText = jsrsArrayFromString(aryOptions[i],"~")[1];

			SelectBox.options[i + 1] = new Option(strText, strValue, false, (strValue == SelectedOption))
		}
	}
}

