	var labelArray;
		
	labelArray = [
		['Inquier Organization:<span class="comp">*</span>'],
		['Inquier Org. Address:<span class="comp">*</span>'],
		['District Name:<span class="comp">*</span>'],
		['Phone No:<span class="comp">*</span>'],
		['Mobile No:<span class="comp">*</span>'],
		['Contact Person:<span class="comp">*</span>'],
		['Appointment Date:<span class="comp">*</span>'],
		['Appointment Time:<span class="comp">*</span>']
	];	
		
	var districtsore = new Ext.data.Store({
		url: 'data/getdistrictcode.php',          
		reader: new Ext.data.JsonReader({			
			fields: ['dist_id','dist_nameeng'],
			root: 'rows'
		}),autoLoad:true
	});
   
	var encode = false;
	var local = false;

    var fm = Ext.form;
    var checkColumn = new Ext.grid.CheckColumn({
    	header: 'Select',
	  	dataIndex:'selected',
	  	id: 'selected',
	  	name: 'selected',
	  	width: 55
    });
    var userstore = new Ext.data.Store({
		url: 'data/getusername.php',          
		reader: new Ext.data.JsonReader({			
			fields: ['user_id','user_name'],
			root: 'rows'
		}),autoLoad:true
	}); 

	var cust_org_store = new Ext.data.Store({
			url: 'data/getorgname.php',          
	        reader: new Ext.data.JsonReader({			
                fields: ['customer_orgname'],
                root: 'rows'
            }),autoLoad:true
        });	
	store = new Ext.data.JsonStore({
       	autoDestroy: true,
        url:'data/getproduct-data.php',
        remoteSort: true,
        sortInfo: {
            field: 'product_id',
            direction: 'ASC'
        },        
        idProperty: 'product_id',
        root: 'data',
        totalProperty: 'total',
        fields: [
			{ name: 'product_id', type: 'int'},
			{ name: 'product_name'},
			{ name: 'product_price'},
			{ name:'selected', type: 'bool'}
		]
    });
    
	Ext.ux.grid.PagingRowNumberer = Ext.extend(Ext.grid.RowNumberer, {
    renderer: function(v, p, record, rowIndex, colIndex, store) {
        if (this.rowspan) {
            p.cellAttr = 'rowspan="' + this.rowspan + '"';
        }
        var so = store.lastOptions;
        var sop = so ? so.params: null;
        return ((sop && sop.start) ? sop.start: 0) + rowIndex + 1;
    }
	});
	
	var createColModel = function (finish, start) {
		var columns = [
			new Ext.ux.grid.PagingRowNumberer(),
			{
				dataIndex: 'product_id',
				header: 'Product ID',
				hidden:true
			},{
				dataIndex: 'product_name',
				header: 'Product Name',
				width: 400
			},{
				dataIndex: 'product_price',
				header: 'Price',
				width:120
			},checkColumn
		];
		return new Ext.grid.ColumnModel({
			columns: columns.slice(start || 0, finish),
			defaults: {
				sortable: true
			}
		});
	};
		
	var grid = new Ext.grid.GridPanel({
		store: store,
		colModel: createColModel(40), 
		loadMask: true,
		frame: false,
		enableColLock:false,
		height:150,    
		width: 620,
		layout: 'fit',
		columnLines:true,
		selModel: new Ext.grid.RowSelectionModel({singleSelect:false}),
		plugins: checkColumn
	});
	
	var nameAndCompany = { columnWidth: 1,
		layout: 'form',
		labelWidth: 130,
		bodyStyle:'padding-top:1px',
		items: [
			{
							   xtype: 'combo',
							   name: 'customer_orgname',
							   id:'customer_orgname',
							 // hidden:'hidden_customer_orgname',
							   tabIndex:1,
							   labelSeparator:'',	
							   fieldLabel :'Orgenization Name' ,
							   displayField:'customer_orgname',
							   valueField: 'customer_orgname',
							   width: 200,
							   blankText: '',
							   emptyText:'',
							   allowBlank:true,
							   triggerAction:'all',
							   mode:'local',			
							   selectOnFocus: false,	
							   store: cust_org_store,
							   forceSelection:false,
							   editable:true
					},
			
			{
				xtype: 'textfield',
				name: 'inquierorg_name',
				id:'inquierorg_name',
				labelSeparator:'',	
				width:200,
				tabIndex:1,
				fieldLabel :labelArray[0] ,
				allowBlank:true,
				readOnly:false
		   	},{ 
				xtype: 'textarea',
				growAppend: "",
				name: 'inquier_add',
				id:'inquier_add',
				labelSeparator:'',	
				tabIndex:2,
				allowBlank:false,
				fieldLabel :labelArray[1] ,
				maxLength: 250,
				maxLengthText: 'max 250 characters',
				width: 200,
				height:50
		   	},{
				xtype: 'combo',
			   	name: 'dist_id',
			   	id:'dist_id',
				hiddenName: 'dist_code1',
				tabIndex:3,
				labelSeparator:'',	
				fieldLabel :labelArray[2] ,
				displayField:'dist_nameeng',
				tabIndex:3,
				valueField: 'dist_id',
				width: 200,
				blankText: '',
				emptyText:'',
				allowBlank:false,
				triggerAction:'all',
				mode:'local',			
				selectOnFocus: true,	
				store: districtsore,
				forceSelection:true,
				editable:true
		  	},{
			   
				xtype: 'textfield',
				name: 'personname',
				id:'personname',
				tabIndex:4,
				width:200,
				labelSeparator:'',	
				fieldLabel :labelArray[5] ,
				allowBlank:false
			},
			{
					xtype: 'datefield',
					name: 'inquiry_date',
					tabIndex:10,
					labelSeparator:'',	
					fieldLabel :'Inquiry Date' 
					,width:130
					,allowBlank:false
					,id: 'inquiry_date'
					,vtype: 'daterange'
					 
				  }
		]
	}
	
	var internet = { columnWidth: .5,
		layout: 'form',
		labelWidth:70,
		items: [{ xtype: 'fieldset',
			title: 'Internet',
			height:112,
			defaultType: 'textfield',
			items: [{
				fieldLabel: 'Email',
				name: 'email',
				tabIndex:8,
				id: 'email',
				vtype: 'email',
				anchor: '95%'
			},{
				fieldLabel: 'Web Site',
				name: 'webPage',
				id: 'webPage',
				tabIndex:9,
				vtype: 'url',
				anchor: '95%'
			},{
				name: 'product_ids',
				id:'product_ids',
				hidden:true
			},{
				name: 'inquiry_id',
				id:'inquiry_id',
				value:inquiry_id,
				hidden:true
			}]
		}]
	}


	var phones = { columnWidth: .5,
		layout: 'form',
		items: [{ xtype: 'fieldset',
			title: 'Contact Numbers',
			autoHeight: false,
			defaultType: 'textfield',
			items: [{
						xtype: 'textfield',
						name: 'inquier_phoneno',
						id:'inquier_phoneno',
						tabIndex:5,
						labelSeparator:'',	
						width:150,
						fieldLabel :labelArray[3] ,
						//allowBlank:false,
						allowNegative: false,
						allowDecimals: false
						,readOnly:false
						,autoCreate: {tag: 'input', type: 'text',  maxlength: '13'}
						,enableKeyEvents: true
						,listeners:{
							keyup: validphoneno
						}
					},{
						xtype: 'numberfield',
						name: 'inquier_mobileno',
						id:'inquier_mobileno',
						tabIndex:6,
						labelSeparator:'',
						width:150,	
						fieldLabel :labelArray[4] ,
						//allowBlank:false,
						allowNegative: false,
						allowDecimals: false
						,readOnly:false
						,autoCreate: {tag: 'input', type: 'text',  maxlength: '13'}
					 },{
						xtype: 'numberfield',
						name: 'inquier_faxno',
						id:'inquier_faxno',
						tabIndex:7,
						width:150,	
						fieldLabel :'Fax No',
						allowNegative: false,
						allowDecimals: false
						,readOnly:false
						,autoCreate: {tag: 'input', type: 'text',  maxlength: '13'}
				 }]
		}]
		
	}

	var busAddress = { columnWidth: .45,
		layout: 'form',
		labelWidth: 115,
		items: [{
					xtype: 'datefield',
					name: 'appintment_date',
					tabIndex:10,
					labelSeparator:'',	
					fieldLabel :labelArray[6] 
					,width:130
					,allowBlank:false
					,id: 'appintment_date'
					,vtype: 'daterange'
					 
				  },{
				  
					xtype: 'timefield',
					name: 'appintment_time',
					tabIndex:11,
					labelSeparator:'',	
					fieldLabel :labelArray[7] 
					,width:130
					,allowBlank:false
					,id: 'appintment_time'
					 
				  }]
	}

	var homeAddress = {columnWidth: .5,
		layout: 'form',
		labelWidth: 60,
		items: [{
								  layout: 'form',					
								  items:[{	xtype: 'textarea',
											width:240,
											height:45,
											tabIndex:12,
											name: 'comment',
											fieldLabel:'Comment',
											id:'comment'
										 },
										 {
				columnWidth: 1,  
				xtype: 'combo', 
				name: 'user_id',
				id:'user_id',
				autoLoad:true, 
				hiddenName: 'user_idhedden',
				forceSelection:true,
				tabIndex:9,
				labelSeparator:'',	
				fieldLabel:'User Name' , 
				displayField:'user_name',
				valueField: 'user_id',				
				width: 170, 
				//value:user_name,
				allowBlank:false,
				triggerAction:'all',
				mode:'local',			
				selectOnFocus: true,	
				store:userstore, 
				editable:false						 
			},
										 ]				
							  }]
	}


    var inquiryForm = new Ext.FormPanel({
        frame: true,
		url:'save-inquiry.php',
        bodyStyle: 'padding:5px',
        width: 650,				
        items: [{
            bodyStyle: {
                margin: '0px 0px 15px 0px'
            },
            items: [{
                layout: 'column',
                items: [nameAndCompany,{
				  	xtype: 'box',
					style:'margin:0px 20px 0px 0px',
					height		: 140,
					width		: 300,										       
					autoEl: { tag: 'div',
						html: '<img id="pic" src="images/smsimage.jpg" alt="photograph"width=300 height=128/>'
					}}]
			}]
            }, {
                items: [{
                    layout: 'column',
                    items: [phones,internet]
				}]
                },{
                    xtype: 'fieldset',
                    title: 'Appointment',
                    autoHeight: true,
                    hideBorders: true,
                    layout: 'column',
                    items: [busAddress,homeAddress]
		
				},{
                    xtype: 'fieldset',
                    title: 'Product List',
                    autoHeight: true,
                    hideBorders: true,
                    layout: 'column',
                    items: [grid]
		}]
    });	

	var inquiryWindow = new Ext.Window({
		title: 'Product Inquiry',
		width:700,		
		height:650,
		modal:true,
		shadow: false,
		border: false,
		layout: 'fit',
		closeAction:'hide',
		collapsible:true,
		items: inquiryForm,
		buttonAlign: 'center',
	    buttons: [{
      		text: 'Save',	
			tabIndex:13,		
			iconCls: 'icon-save',
			handler: function () {
				//alert(inquiryForm.getForm().findField('appintment_date').getValue());
				phoneno= inquiryForm.getForm().findField('inquier_phoneno').getValue();
			  	mobileno= inquiryForm.getForm().findField('inquier_mobileno').getValue();
			 
				var selectedproduct_ids = "";
				var produdct_id= "";
				for(var i = 0,len = grid.store.getCount(); i < len; i++)
				{
					var record = grid.store.getAt(i);
					if(record.data.selected == true)
					{
						produdct_id =record.data.product_id;
						selectedproduct_ids = selectedproduct_ids + produdct_id + ",";
					}
				}	
				if(produdct_id!="")
				{
					selectedproduct_ids= selectedproduct_ids.substr(0,selectedproduct_ids.length - 1);
					inquiryForm.getForm().findField('product_ids').setValue(selectedproduct_ids);
					//alert(selectedproduct_ids);
					inquiryForm.getForm().submit({
						waitMsg: 'Saving...',		
						success: function () {
							Ext.MessageBox.show({
								title: 'Message',
								msg: 'Data has been saved..',
								buttons: Ext.MessageBox.OK,
								icon:Ext.MessageBox.INFO
							});
							inquiryForm.getForm().reset();
							store.load();
							if(formMode=='edit'){
								location.href="view-product-inquiry.php";
							}
						},
						failure: function () {	
							Ext.MessageBox.show({
								title: 'Error',
								msg: 'Please fill complete form!',
								buttons: Ext.MessageBox.OK,
								icon:Ext.MessageBox.ERROR
							});
						}
					});
				}	
				else
				{
					Ext.MessageBox.show({
						title: 'Error',
					   	msg: 'Please Select Product.',
					   	buttons: Ext.MessageBox.OK,
					   	icon:Ext.MessageBox.ERROR
					});
				}
			   	if(phoneno=="" && mobileno=="")
			   	{
			  		Ext.MessageBox.show({
						title: 'Error',
						msg: 'Please insert one of the contact number',
						buttons: Ext.MessageBox.OK,
						icon:Ext.MessageBox.ERROR
				 	});
			  	}
			}
    	},
		           {
            text: 'Reset',
			tabIndex:14,
			iconCls: 'icon-reset',
			handler: function () {
			if(formMode!='edit'){
				inquiryForm.getForm().reset();
			store.load();
			inquiryForm.getForm().findField('inquierorg_name').focus(true,500);
			}
			
						
			 		
			}
        },
	 	           {
            text: 'Close',
			tabIndex:15,
			iconCls: 'icon-close',
			name:'btnQuotationClose',
			id:'btnQuotationClose',
			handler: function () {
			if(formMode=='edit'){
				location.href="view-product-inquiry.php";
			}else
			{
				inquiryWindow.close();
			 }	
			}
        }
	 ]

	});
	
	var varWidth =  (screen.width - inquiryWindow.width)/2;
	inquiryWindow.setPosition(varWidth,((screen.height/4)-110));
	inquiryForm.getForm().findField('inquierorg_name').focus(true,1000);
	
	if(formMode=='add'){
		store.load();
	}
	
	function ShowWindow(){
		inquiryWindow.show();
	}						
	
	function formatDate(value){
        return value ? value.dateFormat('M d, Y') : '';
    }	