<?php require "_header.php";

	$company_id	= Escape($_REQUEST["ID"]);
	if ($company_id !="" && isset($company_id))
	{ 
		$company_id	= $_REQUEST["ID"];
		$mode="edit";
		
		$limit=1;
		$start=0;
		$condition="";
		$order_field="";
		$order_type="";

		if ($company_id!="") {
           $condition.=" and company_id='$company_id' ";
    	}
				
		$objcompanyMgmt = new companyMgmt();
		$out = $objcompanyMgmt->getAllRecords($limit,$start,$condition,$order_field,$order_type);
		
		if (dbRows($out)){
			$row=dbArray($out);
			extract($row);
				
		}    	
	}	
	else
	{
	
		$mode="add";
		$company_id = 0;
		
	}
?>
<div id="main-content" align="left">
<script language="javascript" type="text/javascript">
 
var formMode = '<?php echo $mode;?>';
var company_id = '<?php  echo $company_id; ?>';
var labelArray;
labelArray = [
	['Employee Name:<span class="comp">*</span>'],
	['Contact Address'],
	['Contact No(R)'],
	['Contact No(M)'],
	['Email'],
	['Designation:<span class="comp">*</span>'],
	['Birth Date:<span class="comp">*</span>'],
	['Basic Salary:<span class="comp">*</span>'],
	['Joining Date:<span class="comp">*</span>']
];


