﻿// 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);

        }

    }

}



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 == '' || form.trim.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.workphone.value == '')

        error = 'No work 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://test.dtagent.net/NewCarForm/Service.asmx/SubmitDtxNewCar";

		form.action="http://www.dtagent.net/newcarform/service.asmx/SubmitDtxNewCar";

		form.submit();

    }

}



