// JScript File
Type.registerNamespace('ActiveBets');

var $ShowPaymentMethodForm = function(id, td) {
    ActiveBets.PaymentMethod.show_form(id, td);
}

var $ClosePaymentForm = function(id, td) {
    ActiveBets.PaymentMethod.close_form(id, td);
}

/*******************  ActiveBets.TransferDirection  ***********************/

ActiveBets.TransferDirection = function() {
    throw Error.invalidOperation();
}
ActiveBets.TransferDirection.prototype = {
    Deposit: 0x00,
    Outpayment: 0x01,
    Both: 0x02
}
ActiveBets.TransferDirection.registerEnum("ActiveBets.TransferDirection", true);


/*******************  ActiveBets.WebPaymentMethod  ***********************/

ActiveBets.WebPaymentMethod = function() {
    ActiveBets.WebPaymentMethod.initializeBase(this);
    ActiveBets.PaymentMethod = this;
    this.loaded_forms_ids = [];
    this.td = 0;
}

ActiveBets.WebPaymentMethod.prototype = {

    show_form: function(id, td) {
        this.td = td;

        for (var i in this.loaded_forms_ids) {
            var item = this.loaded_forms_ids[i];

            var form = $get(String.format('PaymentForm_{0}_{1}', item, 0));
            if (form != null)
                form.style.display = 'none';

            form = $get(String.format('PaymentForm_{0}_{1}', item, 1));
            if (form != null)
                form.style.display = 'none';
        }

        for (var i in this.loaded_forms_ids) {
            var item = this.loaded_forms_ids[i];

            if (item == id) {
                document.getElementById(String.format('PaymentForm_{0}_{1}', id, td)).style.display = '';
                this.get_popup().show();

                var funcName = String.format("Initialize_{0}_{1}", id, td);

                if (this.is_loaded(id) && eval(String.format("typeof({0})", funcName)) != "undefined")
                    eval(funcName + '()');

                return;
            }
        }

        this.send_command(id, td);
    },

    is_loaded: function(id) {
        for (var i in this.loaded_forms_ids)
            if (this.loaded_forms_ids[i] == id)
            return true;

        return false;
    },

    close_form: function(id, td) {
        this.get_popup().hide();
    },

    send_command: function(id, td) {
        var request = new Sys.Net.WebRequest();

        request.set_url(String.localeFormat("{0}PaymentMethod.axd?cmd=get_form&id={1:D}&td={2}&tmp={3}", ActiveBets.App.get_AppVirtualPath(), id, td, new Date()));
        request.set_httpVerb("GET");
        request.add_completed(Function.createDelegate(this, this._send_command_callback));
        request.invoke();
    },

    _send_command_callback: function(executor, eventArgs) {
        var payment_form = Sys.Serialization.JavaScriptSerializer.deserialize(executor.get_responseData());

        Array.add(this.loaded_forms_ids, payment_form.Id);
        //        var paymentForms = $get('ctl00_ctl00_ContentPlaceHolder1_tphCContent_PaymentMethod_PaymentForms').innerHTML += payment_form.Form;
        var paymentForms = GetPaymentForms().innerHTML += payment_form.Form;

        var form = $get(String.format('PaymentForm_{0}_0', payment_form.Id));
        if (form != null)
            form.style.display = 'none';

        form = $get(String.format('PaymentForm_{0}_1', payment_form.Id));
        if (form != null)
            form.style.display = 'none';

        $get(String.format('PaymentForm_{0}_{1}', payment_form.Id, this.td)).style.display = '';

        if (payment_form.Script != null) {
            var headID = document.getElementsByTagName("head")[0];
            var newScript = document.createElement('script');
            newScript.type = 'text/javascript';
            newScript.text = payment_form.Script;
            headID.appendChild(newScript);
        }

        this.get_popup().show();

        var funcName = String.format("Initialize_{0}_{1}", payment_form.Id, this.td);
        if (eval(String.format("typeof({0})", funcName)) != "undefined")
            eval(funcName + '()');
    },

    get_popup: function() {
        return $find('PaymentFormPopup');
    }
}

ActiveBets.WebPaymentMethod.registerClass('ActiveBets.WebPaymentMethod', Sys.Component);
