/**
 *
 * @author Stefan Becker
 *
 */

// public namespace here
var oPaymentIPayment;

var IPAYMENT_TRX_TYPE_AUTH = 'auth';
var IPAYMENT_TRX_TYPE_BASE_CHECK = 'base_check';

(function () {
	/**
	 * @param value
	 */
	var __checkNull = function __checkNull(value) {
		if ('undefined' == typeof value ||
			null === value)
		{
			return null;
		} else {
			return value;
		}
	}

	// private namespace here
	oPaymentIPayment = {
		  account_id: null
		, trxuser_id: null
		, trxpassword: null

		, bUseSecurityHash: null

		/**
		 * @param account_id
		 * @param trxuser_id
		 * @param trxpassword
		 * @param bUseSecurityHash
		 */
		, init: function __init(account_id, trxuser_id, trxpassword, bUseSecurityHash) {
			this.account_id = account_id;
			this.trxuser_id = trxuser_id;
			this.trxpassword = trxpassword;

			this.bUseSecurityHash = (false !== bUseSecurityHash);
		}

		/**
		 * @param trx_type
		 * @param trx_currency
		 * @param trx_amount
		 */
		, startTransaction: function __startTransaction(trx_type, trx_currency, trx_amount) {
			var oTransaction = {
				  trx_currency: trx_currency
				, trx_amount: parseInt(trx_amount)
				, trx_type: trx_type
				, trx_paymenttyp: null
				, trxpassword: null
				, trxuser_id: null

				// silent mode
				, silent: '1'

				, redirect_url: null
				, silent_error_url: null
				, hidden_trigger_url: null

				//
				// , noparams_on_redirect_url: '1'
				// , noparams_on_error_url: '1'
				, send_confirmation_email: '1'

				// credit card data
				, cc_number: null
				, cc_expdate_month: null
				, cc_expdate_year: null
				, cc_checkcode: null

				// ELV data
				, bank_accountnumber: null
				, bank_code: null
				, bank_name: null

				// ELV data international
				, bank_iban: null
				, bank_bic: null

				// address data
				, addr_name: null
				, addr_email: null
				, addr_street: null
				, addr_city: null
				, addr_zip: null
				, addr_country: null

				// mark transaction
				, invoice_text: null
				, shopper_id: null
				, trx_user_comment: null

				/**
				 * @param cc_number
				 * @param cc_expdate_month
				 * @param cc_expdate_year
				 * @param cc_checkcode
				 */
				, setCreditCardData: function __setCreditCardData(cc_number, cc_expdate_month, cc_expdate_year, cc_checkcode) {
					this.trx_paymenttyp = 'cc';

					this.cc_number = __checkNull(cc_number);
					this.cc_expdate_month = __checkNull(cc_expdate_month);
					this.cc_expdate_year = __checkNull(cc_expdate_year);
					this.cc_checkcode = __checkNull(cc_checkcode);
				}

				/**
				 * @param bank_accountnumber
				 * @param bank_code
				 * @param bank_name
				 */
				, setElvData: function __setElvData(bank_accountnumber, bank_code, bank_name) {
					this.trx_paymenttyp = 'elv';

					this.bank_accountnumber = __checkNull(bank_accountnumber);
					this.bank_code = __checkNull(bank_code);
					this.bank_name = __checkNull(bank_name);
				}

				/**
				 * @param bank_iban
				 * @param bank_bic
				 */
				, setElvDataInt: function __setElvDataInt(bank_iban, bank_bic) {
					this.trx_paymenttyp = 'elv';

					this.bank_iban = __checkNull(bank_iban);
					this.bank_bic = __checkNull(bank_bic);
				}

				/**
				 * @param addr_name
				 * @param addr_email
				 * @param addr_street
				 * @param addr_city
				 * @param addr_zip
				 * @param addr_country
				 */
				, setAddressData: function __setAddressData(addr_name, addr_email, addr_street, addr_city, addr_zip, addr_country) {
					this.addr_name = __checkNull(addr_name);
					this.addr_email = __checkNull(addr_email);
					this.addr_street = __checkNull(addr_street);
					this.addr_city = __checkNull(addr_city);
					this.addr_zip = __checkNull(addr_zip);
					this.addr_country = __checkNull(addr_country);
				}

				/**
				 * @param silent_error_url
				 * @param hidden_trigger_url
				 * @param redirect_url
				 */
				, setParameters: function __setParameters(silent_error_url, hidden_trigger_url, redirect_url) {
					this.silent_error_url = __checkNull(silent_error_url);
					this.hidden_trigger_url = __checkNull(hidden_trigger_url);
					this.redirect_url = __checkNull(redirect_url);
				}

				/**
				 * @param invoice_text
				 */
				, setInvoiceText: function __setInvoiceText(invoice_text) {
					this.invoice_text = __checkNull(invoice_text);
				}

				/**
				 * @param shopper_id
				 */
				, setShopperId: function __setShopperId(shopper_id) {
					this.shopper_id = __checkNull(shopper_id);
				}

				/**
				 * @param trx_user_comment
				 */
				, setTrxUserComment: function __setTrxUserComment(trx_user_comment) {
					this.trx_user_comment = __checkNull(trx_user_comment);
				}

				/**
				 * create a html form suitable for submitting the transaction and submit it
				 */
				, request: function __request(transaction_id) {
					var self = this;

					self.getSecurityHash(function __callback(hash) {

						// remove existing form and frame for the case of double submitting
						$('#iPaymentForm-' + transaction_id).remove();
						$('#iPaymentIFrame-' + transaction_id).remove();

						// create new form and iframe
						var sUri = 'https://ipayment.de/merchant/' + oPaymentIPayment.account_id + '/processor/2.0/'
						var oForm = $('<form />');
						var oIFrame = $('<iframe />');

						oForm.attr('action', sUri);
						oForm.attr('id', 'iPaymentForm-' + transaction_id)
						oForm.attr('method', 'POST');
						oForm.attr('target', 'iPaymentIFrame-' + transaction_id);
						oForm.css('display', 'none');

						oIFrame.attr('id', 'iPaymentIFrame-' + transaction_id);
						oIFrame.attr('name', 'iPaymentIFrame-' + transaction_id);
						oIFrame.attr('src', 'about:blank');
						oIFrame.css('display', 'none');

						self.trx_securityhash = hash;
						self.trxpassword = oPaymentIPayment.trxpassword;
						self.trxuser_id = oPaymentIPayment.trxuser_id;

						for (var name in self) {
							var value = self[name];

							// null is an object too (in javascript)
							if (
								'function' !== typeof value
								&& 'object' !== typeof value
								&& name != 'addr_street'
								&& name != 'addr_city'
								&& name != 'addr_zip'
							) {
								var oInput = $('<input />');

								oInput.attr('type', 'hidden');
								oInput.attr('name', name);
								oInput.attr('value', value);

								oForm.append(oInput);
							}
						}
						$('body').append(oForm);
						$('body').append(oIFrame);

						$('#iPaymentForm-' + transaction_id).submit();

					});
				}

				/**
				 * @param fCallback
				 */
				, getSecurityHash: function __getSecurityHash(fCallback) {
					// use security hash?
					if (oPaymentIPayment.bUseSecurityHash) {
						// yes!
						var sUri = '/?eID=json&action=iPaymentSecurityHash';
						var oData = {
							  trxuser_id: oPaymentIPayment.trxuser_id
							, trx_amount: this.trx_amount
							, trx_currency: this.trx_currency
						};

						$.get(sUri, oData, function __returnHash(result) {
							if (false == result['bError']) {
								fCallback(result['mData']['trx_securityhash']);
							} else {
								// an error occurred
								fCallback(null)
							}
						}, 'json');
					} else {
						// no! Return null instead of security hash
						fCallback(null);
					}
				}
			};

			return oTransaction;
		}

		, useAccount: function __useAccount(sLkz) {
			switch (sLkz.toUpperCase()) {
				case 'DE':
					oPaymentIPayment.useDeAccountWithSecurityHash();
					break;

				case 'AT':
					oPaymentIPayment.useAtAccountWithSecurityHash();
					break;
					
				case 'TEST':
					oPaymentIPayment.useTestAccountWithSecurityHash();
					break;

				default:
					throw 'SYSTEM_ERROR';
			}
		}

		/**
		 * debug - use de account with security hash
		 */
		, useDeAccountWithSecurityHash: function __useAccountWithSecurityHash() {
			var account_id = '43551352';
			var trxuser_id = '6685';
			var trxpassword = '96726506';
			var bUseSecurityHash = true;

			oPaymentIPayment.init(account_id, trxuser_id, trxpassword, bUseSecurityHash);
		}

		/**
		 * debug - use de account without security hash
		 */
		, useDeAccountWithoutSecurityHash: function __useAccountWithoutSecurityHash() {
			var account_id = '43551352';
			var trxuser_id = '6685';
			var trxpassword = '96726506';
			var bUseSecurityHash = false;

			oPaymentIPayment.init(account_id, trxuser_id, trxpassword, bUseSecurityHash);
		}

		/**
		 * debug - use at account with security hash
		 */
		, useAtAccountWithSecurityHash: function __useAccountWithSecurityHash() {
			var account_id = '99999';//'74857707';
			var trxuser_id = '99998';
			var trxpassword = '0';
			var bUseSecurityHash = true;

			oPaymentIPayment.init(account_id, trxuser_id, trxpassword, bUseSecurityHash);
		}

		/**
		 * debug - use ataccount without security hash
		 */
		, useAtAccountWithoutSecurityHash: function __useAccountWithoutSecurityHash() {
			var account_id = '99999';//'74857707';
			var trxuser_id = '99998';
			var trxpassword = '0';
			var bUseSecurityHash = false;

			oPaymentIPayment.init(account_id, trxuser_id, trxpassword, bUseSecurityHash);
		}

		/**
		 * debug - use test account
		 */
		, useTestAccountWithSecurityHash: function __useTestAccountWithSecurityHash() {
			var account_id = '99999';
			var trxuser_id = '99998';
			var trxpassword = '0';
			var bUseSecurityHash = true;

			oPaymentIPayment.init(account_id, trxuser_id, trxpassword, bUseSecurityHash);
		}

		/**
		 * debug - use test account
		 */
		, useTestAccountWithoutSecurityHash: function __useTestAccountWithoutSecurityHash() {
			var account_id = '99999';
			var trxuser_id = '99999';
			var trxpassword = '0';
			var bUseSecurityHash = false;

			oPaymentIPayment.init(account_id, trxuser_id, trxpassword, bUseSecurityHash);
		}
	}
})();

$(document).ready(function () {
	// initialize oDonationControl
	oPaymentIPayment.useDeAccountWithoutSecurityHash();

	// example usage
	if (false) {
		oPaymentIPayment.useDeAccountWithSecurityHash();

		var transaction_id = 'unique id';
		var oTransaction = oPaymentIPayment.startTransaction(IPAYMENT_TRX_TYPE_BASE_CHECK, 'EUR', '100');

		oTransaction.setShopperId(transaction_id);

		oTransaction.setAddressData
			('Name', 'Email', 'Strasse + Nr', 'Ort', 'PLZ', 'DE');

		oTransaction.setCreditCardData('1234567809876543', '12', '15', '765');

		oTransaction.request();
	}
});

