<?php require "_header.php";

$schoolid = $_SESSION["cCompany_id"];
$dist_id	= Escape($_REQUEST["ID"]);
if($dist_id !="" && isset($dist_id)){
		$mode="edit";
		if ($EDIT == "false")
		{
			 redirectToPage();
		}
		$limit=1;
		$start=0;
		$condition="";
		$order_field="dist_id";
		$order_type="ASC";

		if ($dist_id!="") {
           $condition.=" and dist_id='$dist_id' ";
    	}
				
		$objDistMgmt = new DistMgmt();
		
		$out = $objDistMgmt->getAllRecords($limit,$start,$condition,$order_field,$order_type);
		if (dbRows($out)){
			$row=dbArray($out);
			extract($row);	
		}    	
	}	
	else{
		$mode="add";
		if ($ADD == "false")
		{
			 redirectToPage();
		}
		$dist_id=0;
		
	}
?>
<script type="text/javascript">
var labelArray;
labelArray = [
			 	['District Code :<span class="comp">*</span>'],
				['District Name :<span class="comp">*</span>']	
			];
var formMode = '<?php echo $mode;?>';

var dist_id = '<?php  echo $dist_id; ?>';
var schoolid = "<?php echo $schoolid ;?>";
var add = '<?php echo $_REQUEST["add"]; ?>';
		Ext.onReady(function(){						
		
		Ext.QuickTips.init();
		
       var simpleForm = new Ext.FormPanel ({
    	layout: 'form',
		
		labelWidth: 150,
       	url:'save-dist.php?ID=<?php  echo $dist_id; ?>',
        method:'post',
		frame:true,
        bodyStyle:'padding:5px 5px 0',
        
        defaultType: 'textfield',
        items: [{
                  	labelSeparator:'',	
					fieldLabel :labelArray[0] ,
				  	name: 'companycode',
				  	allowBlank: false
				},
				{
                  	labelSeparator:'',	
					fieldLabel :labelArray[1] ,
				  	name: 'distnameeng',
				  	allowBlank: false
				
				}]
				       						
		});
		

		
	simpleForm.on({
		actioncomplete: function(form, action){
			if(action.type == 'submit'){	
				if(action.result.success=="false"){
					
					Ext.MessageBox.show({
						title: 'Error',
					   	msg: 'Data not save.',
					   	buttons: Ext.MessageBox.OK,
					   	icon:Ext.MessageBox.ERROR
					});
					//location.href="view-district.php";							
				}
				else if(action.result.success=="true"){
					
					Ext.MessageBox.show({
						title: 'Message',
						msg: 'Data has been save.',
					   	buttons: Ext.MessageBox.OK,
					   	icon:Ext.MessageBox.INFO
					});
					if(formMode=='edit'){
						location.href="view-district.php";				
					}
					else{
						simpleForm.getForm().reset();
					}
				}
			}
		}
	});

		simpleForm.getForm().findField('distnameeng').on('blur',function (){
		
		var vardistnameeng=simpleForm.getForm().findField('distnameeng').getValue();
		var resbool="";		
				
		if(vardistnameeng!="")
		{			
					Ext.Ajax.request({
							  loadMask: true,
							  sync : true,
							  url: 'data/checkdistname.php',
							  params: {distnameeng: vardistnameeng,dist_id : dist_id},
							  success: function(resp) {
							  resbool=Ext.decode(resp.responseText).success;			                     
							  if(resbool=='false')
										{
											
											Ext.MessageBox.show({
													   title: 'Error',
													   msg: 'District Name already exist.',
													   buttons: Ext.MessageBox.OK,
													   icon:Ext.MessageBox.ERROR
				                      });
											simpleForm.getForm().findField('distnameeng').reset();
										}	 			
							  }				
							});		
		}
		else
		{
		}			
		
		}); 
		
  
    
  var DistrictAddEditWindow = new Ext.Window({
        title: 'District Information',
		width:330,		
		height:150,		
		modal:true,
		border: false,
		layout: 'fit',
		closeAction:'close',
		collapsible:true,
		items: simpleForm,
		buttonAlign: 'center',
	    buttons: [ {
            text: 'Save',name:'btnSave',id:'btnSave',				
			handler: function () {
			simpleForm.getForm().submit({
					waitMsg: 'Saving...',		
					success: function () {
						Ext.MessageBox.show({
									   title: 'Message',
									   msg: 'Data has been save.',
									   buttons: Ext.MessageBox.OK,
									   icon:Ext.MessageBox.INFO
				                      });
						simpleForm.getForm().reset();
					},
					failure: function () {	
						Ext.MessageBox.show({
									   title: 'Error',
									   msg: 'Please fill complete form.',
									   buttons: Ext.MessageBox.OK,
									   icon:Ext.MessageBox.ERROR
				                      });
					}
				});
			}

          },
		          {text: 'Reset', name:'btnReset',id:'btnReset',
			handler: function (){
			 simpleForm.getForm().reset();
							 
				}
        	},
			 	  {
				  text: 'Close',name:'btnCancel',id:'btnCancel',			
					handler: function (){
					DistrictAddEditWindow.close();
					  
		    	   }
			}
			    ]

	});
	var varWidth =  (screen.width - DistrictAddEditWindow.width)/2;
	DistrictAddEditWindow.setPosition(varWidth,((screen.height/4)-100));
	DistrictAddEditWindow.show();
	simpleForm.getForm().findField('companycode').focus(true,500);
	  if(formMode=='edit'){
  		
	 	simpleForm.getForm().findField('companycode')	.setValue('<?php echo $company_code; ?>');
	 	simpleForm.getForm().findField('distnameeng')	.setRawValue('<?php echo $dist_nameeng; ?>');
	 		
  }

});

</script> 
<div id="main-content" align="center"></div>
<?php require "_footer.php";?>

