/*
	Name: 		cars_improved.js
	Author: 	Markus Diersbock
	Details: 	Uses make/model arrays and a few lines of code from Jon's original cars.js.
				Added code to work with multiple vehicles.
			
	Revisions:	2009/09/30		Moved from old legacy Olawski code arrays to JSON.
								Modified suppotive code to handle JSON
				2009/04/04		Updated Make/Models
				2008/06/03		Created, with legacy Olawski code
*/
		
	function populateMakes(objForm, selected_num) {
		if(objForm.options.length == 1) {
			for (i = 0; i<obj_make_model.length; i++) {
				objForm.options[i+1] = new Option(obj_make_model[i].Make, obj_make_model[i].Make);
			}
			
			if(selected_num > 0){
				objForm.options[selected_num].selected = true;
			}
		}
	}	

    function makeSelected(objForm, selected_num) {
		// Verify if "Select" wasn't clicked
		if(objForm.selectedIndex>0){
			populateModels(objForm, objForm.selectedIndex - 1);
		}
		
		if(selected_num > 0){
			objForm.options[selected_num].selected = true;
		}
    }

	function populateModels(objForm, make_num) {
	
		// Block that works with muliple rows
		var id_len = objForm.id.length;
		var id_num = objForm.id.substring(id_len, id_len - 1);
		
		if(id_num != parseInt(id_num)){
			objModel = document.getElementById("model");
		}else{
			objModel = document.getElementById("model" + id_num);
		}
		
		// Clear dropdown
		objModel.options.length = 1;

		// Populate models based on index from Make dropdown
        for (i=0; i<obj_make_model[make_num].Models.length; i++) {
              objModel.options[i+1] = new Option(obj_make_model[make_num].Models[i], obj_make_model[make_num].Models[i]);
        }
    }
	  