Ext.onReady(function(){
	Ext.QuickTips.init();
	var picFiles = {
		columnWidth: .65,
		layout: 'form',
		labelAlign:'top',
		items: [
			{
				xtype: 'textfield',
				fieldLabel: 'Current',
				labelSeparator: '',
				name: 'currPic',
				id:'currPic',
				readOnly: true,
				disabled:true
			},
			{
				xtype: 'textfield',
				fieldLabel: 'New (JPG or JPEG only)',
				labelSeparator: '',
				name: 'newPic',
				id:'newPic',
				style:'width: 200px',
				inputType: 'file',
				allowBlank: false
			}
		]}
	
	var pictUploadForm = new Ext.FormPanel({
		   frame: true,
		   bodyStyle: 'padding:5px',		  
		   layout: 'column',
		   url:'companyphotoupload.php',
		   method: 'POST',
		   fileUpload: true,
		   items: [picFiles],	
		   buttons: [{
			  text: 'Upload Picture',
			  handler: function() {
				 var theForm = pictUploadForm.getForm();
				 if (!theForm.isValid()) {
					 Ext.MessageBox.show({
										   title: 'Change Picture',
										   msg: 'Please select a picture!',
										   buttons: Ext.MessageBox.OK,
										   icon:Ext.MessageBox.ERROR
										  });
									return;
				 }
				 if (!validateFileExtension(Ext.getDom('newPic').value)) {
					   Ext.MessageBox.show({
										   title: 'Change Picture',
										   msg: 'Only JPG or JPEG, please.!',
										   buttons: Ext.MessageBox.OK,
										   icon:Ext.MessageBox.ERROR
										  });
									return;
				 }
				 theForm.submit({		 
					params: { act:'setPicture', id:company_id},
					waitMsg: 'Uploading picture'
				 });
			   }
			},
			{
			  text: 'Reset',
			  handler: function () {
						pictUploadForm.getForm().reset();
					}
			}
			]
	});
	
	var win = new Ext.Window({
				title: 'Change Picture',
				height: 200,
				width: 420,
				layout: 'fit',
				modal:true,
				closeAction:'close',
				autoDestroy :true,
				items: pictUploadForm
			});

    pictUploadForm.on({
    actioncomplete: function(form, action) {
					if (action.type == 'load'){
						var varpic = action.result.data;			
						Ext.getDom('pic').src = varpic.file;
						Ext.getCmp('currPic').setValue(varpic.file);
					}
					if (action.type == 'submit') {
						var varpic = action.result.data;
						Ext.getDom('pic').src = varpic.file;
						companyentry.getForm().findField('company_photo').setValue(varpic.photoname);
						Ext.getCmp('currPic').setValue(varpic.file);
						Ext.getDom('newPic').value ='';
						win.hide();
					}
			}
	});
	
	/*pictUploadForm.getForm().load({ 
						  	 params:{ act:'getPicture',id:company_id,photoname:'<?php print $company_logo; ?>'}, 
						  	 waitMsg: 'Loading'
						  });	*/   		
		
		
	var companyentry = new Ext.FormPanel({
        labelWidth: 100,  // label settings here cascade unless overridden
	    url:'save-company.php',
		method:'post',
        frame:true,
        bodyStyle:'padding:5px 5px 0', 
		items:[
			{
				layout:'column',
				items:[
					{
						columnWidth: .6,
						defaultType: 'textfield', 
						layout: 'form',					
						items:[{
										fieldLabel: 'Company Name :<span class="comp">*</span>',
										name: 'company_name',
										labelSeparator:'',	
										id: 'company_name',
										allowBlank:false,
										maxLength:50,
										width: 205,
										tabIndex:1,
										autoCreate: {tag: 'input', type: 'text',  maxlength: '100'}
               						 },{ xtype: 'textarea',
									   growAppend: "",
									   name: 'company_address',
									   id:'company_address',
									   allowBlank:false,
									   fieldLabel: 'Address :<span class="comp">*</span>',
									   labelSeparator:'',
									   maxLength: 200,
									   tabIndex:2,
									   maxLengthText: 'max 250 characters',
									   width: 205
										},{ 
									    xtype:'numberfield',
										labelSeparator:'',
										fieldLabel: 'Phone No :<span class="comp">*</span>',
										id:'comapny_phoneno',
										minLength:10,
										maxLength:13,
									    //emptyText:'07940044005',
										allowBlank:false,
										name: 'comapny_phoneno',
										width: 205,
										tabIndex:3,
										autoCreate: {tag: 'input', type: 'text',  maxlength: '13'}
									},{
									    xtype:'textfield',
										fieldLabel: 'Fax No',
										id:'company_fax',
										minLength:10,
										maxLength:13,
									  //  emptyText:'07940044005',
										name: 'company_fax',
										width: 205,
										tabIndex:4,
										autoCreate: {tag: 'input', type: 'text',  maxlength: '13'}
										,enableKeyEvents: true
										,listeners:{
											keyup: validphoneno
										}
									},{
										fieldLabel: 'Email',
										name: 'company_emailid',
										id: 'company_emailid',
										//emptyText:'heman@auminfosys.com',
										maxLength:50,
										width: 205,
										tabIndex:5,
										vtype:'email',
										autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
									},{
										fieldLabel: 'URL',
										name: 'company_url',
										id: 'company_url',
										//emptyText:'http://www.auminfosys.com',
										width: 205,
										tabIndex:6,
										maxLength:50,
										vtype: 'url',
										autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
                  					},{
										fieldLabel: 'Reg No :<span class="comp">*</span>',
										name: 'register_no',
										labelSeparator:'',
										id: 'register_no',
										allowBlank:false,
										maxLength:50,
										width: 205,
										tabIndex:7,
										autoCreate: {tag: 'input', type: 'text',  maxlength: '100'}
               						 },{
										fieldLabel: 'Slogan',
										name: 'company_slogan',
										id: 'company_slogan',
										maxLength:50,
										width: 205,
										tabIndex:8,
										autoCreate: {tag: 'input', type: 'text',  maxlength: '100'}
               						 },
									 {  xtype: 'textarea',
										   growAppend: "",
										   name: 'company_disciption',
										   id:'company_disciption',
										   fieldLabel: 'Discription',
										   maxLength: 200,
										   tabIndex:9,
										   maxLengthText: 'max 250 characters',
										   width: 205
										}]				
					},						   
					{
						columnWidth: .4,
						layout: 'form',
						height:185,
						style: 'padding:0px 0px 0px 90px',
						items:[{ xtype: 'box',
								  autoEl: { tag: 'div',
											html: '<img id="pic" src="photo.gif" alt="photograph" class="img-contact" width="<?php echo $SiteImageWidth ?>" height="<?php echo $SiteImageHeight ?>"/>'
										  }
								},
								{
									style: 'padding:0px 0px 0px 0px',
									xtype: 'button',
									text: 'Upload',
									width:100,
									tabIndex:10,	
									handler:function() 
									{
											win.show();
											Ext.getDom('newPic').size = 30;
									}
								}
								
								]
						},
					 {
						columnWidth: .4,
						layout: 'form',					
						labelWidth:90,
						items:[
							 {
							xtype: 'textfield',
							name: 'company_tinno',
							id:'company_tinno',
							fieldLabel :'TIN No',
							width:100,
							allowBlank:true,
							tabIndex:11,
							minLength:5,
							maxLength:50,
							autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
							
    				 },{
							xtype:'textfield',
							name:'company_cstno',
							id:'company_cstno',
							width:100,										
							tabIndex:12,
							fieldLabel :'CST No',
							allowBlank:true,
							minLength:5,
							maxLength:50,
							autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
					},{
							xtype: 'textfield',
							name: 'company_panno',
							id:'company_panno',
							fieldLabel :'PAN No',
							width:100,
							tabIndex:13,
							allowBlank:true,
							minLength:5,
							maxLength:50,
							autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
					 },{
							xtype: 'textfield',
							name: 'company_servicetaxno',
							id:'company_servicetaxno',
							fieldLabel :'Service Tax No',
							width:100,
							allowBlank:true,
							tabIndex:14,
							minLength:5,
							maxLength:50,
							autoCreate: {tag: 'input', type: 'text',  maxlength: '50'}
														
					 },
							{xtype: 'textfield',
							   name: 'company_id',
							   id:'company_id',
							   hidden:true,
							   value:company_id
							   },
							{
								xtype: 'textfield',
								name: 'company_photo',
								id:'company_photo',
								hidden: true
							}
						]        				
					}
									 
				]
			}
		] 
    });
	
	
	
	var SchoolAddEditWindow = new Ext.Window({
        title: 'Company Information',
		width:560,	
		height:420,
		border: false,
		closeAction:'close',
		items: companyentry,
		buttonAlign: 'center',		
	    buttons: [{
            text: 'Save',tabIndex:15,	
			iconCls: 'icon-save',		
			handler: function () {
				companyentry.getForm().submit({
					waitMsg: 'Saving...',		
					success: function () {
							  		
					     	  Ext.MessageBox.show({
									   title: 'Message',
									   msg: 'Data has been saved',
									   buttons: Ext.MessageBox.OK,
									   icon:Ext.MessageBox.INFO
				                      });	
													 							    							 
					},
					failure: function () {		// when saving data failed
					    Ext.MessageBox.show({
									   title: 'Error',
									   msg: 'Please fill complete form.',
									   buttons: Ext.MessageBox.OK,
									   icon:Ext.MessageBox.ERROR
				                      });
					}
				});
			}
        },{
            text: 'Reset',tabIndex:16,
			iconCls: 'icon-reset',	
			handler: function () {
			     if(formMode!='edit')
						{
		    				companyentry.getForm().reset();
							companyentry.getForm().findField('company_name').focus(true,500);
							Ext.getDom('pic').src = '<?php print  $SiteImagePath ."default.gif"; ?>';
						}
				
			}
        },{
            text: 'Close',tabIndex:17,
			iconCls: 'icon-close',		
			handler: function () {
			
				SchoolAddEditWindow.close();
					
			}
        }]

	});	
	
	var varWidth =  (screen.width - SchoolAddEditWindow.width)/2;
	SchoolAddEditWindow.setPosition(varWidth,((screen.height/4)-110));
	SchoolAddEditWindow.show();
	
	companyentry.getForm().findField('company_name').focus(true,500);
	
	companyentry.getForm().findField('company_name').on('blur',function (){
		
		var varCname=companyentry.getForm().findField('company_name').getValue();
		
		var company_id=companyentry.getForm().findField('company_id').getValue();	
		
		var resbool="";		
				
		if(varCname!="")
		{			
					Ext.Ajax.request({
							  loadMask: true,
							  sync : true,
							  url: 'data/checkcompanyname.php',
							  params: {Comapanyname: varCname,company_id:company_id},
							  success: function(resp) {
							  resbool=Ext.decode(resp.responseText).success;	
										if(resbool=='false')
										{
										    Ext.MessageBox.show({
											   title: 'Error',
											   msg: 'Company Name already exists!',
											   buttons: Ext.MessageBox.OK,
											   icon:Ext.MessageBox.ERROR
									  		});
										
											companyentry.getForm().findField('company_name').reset();											
											Ext.getCmp('company_name').focus('', 10);
											}
									 			
							  }				
							});		
		}
	       
	});	 

	companyentry.getForm().findField('register_no').on('blur',function (){
		
		var varCRegno=companyentry.getForm().findField('register_no').getValue();
		
		var company_id=companyentry.getForm().findField('company_id').getValue();	
		
		var resbool="";		
				
		if(varCRegno!="")
		{			
					Ext.Ajax.request({
							  loadMask: true,
							  sync : true,
							  url: 'data/checkcompanyRegno.php',
							  params: {RegNo: varCRegno,company_id:company_id},
							  success: function(resp) {
							  resbool=Ext.decode(resp.responseText).success;	
										if(resbool=='false')
										{
										    Ext.MessageBox.show({
											   title: 'Error',
											   msg: 'Reg No already exists!',
											   buttons: Ext.MessageBox.OK,
											   icon:Ext.MessageBox.ERROR
									  		});
										
											companyentry.getForm().findField('register_no').reset();											
											Ext.getCmp('register_no').focus('', 10);
											}
									 			
							  }				
							});		
		}
	       
	});	 

  
	if(formMode=='edit'){
	
	 	//$newCompanyImagePath=$SiteImagePath.$company_id."/";
		Ext.getDom('pic').src = '<?php print $SiteImagePath.$company_id."/".$company_logo ; ?>';
		
		companyentry.getForm().findField('company_name').setValue('<?php print $company_name; ?>');
		 <?php 
		     $company_address 		= replaceSpecial($company_address);
			 $company_description  = replaceSpecial($company_description );
		?> 
		  
		companyentry.getForm().findField('company_address').setValue('<?php print $company_address; ?>');
		companyentry.getForm().findField('comapny_phoneno').setValue('<?php print $company_phone ; ?>');
		companyentry.getForm().findField('company_fax').setValue('<?php print $company_fax; ?>');
		companyentry.getForm().findField('company_emailid').setValue('<?php print $company_email; ?>');
		companyentry.getForm().findField('company_url').setValue('<?php print $company_website; ?>');
		companyentry.getForm().findField('register_no').setValue('<?php print $company_regno; ?>');
		companyentry.getForm().findField('company_slogan').setValue('<?php print $company_slogan; ?>');
		companyentry.getForm().findField('company_disciption').setValue('<?php print $company_description ; ?>');
		
		
		companyentry.getForm().findField('company_tinno').setValue('<?php print $company_tinno; ?>');
		companyentry.getForm().findField('company_cstno').setValue('<?php print $company_cstno; ?>');
		companyentry.getForm().findField('company_panno').setValue('<?php print $company_panno; ?>');
		companyentry.getForm().findField('company_servicetaxno').setValue('<?php print $company_servicetaxno; ?>');
		
				
	
	}
});
</script>
</div>
<?php require "_footer.php";?>