                                                                                                                                                                                                                               
var messagestore = new Ext.data.ArrayStore({		
	fields: ['message_title','message_content'],
	data:Ext.contact.message
});

var groupstore = new Ext.data.ArrayStore({		
	fields: ['group_name'],
	data:Ext.contact.group_name
});

var group = new Ext.form.ComboBox({
	name: 'group_name',
	id:'group_name',
	labelSeparator:'',	
	fieldLabel:'Group:',
	displayField:'group_name',
	valueField: 'group_name',
	editable:true,
	triggerAction:'all',
	mode:'local',
	allowBlank:false,
	store: groupstore
});

var occasionstore = new Ext.data.ArrayStore({		
	fields: ['occasion_name'],
	data:[["Birth Date"],["Anniversary"]]
});

var occasion = new Ext.form.ComboBox({
	name: 'occasion_name',
	id:'occasion_name',
	labelSeparator:'',	
	fieldLabel:'Occasion:',
	displayField:'occasion_name',
	valueField: 'occasion_name',
	editable:true,
	triggerAction:'all',
	mode:'local',
	width:100,
	allowBlank:false,
	store: occasionstore,
	listeners: {'select': function(cmb, rec, idx){
		if(this.getValue()!=""){
			Ext.getCmp('occasiondate').enable();
			Ext.getCmp('occasiondate').setValue(new Date());	
		}
  	}}
});

var occasiondate = new Ext.form.DateField({
	id: 'occasiondate',
	name: 'occasiondate',
	labelSeparator:'',
	fieldLabel:"On",
	enableKeyEvents: true,
	allowBlank : false,
	disabled: true,
	maxValue:new Date()
});	

var contactstore = new Ext.data.JsonStore({
	url:'data/contact-data.php',
    root:'data',
	storeId:'contact_id',
	totalProperty:'count',
	fields:[
		{name:'contact_id',type:'int'},
		{name:'group_name',type:'string'},
		{name:'name',type:'string'},
		{name:'mobile_no1',type:'string'},
		{name:'mobile_no2',type:'string'}
	]
});

contactstore.on({'load':{fn: function(){
	if(contactstore.getCount() <= 0){
		Ext.MessageBox.show({
			title: 'Error',
			msg: 'There is no Data found for this Selection.',
			buttons: Ext.MessageBox.OK,
			icon:Ext.MessageBox.ERROR
		});
	} else{
		sm2.selectAll();
	}
}}});

var sm2 = new Ext.grid.CheckboxSelectionModel();

var colModel = new Ext.grid.ColumnModel([
	new Ext.grid.RowNumberer(),
	{
		dataIndex: 'contact_id',
		header: 'Id', 
		id: 'contact_id',
		hidden: true
	},{
		dataIndex: 'group_name',
		header: 'Group',
		sortable:true,
		width:100
	},{
		dataIndex: 'name',
		header: 'Name',
		sortable:true,
		width:155
	},{
		dataIndex: 'mobile_no1',
		header: 'Mobile (1)',
		sortable:true,
		width:80
	},{
		dataIndex: 'mobile_no2',
		header: 'Mobile (2)',
		sortable:true,
		width:80
	},sm2
]);

var grid_group = new Ext.grid.GridPanel({
	store: contactstore,
	cm: colModel,			
	loadMask: true,
	enableColLock:false,
	height: 275,    
	width: 480,
	sm: sm2,
	clicksToEdit: 1, 
	layout: 'fit',
	columnLines:true
});

Ext.override(Ext.Editor,{
	onBlur: function(){
    	if(this.allowBlur === true && this.editing) {
        	this.completeEdit();
        }
    }
});

var ContactForm = new Ext.FormPanel({
	labelWidth: 60,
	id:'ContactForm',
	frame:true,
	standardSubmit:false,
	title: '',
	bodyStyle:'padding:5px',
	layout:'column',
	items :[{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:1,
		items:[group]
	},{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:.36,
		items:[occasion]
	},{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:.36,
		labelWidth: 20,
		items:[occasiondate]
	},{
		xtype:'container',
		autoEl:{},
		layout:'form',
		columnWidth:.28,
		labelWidth: 20,
		items:[{
			xtype:'button',
			width:50,
			text:'View',
			handler: function(b,e){	
				var group_name = Ext.getCmp('group_name').getValue(),
					occasion_name = Ext.getCmp('occasion_name').getValue(),
					occasion_date = Ext.getCmp('occasiondate').getValue();
				contactstore.removeAll();	
				contactstore.load({
					params:{
						action:'contact-grid',
						group_name:group_name,
						occasion_name:occasion_name,
						occasion_date:occasion_date
					}
				});
			}
		}]
	},{
		xtype:'container',
		autoEl:{},
		style: 'padding:5px 0px 0px 0px',
		layout:'form',
		columnWidth:1,
		items:[grid_group]
	}]
});

var GroupContactWindow = new Ext.Window({
	title:'Import Group Contact',
	height:420,
	width:520,
	modal:true,
	layout:'fit',
	border:false,
	closeAction:'hide',
	collapsible:true,
	items:ContactForm,
	buttonAlign:'center',
	buttons: [{
		text:'Import Numbers',
		handler: function(b, e){	
			if(grid_group.store.getCount() > 0){
				var selections = grid_group.selModel.getSelections();
				var message = "";
				var i,j;				
				var mobile_no1;
				var mobile_no2;
				for(i = 0; i< grid_group.selModel.getCount(); i++){
					mobile_no1 = selections[i].json.mobile_no1;
					mobile_no2 = selections[i].json.mobile_no2;
					if(mobile_no1 != ""){
						message = message + mobile_no1 + ",";
					}
					if(mobile_no2 != ""){
						message = message + mobile_no2 + ",";
					}
				}
				message = message.substr(0,message.length - 1);
				var mobile_number = Ext.getCmp('mobile_number').getValue();
				if(mobile_number != ""){
					Ext.getCmp('mobile_number').setValue(mobile_number+","+message);
				} else{
					Ext.getCmp('mobile_number').setValue(message);
				}
				
				/* START SET MESSAGE */
				for(j = 0; j < messagestore.getCount(); j++){
					record = messagestore.getAt(j);
					if(record.data.message_title == Ext.getCmp('occasion_name').getValue()){
						Ext.getCmp('msg_body').setValue(record.data.message_content);
						handlekeypress();
					}	
				}
				/* END SET MESSAGE */
				
				GroupContactWindow.hide();		
			} else{
				Ext.MessageBox.show({
					title:'Message',
					msg: 'Please select atlist one person to send message.',
					buttons: Ext.MessageBox.OK,
					icon:Ext.MessageBox.INFO
				});
			}
		}
	},{
		text: 'Reset', 
		handler: function () {   
			ContactForm.getForm().reset();
			studentstore.removeAll();
			Ext.getCmp('occasiondate').disable();
			Ext.getCmp('branch_name').focus(true,500);
		}
	}]
});
var varWidth =  (screen.width - GroupContactWindow.width)/2;
GroupContactWindow.setPosition(varWidth,((screen.height/4)-85));	