
//  CONCEITO - CHECKOUT
//  -------------------

// CONSTRUCTOR
var CHKT                 = CHKT || {};

CHKT = ( function( Chckt )
{

    //=====================
    // VARS
    //=====================
    var $cont   = $( '.checkout' );

    var $steps      = $cont.find( '.steps' );
    var $order      = $cont.find( '.myOrder' );
    var $ship       = $cont.find( '.myShipping' );
    var $pay        = $cont.find( '.myPayment' );

    var _steps      = 1;
    var _formCard   = false;
    var _typeShip   = 'economica';
    var _frete      = '0,00';
    var _produtos   = '0,00';

    var _typePay    = null;


    //=====================
    // PUBLIC
    //=====================
    Chckt.init = function()
    {
        if ($cont.length) start();
    }

    //=====================
    // PRIVATE
    //=====================

    function start()
    {
        $cont.find( '.openModal' ).each( setModal );

        $steps.find( '.next' ).on( 'click', goAddress );
        $order.find( '.next' ).on( 'click', goAddress );
        $order.find( '.controls' ).find( '.minus' ).on( 'click', clickMinus );
        $order.find( '.controls' ).find( '.plus' ).on( 'click', clickPlus );
        $order.find( '.delete' ).on( 'click', showConfirm );
        $order.find( '.confirm' ).find( '.bt-no' ).on( 'click', hideConfirm );

        $ship.find( '.back' ).on( 'click', backOrder );
        $ship.find( '.payment' ).on( 'click', goPay );
        $ship.find( '.typeShip' ).on( 'click', choiceShip );

        $pay.find( '.card' ).on( 'click', showFormCard );
        $pay.find( '.bank' ).on( 'click', hideFormCard );

        $pay.find( 'input[type="checkbox"]' ).on( 'click', showAddressPay );
        $pay.find( '.okCupom' ).on( 'click', sendCupom );

        $( window ).bind( 'CHECKOUT-ADDRESS', updateAddress );

        $pay.find( '.finished' ).on( 'click', finishedPay );

    }

    // SET MODALL
    function setModal()
    {
        var $this = $( this );
        var _href = $( this ).attr('href');

        app.components.modals.open( $this, _href );
    }

    //=====================
    // ORDER
    //=====================
    function goAddress()
    {
        $order.fadeOut( 400 );

        $steps.find( '.next' ).fadeOut( 400 );

        setTimeout( function(){ $ship.slideDown() },400 )
        _steps = 2;

        $steps.find( 'li' ).eq(0).removeClass( 'active' );
        $steps.find( 'li' ).eq(1).addClass( 'active' );

    }

    function createCheckBox()
    {
        $ship.find( '.express' ).html();

        var _express = "<span><b>Express</b> &nbsp;&nbsp;&nbsp;<b>$ "+exPrice+"</b></span>";
        _express += "<input type='radio' name='type_delive' value='expressa' data-price='"+exPrice+"' data-total='"+exTotal+"' data-parce='"+exParce+"'>";
        _express += "<em><i></i></em>";
        $ship.find( '.express' ).html( _express );

        $ship.find( '.economic' ).html();
        var _economic = "<span><b>Standard</b> &nbsp;&nbsp;&nbsp;<b>$ "+ecPrice+"</b></span>";
        _economic += "<input type='radio' name='type_delive' checked value='economica' data-price='"+ecPrice+"' data-total='"+ecTotal+"' data-parce='"+ecParce+"'>";
        _economic += "<em><i></i></em>";
        $ship.find( '.economic' ).append( _economic );

    }

    function removeCheckBox()
    {
        $ship.find( '.express' ).html( '' );
        $ship.find( '.economic' ).html( '' );
        $ship.find( '.typeShip' ).off();
    }

    function clickPlus()
    {
        var type        = 'soma';
        var id          = $( this ).parent().data( 'id' );
        var stock       = $( this ).parent().data( 'stock' );
        var $number     = $( this ).parent().parent().find( '.number' );
        var _value      = parseInt( $number.html() ) + 1;
        var _subObj     = $( this ).parent().parent().parent().parent().find( '.subtotal' );

        if( _value <= stock )
        {
            $number.html( _value );
            updateCheckout( id, type, _subObj );
        }
        else
        {
            app.components.modals.openMessage( 'Product sold out' );
        }

    }

    function clickMinus()
    {
        var type        = 'sub';
        var id          = $( this ).parent().data( 'id' );
        var $number     = $( this ).parent().parent().find( '.number' );
        var number      = parseInt( $number.html()) - 1;
        var _value      = number > 1 ? number : 1;
        var _subObj     = $( this ).parent().parent().parent().parent().find( '.subtotal' );

        $number.html( _value );

        updateCheckout( id, type, _subObj );
    }

    function updateCheckout( _id, _type, _sub )
    {

            var id      = _id;
            var type    = _type;

            $.post( GLOBAL_URL+'checkout/update_checkout/', { id: id, tipo: type  },

                function( status )
                {
                    var obj                     = jQuery.parseJSON( status );
                    var qtd                     = obj.total_items;
                    var price                   = obj.total_price;
                    var parce                   = obj.parcela;
                    var sub                     = obj.subtotal;

                    $order.find( '.total' ).html( '$ ' + price + ' AUD');
                    $order.find( '.parcela' ).html( '' );

                    var econimic    = obj.economica;
                    var ecDate      = econimic.prazo;
                    var ecPrice     = econimic.valor;
                    var ecTotal     = econimic.total;
                    var ecParce     = econimic.parcela;

                    var express     = obj.expressa;
                    var exDate      = express.prazo;
                    var exPrice     = express.valor;
                    var exTotal     = express.total;
                    var exParce     = express.parcela;

                    $ship.find( '.total_produtos' ).html( '$ '+ price + ' AUD');
                    $ship.find( '.total_ordem' ).html( '$ '+ ecTotal + ' AUD');
                    $ship.find( '.total_frete' ).html( '$ '+ ecPrice + ' AUD' );
                    $ship.find( '.parcela' ).html( '' );


                    $ship.find( '.express' ).html();
                    var _express = "<span><b>Express</b> &nbsp;&nbsp;&nbsp;<b>$ "+exPrice+" AUD</b></span>";
                    _express += "<input type='radio' name='type_delive' value='expressa' data-price='"+exPrice+"' data-total='"+exTotal+"' data-parce='"+exParce+"'>";
                    _express += "<em><i></i></em>";
                    $ship.find( '.express' ).html( _express );

                    $ship.find( '.economic' ).html();
                    var _economic = "<span><b>Standard</b> &nbsp;&nbsp;&nbsp;<b>$ "+ecPrice+" AUD</b></span>";
                    _economic += "<input type='radio' name='type_delive' checked value='economica' data-price='"+ecPrice+"' data-total='"+ecTotal+"' data-parce='"+ecParce+"'>";
                    _economic += "<em><i></i></em>";
                    $ship.find( '.economic' ).html( _economic );

                    $ship.find( '.typeShip' ).on( 'click', choiceShip );

                    $pay.find( '.total_produtos' ).html( '$ '+ price + ' AUD' );
                    $pay.find( '.total_ordem' ).html( '$ '+ ecTotal + ' AUD' );
                    $pay.find( '.total_frete' ).html( '$ '+ ecPrice + ' AUD');
                    $pay.find( '.parcela' ).html( '' );

                    $( _sub ).html( '$ ' + sub + ' AUD');

                    $( '#id_address' ).trigger('change');

                }
            ).fail( function()
            {
                app.components.modals.openMessageOps();
            } );
    }

    function showConfirm(){

        $( this ).parent().parent().find( '.confirm' ).fadeIn();
    }

    function hideConfirm()
    {

        $( this ).parent().parent().fadeOut();
    }

    //=====================
    // SHIPMENT
    //=====================
    $( '#id_address' ).change( function()
    {
        var _val = $( this ).val();

        $.post( GLOBAL_URL + 'checkout/change_address/' + _val,

            function( status ) {

                var obj         = jQuery.parseJSON( status );
                var address     = obj.endereco;
                var ship        = obj.frete;

                var econimic    = ship.economica;
                var ecDate      = econimic.prazo;
                var ecPrice     = econimic.valor;
                var ecTotal     = econimic.total;
                var ecParce     = econimic.parcela;

                var express     = ship.expressa;
                var exDate      = express.prazo;
                var exPrice     = express.valor;
                var exTotal     = express.total;
                var exParce     = express.parcela;

                $ship.find( '.updateAddress' ).find('a').remove();
                $ship.find( '.updateAddress' ).append( '<a href="' + GLOBAL_URL + 'newAddress/' + _val + '/checkout_address" class="openModal fancybox.iframe">Change address information</a>' )
                $ship.find( '.txt-endereco p' ).html( obj.endereco );

                $cont.find( '.openModal' ).each( setModal );

                $ship.find( '.express' ).html();
                var _express = "<span><b>Express</b> - <b>$ " + exPrice + " AUD</b></span>";
                _express += "<input type='radio' name='type_delive' value='expressa' data-price='" + exPrice + "' data-total='"+exTotal+"' data-parce='"+exParce+"'>";
                _express += "<em><i></i></em>";
                $ship.find( '.express' ).html( _express );
                if (exDate == 0) {
                    $ship.find( '.express' ).hide();
                }else{
                    $ship.find( '.express' ).show();
                }

                $ship.find( '.economic' ).html();
                var price_type_shipping = ecPrice > 0 ? '$ ' + ecPrice : 'Free shipping';
                var _economic = "<span><b>Standard</b> - <b>" + price_type_shipping + " AUD</b></span>";
                _economic += "<input type='radio' name='type_delive' value='economica' data-price='"+ecPrice+"' data-total='"+ecTotal+"' data-parce='"+ecParce+"'>";
                _economic += "<em><i></i></em>";
                $ship.find( '.economic' ).html( _economic );

                if( _typeShip == 'economica' )
                {
                    $ship.find( '.total_frete' ).html( '$ '+ ecPrice + ' AUD');
                    $ship.find( '.total_ordem' ).html( '$ '+ ecTotal + ' AUD' );
                    $ship.find( '.parcela' ).html( '' );

                    $ship.find( '.economic' ).find( 'input' ).prop( 'checked', true );

                    // $pay.find( '.total_produtos' ).html( '$ '+ecTotal );
                    $pay.find( '.total_ordem' ).html( '$ '+ ecTotal + ' AUD' );
                    $pay.find( '.total_frete' ).html( '$ '+ ecPrice + ' AUD' );
                    $pay.find( '.parcela' ).html( '' );

                    _frete = ecParce;
                }
                else
                {
                    $ship.find( '.total_frete' ).html( '$ '+ exPrice );
                    $ship.find( '.total_ordem' ).html( '$ '+ exTotal );
                    $ship.find( '.parcela' ).html( '' );

                    $ship.find( '.express' ).find( 'input' ).prop( 'checked', true );

                    // $pay.find( '.total_produtos' ).html( '$ '+exTotal );
                    $pay.find( '.total_ordem' ).html( '$ '+ exTotal );
                    $pay.find( '.total_frete' ).html( '$ '+ xcPrice );
                    $pay.find( '.parcela' ).html( '' );

                    _frete = exParce;
                }

            }
            ).fail( function()
            {
                app.components.modals.openMessageOps();
            } );

    });

    function backOrder()
    {
        $ship.fadeOut( 400 );
        setTimeout( function(){ $order.slideDown() },400 )
        _steps = 1;

        $steps.find( 'li' ).eq(1).removeClass( 'active' );
        $steps.find( 'li' ).eq(0).addClass( 'active' );

        // removeCheckBox();
    }

    function goPay()
    {
        var selected_option = $ship.find( '#id_address' ).val();

        if( selected_option != null )
        {
            if( _typeShip )
            {
                $ship.fadeOut( 400 );

                $cont.find( '.loading' ).css('display','');

                createOrder(true);

            }
            else
            {
                app.components.modals.openMessage( 'Selecione o tipo de entrega.' );
            }

        }
        else
        {
            app.components.modals.openMessage( 'Cadastre um endereço para prosseguir.' );
        }

    }

    function choiceShip()
    {
        _typeShip = $( this ).find( 'input' ).val();

        var _price = $( this ).find( 'input' ).data( 'price' );
        var _total = $( this ).find( 'input' ).data( 'total' );
        var _parce = $( this ).find( 'input' ).data( 'parce' );


        $ship.find( '.total_frete' ).html( '$ '+ _price );
        $ship.find( '.total_ordem' ).html( '$ '+ _total );
        $ship.find( '.parcela' ).html( '' );

        $pay.find( '.total_frete' ).html( '$ '+ _price );
        $pay.find( '.total_ordem' ).html( '$ '+ _total );
        $pay.find( '.parcela' ).html( '' );

        // $pay.find( '.total_produtos' ).html( '$ ' + _parce );


    }

    function updateAddress()
    {
        $.post( GLOBAL_URL+'checkout/add_new_address/',

            function( data ) {

                var checkout_deliver    = $('.myShipping');
                var html_select_address = '';
                var html_txt_endereco   = '';

                checkout_deliver.find('select[name="id_address"]').html('');

                $.each(data.addresses, function( i, val ) {
                    var selected = data.address_selected == i ? 'selected="selected"' : '';
                    html_select_address += '<option value="'+i+'" '+selected+'>'+val+'</option>';
                });

                checkout_deliver.find('select[name="id_address"]').html (html_select_address );


                html_txt_endereco = data.address_first.address + '<br>';

                if(data.address_first.city != null){
                    html_txt_endereco += data.address_first.city;
                }

                if(data.address_first.state != null){
                    html_txt_endereco += ' - ' + data.address_first.state + '<br>';
                }

                if(data.address_first.zipcode != null){
                    html_txt_endereco += data.address_first.zipcode;
                }

                if(data.address_first.country != null){
                    html_txt_endereco += ' - ' + data.address_first.country;
                }

                $ship.find( '.txt-endereco' ).html();
                $ship.find( '.txt-endereco' ).html( '<p>'+html_txt_endereco+'</p>' );

                $ship.find('.updateAddress').find('a').remove();
                $ship.find('.updateAddress').append( '<a href="'+GLOBAL_URL+'newAddress/'+data.address_selected+'/checkout_address" class="openModal fancybox.iframe">Change address information</a>' )
                $cont.find( '.openModal' ).each( setModal );


                var ship        = data.frete;
                var econimic    = ship.economica;
                var ecDate      = econimic.prazo;
                var ecPrice     = econimic.valor;
                var ecTotal     = econimic.total;
                var ecParce     = econimic.parcela;

                var express     = ship.expressa;
                var exDate      = express.prazo;
                var exPrice     = express.valor;
                var exTotal     = express.total;
                var exParce     = express.parcela;

                $ship.find( '.express' ).html();
                var _express = "<span><b>Express</b> &nbsp;&nbsp;&nbsp;<b>$ "+exPrice+"</b></span>";
                _express += "<input type='radio' name='type_delive' value='expressa' data-price='"+exPrice+"' data-total='"+exTotal+"' data-parce='"+exParce+"'>";
                _express += "<em><i></i></em>";
                $ship.find( '.express' ).html( _express );
                if (exDate == 0) {
                    $ship.find( '.express' ).hide();
                }else{
                    $ship.find( '.express' ).show();
                }

                $ship.find( '.economic' ).html();
                var _economic = "<span><b>Standard</b> &nbsp;&nbsp;&nbsp;<b>$ "+ecPrice+"</b></span>";
                _economic += "<input type='radio' name='type_delive' value='economica' data-price='"+ecPrice+"' data-total='"+ecTotal+"' data-parce='"+ecParce+"'>";
                _economic += "<em><i></i></em>";
                $ship.find( '.economic' ).html( _economic );

                if( _typeShip == 'economica' )
                {
                    $ship.find( '.total_frete' ).html( '$ '+ ecPrice );
                    $ship.find( '.total_ordem' ).html( '$ '+ ecTotal );
                    $ship.find( '.parcela' ).html( '' );

                    $ship.find( '.economic' ).find( 'input' ).prop( 'checked', true );

                    $pay.find( '.total_frete' ).html( '$ '+ ecPrice );
                    $pay.find( '.total_ordem' ).html( '$ '+ ecTotal );
                    $pay.find( '.parcela' ).html( '' );

                    _frete = ecParce;
                }
                else
                {
                    $ship.find( '.total_frete' ).html( '$ '+ exPrice );
                    $ship.find( '.total_ordem' ).html( '$ '+ exTotal );
                    $ship.find( '.parcela' ).html( '' );

                    $ship.find( '.express' ).find( 'input' ).prop( 'checked', true );

                    $pay.find( '.total_frete' ).html( '$ '+ exPrice );
                    $pay.find( '.total_ordem' ).html( '$ '+ exTotal );
                    $pay.find( '.parcela' ).html( '' );

                    _frete = exParce;
                }

                $('.newAddress .bt-close').trigger('click');
                $( '#id_address' ).trigger('change');

            },'json').fail( function()
            {
                app.components.modals.openMessageOps();
            } );
    }

    //=====================
    // PAYMENT
    //=====================

    function showFormCard()
    {
        $( '#payment_type' ).val('credito_a_vista');
        _typePay = 'cartaoCredito';
        $pay.find( '.formInputs' ).slideDown();
        $pay.find( '.barCode' ).slideUp();

        $pay.find( '.finished' ).fadeIn();
        $pay.find( '.parcela' ).show();
    }

    function hideFormCard()
    {
        $( '#payment_type' ).val('itau_shopline');
        _typePay = 'boleto';
        $pay.find( '.formInputs' ).slideUp();
        $pay.find( '.barCode' ).slideDown();

        $pay.find( '.finished' ).fadeIn();

        $pay.find( '.parcela' ).hide();
    }

    function showAddressPay()
    {
        if( !this.toggle )
        {
            this.toggle = true;
            $pay.find( '.addressPay' ).slideDown().css( { 'diplay':'table' } );
        }
        else
        {
            this.toggle = false;
            $pay.find( '.addressPay' ).slideUp();
        }
    }

    function sendCupom()
    {
        var idOrder     = $pay.find( '#order_id' ).val();
        var numberCupom = $pay.find( '#numberCupon' ).val();

        $.post( GLOBAL_URL+'checkout/check_cupon/', { id: idOrder, cupon: numberCupom },

            function( status ){

                var obj = jQuery.parseJSON( status );

                if( !obj.result )
                {
                    app.components.modals.openMessage( obj.message );
                }

                var tProdutos   = obj.total_produtos;
                var tFrete      = obj.total_frete;
                var tCupom      = obj.total_cupom;
                var tOrdem      = obj.total_ordem;
                var tParcela    = obj.parcela;
                var tNCupom     = obj.new_cupom;

                var parcelaSelected = $( '.act-cartao-parcelas' ).find('option:selected').val();
                $( '.act-cartao-parcelas' ).find('option').remove();

                for ( var chave in obj.parcelas_arr ){
                    $( '.act-cartao-parcelas' ).append('<option value="'+chave+'">'+obj.parcelas_arr[chave] +'</option>')
                }
                $( '.act-cartao-parcelas' ).val(parcelaSelected);

                $pay.find( '.total_produtos' ).html( '$ '+tProdutos );
                $pay.find( '.total_frete' ).html( '$ '+tFrete );
                $pay.find( '.total_cupom' ).html( '$ '+tCupom );
                $pay.find( '.total_ordem' ).html( '$ '+tOrdem );
                $pay.find( '.parcela' ).html( '' );

                if( parseInt(tNCupom) > 0 )
                {
                    $( '.boxInfos' ).find( '.col-right' ).append( '<p style="line-height: 34px;">New Cupom:</p>' );
                    $( '.boxInfos' ).find( '.colValue' ).append( '<p style="line-height: 34px;" class="new_cupom"> '+tNCupom+' </p>' );
                }

                if( tOrdem == '0,00' )
                {
                    $('#payment_type').val('gift_card');

                     _typePay = 'gift_card';

                    $pay.find( '.formInputs' ).slideUp();
                    $pay.find( '.barCode' ).slideUp();
                    $pay.find( '.finished' ).fadeIn();
                    $pay.find( '.parcela' ).hide();

                    $pay.find( '.hint' ).fadeIn();
                }

            }
        ).fail( function()
        {
            app.components.modals.openMessageOps();
        } );
    }

    function createOrder(primeiro)
    {

    	if(primeiro){
    		var return_post = $.post(GLOBAL_AJAX + 'checkout/create_order/',{ id_address: $('#id_address').val(), type_deliver: _typeShip },

    	            function( data )
    	            {
    	                if( data.erro == false ){

    	                    // $('.total-produtos').html( data.total_produtos );
    	                    $('.total-frete').html( data.total_frete );
    	                    $('.total-order').html( data.total_ordem) ;
    	                    $( '#order_id' ).val( data.order_id );

    	                    var tProdutos   = data.total_produtos;
    	                    var tFrete      = data.total_frete;
    	                    var tOrdem      = data.total_ordem;
    	                    var tParcela    = data.parcela;
    	                    var tNCupom     = data.new_cupom;

    	                    // $pay.find( '.total_produtos' ).html( '$ ' + _produtos );
    	                    $pay.find( '.total_frete' ).html( '$ ' + tFrete + ' AUD');
    	                    $pay.find( '.total_ordem' ).html( '$ ' + tOrdem + ' AUD' );
    	                    // $pay.find( '.parcela' ).html( 'ou até ' + _frete );

    	                    // console.log( data );

    	                    for ( var chave in data.parcelas ){
    	                        //$( '.act-cartao-parcelas' ).append('<option value="'+chave+'">'+chave+'x de $ '+ data.parcelas[chave] +'</option>')
    	                    }

    	                } else {

    	                    $( window ).trigger( 'STOCK-FULL' );

    	                    app.components.modals.openMessageOk( data.mensagem );
    	                }

    	        },'json').fail( function()
    	        {
    	            app.components.modals.openMessageOps();
    	        } )
    	        .success(function() {
    	        	createOrder();
    	        });
    	}else{

    		setTimeout( function(){ $pay.slideDown() },400 )
            _steps = 3;

    		$cont.find( '.loading' ).css('display','none');
    		$steps.find( 'li' ).eq(1).removeClass( 'active' );
            $steps.find( 'li' ).eq(2).addClass( 'active' );

          var params = {};

            if (location.search) {
                var parts = location.search.substring(1).split('&');

                for (var i = 0; i < parts.length; i++) {
                    var nv = parts[i].split('=');
                    if (!nv[0]) continue;
                    params[nv[0]] = nv[1] || true;
                }
            }

            if(params.teste == "testeupp"){
            	$('input[name="bandeira_cartao"]').val('visa');
            	$('input[name="cartao_numero"]').val('4929697394152303');
            	$('input[name="nome_titular_cartao"]').val('TESTE');
            	$('select[name="cartao_validade_mes"]').val('02');
            	$('select[name="cartao_validade_ano"]').val('2019');
            	$('input[name="cartao_cvv"]').val('123');
            }



    	}

    }

    $('input[name="cartao_numero"]').keyup(function(){
        var valor = $(this).val();
        getCreditCardLabel( valor );
    });

    function getCreditCardLabel( _cardNumber )
    {
        var cardNumber = _cardNumber.replace(/\s/g,'');

        var regexVisa           = /^4[0-9]{12}(?:[0-9]{3})?/;
        var regexMaster         = /^5[1-5][0-9]{14}/;
        var regexAmex           = /^3[47][0-9]{13}/;
        var regexDiners         = /^3(?:0[0-5]|[68][0-9])[0-9]{11}/;
        var regexDiscover       = /^6(?:011|5[0-9]{2})[0-9]{12}/;
        var regexJCB            = /^(?:2131|1800|35\d{3})\d{11}/;
        var regexElo            = /^((((636368)|(438935)|(504175)|(451416)|(636297))\d{0,10})|((5067)|(4576)|(4011))\d{0,12})$/;
        var regexHipercard      = /^(606282\d{10}(\d{3})?)|(3841\d{15})$/;
        var regexAura           = /^50?/;

        var cartao = null;

        if( regexVisa.test(cardNumber)){
            cartao = 'visa';
        }

        if(regexMaster.test(cardNumber)){
            cartao = 'mastercard';
        }

        if(regexAmex.test(cardNumber)){
           // cartao = 'amex';
        }

        if(regexDiners.test(cardNumber)){
            cartao = 'diners';
        }



        switch( cartao )
        {
            case 'visa':
                $pay.find( '.visa' ).removeClass( 'deselectCard' ).addClass( 'selectCard' );
                $pay.find( '.master' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
                //$pay.find( '.america' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
                $pay.find( '.dinners' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
            break;
            case 'mastercard':
                $pay.find( '.visa' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
                $pay.find( '.master' ).removeClass( 'deselectCard' ).addClass( 'selectCard' );
                //$pay.find( '.america' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
                $pay.find( '.dinners' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
            break;
            case 'amex':
                $pay.find( '.visa' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
                $pay.find( '.master' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
                $pay.find( '.america' ).removeClass( 'deselectCard' ).addClass( 'selectCard' );
                $pay.find( '.dinners' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
            break;
            case 'diners':
                $pay.find( '.visa' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
                $pay.find( '.master' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
                //$pay.find( '.america' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
                $pay.find( '.dinners' ).removeClass( 'deselectCard' ).addClass( 'selectCard' );
            break;
            default:
                $pay.find( '.visa' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
                $pay.find( '.master' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
                //$pay.find( '.america' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
                $pay.find( '.dinners' ).removeClass( 'selectCard' ).addClass( 'deselectCard' );
            break;
        }


        return $('input[name="bandeira_cartao"]').val(cartao);

    }

    function finishedPay()
    {
        if( _typePay == 'boleto' || _typePay == 'gift_card' )
        {
            $( "#formPayment" )[0].submit();
        }
    }

    return Chckt;

}( CHKT || {} ));

// INIT
$( CHKT.init );
