(function(B){var A=true;var C=DDC.CfcTradeIn={data:{},call:new ApiCall(),models:null,loadingDiv:"#q_loading",currentData:null,franchises:new Array(),selectLabel:{model:"",trim:""},init:function(D){this.data=D;this.call.reseller="ddc";this.call.accountId=this.data.accountId;this.call.service="builder";this.franchises=this.data.franchises;this.selectLabel.model=B("#cashForClunkersTradein label.model option:eq(0)").text();this.selectLabel.trim=B("#cashForClunkersTradein label.trim option:eq(0)").text();B("#cashForClunkersTradein select").each(function(){if(this.name=="make"||this.name=="model"||this.name=="trim"){B("option:gt(0)",this).remove();}});B("#cashForClunkersTradein .make select, #cashForClunkersTradein .model select, #cashForClunkersTradein .trim select").change(function(){DDC.CfcTradeIn.selectOnChange(this);});B("#CfcTradeIn .widgetWrap").prepend(this.data.message);B("#cashForClunkersTradein select[name=model]").attr("disabled","true");B("#cashForClunkersTradein select[name=trim]").attr("disabled","true");this.doLoadMakes();},get:function(E,D){if(D){return B("#cashForClunkersTradein select[name="+E+"]");}else{return B("#cashForClunkersTradein select[name="+E+"]").get(0);}},doLoadMakes:function(){var D=this.get("make");for(var E=0;E<this.franchises.length;E++){D.options[D.length]=new Option(this.franchises[E],this.franchises[E]);}this.autoSelect(D);},doLoadModels:function(D){this.call.operation="models";this.call.callback="DDC.CfcTradeIn.buildModels";this.call.param("make",D);this.call.execute();},buildModels:function(H){this.currentData=H;var E=this.get("model");var J=new Date();var I=(this.data.minYear)?this.data.minYear:0;var D=(this.data.maxYear)?this.data.maxYear:J.getFullYear()+1;var G=45000;E.options[0]=new Option(this.selectLabel.model,"");for(var F in H.models){if(H.models[F].modelYear){if((H.models[F].modelYear>=I&&H.models[F].modelYear<=D)&&(H.models[F].minMsrPrice<=G)){if(H.models[F].model!=="RL"&&H.models[F].model!=="TL"){E.options[E.length]=new Option(this.modelToString(H.models[F]),F);}}}}B("#model").show();this.get("trim").options[0]=new Option(this.selectLabel.trim,"");this.get("model").disabled=false;this.autoSelect(E);},doLoadTrims:function(D){this.call.operation="trims";this.call.callback="DDC.CfcTradeIn.buildTrims";this.call.param("year",this.currentData.models[D].year);this.call.param("make",this.currentData.models[D].make);this.call.param("model",this.currentData.models[D].model);this.call.param("bodystyle",this.currentData.models[D].bodyStyle);this.call.execute();},buildTrims:function(G){var D=this.get("trim");var F=45000;D.options[0]=new Option(this.selectLabel.trim,"");for(var E in G.trims){if(G.trims[E].trim&&G.trims[E].msrp<=F){D.options[D.length]=new Option(this.trimToString(G.trims[E]),G.trims[E].vehicleId);}}this.get("trim").disabled=false;},autoSelect:function(D){if(D.options.length==2){this.selectOnChange(this.get(D.name,true).find("option:eq(1)").attr("selected",true).end().get(0));}},selectVehicle:function(D){B("#cashForClunkersTradein").get(0).elements.vehicleId.value=this.trims[D].vehicleId;},modelToString:function(D){return D.year+" "+D.make+" "+D.model+" "+D.bodyStyle.replace("Sport Utility Vehicle","SUV");},trimToString:function(D){if(this.data.showPrice){return D.trim+" "+D.msrp.toString().replace(/(\d+)(\d{3})/,"$$$1,$2");}else{return D.trim;}},clearSelect:function(D){var F=D.options.length;for(var E=(F-1);E>=1;E--){D.options[E]=null;}D.options[0]=new Option("Loading...","Loading...");},selectOnChange:function(D){if(D.getAttribute("name")=="make"){this.clearSelect(this.get("model"));this.clearSelect(this.get("trim"));B("#cashForClunkersTradein select[name=model]").removeAttr("disabled").end().eq(1).attr("disabled","true");B("#cashForClunkersTradein option[value=make]").css("color","#888888");B("#cashForClunkersTradein option[value=Model]").css("color","#888888");this.doLoadModels(D.options[D.selectedIndex].value);}else{if(D.getAttribute("name")==="model"&&D.selectedIndex!==0){this.clearSelect(this.get("trim"));B("#cashForClunkersTradein select[name=trim]").removeAttr("disabled").end().eq(2).attr("disabled","true");B("#cashForClunkersTradein option[value=Model]").css("color","#888888");this.doLoadTrims(D.options[D.selectedIndex].value);}else{if(D.getAttribute("name")==="trim"&&D.selectedIndex!==0){document.forms.cashForClunkersTradein.elements.vehicleId.value=D.options[D.selectedIndex].value;}}}}};})(jQuery);