
var insideDelete = 0;
var searches = 0;
var alls = 0;
var make = "";
var model = "";
count = 0;
countM = 0;
boxCount = 1;
function fb(f) {
	if (f.vehicle.length == 1 && f.vehicle[0].value == "") {
		return true;
	}
	return false;
}
function setMake() {

	//addMM();
	if (document.inventory_form.make) {
	//alert("setMake1->make" + make)
		document.inventory_form.feature_includes_make.value = make;
	}
	if (document.inventory_form.model) {
		//alert("setMake2->bang!!" + model)
		if (model == ",") {
			model = "";
		}
		document.inventory_form.fmodel.value = model;
		if (searches == alls) {
			document.inventory_form.fmodel.value = "";
		}
	}
	document.inventory_form.make.value = "";
	document.inventory_form.model.value = "";
	document.inventory_form.vehicle.value = "";
}
/**
 *This function returns all vehicle models for the make
 *identified by the make index.
 *@param makeIndex -> the index of the appropriate make
 *@return mdAllMod all the models.
 */
function getAllModels(makeIndex) {
	var numModels = Makesv[makeIndex].length;
	var mdAllMod = "";
	for (i = 0; i < numModels; i++) {
			//alert("bang!!!");
		mdAllMod = mdAllMod + "," + Makesv[makeIndex][i];
	}
	return mdAllMod;
}
function set_models(Num) {
	document.inventory_form.fmodel.length = 0;
	var i;
	document.inventory_form.fmodel.options[document.inventory_form.fmodel.length] = new Option("Select a Model", "");
	var model_all = 0;
	if (Num > 0) {
		for (i in Makes[Num]) {
			if (Makes[Num][i] == "All") {
				model_all = 1;
			}
			document.inventory_form.fmodel.options[document.inventory_form.fmodel.length] = new Option(Makes[Num][i], Makesv[Num][i]);
		}
		var lcnt;
		if (model_all == 1) {
			lcnt = Makes[Num].length + 1;
		} else {
			lcnt = Makes[Num].length;
		}
		document.inventory_form.fmodel.selectedIndex = 0;
		document.inventory_form.fmodel.length = lcnt;
	}
}
function set_modelsAS(Num) {
	document.inventory_form.model.length = 0;
	var i;
	document.inventory_form.model.options[document.inventory_form.model.length] = new Option("Select a Model", "");
	var model_all = 0;
	if (Num > 0) {
		for (i in Makes[Num]) {
			if (Makes[Num][i] == "All") {
				model_all = 1;
			}
			document.inventory_form.model.options[document.inventory_form.model.length] = new Option(Makes[Num][i], Makesv[Num][i]);
		}
		var lcnt;
		if (model_all == 1) {
			lcnt = Makes[Num].length + 1;
		} else {
			lcnt = Makes[Num].length;
		}
		document.inventory_form.model.selectedIndex = 0;
		document.inventory_form.model.length = lcnt;
	}
}
function getModelLength(f) {
	return f.fmodel.value.length;
}
function changeSubMethod(f) {
	if (getModelLength(f) >= 2050) {
		f.method = "post";
		return;
	}
	return;
}
function build_makesAS(f) {
	f.make.length = 0;
	f.make.options[0] = new Option("Select a Make", "");
	for (i = 1; i < Make.length; i++) {
		f.make.options[i] = new Option(Make[i], Make[i]);
	}
}

function build_makes(f, x) {
	f.fmake.length = 0;
	f.fmake.options[0] = new Option("Select a Make", "");
	for (i = 1; i < Make.length; i++) {
		f.fmake.options[i] = new Option(Make[i], Make[i]);
	}
	f.fmake.value = x;
}

function build_vehicle(f) {
	var c = 0;
	for (var j = 0; f.elements.length; j++) {
		if (f.elements[j].name == "vehicle") {
			c = j;
			break;
		}
	}
	for (var i = 0; i < f.fmodel.options.length; i++) {
		if (f.fmodel.options[i].selected) {
			f.elements[c].value = f.fmodel.options[i].value;
			c++;
		}
	}
}
function mmAddMM(model, make) {
	var mmlist = new Array(6);
	var mlist = ["", "", "", "", "", ""];
	var vallist = new Array(6);
	var nlist = ["1. ", "2. ", "3. ", "4. ", "5. ", "6. "];
	var i;
	var ct = 0;
	var j = 0;
	var undefined;
	var imod = model;
	var imak = make;
	var allMdMod;
	makeCookieName = "mak" + boxCount;
	modelCookieName = "mod" + boxCount;
	addmdw(Make[imak]);
	WriteCookie(makeCookieName, imak);
	if (Makesv[imak][imod - 1] == "") {
		searches++;
		alls++;
		var allMdMod = getAllModels(imak);
		addmdwM(allMdMod);
	} else {
		searches++;
		addmdwM(Makesv[imak][imod - 1]);
	}
	WriteCookie(modelCookieName, imod);
	boxCount++;
	WriteCookie("boxCount", boxCount);	
	  
	  // init vals
	  
	  // check for valid selection
	if (imod == 0) {
		alert("Please select a Make and/or Model.");
		return;
	}
	imod--;
	  // check for max
	for (i = 0; i < 6; i++) {
		var myval = document.inventory_form.vehicle.options[i].value;
		if (myval == undefined) {
			myval = "null";
		}
		if (myval == "null") {
			ct = i;
			break;
		} else {
			var myarray;
			var myitem;
			myarray = document.inventory_form.vehicle.options[i].text.split(". ");
			myitem = myarray[1];
			mmlist[j] = myitem;
			vallist[j] = document.inventory_form.vehicle.options[i].value;
			j++;
			if (j >= 6) {
				alert("6 is the maximum number to search for.");
				return;
			}
		}
	}
	  
	  // check for duplicates
	j = 0;
	for (i = 0; i < document.inventory_form.vehicle.length; i++) {
		var myarray;
		var myitem;
		myarray = document.inventory_form.vehicle.options[i].text.split(". ");
		myitem = myarray[1];
		if (myitem == document.inventory_form.make.options[imak].value + " - " + Makes[imak][imod]) {
			return;
		}
	}
	  			//alert(Make[imak]);
	mmlist[ct] = document.inventory_form.make.options[imak].value + " - " + Makes[imak][imod];
	vallist[ct] = Makesv[imak][imod];
	  // sort list
	mmlist.sort();
	vallist.sort();
	for (i = 0; i < 6; i++) {
		var myarray;
		var myitem;
		if (mmlist[i] == undefined) {
			myitem = "";
		} else {
			myitem = mmlist[i];
		}
		if (myitem == "") {
			document.inventory_form.vehicle.options[i].text = nlist[i] + "                                                         ";
			document.inventory_form.vehicle.options[i].value = "null";
		} else {
			document.inventory_form.vehicle.options[i].text = nlist[i] + mmlist[i];
			document.inventory_form.vehicle.options[i].value = vallist[i];
		}
	}
}

