
if ( Object.isUndefined(ID) ) { var ID = { }; }

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

ID.Payment = Class.create({
	log: function(str) {
		site.log('[ID.Payment] '+str);
	},
	initialize: function(elementId,basketId,options) {
		this.formValues = {};
		this.element = $(elementId);
		this.basketId = basketId;
		this.options = Object.extend({
			'paymentURL':	'https://payment.architrade.com/paymentweb/start.action',
			'frameName':	'dibsPayment'
		},options||{});

		this._loadvalues();
	},
	_loadvalues: function() {
		this.log('Loading form values..');

		// Use Microsoft Ajax stub
		dk.ide.webservice.BasketService.GetPaymentPostData(this.basketId,this.onValuesLoaded.bind(this));

		/*
		this.formValues = {
			'merchant':		'90054063',
			'orderid':		'test-20110210155255',
			'amount':		'12345',
			'currency':		'208',
			'accepturl':	'https://www.ide.dk/payment/accept',
			'cancelurl':	'https://www.ide.dk/payment/cancel',
			'test':			'yes',
			'color':		'none'
		};
		*/
	},
	onValuesLoaded: function(result) {
		var data = (!Object.isUndefined(result.d))?result.d:result;

		if (data.amount === null) {
			this._reportError('Invalid/empty POST data received from server');
			return;
		}

		this.formValues = {
			'merchant':		data.HTTP_COOKIE,
			'accepturl':	data.accepturl,
			// 'amount':		data.amount,
			'callbackurl':	data.callbackurl,
			'cancelurl':	data.cancelurl,
			'color':		data.color,
			'currency':		data.currency,
			'md5key':		data.md5key,
			'merchant':		data.merchant,
			'orderid':		data.orderid
		};
		if ( data.test == 'yes' )
		{
			this.formValues.test = 'yes';
		}
		
		if ( data.splits > 1 )
		{
			this.formValues.split = data.splits;
			
			var i = 1;
			var amounts = $H();
			
			data.splitTransactions.each(function(split) {
				amounts.set('amount'+split.transactIdx, split.amount);
			});
			
			/***
			data.splitamounts.each(function(splitamount) {
				amounts.set('amount'+i, splitamount);
				i += 1;
			});
			/***/
			
			Object.extend(this.formValues, amounts.toObject());
		}
		else
		{
			this.formValues.amount = data.amount;
		}

		// Now render the form
		this._render();
	},
	_render: function() {
		// Create IFRAME
		this.frame = new Element('IFRAME',{
			'height':		450,
			'width':		600,
			'scrolling':	'no',
			'frameBorder':	'0',
			'name':			this.options.frameName,
			'id':			this.options.frameName
		});

		this.frame.addClassName('paymentFrame');

		// Create FORM
		this.form = new Element('FORM',{
			'name':			'paymentForm',
			'method':		'post',
			'action':		this.options.paymentURL,
			'target':		this.options.frameName
		});

		// IE7?
		this.form.target = this.options.frameName;

		// Add elements to form
		$H(this.formValues).each(function(t){
			var formField = new Element('INPUT');
			formField.type = 'hidden';
			formField.name = t.key;
			formField.value = t.value;
			this.form.insert({'bottom':formField});
		},this);

		// Add form and frame to page
		this.log('Inserting elements into DOM..');
		this.element.update(); // Could cause problems on ID and require a defer()
		this.element.insert({'bottom':this.form});
		this.element.insert({'bottom':this.frame});

		// Queue submit
		this._checkRendered.bind(this).defer();
	},
	_checkRendered: function() {
		if ($(this.options.frameName)) {
			this._submit();
		} else {
			this._checkRendered.delay(1);
		}
	},
	_submit: function() {
		this.log('Submitting form..');
		this.form.submit();
						
		// page ready
		(function(){$('receiver').removeClassName('pending')}).delay(3.5);
	},
	_reportError: function(str) {
		this.log('Error: '+str);
		document.fire('custom:payment','error');
	}
});

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


