ASC.Chrysler.BMO.ModelObject=function(){
	var description = null;
	var msrpAsConfigured = null;
	var baseMSRP = null;
	var destination = null;
	var forcedOptionsPrice = null;
	var CCode = null;
	var lowerLevelPackageCode = null;
	var defExtImage = null;
	var defIntImage = null;
		
	var employeeDiscount = null;
	var employeeDiscountDescription = null;
	
	var cma = null;
	var fwp = null;
	var ppa = null;
	var daa = null;
	var dealerState = null;
	var employeeDiscountType = null;
	var flags = null;
}

/*
 * 
 * ASC.Chrysler.BMO.VehicleList
 * 
 * */
ASC.namespace('ASC.Chrysler.BMO.VehicleList');

ASC.Chrysler.BMO.VehicleList= ASC.extend(Ext.util.Observable,{
	
	renderTo:null,
	modelList:null,
	index:-1,
	
	constructor: function (modelList, selIndex, cfg)
	{
	ASC.Chrysler.BMO.VehicleList.superclass.constructor.apply(this);
	ASC.apply(this, cfg);

	this.addEvents({"render":true});
	this.initTemplates();
	this.loadData(modelList, selIndex);
	},
	loadData:function(modelList, selIndex)
	{
		this.modelList = modelList;		
		this.index = selIndex;
		this.render();
	},
		
	renderLinks: function (vehicleDesc)
	{
		this.LinksTemplate.overwrite('modelLinks', {vehicleDescription:vehicleDesc, models: Page.modelDesc});
	},
	
	render:function()
	{
		this.widgetTpl.overwrite(this.renderTo, {models:this.modelList, mindex:this.index});		
		this.fireEvent("render");
		
		if(this.modelList.length > 0){
			Page.trim = this.modelList[this.index - 1].description;		
			this.renderLinks(Page.modelDesc + (Page.trim != null ? ' ' + Page.trim: ''));
		}		
	},
	initTemplates: function ()
	{
			this.widgetTpl = new Ext.XTemplate(
        		'<table class="left_models_frame" cellpadding="0" cellspacing="0" border="0">',
        			'<tr class="modelsHeader">',
        				'<td colspan="3">',
        					'<div class="modelsTitle">{[this.translate("bmo_model")]}</div>',
        					'<div class="modelsStartingAt">{[this.translate("bmo_startingMsrpAt")]}</div>',
        				'</td>',
        			'</tr>',
    				'<tpl for="models">',
    					'<tr class="{[xindex === parent.mindex ? "model_selected":"selected_line"]}">',
    						'<td class="models_radio">',
    								'<input type="radio" name="models" value="{description}" {[xindex === parent.mindex ? "checked":""]} id="{CCode}_{lowerLevelPackageCode}" onClick="javascript:Page.setSelectedModel({[xindex]}, this.id, \'{[this.encodeString(values.description)]}\');"/> &nbsp;&nbsp;',
    						'</td>',
    						'<td class="left_models_item_left">',
    							'<label for="{CCode}_{lowerLevelPackageCode}">{description}</label>&nbsp;&nbsp;',
    							//'<img id="{CCode}_{lowerLevelPackageCode}_img" src="{[this.getImgLocalePath()]}/bmo/bodycontainer_icon_09.gif"/>',
     						'</td>',
        					'<td class="left_models_item_right">{[this.formatCurrency(values.msrpAsConfigured)]}</td>',
    					'</tr>',
           			'</tpl>',					           			
				 '</table>'		
           	,{
				
				translate: function (key)
				{
					return ASC.message(key);
				},
				
				formatCurrency: function (str)
				{
					var val = ASC.util.getFloat(str);
					if (isNaN(val)) { 
						return;
					}					
					return ASC.util.formatCurrency(val, 0);					
				},
				
				encodeString: function(string){
					return string.replace('"','&quot;');
				},
				
				getImgLocalePath: function(  ){
					return ASC.cfg.getImgLocalePath();
				}
				
			}
		);			
		this.widgetTpl.compile();
		
		this.LinksTemplate = new Ext.XTemplate(
           		'<table >',
           			'<tr>',
           				'<td >',
           					//'<ul>',
           				'<tpl if="this.multipleTrims()">',
           						'To see model details use <a class="left_models_frame_vehicleLinks" href="javascript:Page.showAllTrims();ASC.Chrysler.trackLinkMetrics(\'content\',\'compare_models\');">Compare All Models</a>.',
           				'</tpl>',
           						//'<li><a href="javascript:Page.showAllMakeTrims();ASC.Chrysler.trackLinkMetrics(\'content\',\'view_all\');">View All {vehicleDescription} features</a></li>',
           				//	'</ul>',
           				'</td>',
           			'</tr>',
           		'</table>'
           		,{
           			multipleTrims:function()
					{
						if(Page.numberOfTrims > 1){
							return true;
						}
						return false;
					}           		
           		}		
		);
		this.LinksTemplate.compile();
	},
		
	updateModelList:function(vehicleData)
	{
		if(vehicleData)
		{
			this.loadData(vehicleData, 1);
			this.render();
		}
	},
	
	update: function( modelList, selModelIndex ){
		if(modelList)
		{
			this.loadData(modelList, selModelIndex);
			this.render();
		}
	},
	
	updateSelectedIndex:function( index ){
		this.index = index;
		this.render();
	}
});

ASC.namespace('ASC.Chrysler.BMO.FilterList');

