PackageList=function(){
	var description = null;
	var code = null;
	var ecc = null;
}
GroupList=function(){
	var groupdesc = null;
	var packageList = [];
}
ASC.namespace('ASC.Chrysler.Configurator.Dialogs.Delta');

(function () {

	ASC.loadBundle({
	    locale: 'en_CA',
	    
	    messages: {        
	        'delta_title': 'YOUR SELECTION REQUIRES CHANGES',
	        'delta_totalPriceIncreaseBy': 'The total price of this vehicle will <b> increase </b> by',
	        'delta_totalPriceDescreaseBy': 'The total price of this vehicle will <b> decrease </b> by',
	        'delta_selectThisOption': 'Would you like to select this option?',
	        'delta_removeThisOption': 'Your selection requires that you remove option(s):',
	        'delta_helpMeHdr': 'Help me understand this',
	        'delta_helpMeText': 'Some options are available only for certain models or in combination with other options. And there are some options of which you can only select one (like stereos or tires). If you change these, other selected options may no longer be available. If you make a change which requires another option to be added or removed, you will be offered the choice of continuing with the changes or cancelling',
	        'delta_close':'Close',
	        'delta_no':'No',
	        'delta_yes':'Yes'
	    }
	});
	
	ASC.loadBundle({
	    locale: 'fr_CA',
	    
	    messages: {        
	    	'delta_title': 'YOUR SELECTION REQUIRES CHANGES',
	        'delta_totalPriceIncreaseBy': 'The total price of this vehicle will <b> increase </b> by',
	        'delta_totalPriceDescreaseBy': 'The total price of this vehicle will <b> decrease </b> by',
	        'delta_select' : 'You have selected ',
	        'delta_selectAdd' : 'which requires the addition of  ',
	        'delta_selectRemove' : 'which requires the removal of ',
	        'delta_includes' : ' includes:',
	        'delta_removing' : ' Removing ',
	        'delta_willRemove' : ' will remove:',
	        'delta_and' : ' <font class=normalfont >and</font> ' ,
	        'delta_group' : ' Groups' ,
	        'delta_Separate' : ', ',
	        'delta_makeThisChange': 'Would you like to make this change?',       
	        'delta_selectThisOption': 'Would you like to select this option?',
	        'delta_removeThisOption': 'Your selection requires that you remove option(s):',
	        'delta_helpMeHdr': 'Help me understand this',
	        'delta_helpMeText': 'Some options are available only for certain models or in combination with other options. And there are some options of which you can only select one (like stereos or tires). If you change these, other selected options may no longer be available. If you make a change which requires another option to be added or removed, you will be offered the choice of continuing with the changes or cancelling',
	        'delta_close':'Close',
	        'delta_no':'No',
	        'delta_yes':'Yes'
	    }
	});
	
	ASC.loadBundle({
	    locale: 'en_US',
	    
	    messages: {        
	    	'delta_title': 'YOUR SELECTION REQUIRES CHANGES',
	        'delta_totalPriceIncreaseBy': 'The total price of this vehicle will <b> increase </b> by',
	        'delta_totalPriceDescreaseBy': 'The total price of this vehicle will <b> decrease </b> by',
	        'delta_select' : 'You have selected ',
	        'delta_selectAdd' : 'which requires the addition of  ',
	        'delta_selectRemove' : 'which requires the removal of ',
	        'delta_includes' : ' includes:',
	        'delta_removing' : ' Removing ',
	        'delta_willRemove' : ' will remove:',
	        'delta_and' : ' <font class=normalfont >and</font> ' ,
	        'delta_group' : ' Groups' ,
	        'delta_Separate' : ', ',
	        'delta_makeThisChange': 'Would you like to make this change?',
	        'delta_selectThisOption': 'Would you like to select this option?',
	        'delta_removeThisOption': 'Your selection requires that you remove option(s):',
	        'delta_helpMeHdr': 'Help me understand this',
	        'delta_helpMeText': 'Some options are available only for certain models or in combination with other options. And there are some options of which you can only select one (like stereos or tires). If you change these, other selected options may no longer be available. If you make a change which requires another option to be added or removed, you will be offered the choice of continuing with the changes or cancelling',
	        'delta_close':'Close',
	        'delta_no':'No',
	        'delta_yes':'Yes'
	    }
	});


ASC.Chrysler.Configurator.Dialogs.Delta = ASC.extend(Ext.Window, {
	
	configResponse: null,
	
	deltaTpl: null,
	listTpl: null,
	conflictinglist : [],
	
	selectGroupName : [] ,
	selectGroupList : [],
	unselectGroupList : [],
	unselectGroupName : [],
	curOptionDesc : null ,
	unselectGroupNameStr : null ,
	selectGroupNameStr : null ,
	conflictingStr : null ,
	curCode : null  ,
	curEcc : null ,
	/**
	 * @param {Object} cfg Window configurator parameters that are passed on to Ext.Window
	 */
	constructor: function (/*Object*/ configResponse,selectedCode, /*[Object]*/ cfg)
	{
		var andStr = ASC.message('delta_and');
		var conflictSepStr = ASC.message('delta_Separate');
		this.curCode = selectedCode ;
		this.configResponse = configResponse;
		this.setcurOptionDesc();
		this.getGroupNameAndGroupList();
		this.selectGroupNameStr = this.selectGroupName.join(andStr) ;
		this.unselectGroupNameStr = this.unselectGroupName.join(andStr) ;
		this.conflictingStr = "";
		if (this.conflictinglist.length > 0)
			this.conflictingStr = this.conflictinglist.join(conflictSepStr) + andStr ;
		/*set the window id*/
		this.id = Ext.id(); 
		
		this.initDlgTemplates();				
		var html = this.buildContents();	
		var txtItem = new Ext.Toolbar.TextItem('<h5>' + ASC.message('delta_makeThisChange') + '</h5>');
		var spacer = new Ext.Toolbar.Spacer();
		var me = this;
		
		var btnClose = new Ext.Button({
			text: ASC.message('delta_no'), 
			cls: "x-btn-text",
			scope: this,
			ctCls: 'dlgBtn',
			handler: function () { me.fireEvent('cancel'); }
		});
		
		var btnSelect = new Ext.Button({
			text: ASC.message('delta_yes'), 
			cls: "x-btn-text",
			scope: this,
			ctCls: 'dlgBtn',			
			handler: function () { me.fireEvent('select', me.configResponse); }								
		});	
		
		
		var winCfg = ASC.apply({
			/*id: windowId,*/
			ctCls: 'ppweb-deltaDlg',
			constrainHeader: true,
			draggable: true,
			title: ASC.message('delta_title'),
			headerAsText: true,
			closable: false,
			plain: true,
			layout: 'fit',
			border: false,
			modal: true,
			resizable: false,
			width: 450,
									
			items: {
				html: html
			},
			
			buttons: [
			    txtItem,
			    spacer,
			    spacer,
				btnSelect,
				btnClose
			]	
		}, cfg);				
				
		ASC.Chrysler.Configurator.Dialogs.Delta.superclass.constructor.call(this, winCfg);
		
		this.addEvents({
			select: true,
			cancel: true
		});
		
	},
	//get select option and unselect option group list 
	getGroupNameAndGroupList: function()
	{
		var selectList = this.configResponse.delta.selectedList;
		var groupName = '' ;
		var code = null ;
		var i = 0;
		this.conflictinglist = [] ;
		this.selectGroupName = [] ;
		this.selectGroupList = [];
		this.unselectGroupList = [];
		//get selectOption group name
		for( i=0; i<selectList.length;i++ ){
			code = selectList[i].code ;
			groupName = selectList[i].description ;
			this.selectGroupName.push(groupName);
			var selectPackageList = this.setSelectPackageList(code);
			if (selectPackageList.length > 0 ){
				var groupList = new GroupList();
				groupList.packageList = selectPackageList ;
				groupList.groupdesc = groupName;
				this.selectGroupList.push(groupList);
			}
		}		
		//get unselectOption Group list
		var unselectList = this.configResponse.delta.unselectedList;
		code = null ;
		i = 0;
		this.unselectGroupName = [] ;
		//get unselectOption group name
		for( i=0; i<unselectList.length;i++ ){
			code = unselectList[i].code ;
			groupName = unselectList[i].description ;
			this.unselectGroupName.push(groupName);
			var unselectPackageList = this.setUnSelectPackageList(code);
			if (unselectPackageList.length > 0 ){
				var groupList = new GroupList();
				groupList.packageList = unselectPackageList ;
				groupList.groupdesc = groupName ;
				this.unselectGroupList.push(groupList);
			}
		}		
		

	},
	setSelectPackageList : function(code){
		var i = 0;
		var contentList = [];
		var selectPackageList = [];
		var options = this.configResponse.options ;
		for (i=0;i < options.length; i++){
			if (code == options[i].optCode) {
				//get content list
				contentList = options[i].contentList ;
				if (contentList != null && contentList.length > 0 ){
					var j = 0 ;
					for (j = 0 ; j < contentList.length ; j++ ) {
						var packageList = new PackageList(); 
						packageList.description = contentList[j].description ;
						selectPackageList.push(packageList);
						if( contentList[j].contentList != null && contentList[j].contentList.length > 0){
							for(k=0; k< contentList[j].contentList.length; k++){
								var packageList1 = new PackageList(); 
								packageList1.description = contentList[j].contentList[k].description ;
								selectPackageList.push(packageList1);
							}
						}
					}
				}
				break ;
			}
		}
		return selectPackageList ;
	},

	setUnSelectPackageList : function(code){
		var i = 0;
		var contentList = [];
		var unselectPackageList = [] ;
		var options = this.configResponse.options ;
		for (i=0;i < options.length; i++){
			if (code == options[i].optCode) {
				//get content list
				contentList = options[i].contentList ;
				if (contentList != null && contentList.length > 0 ){
					var j = 0 ;
					for (j = 0 ; j < contentList.length ; j++ ) {
						var packageList = new PackageList(); 
						packageList.description = contentList[j].description ;
						unselectPackageList.push(packageList);
						if( contentList[j].contentList != null && contentList[j].contentList.length > 0){
							for(k=0; k< contentList[j].contentList.length; k++){
								var packageList1 = new PackageList(); 
								packageList1.description = contentList[j].contentList[k].description ;
								unselectPackageList.push(packageList1);
								if (this.curEcc == contentList[j].contentList[k].ecc){
									this.conflictinglist.push(contentList[j].contentList[k].description);
								}
							}
						}
						if (this.curEcc == contentList[j].ecc){
							this.conflictinglist.push(contentList[j].description);
						}
					}
				}
				break ;
			}
		}
		return unselectPackageList ;
	},
	setcurOptionDesc: function()
	{
		var i = 0;
		var options = this.configResponse.options ;
		for (i=0;i < options.length; i++){
			if (this.curCode == options[i].optCode) {
				this.curOptionDesc = options[i].description;
				this.curEcc = options[i].ecc;
				//var mySelectObject = new SelectObject();
				//mySelectObject.code = this.curCode;
				//mySelectObject.description = options[i].description;
				//mySelectObject.ecc = options[i].ecc;
				//this.configResponse.delta.selectedList.push(mySelectObject);
				
				return ;
			}
		}
		
	},
	//private
	buildContents: function ()
	{
		var d = this.configResponse.delta;
		var listHTML = [];
		 
		if (d.selectedList.length > 0) {
			listHTML.push(this.buildOptionList(this.requiresSelectTpl,this.selectGroupList,true));
		}
		if (d.unselectedList.length > 0) {
			listHTML.push(this.buildOptionList(this.requiresUnselectTpl, this.unselectGroupList,false));
		}
		
		//determine if there is a price change that needs to be displayed
		var msrp = ASC.util.getFloat(d.deltaTotalMsrp);
		var hasPriceChange = !isNaN(msrp) && msrp !== 0;
		var isPositive = hasPriceChange && msrp > 0;
				
		return this.deltaTpl.applyTemplate({
			bodyHTML: listHTML.join('\n'),			
			hasPriceChange: hasPriceChange,
			msrp: ASC.util.formatCurrency(msrp, 0), 
			isPositive: isPositive, 
			isNegative: !isPositive,
			windowId: this.id			
		});
	},
		
	//private
	initDlgTemplates: function ()
	{		
		this.deltaTpl = new Ext.XTemplate(
			'<div class="a-delta-body">',
				'<form action="javascript:void(0);" method="get">',
					'<div class="viewPort">',
						'{bodyHTML}',
					'</div>',
					'<tpl if="hasPriceChange">',
						'<div >',
							'<tpl if="isPositive">',
								ASC.message('delta_totalPriceIncreaseBy') + ' <span class="positive">{msrp}</span>',
							'</tpl>',
							'<tpl if="isNegative">',
								ASC.message('delta_totalPriceDescreaseBy') + ' <span class="negative">{msrp}</span>',
							'</tpl>',
						'</div>',
					'</tpl>',	
					'<div>',
						'<div class="a-helpMe-hdr">',
							'<a href="{[this.getHelpMeHref(values.windowId)]}">' + ASC.message('delta_helpMeHdr') + '</a>',
							'<a href="{[this.getHelpMeHref(values.windowId)]}"><img src="{[ASC.cfg.getImgLocalePath()]}/bmo/bodycontainer_icon_09.gif" width="16" height="15" alt="" /></a>',
							'<a id="{windowId}_a-helpMe_close" href="{[this.getHelpMeHref(values.windowId)]}" style="visibility: hidden;">'+ASC.message('delta_close')+'</a>',
						'</div>',
						'<div id="{windowId}_helpMe_text" class="a-help-me" style="display:none;">' + ASC.message('delta_helpMeText') + '</div>',
					'</div>',
				'</form>',
			'</div>', {
				getHelpMeHref: function (windowId) {
					return 'javascript:var win=Ext.getCmp(\'' + windowId + '\'); if(win)win.toggleHelpMe();';
				}
			}
		);
		this.deltaTpl.compile();
				
		this.listTpl = new Ext.XTemplate(
			'<tpl for="groups">',
				'<tpl if="parent.isAddition">',
					'<div class=group_div><font class=specfont>{groupdesc}</font>'  + ASC.message('delta_includes') + '</div>',
				 '</tpl>',
				'<tpl if="!parent.isAddition">',
				'<div class=group_div>' + ASC.message('delta_removing')   + '<font class=specfont>{groupdesc}</font>' + ASC.message('delta_willRemove')+ '</div>',
				'</tpl>',
				'<ul >',
					'<tpl for="values.packageList">',
						'<li >{description}</li>',
					'</tpl>',
				'</ul>',
			'</tpl>'
		);
		this.listTpl.compile();
		
		var selectList = this.configResponse.delta.selectedList;
		
		var requiredMsg = '';
		if(selectList.length >1 || (selectList.length==1&&selectList[0].code != this.curCode))
		{
			requiredMsg = ', '+  ASC.message('delta_selectAdd')   + '<font class=p1>' + this.selectGroupNameStr +  '</font>';
		}
		this.requiresSelectTpl = new Ext.Template(
			'<h5>' + ASC.message('delta_select') + '<font class=p1>' + this.curOptionDesc + '</font>'+ requiredMsg +'.</h5>',
			'{list}'
		);
		this.requiresSelectTpl.compile();
		
		this.requiresUnselectTpl = new Ext.Template(
			'<h5>' + ASC.message('delta_select') + '<font class=p1>' + this.curOptionDesc + '</font>, ' +  ASC.message('delta_selectRemove')   + '<font class=p1>' + this.conflictingStr + this.unselectGroupNameStr +  '</font>.</h5>',			 
			'{list}'
		);
		this.requiresUnselectTpl.compile();
	},
	
	//private 
	buildOptionList: function (rootTpl, groupList,isAddition)
	{
		var list = this.listTpl.applyTemplate({groups: groupList,isAddition: isAddition});
		return rootTpl.applyTemplate({list: list});
	},
	
	toggleHelpMe: function ()
	{		
		var el=Ext.get(this.id + '_helpMe_text');
		if(!el){ return; }
		
		if( el.dom.style.display == 'none' ){
			ASC.Chrysler.trackLinkMetrics('selection_requires_change','help_me_understand_this');
		}
		
		el.enableDisplayMode(); el.toggle();
		
		var close = Ext.get(this.id + '_a-helpMe_close');
		if (close) {
			close.setVisible(el.isDisplayed());
		}
		
		var o = this.getSize();
		o.height = (el.isDisplayed() ? o.height + 85 : o.height - 85);
		this.setSize(o);
	}
});

})();
