<!--


    // Function to create our array of XMLHTTPRequest Objects
    function createHttpArray(n) {

        var conn = new Array();

        for( var i = 0; i < n; i++ ) {

            tmp = new Array();
            tmp['obj']          = createRequestObject();
            tmp['busy']         = 0;
            tmp['readyState']   = tmp['obj'].readyState;

            conn[i] = tmp;
        }

        return conn;
    }

    // Funtion to create our http request object
    function createRequestObject() {
        var ro;
        var browser = navigator.appName;
        if(browser == "Microsoft Internet Explorer") { // Here is our IE vs Mozilla work-around
            ro = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            ro = new XMLHttpRequest();
        }
        return ro;
    }

    // function to remove white space
    function trimString (str) {
        while (str.charAt(0) == ' ') {
            str = str.substring(1);
        }
        while (str.charAt(str.length - 1) == ' ') {
            str = str.substring(0, str.length - 1);
        }
        str = str.replace(/\n/g,"");
        return str;
    }

    // If we have a URL string, we need to escape it so we don't break things.
    function escapeURLStr(str) {
        return unescape(str.replace(/\+/g, " "));
    }

    // Setting the full trim string.
    function setFullTrim(trim,n) {
        eval('vform.trims' + n).value = trim;
    }

    // Checking to see if an object is an array.
    function isArray(obj) {
        if (obj.constructor.toString().indexOf("Array") == -1) {
            return false;
        } else {
            return true;
        }
    }

    // Sending a XMLHTTPRequest
    function sendRequest(action,n) {

        // local variables
        var url     = './ad.ajax.php';
        var params = '';
        var ac = 'get_makes';

        var make_id     = 'makeid' + n;
        var year        = 'year' + n;

        act = action;
        intr = n;

        make_id     = eval('vform.makeid' + n).value;
        year        = eval('vform.year' + n).value;
        model_id    = eval('vform.trims' + n).value;
        if(model_id == "") {
            model_id = defaultModelID;
        }

        // determining what our 'action' is.
        switch(action) {
            case 'get_makes':
                ac = 'get_makes';
                params = '';
            break;
            case 'get_years':
                ac = 'get_years';
                params = '&make=' + make_id;
            break;
            case 'get_trims':
                ac = 'get_models';
                params = '&make=' + make_id + '&year=' + year;
                //if(eval('vform.year' + n).value == '') {
                //    showYears(n);
                //}
            break;
        }


        // Making the connection, setting the readyStateChange function and sending.
        conn[n]['obj'].open('GET',url + '?action=' + ac + params,true);
        conn[n]['obj'].onreadystatechange = handleResponse;
        conn[n]['obj'].send(null);

    }

    // We handle the responses back from the server.
    function handleResponse() {

        // We need to loop through each index of our connection ( conn ) array.
        for( var i = 0; i < conn.length; i++) {

            // Checking to see if the readyState = 4.
            if(conn[i]['obj'].readyState == 4) { 

                // Assigning some variables for our responseText and an update array to loop through and parse.
                var response = conn[i]['obj'].responseText;
                var update = new Array();

                // Checking to see if the response has the data we are looking for.
                if(response.indexOf("::" != -1)) {

                    // Populating the update array.
                    update = response.split("::");

                    // Checking our action ( act ) in this switch statement.
                    switch(act) {

                        case 'get_makes':

                            // Putting 'Select Make' at the beginning of our update array.
                            update.unshift('|Select Make');
                            update.pop();

                            // Looping through update
                            for(var j = 0; j < update.length; j++) {

                                // Splitting the string of update[j] into an array (vals).
                                var vals = update[j].split("|");

                                // Declaring the variable name to identify the form field with.
                                var make_id = 'makeid' + i;

                                // Checking to see if we have a value to populate the menu with.
                                if(vals[1] != 'undefined') {

                                    // Populating the menu.
                                    eval('vform.' + make_id).options[j] = new Option(vals[1],vals[1]);
                                    if(trimString(vals[1]) == defaultMake) {
                                        eval('vform.' + make_id).options[j].selected = true;
                                    }

                                }

                            }
                            //alert('get_makes end');
                            //sendRequest('get_models',intr);
                        break;

                        case 'get_trims':

                            if(intr == i) {

                                var trims = 'trims' + i;
                                eval('vform.' + trims).options.length = 0;


                                for(j = 0; j < update.length; j++) {
                                    //var vals = update[j].split(";");
                                    //alert(vals[1]);
                                    //if(vals[1]) {
                                        var ttrim = update[j].split(";");
                                        var trimStr = escapeURLStr(ttrim[2] + ' - ' + ttrim[3]);
										if(ttrim[2] != undefined) {
                                        	eval('vform.' + trims).options[j] = new Option(trimStr,update[j]);

	                                        if(trimString == defaultModel) {
	                                            eval('vform.' + trims).options[j].selected = true;
	                                            setFullTrim(escapeURLStr(update[j]),i);
	                                        } else {
	                                            if(j == 0) {
	                                                eval('vform.' + trims).options[j].selected = true;
	                                                setFullTrim(escapeURLStr(update[j]),i);
	                                            }
	                                        }
	                                    } else {
	                                    	eval('vform.' + trims).options[j] = new Option('No Models For Selected Make / Year','');
	                                    }
                                    //} else {
                                    //    if( eval('vform.year' + i).value == 'Select Year' ) {
                                    //        eval('vform.' + trims).options[j] = new Option('Select Year','');
                                    //    } else {
                                     //       eval('vform.' + trims).options[j] = new Option('No Vehicles For Selected Year','');
                                     //   }
                                    //}
                                }

                            }

                        break;

                        case 'get_years':
							alert(update);
                        break;

                        default:
                        break;

                    }

                }

            }

        }
    }

    // Populating the year drop down menus.
    function showYears(n) {

        for( var j = 0; j < n; j++ ) {

            var yrName = 'year' + j;
            var k = 0;

            if(eval('vform.year' + j).options.length > 0) {
                eval('vform.year' + j).options.length = 0;
            }

			eval('vform.year' + j).options[k] = new Option('Select Year','');
			
			for ( var i = 0; i < yearFlds.length; i++ ) {
                k++;
                if(eval('vform.year' + j)) {
                    //vform.eval(yrName).options[i] = new Option(yearValues[i],yearValues[i]);
                    if(yearValues[i] == defaultYear) {
                        //alert(yearValues[i] + ' ' + defaultYear);
                        eval('vform.year' + j).options[k] = new Option(yearValues[i],yearValues[i],0,0);
                        //eval('vform.year' + j).options[k].selected = true;  // This line is here because IE is a P.O.S!
                    } else {
                        eval('vform.year' + j).options[k] = new Option(yearValues[i],yearValues[i]);
                    }
                }
            }

        }
        //alert('showYears end');

    }

    function setFieldDefaults(n) {

        // Defaulting the trims.
        eval('vform.trims' + n).options.length = 0;
        eval('vform.trims' + n).options[0] = new Option('Model','');
        
        showYears(fldArr.length);

    }

    // We call this function when we first load the page.
    function formInit(fldArr) {

        vform = document.forms['leadInfo'];

        for( var i = 0; i < fldArr.length; i++) {
            sendRequest('get_makes',i);
//            if(conn[i]['obj'].readyState == 4) {
//                sendRequest('get_trims',i);
//            }
            setFieldDefaults(i);
        }

    }

//-->

