﻿// 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('year');
    var makeSelect = document.getElementById('make');
    var modelSelect = document.getElementById('model');
    var trimSelect = document.getElementById('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:
        {
            makeSelect.options.length = 0;
            db.GetMakeList(UpdateSelectBox, yearSelect.value);
        }
        case makeSelect:
        {
            modelSelect.options.length = 0;
            if (current == makeSelect) {
                db.GetModelList(UpdateSelectBox, yearSelect.value, makeSelect.value);
            }
        }
        case modelSelect:
        {
            trimSelect.options.length = 0;
            if (current == modelSelect) {
                db.GetTrimList(UpdateSelectBox, yearSelect.value, makeSelect.value, modelSelect.value);
            }
            break;
        }
    }
}

function UpdateSelectBox(box, optionValues) {
    // 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++) {
            option = new Option(optionValues[i], optionValues[i], false, false);
            box.options.add(option);
        }
        
        // if we are pre-populating the make then set it here
        if (box.id == 'make' && g_make && g_make != '') {
            box.value = g_make;
        }
        // if we pre-pop the model set it here
        if (box.id == 'model' && g_model && g_model != '') {
            box.value = g_model;
        }
    }
}

function CheckForm() {
    // check for errors or fields that are empty
    var error = '';
    var form = document.NewCarForm;
    if (form.year.value == '' || form.make.value == '' || form.model.value == '')
        error = 'No vehicle selected.';
    if (form.firstName.value == '')
        error = 'No first name given.';
    if (form.lastName.value == '')
        error = 'No last name given.';
    if (form.address.value == '')
        error = 'No state given.';
    if (form.address.value == '')
        error = 'No address given.';
    if (form.zipCode.value == '')
        error = 'No zip code given.';
    if (form.zipCode.value.length < 5)
        error = 'Invalid zip code.';
    if (form.email.value == '')
        error = 'No email given.';
    if (form.homePhone.value == '')
        error = 'No phone given.';
    if (form.interiorColor.value == '')
        form.interiorColor.value = 'n/a';
    if (form.exteriorColor.value == '')
        form.exteriorColor.value = 'n/a';
    if (error != '') {
        alert(error);
    }
    else {
        form.action="http://www.dtagent.net/NewCarForm/Service.asmx/SubmitDtxNewCarV2";
        form.submit();
    }
}

function InitForm(year, make, model) {
    var yearSelect = document.getElementById('year');
    var makeSelect = document.getElementById('make');
    var modelSelect = document.getElementById('model');
    
    g_make = make;
    g_model = model;
    
    yearSelect.value = year;
    
    // populate the make list
    OnCarChange(yearSelect);
    
    // firefox will hit the list without waiting for it to have values in it
    // so we wait until everything is loaded before moving on to populate the
    // model list box.
    var intervalId;
    
    function populateTrimList() {
        if (typeof(modelSelect.value) != 'undefined' || 
            modelSelect.value != null || modelSelect.value != '') {
            // populate the trim list
            OnCarChange(modelSelect);
            clearInterval(intervalId);
       }
    }
    
    function populateModelList() {
        if (typeof(makeSelect.value) != 'undefined' || 
            makeSelect.value != null || makeSelect.value != '') {
            // populate the model list
            OnCarChange(makeSelect);
            
            // start the same process for the trim list
            clearInterval(intervalId);
            intervalId = setInterval(populateTrimList, 500);
       }
    }
    
    intervalId = setInterval(populateModelList, 500);
}