/**
 * @author schiesser
 */
Ext.ns('efiport.book.controller');

efiport.book.controller.commit = function(widget){
    var that = {};
    
	var buyCC = function(goid) {
		Ext.Ajax.simpleRequest({
			url: '/axis2/services/EventService/bookEventPerCC',
			success: function(safepayURL){
				window.location.href = safepayURL;
			},
			params: {
				successLink: window.location.href,
				backLink: window.location.href,
				failLink: window.location.href,
				goid: goid
			}
		});
	};
	
	var regHandler = function(registrationGoid){
					Ext.MessageBox.hide();
					if (registrationGoid !== "0") {
						Ext.Msg.alert('Vielen Dank', 'Vielen Dank für Ihren Einkauf. Ihre Bestellbestätigung erhalten Sie per E-Mail.', function(){
							open(widget.boughtLink, '_self');
						});
					}
				};
	
    that.buy = function() {
		var goid = widget.model.itemToBook.getAt(0).id;
		if (widget.model.paymentType === 'creditCard') {
			buyCC(goid);
		}
		else {
			Ext.MessageBox.wait(widget.waitText);
			Ext.Ajax.simpleRequest({
				url: '/axis2/services/EventService/bookEvent',
				success: regHandler,
				params: {
					sendEmail: true,
					goid: goid
				}
			});
		}
	};
	
	that.finalizeCC = function(goid) {
		var data = Extreme.util.URLParser.parse('DATA', true);
		var signature = Extreme.util.URLParser.parse('SIGNATURE');
		if (widget.creditCardPayment && data !== '' && signature !== '') {
			Ext.Ajax.simpleRequest({
				url: '/axis2/services/EventService/finalizeCCBooking',
				success: regHandler,
				params: {
					signature: signature,
					data: data,
					goid: goid
				}
			});
			return true;
		}
		else {
			return false;
		}
	};

    return that;
};

