var oMake = new Object;
	oMake.items = new Array;
	oMake.levels = new Array;
	oMake.additem = addItem;
	oMake.addlevel = addLevel;
	oMake.populate = populateList;
	
function addItem(level, value) {
	var pos = oMake.items.length;
	oMake.items[pos] = new Object;
	oMake.items[pos].level = level;
	oMake.items[pos].value = value;
}
	
function addLevel(level,depends,repopulate,init) {
	var pos = oMake.levels.length;
	oMake.levels[pos] = new Object;
	oMake.levels[pos].desc = level;
	oMake.levels[pos].depends = depends;
	oMake.levels[pos].repopulate = repopulate;
	oMake.levels[pos].init = init;
}
	
	
function insertOption(list, value, level, select) {
	var noadd = false;
	var mvalue;
	var msel;
	var len;
	var i = 1;
	
	// increment i and create options in numerical order
	// treat level '0' differently
	if (level == '0') {
		if(isInteger(value)){
			for (i; ((i < list.options.length) && (list.options[i].text > value)); i++) {
			}
		} else {
			i = list.options.length;
		}	
	} else {
		for (i; ((i < list.options.length) && (list.options[i].text < value)); i++) {
		}
	}
	
	if (i < list.options.length) {
		if (list.options[i].text > value) {
			len = list.options.length;
			for (var j=len; j > i; j--) {
				mvalue = list.options[j-1].text;
				msel = list.options[j-1].selected;
				list.options[j] = new Option(mvalue,mvalue,false,msel);
			}
		}
	}
	
	list.options[i] = new Option(value,value,false,select);
}
	
function populateList(start) {
	var state;
	var curlevel;
	var curvalue;
	var curlist;
	var usecheck;
	var usebranch;
	var list;
	var process;
	var level;
	var leveldesc;
	var curleveldesc;
	var leveldep;
	var restrict;
	var depstring;
	var curdepends;
	var levelrepop;
	var leveldefault;
	var oForm;

	oForm = GetForm('invform');
	process = false;
	for (var level=0; level < oMake.levels.length; level++) {
		leveldefault = oMake.levels[level].init;
		leveldesc = oMake.levels[level].desc;
		
		if (leveldesc == start) process = true;
		if (process == true) {
			usebranch = true;
			usecheck = oMake.levels.length;
			leveldep = oMake.levels[level].depends;
			levelrepop = oMake.levels[level].repopulate;
			
			var strlevel;
			switch (leveldep.toLowerCase()){
				case 'vehicletype':
					strlevel = 'Vehicle Type';
					break;
				case 'cabstyle':
					strlevel = 'Cab Style';
					break;
				default:
					strlevel = leveldep;
					break;
			}
			depstring = 'Select a ' + strlevel + ' first';
			
			list = oForm[leveldesc];
			switch(leveldep) {
				case '':
					restrict = 'none';
					break;
				case 'nodisplay':
					restrict = 'nodisplay';
					break;
				default:
					deplist = oForm[leveldep];
					if (deplist.options[deplist.selectedIndex].value == '') {
						restrict = 'noselect';
					} else {
						restrict = 'none';
					}
					break;
			}
			switch(restrict) {
				case 'noselect':
					list.options.length = 0;
					list.options[0] = new Option(depstring,'',false,true);
					list.selectedIndex = 0; 
					break;
				case 'none':
					state = leveldefault;
					if ((list.options.length > 0) && (levelrepop == false))
						state = list.options[list.selectedIndex].text;
					list.options.length = 0;
					if(leveldesc == 'make'){
						list.options[list.options.length] = new Option('All',sAllNewBrands,true,true);
					} else {
						list.options[list.options.length] = new Option('All','',true,true);
					}
					for (var i=0; i < oMake.items.length; i++) {
						curlevel = oMake.items[i].level;
						if(oMake.levels[curlevel]){
							curleveldesc = oMake.levels[curlevel].desc;
							curdepends = oMake.levels[curlevel].depends;
							
							// need to check for quicksearch inventory req.
							// using radio button for first list so oMake.levels[0].depends = 'nodisplay'
							if(curleveldesc == 'newused')  curdepends = "";
							
							if (curdepends != 'nodisplay') {
								curvalue = oMake.items[i].value;
								curlist = oForm[curleveldesc];
								
								if(curlist.options){								
									// for select objects only
									if (curlist.options.length == 0) {
										usebranch = true;
									} else {
										if (curlist.selectedIndex < 0) {
											curlist.selectedIndex = '0';
										}
										if ((curlist.options[curlist.selectedIndex].text == 'All') || (curlist.options[curlist.selectedIndex].text == curvalue)) {
											if (usecheck == curlevel) {
												usebranch = true;
											}
										} else {
											if (((usebranch == true) || (usecheck > curlevel)) && (curlevel < level)) {
												usebranch = false;
												usecheck = curlevel;
											}
										}
									}
								} else {
									// for radio control lists
									if (start == 'newused' && curvalue == 'New') {
										usebranch = true;
									} else {
										if (usecheck == curlevel) {
											usebranch = true;
										}
										var curlistvalue = getRadioValue(curlist);										
										if (curlistvalue == curvalue) {
											if (usecheck == curlevel) {
												usebranch = true;
											}
										} else {
											if (((usebranch == true) || (usecheck > curlevel)) && (curlevel < level)) {
												usebranch = false;
												usecheck = curlevel;
											}
										}
									}
								}								
								if ((curlevel == level) && (usebranch == true)){
									insertOption(list,curvalue,curlevel,(curvalue == state));
								}
							}
						}
					}
					/*
					// Fixed - Default Year 2002
						if (level=='0' && list.options.length > 3) {
							list.selectedIndex = 2; 
							//Required For NS Browser 
						} else {
							list.selectedIndex = 0; 
							//Required For NS Browser 
						}
					*/
					list.selectedIndex = 0; 
					break;
				default:
					break;
			}
		}
		oMake.levels[level].init = '';
	}
}

function isInteger (s) {
	var i;
	var bReturn = true;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (!isDigit(c)) bReturn = false;
    }
    return bReturn;
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function getRadioValue(curlist){
	var sReturn = "";
	var rLen = curlist.length;	
	for(var r=0; r<rLen; r++){
		if(curlist[r].checked) 
			sReturn = curlist[r].value;
	}
	return sReturn;
}