﻿function getModels(sender, condition){
    CS.getModels(sender, condition);
}

window.CS = {
    newListPagePath:'http://dealers.autosweet.com/ilistn.aspx',
    usedListPagePath:'http://dealers.autosweet.com/carlistu.aspx',
    
    createListPageLink:function(condition){
        var that = CSHelper.getThis();
        var anch, href, cond, yearLow, yearHigh, priceLow, priceHigh, makeID, modelID, mileage, bodyStyleID, dealerID, groupID;
        dealerID = document.getElementById(ServerObjects.hdnDealerID).value;
        groupID = document.getElementById(ServerObjects.hdnGroupID).value;
        switch(condition){
            case 'N':
                anch = document.getElementById('NewSearchButton');
                var makeDDL = document.getElementById(ServerObjects.newCarMakes);
                makeID = makeDDL.options[makeDDL.selectedIndex].value;
                var modelDDL = document.getElementById('NewModels');
                modelID = modelDDL.options[modelDDL.selectedIndex].value;
                bodyStyleID = -1;
                yearLow = -1;
                yearHigh = -1;
                priceLow = -1;
                priceHigh = -1;
                mileage = -1;
                cond = 1;
                break;
            case 'U':
                anch = document.getElementById('UsedSearchButton');
                var makeDDL = document.getElementById(ServerObjects.usedCarMakes);
                makeID = makeDDL.options[makeDDL.selectedIndex].value;
                var modelDDL = document.getElementById('UsedModels');
                modelID = modelDDL.options[modelDDL.selectedIndex].value;
                bodyStyleID = -1;
                var yearLowDDL = document.getElementById('UsedCarLowYear');
                yearLow = yearLowDDL.options[yearLowDDL.selectedIndex].value;
                yearHigh = yearLow;
                priceLow = -1;
                priceHigh = -1;
                mileage = -1;
                cond = 0;
                break;
        }
        //only the args can be escaped, not the keys
        var queryString = '?ydmx=' + escape(dealerID + ';' + yearLow + ';' + makeID + ';' + modelID + ';' + yearHigh + ';-1;' + cond + ';' + mileage + ';' + priceLow + ';' + priceHigh + ';-1;-1;false;' + bodyStyleID + ';-1;10') + '&gid=' + groupID;
        anch.href = anch.href + queryString;
    },
    
    //toChange represents which year list we should automatically change should the 
    //low year be higher than the high year.
    validateYears:function(toChange){
        var that = CSHelper.getThis();
        var lowYearDDL = document.getElementById('UsedCarLowYear');
        var highYearDDL = document.getElementById('UsedCarHighYear');
        var lowYear = parseInt(lowYearDDL.options[lowYearDDL.selectedIndex].value, 10);
        var highYear = parseInt(highYearDDL.options[highYearDDL.selectedIndex].value, 10);
        //both years have to have something other than "- year -" selected to even bother validating
        if(lowYear != -1 && highYear != -1){
            //if the low year is greater than the high year, select the toChange option to the other year. 
            if(lowYear > highYear){
                switch(toChange){
                    case 'H': //change the high year
                        that.selectDDLByVal(highYearDDL, lowYear);
                        break;
                    case 'L': //change the low year
                        that.selectDDLByVal(lowYearDDL, highYear);
                        break;
                }
            }    
        }    
    },
    
    //toChange represents which price list we should automatically change should the 
    //low price be higher than the high price.
    validatePrices:function(toChange){
        var that = CSHelper.getThis();
        var lowPriceDDL = document.getElementById('UsedCarLowPrice');
        var highPriceDDL = document.getElementById('UsedCarHighPrice');
        var lowPrice = parseInt(lowPriceDDL.options[lowPriceDDL.selectedIndex].value, 10);
        var highPrice = parseInt(highPriceDDL.options[highPriceDDL.selectedIndex].value, 10);
        //both prices have to have something other than "- price -" selected to even bother validating
        if(lowPrice != -1 && highPrice != -1){
            //if the low price is greater than the high price, select the toChange option to the other price. 
            if(lowPrice > highPrice){
                switch(toChange){
                    case 'H': //change the high price
                        that.selectDDLByVal(highPriceDDL, lowPrice);
                        break;
                    case 'L': //change the low price
                        that.selectDDLByVal(lowPriceDDL, highPrice);
                        break;
                }
            }    
        }
    },

    getModels:function(sender, condition){
        var context, makesDDL, modelsDDL;
        switch(condition){
            case 'N':
                context = 'getNewModels';
                modelsDDL = document.getElementById('NewModels');
                break;
            case 'U':
                context = 'getUsedModels';
                modelsDDL = document.getElementById('UsedModels');
                break;
        }
        makesDDL = sender;
        var makeID = makesDDL.options[makesDDL.selectedIndex].value;
        //only get the model list if the modelID is not -1, since -1 means "all makes"
        //if all makes is selected, just clear the model ddl
        if(makeID != -1){
            CallServer(context + '~^~' + makeID, context);
        }else{
            //clear all the existing options
            while(modelsDDL.firstChild){
                modelsDDL.removeChild(modelsDDL.firstChild);
            }
            //add the always present "all models"
            var opt = document.createElement('option');
            opt.value = '-1';
            var tn = document.createTextNode('- All Models -');
            opt.appendChild(tn);
            modelsDDL.appendChild(opt);
        }
    },
    
    handleGetModels:function(response, condition){
        var models = eval('(' + response + ')');
        var modelsDDL, opt, tn;
        switch(condition){
            case 'N':
                modelsDDL = document.getElementById('NewModels');
                break;
            case 'U':
                modelsDDL = document.getElementById('UsedModels');
                break;
        }
        //clear all the existing options
        while(modelsDDL.firstChild){
            modelsDDL.removeChild(modelsDDL.firstChild);
        }
        //add the always present "all models"
        opt = document.createElement('option');
        opt.value = '-1';
        tn = document.createTextNode('- All Models -');
        opt.appendChild(tn);
        modelsDDL.appendChild(opt);
        //add an option for every model available
        for(var i = 0; i < models.length; i++){
            opt = document.createElement('option');
            opt.value = models[i].ModelID;
            tn = document.createTextNode(models[i].ModelName);
            opt.appendChild(tn);
            modelsDDL.appendChild(opt);
        }
    },
    
    receiveServerData:function(response, context){
        var that = CSHelper.getThis();
        switch(context){
            case 'getNewModels':
                that.handleGetModels(response, 'N');
                break;
            case 'getUsedModels':
                that.handleGetModels(response, 'U');
                break;
        }
    },
    
    serverError:function(){
    
    },
    
    selectDDLByVal:function(ddl, val){
        for(var i = 0; i < ddl.options.length; i++){
            if(ddl.options[i].value == val){
                ddl.options[i].selected = true;
                break;
            }
        }
    }
};

window.CSHelper = {
    getThis:function(){
        return CS;
    }
};
