EMA.Alert.form.Transaction = Ext.extend(Ext.FormPanel, {
	labelWidth: 75,
    frame:true,
    bodyStyle:'padding:5px 5px 0',
    width: 350,
    defaults: {width: 230},
    defaultType: 'textfield',
    waitMsgTarget: true,
	constructor: function(config) {
		this.url = config.url;
		this.txnType = config.txnType;
		this.parentWindow = config.parentWindow;
		 /*
    	 * Fill form with value of our Account
    	 */
		this.data = config.data;
		this.callback = config.callback;
		this.items = [
		    {
	            fieldLabel: 'Account No',
	            name: 'data[Account][id]',
	            value: this.data['Account.id'],
        		xtype: 'displayfield'
	        }, {
	            fieldLabel: 'First Name',
	            name: 'data[Account][firstname]',
	            value: this.data['Account.firstname'],
        		xtype: 'displayfield'
	        }, {
	            fieldLabel: 'Last Name',
	            name: 'data[Account][lastname]',
	            value: this.data['Account.lastname'],
        		xtype: 'displayfield'
	        }, {
	        	id: 'txnAmount',
	            fieldLabel: 'Amount',
	            name: 'data[Transaction][amount]',
	            allowNegative: false,
	            allowDecimals: false,
	            xtype: 'numberfield',
	            minValue: 1,
	            allowBlank: false,
	            width: 150
	        }, {
	            xtype: 'hidden',
	            name: 'data[Transaction][receiver_id]',
	            value: this.data['Account.id']
	        }, {
	            xtype: 'hidden',
	            name: 'data[Transaction][commit]',
	            value: 0,
	            id: 'transaction_commit'
	        }
        ];
		
		this.buttons = [
			{
		        text: 'Save',
		        listeners: {
		        	scope: this,
		        	click: function() {
		        		this.onSubmit();
		        	}
		        }
		    }, {
		        text: 'Cancel',
		        listeners: {
		        	scope: this.parentWindow,
		        	click: function(e) {
		        		this.hide();
		        	}
		        }
			}
		];
	    
		EMA.Alert.form.Transaction.superclass.constructor.apply(this, arguments);
	},
	onSubmit: function () {
		var parentWindow = this.parentWindow;
		var transactionForm = this.getForm();
		var data = this.data;
		var callback = this.callback;
		var amount = Ext.util.Format.usMoney(Ext.getCmp('txnAmount').value);
		var playerName = data['Account.firstname'] + ' ' + data['Account.lastname'];
		if (this.txnType == 'withdraw') {
			var confirmMsg = 'Are you sure you want to <b>withdraw '+ amount +
				'</b><br/>from the account of <b>' + playerName + '</>?';
			var successMsg = 'You can now give <b>'+ amount +'</b> of chips to <b>'+ playerName +'</b>.<br/><br/>' +
				'Make sure to print and sign the receipt.';
			var printUrl = '/transactions/print_receipt/';
		} else if (this.txnType == 'deposit') {
			var confirmMsg = 'Are you sure you want to <b>deposit '+ amount +
				'</b><br/>into the account of <b>' + playerName + '</>?';
			var successMsg = 'You can now take <b>'+ amount +'</b> from <b>'+ playerName +'</b>.<br/><br/>' +
				'Make sure to print and sign the receipt.';
			var printUrl = '/transactions/print_receipt/';
		}
		
		if (this.getForm().isValid()) {
			this.getForm().submit({
				clientValidation: true,
				success: function(form, action) {
					
					Ext.Msg.confirm('Confirm', confirmMsg, function(btn, text) {
						if (btn == 'yes') {
							Ext.getCmp('transaction_commit').setValue(1);
							transactionForm.submit({
								clientValidation: true,
								success: function(form, action) {
									parentWindow.hide();
									Ext.MessageBox.buttonText.ok = 'Print'
									Ext.Msg.show({
										title: 'Success',
										msg: 'The transaction #' + action.result.id + ' was approved.<br/>' + successMsg,
										buttons: Ext.Msg.OK,
										closable:false,
										fn: function(btn) {
											if (userGroup == GROUP_ADMIN || userGroup == GROUP_ROOT) {
												window.open(printUrl + action.result.id);
											} else {
												location.href = printUrl + action.result.id;
											}
										}
									});
									Ext.MessageBox.buttonText.ok = 'Ok'
									// No callback for dealers
									if (userGroup == GROUP_ADMIN || userGroup == GROUP_ROOT) {
										callback(data['Account.id']);
									}
								},
								failure: function(form, action) {
									parentWindow.hide();
									Ext.Msg.alert('Failed', action.result.errorMessage);
				        	    },
								waitMsg: 'Processing'
			        		});
						} else {
							parentWindow.hide();
						}
					});
	    	    },
	    	    failure: function(form, action) {
	    	    	parentWindow.hide();
	    	    	Ext.Msg.alert('Failed', action.result.errorMessage);
	    	    },
				waitMsg: 'Validating'
			});
		}
	}
});
