// form.js

// We retrieve all vehicle information from this object
var db = new VehicleDB('FILES');

function OnCarChange(current) {
    // get the make node
    var yearSelect = document.getElementById('vehicle_year');
    var makeSelect = document.getElementById('vehicle_make');
    var modelSelect = document.getElementById('vehicle_model');
    var trimSelect = document.getElementById('vehicle_trim');
    
    // the next box which will take the new option values
    var next;
    
    // clear the select boxes that come after the current selection
    // and get the values for the box that comes immediately after
    // the selection.

	    switch(current) {
	        case yearSelect:
	        {            
	        	
	            if(current.value.length > 0){
	            	makeSelect.options.length = 0;
	        		db.GetMakeList(UpdateSelectBox, yearSelect.value);
	            }
	            else {
	            		makeSelect.options.length = 0;
						modelSelect.options.length = 0;
		 				trimSelect.options.length = 0;		 				
	            }	
	        }
	        case makeSelect:
	        {
	        	if(makeSelect.value.length >0){
		            modelSelect.options.length = 0;
		            if (current == makeSelect) {
		                db.GetModelList(UpdateSelectBox, yearSelect.value, makeSelect.value);
		            }
		       }
	            else {
						modelSelect.options.length = 0;
		 				trimSelect.options.length = 0;
	            }	     
	        }
	        case modelSelect:
	        {
				if(modelSelect.value.length >0){	        	
		            trimSelect.options.length = 0;
		            if (current == modelSelect) {
		                db.GetTrimList(UpdateSelectBox, yearSelect.value, makeSelect.value, modelSelect.value);
		            }
		            break;
		        
	        }
	        else{
		 				trimSelect.options.length = 0;		       
		       }    
	    }
	   } 
	
}

function ValidateFormSubmit() {
    // check for errors or fields that are empty
    var error = '';
    var form = document.NewCarForm;
    if (form.vehicle_year.value == '' || form.vehicle_make.value == '' || form.vehicle_model.value == '' || form.vehicle_trim.value == '')
        error = 'No vehicle selected.';
    if (error != '') {
        alert(error);
        return false;
    }
    else {        
        return true;
    }
}

function toggleDiv(divID) {
	divRef	= document.getElementById(divID);
	if ( divRef.style.display=='block' ) {
		divRef.style.display='none'
	} else {
		divRef.style.display='block'
	}
}

function UpdateSelectBox(box, optionValues, selectedValue) {
    // make sure we have something then...
    if (optionValues != null) {
        // always have the first option be blank
        var option = new Option('', '', false, false);
        box.options.add(option);
        
        // populate the select box
        for (var i = 0; i < optionValues.length; i++) {
            
        	if(selectedValue == optionValues[i]){
        		option = new Option(optionValues[i], optionValues[i], true, true);
        	}
        	else{
        		option = new Option(optionValues[i], optionValues[i], false, false);
        	}
            box.options.add(option);
        }
    }
}

function onPageLoad(carYear, carMake, carModel, carTrim){
	
	//get area (city,state,zip)	
	var div_area = document.getElementById('div_area');
if(div_area){		
 if (typeof div_area != 'undefined'){ 
		
		div_area_display = div_area.style.display;
		
		if(div_area_display == 'none'){
			document.NewCarForm.city.value = document.NewCarForm.hidden_city.value;
			document.NewCarForm.state.value = document.NewCarForm.hidden_state.value;
			document.NewCarForm.city.disabled = false;
			document.NewCarForm.state.disabled = false;				
		}
		else {	
			getArea();
		}
	}
}
	
	//alert("welcome");
    // get the make node
    
    /*
    var carYear = '2008';
    var carMake = 'BMW';
    var carModel = 'X3';
    var carTrim = 'AWD 4dr 3.0si';
    */

    
    
    // the next box which will take the new option values
    var next;
    
    var yearSelect = document.getElementById('vehicle_year');
    var makeSelect = document.getElementById('vehicle_make');
    var modelSelect = document.getElementById('vehicle_model');
    var trimSelect = document.getElementById('vehicle_trim');
    
    // clear the select boxes that come after the current selection
    // and get the values for the box that comes immediately after
    // the selection.
    
    // use form helper
    //yearSelect.options[1].selected=true;
    
	            if(carYear.length > 0){	            	
	        		db.GetMakeList(UpdateSelectBox, carYear, carMake);

	            
		            if(carMake.length > 0){		            	
		        		db.GetModelList(UpdateSelectBox, carYear, carMake, carModel);
		            }
	            
			            if(carModel.length > 0){		            	
			        		db.GetTrimList(UpdateSelectBox, carYear, carMake, carModel, carTrim);
			            }
	            
	            }

	           
	       
	    
	

}