ASC.Chrysler.BMO.FilterList= ASC.extend(Ext.util.Observable,{
	
	renderTo:null,
	driveList:null,
	index:-1,
	filterID:null,
	
	constructor: function (filterList, filterID, index, cfg)
	{
	
		ASC.Chrysler.BMO.FilterList.superclass.constructor.apply(this);
		ASC.apply(this, cfg);
	
		this.addEvents({"render":true});
		this.initTemplates();
		this.loadData(filterList, filterID, index);
		var me = this;
	},
	
	loadData:function(filterList, filterID, index)
	{
		this.filterList = filterList;
		this.filterID = filterID;
		this.index = parseInt(index);
		this.render();
	},
		render:function()
		{
			var me = this;
			this.driveTpl.overwrite(this.renderTo, 
									{filterList:me.filterList, 
									 dindex: me.index,
									 filterID:me.filterID
									 });		
			this.fireEvent("render");
		},
		
		setSelectedDrive:function(index){
			this.index = index;
			this.render();	
		},
		
		initTemplates: function ()
		{
			this.driveTpl = new Ext.XTemplate(
				'<tpl if="this.displayFilter(filterList)">',
				'<table class="left_models_frame" border="0" cellpadding="0" cellspacing="0">',
					'<tr>',
						'<td class="left_models_header_left">',
							'<tpl if="this.isDrive(filterID)">',
								'Drive:',
							'</tpl>',
							'<tpl if="this.isBodystyle(filterID)">',
								'Bodystyle:',
							'</tpl>',
							'<tpl if="this.isCabBox(filterID)">',
								'Cab/Box:',
							'</tpl>',
							'<tpl if="this.isCab(filterID)">',
								'Cab:',
							'</tpl>',
							'<tpl if="this.isWheelbase(filterID)">',
								'Wheelbase:',
							'</tpl>',
							'<tpl if="this.isType(filterID)">',
								'Type:',
							'</tpl>',
							'<tpl if="this.isSeries(filterID)">',
								'Series:',
							'</tpl>',
							'<tpl if="this.isRoof(filterID)">',
								'Roof:',
							'</tpl>',
						'</td>',
					'</tr>',
					'<tr>',					
						'<td>',
							'<table id="driveCabTypes">',
								'<tr>',
									'<tpl for="filterList">',
										
											'<td class="drive_cab_selected">',
												'<table border="0" cellpading="0" cellspacing="0">',
													'<tr>',
														'<td>',
															'<input style="padding-bottom:10px;" type="radio" name="radioGroup{parent.filterID}" value="{description}" {[values.id === parent.dindex ? "checked":""]} id="{[this.getFilterId(xindex, parent.filterID)]}" onClick="javascript:Page.setSelectedFilterType({[xindex]}, \'{parent.filterID}\', \'{[this.getFilterId(xindex, parent.filterID)]}\');ASC.Chrysler.trackLinkMetrics(\'content\',\'{[this.cleanName(values.description)]}\');"/>',
														'</td>',
														'<td style="width:95px;text-align:center;">',
															'<a href="javascript:Page.setSelectedFilterType({[xindex]}, \'{parent.filterID}\', \'{[this.getFilterId(xindex, parent.filterID)]}\'); ASC.Chrysler.trackLinkMetrics(\'content\',\'{[this.cleanName(values.description)]}\');" >',
																'<img src="{[this.getFilterImage(values.description, parent.filterID, values.id === parent.dindex)]}" alt="{description}" id="{[this.getFilterId(xindex, parent.filterID)]}" width="76" height="33" />',
															'</a>',
														'</td>',
													'</tr>',
													'<tr>',
														'<td></td>',
														'<td style="text-align:center;">',
															'<a class={[values.id === parent.dindex ? "filter_selected":"filter_not_selected"]} href="javascript:Page.setSelectedFilterType({[xindex]}, \'{parent.filterID}\', \'{[this.getFilterId(xindex, parent.filterID)]}\')">',
																'{values.description}',
															'</a>',
														'</td>',
													'</tr>',
												'</table>',
											'</td>',
										
										
										'<tpl if="xindex%2 === 0">',
											'</tr>',
											'<tr>',
										'</tpl>',
									'</tpl>',	
								'</tr>',
							'</table>',
						'</td>',
					'</tr>',
				'</table>',
				'</tpl>'
           	,{
           		cleanName: function (unClean)
				{
					return unClean.replace('\'','').replace('"','');
				},
				
				translate: function (key)
				{
					return ASC.message(key);
				},
				
				isDrive:function( filterID ){
					return filterID == "1";
				},
				
				isBodystyle:function( filterID ){
					return filterID == "2";
				},
				
				isCab:function( filterID ){
					return filterID == "-2";
				},
				
				isCabBox:function( filterID ){
					return filterID == "-1";
				},
				
				isWheelbase:function( filterID ){
					return filterID == "11";
				},
				
				isType: function( filterID ){
					return filterID == "-3";
				},
				
				isSeries: function( filterID ){
					return filterID == "13";
				},
				
				isRoof: function( filterID ){
					return filterID == "14";
				},
				
				displayFilter: function( filterValues ){
					return filterValues != null && filterValues.length > 1;
				},
				
				getFilterImage: function( desc, filterType, isSelected ){
					if( this.isDrive( filterType )){
						if( isSelected ){
							return ASC.cfg.getImgPath() +'/en/shared/new/img_'+ desc.toLowerCase() +'_on.gif';
						}
						return ASC.cfg.getImgPath() +'/en/shared/new/img_'+ desc.toLowerCase() +'_off.gif';
					}else if( this.isCabBox( filterType ) ){
						var index1 = desc.indexOf(' ');
						var cabType = desc.substring(0, index1).toLowerCase();						
						var index2 = desc.lastIndexOf('\"');
						if( index2 < 0 )
							index2 = desc.lastIndexOf('\'');
						var boxLen = desc.substring(index1+2, index2).replace(/\s/gi,'');;
						boxLen = boxLen.replace(/[\'.]/g,'-');
						var index3 = boxLen.lastIndexOf('-');
						if( index3 < 0 )
							boxLen = boxLen + '-0';
						if( isSelected ){
							return ASC.cfg.getImgPath() +'/en/shared/new/img_'+ cabType+'_'+ boxLen +'_on.gif';
						}
						return ASC.cfg.getImgPath() +'/en/shared/new/img_'+ cabType+'_'+ boxLen +'_off.gif';
					}
					return;
				},
				
				getFilterId: function( index, filterType ){
					if( this.isDrive( filterType ) ){
						return 'drive_'+index;
					}else if( this.isCabBox( filterType ) ){
						return "cab_box_"+index;
					}else if( this.isCab( filterType ) ){
						return "cab_"+index;
					}else if( this.isBodystyle( filterType ) ){
						return "bodystyle_"+index;
					}else if( this.isWheelbase( filterType ) ){
						return "wheelbase_"+index;
					}else if( this.isType( filterType ) ){
						return "type_"+index;
					}else if( this.isRoof( filterType ) ){
						return "roof_"+index;
					}else if( this.isSeries( filterType ) ){
						return "series_"+index;
					}
				}
				
			}
			);
		
			this.driveTpl.compile();

	},
	update:function(index, updateModelList)
	{
		this.setSelectedDrive(index);
		if( updateModelList ){
			Page.updateModelList(this.filterID);
		}
	}
});

ASC.namespace('ASC.Chrysler.BMO.FilterListRadio');

ASC.Chrysler.BMO.FilterListRadio= ASC.extend(Ext.util.Observable,{
	
	renderTo:null,
	driveList:null,
	index:-1,
	filterID:null,
	
	constructor: function (filterList, filterID, index, cfg)
	{
	
		ASC.Chrysler.BMO.FilterListRadio.superclass.constructor.apply(this);
		ASC.apply(this, cfg);
	
		this.addEvents({"render":true});
		this.initTemplates();
		this.loadData(filterList, filterID, index);
		var me = this;
	},
	
	loadData:function(filterList, filterID, index)
	{
		this.filterList = filterList;
		this.filterID = filterID;
		this.index = parseInt(index);
		this.render();
	},
		render:function()
		{
			var me = this;
			this.driveTpl.overwrite(this.renderTo, 
									{filterList:me.filterList, 
									 dindex: me.index,
									 filterID:me.filterID
									 });		
			this.fireEvent("render");
		},
		
		setSelectedDrive:function(index){
			this.index = index;
			this.render();	
		},
		
		initTemplates: function ()
		{
			this.driveTpl = new Ext.XTemplate(
				'<tpl if="this.displayFilter(filterList)">',
				'<table class="left_models_frame" border="0" cellpadding="0" cellspacing="0">',
					'<tr>',
						'<td class="left_models_header_left">',
							'<tpl if="this.isDrive(filterID)">',
								'Drive:',
							'</tpl>',
							'<tpl if="this.isBodystyle(filterID)">',
								'Bodystyle:',
							'</tpl>',
							'<tpl if="this.isCabBox(filterID)">',
								'Cab/Box:',
							'</tpl>',
							'<tpl if="this.isCab(filterID)">',
								'Cab:',
							'</tpl>',
							'<tpl if="this.isWheelbase(filterID)">',
								'Cab-Axle Length / Wheelbase:',
							'</tpl>',
							'<tpl if="this.isType(filterID)">',
								'Type:',
							'</tpl>',
							'<tpl if="this.isSeries(filterID)">',
								'Series:',
							'</tpl>',
							'<tpl if="this.isRoof(filterID)">',
								'Roof:',
							'</tpl>',
						'</td>',
					'</tr>',
					'<tr>',					
						'<td>',
							'<table id="driveCabTypes">',
									'<tpl for="filterList">',
										'<tr>',
											'<td class="radio_filters">',
												'<input type="radio" name="radioGroup{parent.filterID}" value="{description}" {[values.id === parent.dindex ? "checked":""]} id="{[this.getFilterId(xindex, parent.filterID)]}" onClick="javascript:Page.setSelectedFilterType({[xindex]}, \'{parent.filterID}\', \'{[this.getFilterId(xindex, parent.filterID)]}\');"/>',
											'</td>',
											'<td>',
												'{values.description}',
											'</td>',
										'</tr>',
									'</tpl>',	
							'</table>',
						'</td>',
					'</tr>',
				'</table>',
				'</tpl>'
           	,{
				
				translate: function (key)
				{
					return ASC.message(key);
				},
				
				isDrive:function( filterID ){
					return filterID == "1";
				},
				
				isBodystyle:function( filterID ){
					return filterID == "2";
				},
				
				isCab:function( filterID ){
					return filterID == "-2";
				},
				
				isCabBox:function( filterID ){
					return filterID == "-1";
				},
				
				isWheelbase:function( filterID ){
					return filterID == "11";
				},
				
				isType: function( filterID ){
					return filterID == "-3";
				},
				
				isSeries: function( filterID ){
					return filterID == "13";
				},
				
				isRoof: function( filterID ){
					return filterID == "14";
				},
				
				displayFilter: function( filterValues ){
					return filterValues != null && filterValues.length > 1;
				},
				
				getFilterImage: function( desc, filterType, isSelected ){
					if( this.isDrive( filterType )){
						if( isSelected ){
							return ASC.cfg.getImgPath() +'/en/shared/img_'+ desc.toLowerCase() +'_on.gif';
						}
						return ASC.cfg.getImgPath() +'/en/shared/img_'+ desc.toLowerCase() +'_off.gif';
					}else if( this.isCab( filterType ) ){
						var index1 = desc.indexOf(' ');
						var cabType = desc.substring(0, index1).toLowerCase();						
						var index2 = desc.lastIndexOf('\"');
						if( index2 < 0 )
							index2 = desc.lastIndexOf('\'');
						var boxLen = desc.substring(index1+2, index2).replace(/\s/gi,'');;
						boxLen = boxLen.replace(/[\'.]/g,'-');
						var index3 = boxLen.lastIndexOf('-');
						if( index3 < 0 )
							boxLen = boxLen + '-0';
						if( isSelected ){
							return ASC.cfg.getImgPath() +'/en/shared/img_'+ cabType+'_'+ boxLen +'_on.gif';
						}
						return ASC.cfg.getImgPath() +'/en/shared/img_'+ cabType+'_'+ boxLen +'_off.gif';
					}
					return;
				},
				
				getFilterId: function( index, filterType ){
					if( this.isDrive( filterType ) ){
						return 'drive_'+index;
					}else if( this.isCabBox( filterType ) ){
						return "cab_box_"+index;
					}else if( this.isCab( filterType ) ){
						return "cab_"+index;
					}else if( this.isBodystyle( filterType ) ){
						return "bodystyle_"+index;
					}else if( this.isWheelbase( filterType ) ){
						return "wheelbase_"+index;
					}else if( this.isType( filterType ) ){
						return "type_"+index;
					}else if( this.isRoof( filterType ) ){
						return "roof_"+index;
					}else if( this.isSeries( filterType ) ){
						return "series_"+index;
					}
				}
				
			}
			);
		
			this.driveTpl.compile();

	},
	update:function(index, updateModelList)
	{
		this.setSelectedDrive(index);
		if( updateModelList ){
			Page.updateModelList(this.filterID);
		}
	}
});

ASC.namespace('ASC.Chrysler.BMO.AllTrims');
ASC.Chrysler.BMO.AllTrims = ASC.extend(Ext.util.Observable, {
	
	window: null,
	constructor: function ()
	{
		ASC.Chrysler.BMO.AllTrims.superclass.constructor.apply(this);		
	},
		
	initWindow: function (windowHtml)
	{
		var me = this;
		var windowId = Ext.id();
		//var html = '<iframe src="' + ASC.cfg.getContextPath() + '/vsmc/vehicleSpecModels.do?modelYearCode='+Page.modelYearCode+'&popup=true&bmoTabName=models&pageId=1&ccode='+Page.ccode+'&llp='+Page.llp+'" style="width:100%; height:100%; border: none;" id = "allTrimsFrame" name="allTrimsFrame" frameborder="0"></iframe>';
		var html = '<iframe src="' + ASC.cfg.getContextPath() + '/vsmc/vehicleSpecModels.do?modelYearCode='+Page.modelYearCode+'&popup=true&bmoTabName=models&pageId=1" style="width:100%; height:100%; border: none;" id = "allTrimsFrame" name="allTrimsFrame" frameborder="0"></iframe>';

		var btnClose = new Ext.Button({
			id: 'btnClose',
			text: ASC.message('bmo_close'), 
			cls: '',
			scope: this,
			ctCls: '',
			handler: function () { me.window.hide(); }
		});

		this.window = new Ext.Window({
			id: windowId,
			cls: '',
			title: Page.vehicleDescription + ': Compare All Models <a href="javascript:Page.hideWindow(\'compare\');" style="position:absolute;right:13px;text-decoration:none;"><div style="width:40px;text-align:center;height:20px;line-height:17px;background-color:'+ this.getBrandColor() +';color:#fff;font-size:10px;">Close</div></a>',
			constrainHeader: true,
			closable: false,
			closeAction: 'hide',
			draggable: false,
			header:true,
			width: 980,
			height: 500,
			modal: true,
			resizable: false,
			plain: true,			
			layout: 'fit',
			border: false,
			items: {
				html: html,
				border: false
			},
			
			buttons: [
				btnClose
			]			
		});
	},
	
	hide: function ()
	{
		this.window.hide();
	},
	
	show: function ()
	{
		if(this.window==null){
			this.initWindow();
		}
		this.window.show();
	},
	changeTitle:function(title)
	{
		this.window.setTitle(title + ': Compare All Models <a href="javascript:Page.hideWindow(\'compare\');" style="position:absolute;right:13px;text-decoration:none;"><div style="width:40px;text-align:center;height:20px;line-height:17px;background-color:'+ this.getBrandColor() +';color:#fff;font-size:10px;">Close</div></a>');
	},
	destroyWindow: function ()
	{
		if(this.window != null){
			this.window.destroy();
			delete this.window;
		}
	},
	
		
	getBrandColor: function()
	{
		if( ASC.cfg.getTheme() == 'chrysler' ){
			return '#00628E';
		}else if( ASC.cfg.getTheme() == 'dodge' ){
			return '#8B2323';
		}else if( ASC.cfg.getTheme() == 'jeep' ){
			return '#3A3225';
		}
		return '';
	}
	
});

/*for chassis cab and ram models*/
ASC.namespace('ASC.Chrysler.BMO.ModelFilter');
ASC.Chrysler.BMO.ModelFilter = ASC.extend(Ext.util.Observable, {
	
	modelList:null,
	selectedModelYear:null,
	
	constructor: function(modelList, selectedModelYear, cfg){
		ASC.Chrysler.BMO.ModelFilter.superclass.constructor.apply(this);
		ASC.apply(this, cfg);
		
		this.addEvents({"render":true});
		this.initTemplates();
		this.loadData(modelList, selectedModelYear);
	},
	
	loadData: function(modelList, selectedModelYear){
		this.modelList = modelList;
		this.selectedModelYear = selectedModelYear;
		this.render();
	},
	
	render: function(){
		this.modelsTpl.overwrite(this.renderTo,{models:this.modelList, selModelYear:this.selectedModelYear});
		this.fireEvent("render");
	},
	
	initTemplates: function(){
		this.modelsTpl = new Ext.XTemplate(
			'<table class="left_models_frame" border="0" cellpadding="0" cellspacing="0">',
					'<tr>',
						'<td class="left_models_header_left">',
							'Vehicle:',
						'</td>',
					'</tr>',
					'<tr>',					
						'<td>',
							'<table border="0">',
									'<tpl for="models">',
										'<tr>',
											'<td class="radio_filters">',
												'<input type="radio" name="radioGroupVehicles" {[values.modelYear === parent.selModelYear ? "checked":""]} id="{modelYear}" onClick="javascript:Page.setSelectedVehicle(\'{modelYear}\', \'{description}\');"/>',
											'</td>',
											'<td>',
												'{values.description}',
											'</td>',
										'</tr>',
									'</tpl>',	
							'</table>',
						'</td>',
					'</tr>',
				'</table>'
		);
		this.modelsTpl.compile();
	}
	
});

ASC.apply(Page, {
	
    vehicleList: null,
    incentives:null,
	selectedOptions:null,
	viewSelections:false,
	paymentEstimator:null,
	modelYearCode:null,
	vehicleDescription: null,
	trim: null,
	modelDesc: null,
	yearDesc: null,
	brand: null,
	
	dindex:-1,//drive
	bindex:-1,//bodystyle
	cindex:-1,//cab
	cbindex:-1,//cab-box
	windex:-1,
	sindex:-1,
	tindex:-1,
	rindex:-1,
	
	driveFilter:null,
	bodystyleFilter:null,
	cabFilter: null,
	cabBoxFilter: null,
	wheelbaseFilter:null,
	vehicleTypeFilter:null,
	roofFilter:null,
	seriesFilter:null,
	
	filterGroups:null,
	filterGroupIDs:null,
	combinations:null,
	allColors:null,
	colorGroups:null,
	currentModelList:null,
	currentColorType:null,
	currentColorIndex:null,
	currentModel:null,
	
	baseMsrp:0,
	destCharge:0,
	netTotal:0,
	configMSRP:0,
	incentivesTotal:0,
	
	employeeDiscount: 0,
	employeeDiscountDescription: null,
	    
	    
	cma: 0,
	fwp: 0,
	ppa: 0,
	daa: 0,
	dealerState: null,
	employeeDiscountType: null,	 
	
	flags: 0,   
    
    allTrims: new ASC.Chrysler.BMO.AllTrims(),
	expandImage: new ASC.Chrysler.BMO.ExpandImage(),
	viewAllMakeTrims: new ASC.Chrysler.BMO.ViewAllMakeTrims('models'),
    
    init: function () {
    
    	this.initWidgets();
    	this.initPaymentEstimator();
		this.initAjaxEngine();

		this.dialogMgr = new ASC.Chrysler.Configurator.DialogManager({
			events: {
	   			select: {
	   				fn: function (optCode) {
	   					this.selectColor(optCode);
	   				},
	   				scope: Page
	   			},
	   		
	   			cancel: {
	   				fn: function ()
	   				{
	   					this.cancelSelectOption();
	   				},
	   				scope: Page
	   			},
	   			
	   			complete: {
	    			fn: function (configResponse) {
	    				Page.selectComplete(configResponse);
	    			},
	    			scope: Page
	    		}
	   		}
		});
	},
	
	showAllTrims: function()
	{
		this.allTrims.show(this.modelYearCode);
	},
	getVehicleDescription:function(response, ioArgs)
	{
		this.allTrims.changeTitle(response.vehicleDescription);
	},
	changeWindowTitle:function(modelYearCode)
	{
		this.ajax.sendRequest('vehicleDescription',
				{
			    params:{
			          modelYearCode:modelYearCode,
			          ccode:"",
			          lowLevelPackageCode:""
			        }
				}		
		);	
	},
	initAjaxEngine: function ()
	{
		var ae = this.ajax;	
		var contextPath = ASC.cfg.getContextPath();	

		ae.registerRequest('SET_SELECTED_CCODE', contextPath + '/bmo/selectedModel.ajax');
		ae.registerAjaxObject('updateIncentives', {
				ajaxUpdateJSON: function (response, ioArgs) {
					var incentivesTotal = 0;
					var model = Page.currentModel;
					var incentives = response.data;
						
					if( Page.incentives == null ){
						Page.incentives = new ASC.Chrysler.BMO.CombinedIncentives(incentives, Page.incentivesCategory, {renderTo:"combined_incentives"});
					}else{
						Page.incentives.update( incentives, Page.incentivesCategory );
					}
						
					for( i=0; i<incentives.length;i++ ){
						if( incentives[i].incentiveType != 'SCB' ){
							incentivesTotal += parseFloat(incentives[i].programCashValue);
						}
					}		
					var ccode = ioArgs.data;
					ccode = ccode.split('&');
					ccode = ccode[0];
					ccode = ccode.split('=');
					ccode = ccode[1];
					Page.ccode=ccode;
					this.incentivesTotal = incentivesTotal;
					Page.configMSRP = model.baseMSRP + model.destination + model.forcedOptionsPrice;
					Page.netTotal = Page.configMSRP - incentivesTotal;
			
					document.getElementById("base_msrp").innerHTML = ASC.util.formatCurrency(model.baseMSRP, 0);
					document.getElementById("destination").innerHTML = ASC.util.formatCurrency(model.destination,0);
					document.getElementById("configMSRP").innerHTML = ASC.util.formatCurrency(Page.configMSRP, 0);
					document.getElementById("selected_opt").innerHTML =  ASC.util.formatCurrency(model.forcedOptionsPrice, 0);
					document.getElementById("vehicle_desc").innerHTML = Page.vehicleDescription; 	
					
					if (model.employeeDiscount < 0) {
						document.getElementById("employee_discount").innerHTML = ASC.util.formatCurrency(model.employeeDiscount, 0);
						document.getElementById("employee_discount_desc").innerHTML = model.employeeDiscountDescription;
						Page.netTotal += model.employeeDiscount;
					} else {
						document.getElementById("employee_discount").innerHTML = ''; 
						document.getElementById("employee_discount_desc").innerHTML = '';
					}
					document.getElementById("total_msrp").innerHTML = ASC.util.formatCurrency(Page.netTotal, 0);
					
					document.getElementById("view_my_selections").style.display = "none";
					document.getElementById("my_selected_options").style.display = "none";				
				}
		});

		ae.registerRequest('GET_EXTERNAL_LINK_INFO', contextPath + '/util/linkInformation.ajax');
		ae.registerAjaxObject('getExternalLinkInfo', {
				ajaxUpdateJSON: this.navigateToExternal
		});
		
		ae.registerRequest('GET_PACKAGE_CONTENT', contextPath + '/bmo/getPackageContent.ajax');
		ae.registerAjaxObject('packageContent', {
				ajaxUpdateJSON: this.displayPackageContent
		});
		ae.registerRequest('vehicleDescription', ASC.cfg.getContextPath() + '/sni/vehicleDescription.ajax');
		ae.registerAjaxObject('vehicleDesc', new ASC.Ajax.Response({
			events: {
				success: {
					fn: this.getVehicleDescription,
					scope: Page
				}
			}
		}));		
		//put common ajax routine in bmoCommon.js
		this.postInitAjaxEngine();
	},
	
	navigateToExternal: function(response, ioArgs)
	{
		var info = response.data;
		var target=ioArgs.options.params.target;
		var params='?target='+target+'&vehicle='+info.model+'&year='+info.year
		var zipCode = ASC.Chrysler.Cookies.getZipCode();
		if (zipCode!='undefined' && zipCode!='') {
			params=params+"&zipcode"+zipCode;
		}		
		params=params+'&model='+escape(info.vehicle);
		
		var externalFullUrl=ASC.cfg.getBridgeUrl() +params;
		window.location=externalFullUrl;
	},
	
	getAQuote: function() {
		this.ajax.sendRequest('GET_EXTERNAL_LINK_INFO', {
			params: {
				target: 'gaq'
			}
		});
	},
	
	setSelectedFilterType:function( index, filterTypeID, imgId ){
		if( filterTypeID == "1" && this.driveFilter){
			//this.dindex = index;
			this.dindex = this.filterGroups["driveType"][index-1].id;
			this.driveFilter.update( this.dindex, true );
		}else if( filterTypeID == "2" && this.bodystyleFilter){
			//this.bindex = index;
			this.bindex = this.filterGroups["bodystyle"][index-1].id;
			this.bodystyleFilter.update( this.bindex, true );
		}else if( filterTypeID == "-1" && this.cabBoxFilter){
			//this.cbindex = index;
			this.cbindex = this.filterGroups["cab_box"][index-1].id;
			this.cabBoxFilter.update( this.cbindex, true );
		}else if( filterTypeID == "-2" && this.cabFilter){
			//this.cindex = index;
			this.cindex = this.filterGroups["cab"][index-1].id;
			this.cabFilter.update( this.cindex, true );
		}else if( filterTypeID == "11" && this.wheelbaseFilter){
			//this.windex = index;
			this.windex = this.filterGroups["wheelbase"][index-1].id;
			this.wheelbaseFilter.update( this.windex, true );
		}else if( filterTypeID == "-3" && this.vehicleTypeFilter){
			//this.tindex = index;
			this.tindex = this.filterGroups["type"][index-1].id;
			this.vehicleTypeFilter.update( this.tindex, true );
		}else if( filterTypeID == "13" && this.seriesFilter){
			//this.sindex = index;
			this.sindex = this.filterGroups["series"][index-1].id;
			this.seriesFilter.update( this.sindex, true );
		}else if( filterTypeID == "14" && this.roofFilter){
			//this.rindex = index;
			this.rindex = this.filterGroups["roof"][index-1].id;
			this.roofFilter.update( this.rindex, true );
		}
		if( index != -1 ){	
			if(ASC.getEl(imgId).dom.src.charAt('on')!=-1){		
				ASC.getEl(imgId).dom.src = ASC.getEl(imgId).dom.src.replace('off','on');
			}	
		}
	},
	
	setSelectedModel:function( mindex, colorGroupId, trim ){
		var clearTrim = trim.replace(/\/\//g,'\/').replace(/\//g,'_').toLowerCase();
		this.setMetrics(clearTrim);	
		var me = this;		
		me.modelList.updateSelectedIndex(mindex);
		var index = colorGroupId.indexOf('_');
		this.ccode = colorGroupId.substring(0, index);
		this.llp = colorGroupId.substring( index+1, colorGroupId.length);
		
		var modelList = this.getModelList();
		var selModel = modelList[mindex-1];
		
		me.setSelectedCcode( this.ccode, this.llp , colorGroupId);
		me.trim = trim;		
		me.modelList.renderLinks(me.modelDesc + ' ' + me.trim);
		var ccode_llp = colorGroupId.split('_');		
		ASC.getEl('ccode').setValue(ccode_llp[0]);
		ASC.getEl('llp').setValue(ccode_llp[1]);
		Page.allTrims.destroyWindow();
		this.resetPaymentEstimations();
	},
	
	getModelList:function(){
		var me = this;
		var combinations = me.combinations;
		var modelList = this.filterGroups["realTrim"];
		var newCombList = [];
		var newModelList = [];
		
		for(i=0; i<combinations.length; i++){
			var comb = combinations[i];
			var cids = comb.cids;
			var include = ( me.dindex == -1 || cids[me.filterGroupIDs["driveType"]] == me.dindex )&&
						  ( me.cbindex == -1 || cids[me.filterGroupIDs["cab_box"]] == me.cbindex)&&
						  ( me.cindex == -1 || cids[me.filterGroupIDs["cab"]] == me.cindex)&&
						  ( me.bindex == -1 || cids[me.filterGroupIDs["bodystyle"]] == me.bindex)&&
						  ( me.windex == -1 || cids[me.filterGroupIDs["wheelbase"]] == me.windex)&&
						  ( me.tindex == -1 || cids[me.filterGroupIDs["type"]] == me.tindex)&&
						  ( me.sindex == -1 || cids[me.filterGroupIDs["series"]] == me.sindex)&&
						  ( me.rindex == -1 || cids[me.filterGroupIDs["roof"]] == me.rindex);
			
			if( cids[me.filterGroupIDs["realTrim"]] != null && include && newCombList[cids[me.filterGroupIDs["realTrim"]]] == null){
				newCombList[cids[me.filterGroupIDs["realTrim"]]]=comb;
			}
		}
		
		for( i=0; i<modelList.length; i++ ){
			var model = modelList[i]; 
			var comb = newCombList[model.id];
			if( comb != null ){
				var myModel = new ASC.Chrysler.BMO.ModelObject();
				myModel.description = model.description;
				myModel.msrpAsConfigured = comb.msrpAsConfigured;
				myModel.baseMSRP = comb.baseMsrp;
				myModel.destination = comb.destination;
				myModel.forcedOptionsPrice = comb.forcedOptionsValue;
				myModel.CCode = comb.ccode;
				myModel.lowerLevelPackageCode = comb.lowerLevelPackageCode;
				myModel.defExtImage = comb.defExtImage;
				myModel.defIntImage = comb.defIntImage;
				
				myModel.cma = comb.CMA;
				myModel.fwp = comb.FWP;
				myModel.ppa = comb.PPA;
				myModel.daa = comb.DAA;
				
				myModel.flags = comb.flags;
				
				if (comb.employeeDiscount != null) {
					myModel.employeeDiscount = comb.employeeDiscount.msrp;
					myModel.employeeDiscountDescription = comb.employeeDiscount.name;
				}else{
					myModel.employeeDiscount = 0;
					myModel.employeeDiscountDescription = '';
				}
				newModelList.push(myModel);
			}
		}
		
		var sortModels = new Ext.util.MixedCollection(false, function (field) {
			return field.CCode + "_" + field.lowerLevelPackageCode;	
		});
		
		for( i=0; i<newModelList.length; i++ ){
			sortModels.add(newModelList[i]);
		}
		
		var sort = function (lhs, rhs) {
			return lhs.msrpAsConfigured>rhs.msrpAsConfigured ? 1 : lhs.msrpAsConfigured<rhs.msrpAsConfigured ? -1 : 0; 
		};
		
		sortModels.sort('ASC',sort);
		
		newModelList = sortModels.items;
		
		return newModelList;
	},
	
	updateModelList:function( variationId ){
		var modelList = this.getModelList();
		if( modelList.length == 0){
			this.findClosestMatchComb( variationId );
			modelList = this.getModelList();
		}
		this.currentModelList = modelList;
		this.modelList.updateModelList(modelList);
		var colorsGroupID = modelList[0].CCode + "_" + modelList[0].lowerLevelPackageCode;
		this.resetPaymentEstimations();
		this.ccode = modelList[0].CCode;
		this.llp = modelList[0].lowerLevelPackageCode;
		this.setSelectedCcode(modelList[0].CCode, modelList[0].lowerLevelPackageCode, colorsGroupID);
		var trim = modelList[0].description;
		var clearTrim = trim.replace(/\//g,'').toLowerCase();
		//ASC.Chrysler.trackPageMetrics('/'+this.lang+'/'+this.yearDesc+'/'+this.modelDesc+'/build_my_own/'+clearTrim+'/models');
		this.setPackageToolTips(modelList);
	},
	
	findClosestMatchComb: function( variationId ){
		var maxMissingFilter = 1;
		var flag = true;
		var combinations = this.combinations;
		var cids = null;
		var filter = this.getFilterIdAndSelectedIndex(variationId);
		//Sprinter
		//find combinations that match the selected filterID
		var selComb = [];
		var cids = null;
		for( i=0; i<combinations.length; i++ ){
			cids = combinations[i].cids;
			if( cids[filter[0]] == filter[1] ){
				selComb.push(combinations[i]);
			}
		}
		if( selComb.length > 0 ){
				combinations = selComb;
				selComb = [];
		}
		
		//find comb for selected type, if type is not the selected filter
		if(this.filterGroupIDs["driveType"] && filter[0] != this.filterGroupIDs["driveType"] && combinations.length > 1){
			for( i=0; i<combinations.length; i++ ){
				var cids = combinations[i].cids;
				if( cids[this.filterGroupIDs["driveType"]] == this.dindex ){
					selComb.push(combinations[i]);
				}
			}
			if( selComb.length > 0 ){
				combinations = selComb;
				selComb = [];
			}
		}
		
		//find comb for selected type, if type is not the selected filter
		if(this.filterGroupIDs["type"] && filter[0] != this.filterGroupIDs["type"] && combinations.length > 1){
			for( i=0; i<combinations.length; i++ ){
				var cids = combinations[i].cids;
				if( cids[this.filterGroupIDs["type"]] == this.tindex ){
					selComb.push(combinations[i]);
				}
			}
			if( selComb.length > 0 ){
				combinations = selComb;
				selComb = [];
			}
		}
		
		//find comb for selected series, if series is not the selected filter
		if(this.filterGroupIDs["series"] && filter[0] != this.filterGroupIDs["series"] && combinations.length > 1){
			for( i=0; i<combinations.length; i++ ){
				var cids = combinations[i].cids;
				if( cids[this.filterGroupIDs["series"]] == this.sindex ){
					selComb.push(combinations[i]);
				}
			}
			if( selComb.length > 0 ){
				combinations = selComb;
				selComb = [];
			}
		}
		
		//find comb for selected roof, if roof is not the selected filter
		if(this.filterGroupIDs["roof"] && filter[0] != this.filterGroupIDs["roof"] && combinations.length > 1){
			for( i=0; i<combinations.length; i++ ){
				var cids = combinations[i].cids;
				if( cids[this.filterGroupIDs["roof"]] == this.rindex ){
					selComb.push(combinations[i]);
				}
			}
			if( selComb.length > 0 ){
				combinations = selComb;
				selComb = [];
			}
		}
		
		//find comb for selected cab, if cab is not the selected filter
		if(this.filterGroupIDs["cab"] && filter[0] != this.filterGroupIDs["cab"] && combinations.length > 1){
			for( i=0; i<combinations.length; i++ ){
				var cids = combinations[i].cids;
				if( cids[this.filterGroupIDs["cab"]] == this.cindex ){
					selComb.push(combinations[i]);
				}
			}
			if( selComb.length > 0 ){
				combinations = selComb;
				selComb = [];
			}
		}
		
		//find comb for selected wheelbase, if wheelbase is not the selected filter
		if(this.filterGroupIDs["wheelbase"] && filter[0] != this.filterGroupIDs["wheelbase"] && combinations.length > 1){
			for( i=0; i<combinations.length; i++ ){
				var cids = combinations[i].cids;
				if( cids[this.filterGroupIDs["wheelbase"]] == this.windex ){
					selComb.push(combinations[i]);
				}
			}
		}
						
		cids = combinations[0].cids;
		this.setFiltersForCombination(cids);
		return;
		
	},
	
	setFiltersForCombination: function(cids){
		if( cids!=null ){
			if( this.driveFilter && this.dindex != cids[this.filterGroupIDs["driveType"]]){
				this.dindex = cids[this.filterGroupIDs["driveType"]];
				this.driveFilter.update(this.dindex, false);
			}
			if( this.bodystyleFilter && this.bindex != cids[this.filterGroupIDs["bodystyle"]]){
				this.bindex = cids[this.filterGroupIDs["bodystyle"]];
				this.bodystyleFilter.update(this.bindex, false);
			}
			if( this.cabBoxFilter && this.cbindex != cids[this.filterGroupIDs["cab_box"]]){
				this.cbindex = cids[this.filterGroupIDs["cab_box"]];
				this.cabBoxFilter.update(this.cbindex, false);
			}
			if( this.cabFilter && this.cindex != cids[this.filterGroupIDs["cab"]]){
				this.cindex = cids[this.filterGroupIDs["cab"]];
				this.cabFilter.update(this.cindex, false);
			}
			if( this.wheelbaseFilter && this.windex != cids[this.filterGroupIDs["wheelbase"]]){
				this.windex = cids[this.filterGroupIDs["wheelbase"]];
				this.wheelbaseFilter.update(this.windex, false);
			}
			if( this.vehicleTypeFilter && this.tindex != cids[this.filterGroupIDs["type"]]){
				this.tindex = cids[this.filterGroupIDs["type"]];
				this.vehicleTypeFilter.update(this.tindex, false);
			}
			if( this.seriesFilter && this.sindex != cids[this.filterGroupIDs["series"]]){
				this.sindex = cids[this.filterGroupIDs["series"]];
				this.seriesFilter.update(this.sindex, false);
			}
			if( this.roofFilter && this.rindex != cids[this.filterGroupIDs["roof"]]){
				this.rindex = cids[this.filterGroupIDs["roof"]];
				this.roofFilter.update(this.rindex, false);
			}
		}
		
	},
	
	getFilterIdAndSelectedIndex: function( filterTypeID ){
		var filterId = [];
		if( filterTypeID == "1" && this.driveFilter){
			filterId[0] = this.filterGroupIDs["driveType"];
			filterId[1] = this.dindex;
		}else if( filterTypeID == "2" && this.bodystyleFilter){
			filterId[0] = this.filterGroupIDs["bodystyle"];
			filterId[1] = this.bindex;
		}else if( filterTypeID == "-1" && this.cabBoxFilter){
			filterId[0] = this.filterGroupIDs["cab_box"];
			filterId[1] = this.cbindex;
		}else if( filterTypeID == "-2" && this.cabFilter){
			filterId[0] = this.filterGroupIDs["cab"];
			filterId[1] = this.cindex;
		}else if( filterTypeID == "11" && this.wheelbaseFilter){
			filterId[0] = this.filterGroupIDs["wheelbase"];
			filterId[1] = this.windex;
		}else if( filterTypeID == "-3" && this.vehicleTypeFilter){
			filterId[0] = this.filterGroupIDs["type"];
			filterId[1] = this.tindex;
		}else if( filterTypeID == "13" && this.seriesFilter){
			filterId[0] = this.filterGroupIDs["series"];
			filterId[1] = this.sindex;
		}else if( filterTypeID == "14" && this.roofFilter){
			filterId[0] = this.filterGroupIDs["roof"];
			filterId[1] = this.rindex;
		}
		return filterId;
	},
	
	//send request to server to set the selected ccode and update the vehicle tally
	setSelectedCcode: function (ccode, llp, colorsGroupID)
	{
		var modelList = this.currentModelList;
		var model = this.getModelObj(ccode, llp);
		this.currentModel = model;
		this.baseMsrp = parseFloat(model.baseMSRP);
		this.destCharge = parseFloat(model.destination);
		
		var forcedOptionsPrice = parseFloat(model.forcedOptionsPrice);
		this.configMSRP = this.baseMsrp + this.destCharge + forcedOptionsPrice;
		this.netTotal = this.configMSRP;
		
		var vehicleDesc = this.yearDesc + ' ' + this.modelDesc;
		
		this.employeeDiscount = model.employeeDiscount;
		this.employeeDiscountDescription = model.employeeDiscountDescription;
		
		if( this.cbindex != -1 ){
			var cabBoxFilters = this.filterGroups["cab_box"];
			var cbfilter = null;
			for( i=0; i<cabBoxFilters.length; i++ ){
				cbfilter = cabBoxFilters[i];
				if( cbfilter.id == this.cbindex ){
					break;
				}
			}
			
			var cabBoxDesc = cbfilter.description;
			var index = cabBoxDesc.indexOf(' ');
			vehicleDesc = vehicleDesc + ' ' + cabBoxDesc.substring(0, index);
		}  
		
		 vehicleDesc = vehicleDesc + ' ' + this.currentModel.description;
		
		if( this.dindex != -1){ 
			var driveFilters = this.filterGroups["driveType"];
			var dfilter = null;
			for( i=0; i<driveFilters.length; i++ ){
				dfilter = driveFilters[i];
				if( dfilter.id == this.dindex ){
					break;
				}
			}
			var driveDesc = dfilter.description;
			vehicleDesc = vehicleDesc + ' ' + driveDesc;
		} 
		
		this.vehicleDescription = vehicleDesc;
		
		this.updateVehicleImage("PC", this.currentModel.defExtImage);
		this.updateVehicleImage("FS", this.currentModel.defIntImage);
		
		this.ajax.sendRequest('SET_SELECTED_CCODE', {
			params: {
				ccode: ccode,
				llp: llp,
				driveTypeId: this.dindex,
				cabTypeId: this.cindex,
				vehTypeId: this.tindex,
				cabBoxTypeId: this.cbindex,
				vehicleDesc: vehicleDesc,
				exteriorImage: this.currentModel.defExtImage,
				interiorImage: this.currentModel.defIntImage,
				trimDesc: this.currentModel.description,
				
				cma: this.currentModel.cma,
				fwp: this.currentModel.fwp,
				ppa: this.currentModel.ppa,
				daa: this.currentModel.daa,				
				flags: this.currentModel.flags,
				baseMsrp: this.currentModel.baseMSRP

			}
		});
	},
	
	getModelObj:function( ccode, llp ){
		var modelList = this.currentModelList;
		for( i=0; i<modelList.length; i++ ){
			if( modelList[i].CCode == ccode && modelList[i].lowerLevelPackageCode == llp ){
				return modelList[i];
			}
		}
		return null;
	},
	
	updateVehicleImage: function(colorType, imgName){
		var imageToChange = '';
		if(colorType==='FS'){
			imageToChange = 'interiorImage';
		}else if( colorType === 'PC' ){
			imageToChange = 'exteriorImage';
		}		
		ASC.getEl(imageToChange).dom.setAttribute('onerror','ASC.Chrysler.imageOnError(this);');
		ASC.getEl(imageToChange).dom.src= ASC.cfg.getIofUrl() + imgName + '&width=211&height=115&NOIMG=not_found_'+Page.brand+'.gif';
		ASC.getEl(imageToChange).dom.setAttribute('asc:imgName', imgName);
	},
	
	setMetrics: function( trimDesc ){ 
		ASC.Chrysler.trackLinkMetrics('select_model',trimDesc);
//		ASC.Chrysler.trackPageMetrics('/'+this.lang+'/'+this.yearDesc+'/'+this.modelDesc+'/build_my_own/'+trimDesc+'/models');
	},
	
	setSelectedVehicle: function( modelYear, desc ){
		ASC.Chrysler.trackLinkMetrics('content','vehicle_'+desc);
		window.location = ASC.cfg.getContextPath()+'/bmo/models.do?modelYearCode='+modelYear;
	},
	
	getPackageContent: function( ccode, llp ){
		this.ajax.sendRequest('GET_PACKAGE_CONTENT', {
			params: {
				ccode: ccode,
				lowLevelPackageCode: llp
			}
		});
	},
	
	displayPackageContent: function( response, ioArgs ){
		var options = response.data.options;
		var ccode = response.ccode;
		var llp = response.llp;
		//Page.setToolTip( ccode+'_'+llp+'_img', options );
	},
	
	setToolTip: function(ccode, llp)
	{
		var ttp = new Ext.ToolTip({
			target: ccode + '_' + llp + '_img',
			floating: true,
			width: 250,
			shadow: false,
			dismissDelay: 0,
			autoLoad: {url:ASC.cfg.getContextPath() + '/bmo/getPackageContent.ajax?ccode='+ccode+'&lowLevelPackageCode='+llp}		    
		});
	}
	
});

ASC.onReady(Page.init, Page);
