<?php require "_header.php";
//error_reporting(E_ALL); ?>
<link rel="stylesheet" type="text/css" href="icons.css" />
<script language="javascript" type="text/javascript" src="js/acmastercommon.js"></script>
<?php
$year = $_SESSION["cYear"];

$qry = "SELECT year_name FROM lms_yearmaster WHERE year_id = $year ";
$out = dbQuery($qry);
$result = dbArray($out);
$result['year_name'];

$voucher_id = Escape($_REQUEST["ID"]) ;
if ($voucher_id != "" && isset($voucher_id)) {

	$mode = "edit";
	$limit = 1;
	$start = 0;
	$condition = " and vm.voucher_id = '$voucher_id' ";
	$order_field = "";
	$order_type = "";

	$objVoucherMasterMgmt = new VoucherMasterMgmt();
	$out = $objVoucherMasterMgmt->getAllRecords($limit, $start, $condition, $order_field, $order_type);


	if (dbRows($out)) {
		$row = dbArray($out);
		extract($row);
	}
	$condition = "";
	$condition = " and voudet.voucher_id = '$voucher_id' ";
	$order_field = "";
	$order_type = "";

	$objVoucherDetailMgmt = new VoucherDetailMgmt();

	$out_det = $objVoucherDetailMgmt->getAllRecords($limit, $start, $condition, $order_field, $order_type);

} else {
	$mode = "add";
}
?>
<script language="javascript" type="text/javascript">

	var labelArray;
	labelArray = [
		['Voucher No :'],
		['Date:<span class="comp">*</span>'],
		['Receipt Type:<span class="comp">*</span>'],
		['TOTAL CREDIT AMOUNT :'],
		['CREDIT DETAIL :'],
		['TOTAL DEBIT AMOUNT :'],
		['DEBIT DETAIL :']
	];


	var formMode = '<?php echo $mode; ?>';


	var cYear = '<?php print $result['year_name']; ?>';

	var start = '1/4/';
	start += cYear.substring(0, 4);

	var end = '31/3/';
	end += cYear.substring(5, 9);

	Ext.onReady(function () {

		Ext.QuickTips.init();
		var i = 0;
		var total = 0;
		var total_debit = 0;
		var j = 0;

		var h = 0;
		var d = 0;
		var f = "";
		var n = "";
		var CurrentTextboxId = "";



		var dt = new Date();

		var Creditcount = 0;
		var Debitcount = 0;
		var getcreditid = 0;
		var getparentid = 0;
		var CAccountHiddenId = "";
		var DAccountHiddenId = "";



		var popupaccountliststore = new Ext.data.Store({
			url: 'data/getVoucherAccount.php',
			totolProperty: 'total',
			idProperty: 'account_id',
			reader: new Ext.data.JsonReader({
				fields: [
					{ name: "account_id" },
					{ name: "account_name" }

				],
				root: 'rows'
			})
		});
		var departmentDv = new Ext.ListView({
			store: popupaccountliststore,
			singleSelect: true,
			style: 'background-color: #FFFFFF;',
			columns: [
				{
					header: 'Account Name',
					dataIndex: 'account_name'
				}/*,
			 {
				header    : 'Account ID',
				dataIndex : 'account_id',
				hidden:true
			}*/
			],
			listeners: {
				click: function (a, b) {

					var record = a.store.getAt(b);

					Ext.getCmp(CurrentTextboxId).setValue(a.store.getAt(b).data.account_name);

					var account_id = (a.store.getAt(b).data.account_id);

					var s = CurrentTextboxId.substr(0, 1);

					if (s == 'C') {
						var ind = CurrentTextboxId.replace("CreditAccount", "");
						Ext.getCmp('CreditAccountHiddenId' + ind).setValue(account_id);
					}
					else {
						var ind = CurrentTextboxId.replace("DebitAccount", "");

						Ext.getCmp('DebitAccountHiddenId' + ind).setValue(account_id);
					}
					winaccount.hide();
				}
			}
		});

		function getCreditTotal(Id, parentId) {
			var cntElement = Ext.getCmp(parentId).items.length;

			Creditcount = cntElement;

			var cnt;
			var amount = 0;
			var TotalAmount = 0;
			var t = 0;

			for (t = 0; t < cntElement; t++) {

				cnt = Ext.getCmp(parentId).items.get(t);
				amount = Ext.getCmp(cnt.id).getValue();

				if (cnt.id != Id) {
					if (amount > 0) {
						TotalAmount = TotalAmount + parseFloat(amount);
					}
				}

			}
			return TotalAmount;
		}

		function getDebitTotal(Id, parentId) {

			var cntElement = Ext.getCmp(parentId).items.length;

			Debitcount = cntElement;

			var cnt;
			var amount = 0;
			var TotalAmount = 0;
			var t = 0;

			for (t = 0; t < cntElement; t++) {

				cnt = Ext.getCmp(parentId).items.get(t);
				amount = Ext.getCmp(cnt.id).getValue();

				if (cnt.id != Id) {
					if (amount > 0) {
						TotalAmount = TotalAmount + parseFloat(amount);
					}
				}

			}
			return TotalAmount;
		}

		function TotalAccount(CreditTotal, creditAmountValue) {

			if (creditAmountValue > 0) {
				CreditTotal = CreditTotal + parseFloat(creditAmountValue);

			}

			Ext.getCmp('txtTotalCredit').setValue(CreditTotal);


		}
		function TotalAccountdebit(DebitTotal, debitAmountValue) {
			if (debitAmountValue > 0) {
				DebitTotal = DebitTotal + parseFloat(debitAmountValue);
			}
			Ext.getCmp('txtTotalDebit').setValue(DebitTotal);

		}

		function CheckItem(parentId) {

			var cntElement = Ext.getCmp(parentId).items.length;

			if (cntElement == 0)
				return true;

			for (t = 0; t < cntElement; t++) {

				cnt = Ext.getCmp(parentId).items.get(t);
				cntValue = Ext.getCmp(cnt.id).getValue();

				if (cntValue == "") {

					Ext.MessageBox.show({
						title: 'Error',
						msg: 'Empty not allowed !',
						buttons: Ext.MessageBox.OK,
						icon: Ext.MessageBox.ERROR
					});
					return false;
				}
			}
			return true;
		}

		function account() {
			var Add_CreditAccount = Ext.getCmp("formParamsTableau_CreditAccount");

			var Add_CreditAmount = Ext.getCmp("formParamsTableau_CreditAmount");

			var Add_CreditDelete = Ext.getCmp("formParamsTableau_CreditDelete");

			var Add_CreditAccountHiddenId = Ext.getCmp("formParamsTableau_CreditAccountHiddenId");

			if (CheckItem(Add_CreditAmount.id) && CheckItem(Add_CreditAccount.id)) {

				i++;

				var CreditAccount = 'CreditAccount' + i;

				var CreditAmount = 'CreditAmount' + i;

				var CreditDelete = 'CreditDelete' + i;

				var CreditAccountHiddenId = 'CreditAccountHiddenId' + i;

				//Creditaccountid[i-1] = "" ;			

				var txtcreditAccount = new Ext.form.TextField({
					width: 190,
					height: 20,
					allowBlank: false,
					id: CreditAccount,
					name: 'CreditAccount',
					listeners: {
						focus: function () {
							CurrentTextboxId = this.id;
							receipt = FormVoucher.getForm().findField('receipttype').getValue();
							popupaccountliststore.load({ params: { receipttype: receipt } });
							winaccount.show();

						}
					}
				});

				var AccountHiddenId = new Ext.form.TextField({
					width: 190,
					height: 20,
					allowBlank: false,
					id: CreditAccountHiddenId,
					hidden: true,
					name: 'CreditAccountHiddenId'


				});

				Add_CreditAccount.add(txtcreditAccount);

				Add_CreditAccountHiddenId.add(AccountHiddenId);

				var txtcreditAmount = new Ext.form.NumberField({
					allowBlank: false,
					width: 100,
					height: 20,
					id: CreditAmount,
					name: 'CreditAmount',
					allowNegative: false,

					blankText: 'This field required only positive value',
					enableKeyEvents: true,
					autoCreate: { tag: 'input', type: 'text', maxlength: '8' },
					listeners: {
					}

				});

				Add_CreditAmount.add(txtcreditAmount);

				txtcreditAmount.on('keyup', function () {

					creditAmountValue = this.getValue();
					getcreditid = this.getId();
					getparentid = Add_CreditAmount.id;
					CreditTotal = getCreditTotal(getcreditid, getparentid);
					TotalAccount(CreditTotal, creditAmountValue);
				});

				Add_CreditDelete.add({
					xtype: 'button',
					iconCls: 'remove',
					id: CreditDelete,
					height: 20,
					width: 20,
					style: { margin: '0px 0px 0px 0px', padding: '0px 0px 2px 0px' },
					handler: function () {
						var Component1 = Ext.getCmp(CreditAccount);
						var Component2 = Ext.getCmp(CreditAmount);
						var Component3 = Ext.getCmp(CreditDelete);
						var Component4 = Ext.getCmp(CreditAccountHiddenId);
						var creditAccountId = Component1.id;

						var delind = creditAccountId.replace("CreditAccount", "");

						delind = parseInt(delind) - 1;

						if (delind != 0) {

							var MinusValue = Component2.getValue();

							if (MinusValue == "") {
								var minusvaluecredit = 0;
								var TotalValue = Ext.getCmp('txtTotalCredit').getValue();
								TotalValue = parseFloat(TotalValue) - parseFloat(minusvaluecredit)
								Ext.getCmp('txtTotalCredit').setValue(TotalValue);
							}
							else {

								var TotalValue = Ext.getCmp('txtTotalCredit').getValue();

								TotalValue = parseFloat(TotalValue) - parseFloat(MinusValue)
								Ext.getCmp('txtTotalCredit').setValue(TotalValue);

							}

							removefield_credit(Component4, Component1, Component2, Component3);
						}
					}

				});

				FormVoucher.doLayout();

			}
		}

		function removefield_credit(comp1, comp2, comp3, comp4) {
			var Add_CreditAccount = Ext.getCmp("formParamsTableau_CreditAccount");
			var Add_CreditAmount = Ext.getCmp("formParamsTableau_CreditAmount");
			var Add_CreditDelete = Ext.getCmp("formParamsTableau_CreditDelete");
			var Add_CreditAccountHiddenId = Ext.getCmp("formParamsTableau_CreditAccountHiddenId");

			Add_CreditAccountHiddenId.remove(comp1);
			Add_CreditAccount.remove(comp2);
			Add_CreditAmount.remove(comp3);
			Add_CreditDelete.remove(comp4);
			FormVoucher.doLayout();

		}
		function removefield_debit(comp1, comp2, comp3, comp4) {
			var Add_DebitAccount = Ext.getCmp("formParamsTableau_DebitAccount");
			var Add_DebitAmount = Ext.getCmp("formParamsTableau_DebitAmount");
			var Add_DebitdDelete = Ext.getCmp("formParamsTableau_DebitdDelete");
			var Add_DebitAccountHiddenId = Ext.getCmp("formParamsTableau_DebitAccountHiddenId");

			Add_DebitAccountHiddenId.remove(comp1);
			Add_DebitAccount.remove(comp2);
			Add_DebitAmount.remove(comp3);
			Add_DebitdDelete.remove(comp4);
			FormVoucher.doLayout();

		}
		function accountdebit() {
			var Add_DebitAccount = Ext.getCmp("formParamsTableau_DebitAccount");

			var Add_DebitAmount = Ext.getCmp("formParamsTableau_DebitAmount");

			var Add_DebitdDelete = Ext.getCmp("formParamsTableau_DebitdDelete");

			var Add_DebitAccountHiddenId = Ext.getCmp("formParamsTableau_DebitAccountHiddenId");

			if (CheckItem(Add_DebitAccount.id) && CheckItem(Add_DebitAmount.id)) {

				j++;

				var DebitAccount = 'DebitAccount' + j;

				var DebitAmount = 'DebitAmount' + j;

				var DebitdDelete = 'DebitdDelete' + j;

				var DebitAccountHiddenId = 'DebitAccountHiddenId' + j;

				//Debitaccountid[j-1]="";

				var txtdebitAccount = new Ext.form.TextField({
					allowBlank: false,
					width: 190,
					height: 20,
					id: DebitAccount,
					listeners: {
						focus: function () {
							CurrentTextboxId = this.id;
							receipt = FormVoucher.getForm().findField('receipttype').getValue();
							popupaccountliststore.load({ params: { receipttype: receipt } });
							winaccount.show();
						}
					}
				});

				var DAccountHiddenId = new Ext.form.TextField({
					width: 190,
					height: 20,
					allowBlank: false,
					id: DebitAccountHiddenId,
					hidden: true,
					name: 'DebitAccountHiddenId'
				});

				Add_DebitAccount.add(txtdebitAccount);
				Add_DebitAccountHiddenId.add(DAccountHiddenId);
				var txtdebitAmount = new Ext.form.NumberField({
					allowBlank: false,
					width: 100,
					height: 20,
					id: DebitAmount,
					allowNegative: false,
					blankText: 'This field required only positive value',
					enableKeyEvents: true,
					autoCreate: { tag: 'input', type: 'text', maxlength: '8' },
					listeners: {
					}
				});


				Add_DebitAmount.add(txtdebitAmount);

				txtdebitAmount.on('keyup', function () {
					debitAmountValue = this.getValue();
					getdebitid = this.getId();
					getparentid = Add_DebitAmount.id;
					DebitTotal = getDebitTotal(getdebitid, getparentid);
					TotalAccountdebit(DebitTotal, debitAmountValue);
				});

				Add_DebitdDelete.add({
					xtype: 'button',
					iconCls: 'remove',
					id: DebitdDelete,
					height: 20,
					style: { margin: '0px 0px 0px 0px', padding: '0px 0px 2px 0px' },
					handler: function () {

						var Component1 = Ext.getCmp(DebitAccount);
						var Component2 = Ext.getCmp(DebitAmount);
						var Component3 = Ext.getCmp(DebitdDelete);
						var Component4 = Ext.getCmp(DebitAccountHiddenId);

						var debitAccountId = Component1.id;
						var deleteindex = debitAccountId.replace("DebitAccount", ""); deleteindex = parseInt(deleteindex) - 1;


						if (deleteindex != 0) {

							var MinusValueDebit = Component2.getValue();
							if (MinusValueDebit == "") {
								var minusvaluedebit = 0;
								var TotalValueDebit = Ext.getCmp('txtTotalDebit').getValue();
								TotalValueDebit = parseFloat(TotalValueDebit) - parseFloat(minusvaluedebit)
								Ext.getCmp('txtTotalDebit').setValue(TotalValueDebit);
							}
							else {
								var TotalValueDebit = Ext.getCmp('txtTotalDebit').getValue();
								TotalValueDebit = parseFloat(TotalValueDebit) - parseFloat(MinusValueDebit)
								Ext.getCmp('txtTotalDebit').setValue(TotalValueDebit);
							}

							removefield_debit(Component4, Component1, Component2, Component3);
						}
					}
				});

				FormVoucher.doLayout();
			}
		}

		var btn = new Ext.Button({
			id: 'btn',
			iconCls: 'icon-add',
			handler: function () {
				account();
			}
		});

		var btn1 = new Ext.Button({
			id: 'btn1',
			iconCls: 'icon-add',
			handler: function () {
				accountdebit();
			}
		});

		var CreditAccountfieldSet = {
			id: 'formParamsTableau',
			border: false,
			height: 200,
			frame: true,
			labelWidth: 10,
			width: 380,
			autoScroll: true,
			title: '',
			items: [
				{
					layout: 'column',
					id: 'formParamsTableau_CreditAccountHiddenId',
					layout: 'form',
					hidden: true,
					labelWidth: 1,
					width: 20,
					columnWidth: .58
				},
				{
					layout: 'column',
					items: [{
						id: 'formParamsTableau_CreditAccount',
						layout: 'form',
						labelWidth: 1,
						width: 20,
						columnWidth: .58
					},
					{
						id: 'formParamsTableau_CreditAmount',
						name: 'creditamount',
						layout: 'form',
						labelWidth: 1,
						width: 20,
						columnWidth: .32
					}, {
						id: 'formParamsTableau_CreditDelete',
						layout: 'form',
						labelWidth: 1,
						width: 20,
						columnWidth: .10
					}
					]
				}]
		}

		var CreditAccountfieldSet_title = {
			border: false,
			height: 35,
			frame: true,
			labelWidth: 10,
			width: 380,
			autoScroll: true,
			title: '',
			items: [{
				layout: 'column',
				items: [{
					columnWidth: .62,
					xtype: 'box', autoEl: {
						tag: 'div',
						html: '<div align="center"><b>CREDIT ACCOUNT</b></div>'
					}
				},
				{
					columnWidth: .34,
					xtype: 'box', autoEl: {
						tag: 'div',
						html: '<div align="center"><b>AMOUNT</b></div>'
					}
				}, btn
				]

			}]
		}

		var DebitAccountfieldSet = {
			id: 'DebitAccountfieldSet',
			border: false,
			height: 200,
			frame: true,
			labelWidth: 10,
			width: 380,
			autoScroll: true,
			title: '',
			items: [{
				layout: 'column',
				id: 'formParamsTableau_DebitAccountHiddenId',
				layout: 'form',
				hidden: true,
				labelWidth: 1,
				width: 20,
				columnWidth: .58
			},
			{
				layout: 'column',
				items: [{
					id: 'formParamsTableau_DebitAccount',
					layout: 'form',

					labelWidth: 1,
					columnWidth: .58
				}, {
					id: 'formParamsTableau_DebitAmount',
					layout: 'form',
					labelWidth: 1,
					columnWidth: .32
				}, {
					id: 'formParamsTableau_DebitdDelete',
					layout: 'form',
					labelWidth: 1,
					columnWidth: .10
				}
				]

			}]
		}

		var DebitAccountfieldSet_title = {
			border: false,
			height: 35,
			frame: true,
			labelWidth: 10,
			width: 380,
			autoScroll: true,
			title: '',
			items: [{
				layout: 'column',
				items: [{
					columnWidth: .62,
					xtype: 'box', autoEl: {
						tag: 'div',
						html: '<div align="center"><b>DEBIT ACCOUNT</b></div>'
					}
				},
				{
					columnWidth: .34,
					xtype: 'box', autoEl: {
						tag: 'div',
						html: '<div align="center"><b>AMOUNT</b></div>'
					}
				}, btn1
				]

			}]




		}

		var ReceipttypeStore = new Ext.data.ArrayStore({
			fields: ['val_Riceipttype', 'dis_Riceipttype'],
			data: [['Contra', 'Contra'], ['General', 'General'], ['Receipt', 'Receipt'], ['Payment', 'Payment']]
		});

		var Receiptcombo = {
			xtype: 'combo',
			name: 'receipttype',
			id: 'receipttype',
			labelSeparator: '',
			fieldLabel: labelArray[2],
			forceSelection: true,
			displayField: 'dis_Riceipttype',
			valueField: 'val_Riceipttype',
			hiddenName: 'val_Riceipttype',
			width: 100,
			allowBlank: false,
			editable: false,
			triggerAction: 'all',
			mode: 'local',
			store: ReceipttypeStore,
			listeners: {
				select: {
					fn: function (combo, value) {

						receipt = combo.getValue('receipttype');
						//secid =  studententry.getForm().findField('receipttype').getValue(); 
						popupaccountliststore.load({ params: { receipttype: receipt } });




					}
				}
			}



		};

		var radiogroup = {
			xtype: 'radiogroup',
			id: 'cash-check',
			name: 'cash-check',
			items: [{ boxLabel: 'Cheque/DD', name: 'cash', inputValue: 1, checked: true },
			{ boxLabel: 'Cash', name: 'cash', inputValue: 0 }
			]

		}

		var receiptcredit = new Ext.form.TextField({
			id: 'receiptcredit',
			name: 'receiptcredit',
			readOnly: true,
			//fieldLabel:'Receipt No',
			hidden: true,
			width: 65,
			emptyText: '',
			autoCreate: { tag: 'input', type: 'text', maxlength: '4' },

			allowBlank: false
		});

		var receiptdebit = new Ext.form.TextField({
			id: 'receiptdebit',
			name: 'receiptdebit',
			readOnly: true,
			labelSeparator: '',
			fieldLabel: labelArray[0],
			width: 65,
			emptyText: '',
			autoCreate: { tag: 'input', type: 'text', maxlength: '4' },
			allowBlank: false
		});

		var ReceptFieldSet = {
			xtype: 'fieldset',
			title: '',
			border: false,
			//labelWidth  : 90,
			allowBlank: false,
			items: [{
				xtype: 'container',
				autoEl: {},
				border: false,
				layout: 'column',
				defaultType: 'field',
				items: [{
					xtype: 'container',
					autoEl: {},
					layout: 'form',
					columnWidth: .21,
					labelWidth: 80,
					items: [receiptdebit]
				},
				{
					xtype: 'container',
					autoEl: {},
					layout: 'form',
					columnWidth: .21,
					labelWidth: 40,
					items: [{

						xtype: 'datefield',
						name: 'datevoucher',
						id: 'datevoucher',
						labelSeparator: '',
						fieldLabel: labelArray[1],
						minValue: start,
						maxValue: end,
						value: dt.format('m-d-Y')


					}]
				},
				{
					xtype: 'container',
					autoEl: {},
					layout: 'form',
					columnWidth: .27,
					labelWidth: 92,
					items: [Receiptcombo]
				}, {
					xtype: 'container',
					autoEl: {},
					layout: 'form',
					columnWidth: .30,
					labelAlign: 'right',
					labelWidth: 50,
					items: [radiogroup]
				},
				{
					xtype: 'container',
					autoEl: {},
					layout: 'form',
					labelAlign: 'right',
					columnWidth: .15,
					labelWidth: 100,
					items: [receiptcredit]
				}]
			}]

		}

		var TotalaccountFieldSet = {
			xtype: 'fieldset',
			title: '',
			border: false,
			labelWidth: 150,
			allowBlank: false,

			items: [{
				xtype: 'container',
				//border      : false,
				layout: 'column',
				defaultType: 'field',
				autoScroll: true,
				items: [{
					xtype: 'container',
					layout: 'form',
					autoScroll: true,
					columnWidth: .5,
					labelWidth: 170,
					//style: {'text-align': 'right', color: 'green'},
					style: { margin: '0px 0px 0px 40px', padding: '0px 0px 0px 0px' },
					labelAlign: 'right',
					items: [{

						xtype: 'numberfield',
						style: 'text-align: right ;',
						labelSeparator: '',
						fieldLabel: labelArray[3],
						id: 'txtTotalCredit',
						readOnly: true,
						autoCreate: { tag: 'input', type: 'text', maxlength: '10' },
						width: 80
					}]

				},
				{
					xtype: 'container',
					layout: 'form',
					autoScroll: true,
					labelAlign: 'right',
					style: { margin: '0px 0px 0px 40px', padding: '0px 0px 0px 0px' },
					columnWidth: .5,
					labelWidth: 182,
					items: [{
						xtype: 'numberfield',
						id: 'totalamount',
						name: 'totalamount',
						labelSeparator: '',
						fieldLabel: labelArray[5],
						style: 'text-align: right;',
						id: 'txtTotalDebit',
						readOnly: true,
						autoCreate: { tag: 'input', type: 'text', maxlength: '10' },
						width: 80


					}]
				}, {
					xtype: 'container',
					layout: 'form',
					autoScroll: true,
					// style: {'text-align': 30, color: 'green'},
					columnWidth: .52, labelWidth: 170,
					style: { margin: '0px 0px 0px 37px', padding: '0px 0px 0px 0px' },
					labelAlign: 'right',
					items: [{
						xtype: 'textarea',
						name: 'textcredit',
						id: 'textcredit',
						labelSeparator: '',
						fieldLabel: labelArray[4],
						maxLength: 160,
						maxLengthText: 'max 160 characters',
						width: 130,
						height: 60
					}]

				},
				{
					xtype: 'container',
					layout: 'form',
					autoScroll: true,
					labelAlign: 'right',
					style: { margin: '0px 0px 0px 37px', padding: '0px 0px 0px 0px' },
					columnWidth: .48,
					labelWidth: 170,
					items: [{
						xtype: 'textarea',

						name: 'textdebit',
						id: 'textdebit',
						labelSeparator: '',
						fieldLabel: labelArray[6],
						maxLength: 160,
						maxLengthText: 'max 160 characters',
						width: 130,
						height: 60



					}]
				}]
			}]

		}

		var btn2 = new Ext.Button({
			id: 'btn2',
			text: 'Create New Account',
			iconCls: 'icon-add',
			handler: function () {
				groupstore.load({ params: { action: 'group' } });
				subgroupstore.load({ params: { action: 'subgroup' } });
				accountliststore.load();
				ShowAccountMaster();
				Ext.getCmp('btnClose').hide();
			}
		});

		winAccountMaster.on('hide', function () {
			receipt = FormVoucher.getForm().findField('receipttype').getValue();
			popupaccountliststore.load({ params: { receipttype: receipt } });
			//popupaccountliststore.load();
		});

		var winaccount = new Ext.Window({
			height: 300,
			width: 200,
			layout: 'fit',
			closeAction: 'hide',
			autoDestroy: true,
			collapsible: true,
			items: departmentDv,
			buttons: [btn2]
		});

		var onSuccessOrFailcredit = function () {
			Ext.MessageBox.show({
				title: 'Message',
				msg: 'Data has been saved',
				buttons: Ext.MessageBox.OK,
				icon: Ext.MessageBox.INFO
			})

			if (formMode == 'edit') {
				location.href = "view-voucher.php";
			}
			else {
				resetvoucher();
				disable();
				Ext.getCmp('addnew').enable();
			}


		}

		var onSuccessOrFaildebit = function () {
			Ext.MessageBox.show({
				title: 'Error',
				msg: 'Please fill complete form.',
				buttons: Ext.MessageBox.OK,
				icon: Ext.MessageBox.ERROR
			});
		}

		var FormVoucher = new Ext.FormPanel({
			id: 'FormVoucher',
			border: false,
			labelWidth: 10,
			frame: true,
			title: '',
			items: [ReceptFieldSet,

				{
					xtype: 'container',
					layout: 'column',
					defaultType: 'field',
					items: [{
						xtype: 'container',
						layout: 'form',
						columnWidth: .5,
						items: [CreditAccountfieldSet_title]

					},
					{
						xtype: 'container',
						layout: 'form',
						columnWidth: .5,
						labelWidth: 50,
						items: [DebitAccountfieldSet_title]
					}, {
						xtype: 'container',
						layout: 'form',
						columnWidth: .5,
						labelWidth: 50,
						items: [CreditAccountfieldSet]

					},
					{
						xtype: 'container',
						layout: 'form',
						columnWidth: .5,
						labelWidth: 50,
						items: [DebitAccountfieldSet]
					}]
				},
				TotalaccountFieldSet]
		});

		function disable() {
			Ext.getCmp('btn').disable();
			Ext.getCmp('btn1').disable();
			Ext.getCmp('formParamsTableau').disable();
			Ext.getCmp('DebitAccountfieldSet').disable();
			Ext.getCmp('cash-check').disable();
			Ext.getCmp('receipttype').disable();
			Ext.getCmp('txtTotalCredit').disable();
			Ext.getCmp('txtTotalDebit').disable();
			Ext.getCmp('textcredit').disable();
			Ext.getCmp('textdebit').disable();
			Ext.getCmp('datevoucher').disable();
			Ext.getCmp('savevoucher').disable();

		}

		function enable() {
			Ext.getCmp('btn').enable();
			Ext.getCmp('btn1').enable();
			Ext.getCmp('formParamsTableau').enable();
			Ext.getCmp('DebitAccountfieldSet').enable();
			Ext.getCmp('cash-check').enable();
			Ext.getCmp('receipttype').enable();
			Ext.getCmp('txtTotalCredit').enable();
			Ext.getCmp('txtTotalDebit').enable();
			Ext.getCmp('textcredit').enable();
			Ext.getCmp('textdebit').enable();
			Ext.getCmp('datevoucher').enable();
			Ext.getCmp('savevoucher').enable();
		}

		function resetvoucher() {

			var list1 = Ext.getCmp('formParamsTableau').findByType('textfield');
			var list1_button = Ext.getCmp('formParamsTableau').findByType('button');

			var tot_field_rec = list1.length / 3;
			var temp = list1.length / 3;
			var temp_button = list1_button.length;

			while (temp > 1) {
				removefield_credit(
					list1[temp - 1].getId(),
					list1[temp + tot_field_rec - 1].getId(),
					list1[temp + (tot_field_rec * 2) - 1].getId(),
					list1_button[temp_button - 1].getId()
				);
				temp--;
				temp_button--;
			}

			if (temp == 1) {
				Ext.getCmp(list1[temp - 1].getId()).reset();
				Ext.getCmp(list1[temp + tot_field_rec - 1].getId()).reset();
				Ext.getCmp(list1[temp + (tot_field_rec * 2) - 1].getId()).reset();
			}


			var list1 = Ext.getCmp('DebitAccountfieldSet').findByType('textfield');
			var list1_button = Ext.getCmp('DebitAccountfieldSet').findByType('button');

			var tot_field_rec = list1.length / 3;
			var temp = list1.length / 3;
			var temp_button = list1_button.length;

			while (temp > 1) {

				removefield_debit(list1[temp - 1].getId(), list1[temp + tot_field_rec - 1].getId(), list1[temp + (tot_field_rec * 2) - 1].getId(), list1_button[temp_button - 1].getId());
				temp--;
				temp_button--;
			}

			if (temp == 1) {
				Ext.getCmp(list1[temp - 1].getId()).reset();
				Ext.getCmp(list1[temp + tot_field_rec - 1].getId()).reset();
				Ext.getCmp(list1[temp + (tot_field_rec * 2) - 1].getId()).reset();
			}
			//---------***********------ 


			//-----------------------------------

			Ext.getCmp('cash-check').reset();
			Ext.getCmp('receipttype').reset();
			Ext.getCmp('txtTotalCredit').reset();
			Ext.getCmp('txtTotalDebit').reset();
			Ext.getCmp('textcredit').reset();
			Ext.getCmp('textdebit').reset();
			Ext.getCmp('datevoucher').reset();
			Ext.getCmp('receiptcredit').reset();
			Ext.getCmp('receiptdebit').reset();



		}

		var VoucherWindow = new Ext.Window({
			title: 'Account Voucher ',
			layout: 'fit',
			width: 800,
			autoScroll: true,
			height: 520,

			closeAction: 'hide',
			closable: true,
			collapsible: 'true',
			//maximizable:'true',
			bodyStyle: 'padding: 5px',
			items: FormVoucher,
			buttons: [
				{
					text: 'Add New',
					id: 'addnew',
					iconCls: 'icon-add',
					handler: function () {
						enable();
						Ext.getCmp('addnew').disable();
						Ext.Ajax.request({
							loadMask: true,
							sync: true,
							url: 'data/getAccountCode.php',
							params: { action: 'voucherreceipt' },
							waitMsg: 'Loading',
							success: function (resp) {
								varcodeno = Ext.decode(resp.responseText).receipt;
								Ext.getCmp('receiptcredit').setValue(varcodeno);
								Ext.getCmp('receiptdebit').setValue(varcodeno);
								Ext.getCmp('receipttype').setValue("General");

							}
						});
					}
				},
				{
					text: 'Save',
					iconCls: 'icon-save',
					id: 'savevoucher',
					handler: function () {
						var formPanel = Ext.getCmp('FormVoucher');
						if (Ext.getCmp('txtTotalCredit').getValue() != 0 && Ext.getCmp('txtTotalDebit').getValue() != 0) {
							if (Ext.getCmp('txtTotalCredit').getValue() == Ext.getCmp('txtTotalDebit').getValue()) {

								var Add_CreditAmount = Ext.getCmp("formParamsTableau_CreditAmount");

								var Add_CreditAccountHiddenId = Ext.getCmp("formParamsTableau_CreditAccountHiddenId");

								var Add_DebitAmount = Ext.getCmp("formParamsTableau_DebitAmount");

								var Add_DebitAccountHiddenId = Ext.getCmp("formParamsTableau_DebitAccountHiddenId");

								var arrayCreditAmount = new Array();
								var arrayDebitAmount = new Array();
								var arrayCreditAccountId = new Array();
								var arrayDebitAccountId = new Array();

								var varParentID = "";

								var cntElement = 0;

								// Credit Code for get Array
								varParentID = Add_CreditAccountHiddenId.id;

								cntElement = Ext.getCmp(varParentID).items.length;

								for (t = 0; t < cntElement; t++) {
									cnt = Ext.getCmp(varParentID).items.get(t);
									hiddendId = Ext.getCmp(cnt.id).getValue();
									arrayCreditAccountId[t] = hiddendId;
								}

								varParentID = Add_CreditAmount.id;

								cntElement = Ext.getCmp(varParentID).items.length;

								for (t = 0; t < cntElement; t++) {
									cnt = Ext.getCmp(varParentID).items.get(t);
									amount = Ext.getCmp(cnt.id).getValue();

									arrayCreditAmount[t] = amount;
								}

								// Debit Code for get Arra


								var debitParentID = "";

								debitParentID = Add_DebitAccountHiddenId.id;

								cntElement = Ext.getCmp(debitParentID).items.length;

								for (t = 0; t < cntElement; t++) {
									cnt = Ext.getCmp(debitParentID).items.get(t);
									hiddendId = Ext.getCmp(cnt.id).getValue();
									arrayDebitAccountId[t] = hiddendId;
								}

								debitParentID = Add_DebitAmount.id;

								cntElement = Ext.getCmp(debitParentID).items.length;

								for (t = 0; t < cntElement; t++) {
									cnt = Ext.getCmp(debitParentID).items.get(t);
									amount = Ext.getCmp(cnt.id).getValue();

									arrayDebitAmount[t] = amount;
								}



								formPanel.getForm().submit({
									url: 'save-voucher.php?ID=<?php echo $voucher_id != "" ?  $voucher_id  : 0; ?>',
									waitMsg: 'Saving...',
									success: onSuccessOrFailcredit,
									failure: onSuccessOrFaildebit,
									params: {
										action: 'save',
										creditaccountid: Ext.util.JSON.encode(arrayCreditAccountId),
										camountarray: Ext.util.JSON.encode(arrayCreditAmount),
										debittaccountid: Ext.util.JSON.encode(arrayDebitAccountId),
										damountarray: Ext.util.JSON.encode(arrayDebitAmount)
									}

								});

							}
							else {
								// when saving data failed
								Ext.MessageBox.show({
									title: 'Error',
									msg: 'Invalid Total Amount ! please Balance amount both side.',
									buttons: Ext.MessageBox.OK,
									icon: Ext.MessageBox.ERROR
								});
							}
						}

					}
				},
				{
					text: 'Reset',
					id: 'reset',
					iconCls: 'icon-reset',
					handler: function () {
						resetvoucher();
						disable();
						Ext.getCmp('addnew').enable();
					}
				},

				{
					text: 'Close',
					iconCls: 'icon-close',
					handler: function () {
						if (formMode == 'edit') {
							location.href = "view-voucher.php";
						}
						else {
							VoucherWindow.hide();
						}
					}
				}]
		});

		VoucherWindow.show();
		disable();

		if (i == 0) {
			account();
		}
		if (j == 0) {
			accountdebit();
		}
		<?php
		if ($mode == "edit") {
			$credit_index = 1;
			$debit_index = 1;

			while ($row_det = dbArray($out_det)) {
				if ($row_det["voucher_creditamount"] != 0) {

					if ($credit_index != 1) { ?> account(); <?php }
					?>

					FormVoucher.getForm().findField('CreditAccountHiddenId' + '<?php echo $credit_index; ?>').setValue('<?php echo $row_det["account_id"]; ?>');
					FormVoucher.getForm().findField('CreditAccount' + '<?php echo $credit_index; ?>').setValue('<?php echo $row_det["account_name"]; ?>');

					FormVoucher.getForm().findField('CreditAmount' + '<?php echo $credit_index; ?>').setValue('<?php echo $row_det["voucher_creditamount"]; ?>');

					<?php
					$credit_index++;
				}

				if ($row_det["voucher_debitamount"] != 0) {

					if ($debit_index != 1) { ?> accountdebit(); <?php }

					?>
					FormVoucher.getForm().findField('DebitAccountHiddenId' + '<?php echo $debit_index; ?>').setValue('<?php echo $row_det["account_id"]; ?>');
					FormVoucher.getForm().findField('DebitAccount' + '<?php echo $debit_index; ?>').setValue('<?php echo $row_det["account_name"]; ?>');
					FormVoucher.getForm().findField('DebitAmount' + '<?php echo $debit_index; ?>').setValue('<?php echo $row_det["voucher_debitamount"]; ?>');

					<?php
					$debit_index++;
				}
			}

		}
		?>


		if (formMode == "edit") {


			var formPanel = Ext.getCmp('FormVoucher');

			formPanel.getForm().findField('datevoucher').setValue('<?php echo ($row["voucher_date"] != "") ? FormatDate($row["voucher_date"], "d/m/Y") : date('d/m/Y'); ?>');
			formPanel.getForm().findField('receipttype').setValue('<?php echo $row["voucher_type"]; ?>');
			formPanel.getForm().findField('cash-check').setValue('<?php echo $row["voucher_check"]; ?>');
			formPanel.getForm().findField('receiptcredit').setValue('<?php echo $row["receipt_no"]; ?>');
			formPanel.getForm().findField('receiptdebit').setValue('<?php echo $row["voucher_no"]; ?>');
			formPanel.getForm().findField('txtTotalCredit').setValue('<?php echo $row["voucher_totalamount"]; ?>');
			formPanel.getForm().findField('textcredit').setValue('<?php echo $row["voucher_creditdetail"]; ?>');
			formPanel.getForm().findField('totalamount').setValue('<?php echo $row["voucher_totalamount"]; ?>');
			formPanel.getForm().findField('textdebit').setValue('<?php echo $row["voucher_debitdetail"]; ?>');
			enable();

			Ext.getCmp('addnew').disable();
			Ext.getCmp('reset').disable();
		}


	});

</script>
<div align="center">
	<div id="main-content" style="width:600px" align="left"></div>
</div>
<?php require "_footer.php"; ?>