//add level 1 options onLoad
function initOptions(intIndex) {
	var intOptionIndex = 0;
	for(i = 0; i < arrMenus[intIndex].length; i+=2) {
		objElement1.options[intOptionIndex] = new Option(arrMenus[intIndex][i], arrMenus[intIndex][i+1]);
		intOptionIndex ++;
	}
	objElement1.length = arrMenus[intIndex].length / 2;
	objElement1.selectedIndex = 0;
	buildOptions(0);
}

// on some event add level 2 options
function buildOptions(intIndex) {
		// check if user selected option or default option
		blnRealOption = (intIndex > 0) ? true : false;
		if(blnRealOption) {
			// Buid element options
			var intOptionIndex = 0;
			for(i = 0; i <= arrMenus[intIndex].length; i+=2) {
				objElement2.options[intOptionIndex] = new Option(arrMenus[intIndex][i], arrMenus[intIndex][i+1]);
				intOptionIndex ++;
			}
			// Animation
			//imgSwap('','default_prog_bar','anim_prog_bar');
			// Set element length and default index
			objElement2.length = arrMenus[intIndex].length / 2;
			objElement2.selectedIndex = 0;
		}
		else {
			objElement2.options[0] = new Option("Select Your Model","");
			objElement2.length = 1;
			objElement2.selectedIndex = 0;
		}
}

function doFormAction() {
	// Validate actions and selections
	var strMenu1URL = objElement1.options[objElement1.options.selectedIndex].value;
	var strMenu2URL = objElement2.options[objElement2.options.selectedIndex].value;
	if ((strMenu1URL != "") && (strMenu2URL == "")) {
		window.location = strMenu1URL;
	}
	else if ((strMenu1URL != "") && (strMenu2URL != "")) {
		window.location = strMenu2URL;
	}
	else {
		alert("Please select a manufacturer or a manufacturer and a vehicle model.");
	}
	return false;
}

function setIndex(objSelectedElement) {
	// Set dynamic context
	// If Level 1 is defined then build the level 2 options
	if(strLevel_1 != "") {
			for (var i=0; i<objElement1.options.length; i++) {
				if(objElement1.options[i].text == strLevel_1) {
					objElement1.options[i].selected = true;
					//add level 2 options if exists i = array index
					buildOptions(i);
					break;
				}
			}
	}
	// If Level 2 is defined then set the selected index
	if(strLevel_2 != "") {
			for (var i=0; i<objElement2.options.length; i++) {
				if(objElement2.options[i].text == strLevel_2) {
					objElement2.options[i].selected = true;
					break;
				}
			}
	}
}

// do this onload
initOptions(0);
setIndex();
