﻿// JSLint Config:
/*jslint browser: true */
/*global cc, jQuery, tinyMCE,
cc_resx_TR_Redirecting_to_BT_for_Payment, cc_resx_TR_Bad_or_missing_product_code, 
cc_resx_TR_Processing_your_purchase_request, cc_resx_TR_An_error_occurred_processing_payment,
cc_resx_TR_Redirecting_to_payment_provider, cc_resx_TR_AUTH_AJAX_ERROR_REFRESH, cc_resx_TR_AUTH_AJAX_ERROR
*/

//  
//  Author: Kevin Burkitt
//  Date:   2010-04-13
//  
//  Payments Helper Functions
//
cc.payments = function() {

    return {
        subscribeInProcess: false,
        subscribeError: false,
        subscribeRedirect: false,

        init: function(target) {
            jQuery('.cc-subscription').unbind('click').click(function() {
                var product = jQuery(this).attr("cc:product");
                var sessionid = jQuery(this).attr("cc:sessionid");
                var buttonid = jQuery(this).attr("cc:buttonid");
                var result = cc.payments.subscribe(product, sessionid, buttonid);
            });
        },

        subscribe: function(product, sessionid, buttonid) {

            if (typeof cc.payments.subscribeInProcess !== 'undefined' && cc.payments.subscribeInProcess === true) {
                return false;
            }
            cc.payments.subscribeInProcess = true;

            var p = product;
            var page = cc.helpers.encodeQsParam(document.location.href);
            var url = '/payments/bt-subscription/buy/' + p + '/' + sessionid + '/' + buttonid + '/?u=' + page;

            // cc.log('Attempting to buy \'' + p + '\' on page: ' + page + '');

            cc.ajax.getJSON(url, {}, function(resp, textStatus) {
                try {
                    if (resp.success !== true) {
                        // Some Server Failure or Bad Id Encountered In Processing (bad; shouldn't happen)
                        cc.ui.showModalErrorMessage(resp.message);
                    }
                    else {
                        // Processed ok
                        if (resp.redirect !== '') {
                            // redirect user to C&B for authorisation
                            // cc.stars.hideOverlayPleaseWait();
                            cc.ui.showModalErrorMessage(cc_resx_TR_Redirecting_to_BT_for_Payment);
                            location.href = resp.redirect;
                            return;
                        }
                        else {
                            // Here = user already authorised & valid C&B user:

                            // Payment Has Been Attempted! 

                            // if success, reload will be probably be set 
                            if (resp.reload === true) {
                                location.reload(true);
                            }

                            // Or just display resp.message
                            cc.ui.showModalErrorMessage(resp.message);
                        }
                    }
                }
                catch (e) {
                    cc.ui.showModalErrorMessage(cc_resx_TR_AUTH_AJAX_ERROR);
                }
            },
            function() {
                cc.payments.subscribeInProcess = false;

                // Hide Please Wait Overlay
                cc.ui.hideModalErrorMessage();
            });
        },


  


        epochTransactionStart: function(form) {
            // if (typeof cc.payments.subscribeInProcess != 'undefined' && cc.payments.subscribeInProcess == true) {
            //     return false;
            // }
            cc.payments.subscribeInProcess = true;
            cc.payments.subscribeError = false;

            try {

                var f = form;
                var transactionId = jQuery('.x_transactionid', f).val();
                var pi_code = jQuery('.pi_code', f).val();

                if (typeof pi_code === 'undefined' || pi_code === '') {
                    cc.ui.showModalErrorMessage(cc_resx_TR_Bad_or_missing_product_code);
                    return false;
                }

                if (typeof transactionId !== 'undefined' && transactionId !== '') {
                    return true;

                } else {
                    
                    var buttonId = jQuery('.x_tourbuttonid', f).val();
                    var page = cc.helpers.encodeQsParam(document.location.href);
                    var url = '/payments/epoch/start/' + pi_code + '/' + jQuery('#PageSessionId').val() + '/' + buttonId + '/';
                  
                    // cc.log('Attempting to buy \'' + pi_code + '\' on page: ' + page + '');

                    // Redirecting you to our Payment Provider
                    cc.ui.showModalErrorMessage(cc_resx_TR_Processing_your_purchase_request);

                    cc.ajax.getJSON(url, {}, function(resp, textStatus) {
                        try {
                            if (typeof resp === 'undefined') {
                                cc.ui.showModalErrorMessage(cc_resx_TR_An_error_occurred_processing_payment);
                                cc.payments.subscribeError = true;
                            } else {
                                if (resp.success !== true) {
                                    // Some Server Failure or Bad Id Encountered In Processing (bad; shouldn't happen)
                                    cc.ui.showModalErrorMessage(resp.message);
                                    cc.payments.subscribeError = true;
                                }
                                else {
                                    // Processed ok
                                    if (resp.message !== '') {
                                        // We have transaction id:

                                        jQuery('.x_transactionid', f).val(resp.message);
                                        jQuery('.x_url', f).val(location.href);
                                        cc.ui.showModalErrorMessage(cc_resx_TR_Redirecting_to_payment_provider);
                                        cc.payments.subscribeInProcess = false;
                                        cc.payments.subscribeRedirect = true;
                                        jQuery(f).submit();
                                    }
                                    else {
                                        // Or just display resp.message
                                        cc.ui.showModalErrorMessage(cc_resx_TR_AUTH_AJAX_ERROR);
                                        cc.payments.subscribeError = true;
                                    }
                                }
                            }
                        }
                        catch (e) {
                            cc.ui.showModalErrorMessage(cc_resx_TR_AUTH_AJAX_ERROR + ":" + e);
                            cc.payments.subscribeError = true;
                        }
                    },
                    function() {
                        cc.payments.subscribeInProcess = false;

                        if (cc.payments.subscribeError === false && cc.payments.subscribeRedirect === false) {
                            // Hide Please Wait Overlay
                            cc.ui.hideModalErrorMessage();
                            cc.payments.subscribeError = false;
                        }
                    });
                }
            }
            catch (e) {
                cc.ui.showModalErrorMessage(cc_resx_TR_AUTH_AJAX_ERROR_REFRESH);
                cc.payments.subscribeInProcess = false;
            }

            return false;
        }

    };

} ();
