appName = {};
appName.v = {};
appName.resend = {
    spentCards: [],
    cardIDs: [],
    checkedTotal: 0,
    load: function() {

        $('tr').each(function(index) {
            if (index) {
                var order = $(this).find('.order').text();
                var ecard = $(this).find('.ecard').text();
                $(this).find('.resend').html('<input type="checkbox" id="' + ecard + '" class="' + order + ' resendCheckbox"  order="' + order + '" value="" style="width: 20px; height: 20px;">');

                // if ( appName.resend.spentCards.indexOf(ecard) === -1 ) {
                //     appName.resend.spentCards.push( ecard );
                // } else {
                //     $(this).css( 'display', 'none' );
                // }


            }
        });




        
        $(document).ready(function() {
            $('.resendCheckbox').change(function() {
                var order = $(this).attr('order');
                if ($(this).is(':checked')) {
                    if (appName.resend.checkedCound() === 1) {
                        $('.' + order).prop("checked", true);
                        appName.resend.hideAllBut(order);
                        appName.resend.showButtonPallet(order)
                    }
                } else {
                    if (appName.resend.checkedCound() === 0) {
                        appName.resend.showAllCheckboxes();

                    }
                }
            });
        });

        // appName.resend.load();
    }, //==============================================
    addToTotal: function() {
        appName.resend.checkedTotal++;
        // appName.resend.addToTotal();
    }, //==============================================
    checkedCound: function() {
        appName.resend.checkedTotal = 0;
        $('.resendCheckbox').each(function(index) {
            if ($(this).is(':checked')) { appName.resend.addToTotal(); }
        });
        console.log(appName.resend.checkedTotal);
        return appName.resend.checkedTotal;
        // appName.resend.checkedCound();
    }, //==============================================
    hideAllBut: function(omit) {
        $('.resendCheckbox').each(function(index) {
            var order = $(this).attr('order');
            if (omit !== order) {
                $(this).css('display', 'none');
            }
        });
        // appName.resend.hideAllBut();
    }, //==============================================
    showAllCheckboxes: function() {
        $('#selectsomething').remove();
        $('.resendCheckbox').each(function(index) {
            $(this).css('display', 'inline-block');
        });
        // appName.resend.showAllCheckboxes();
    }, //==============================================
    uncheckAll: function() {
        appName.resend.showAllCheckboxes();
        $('.resendCheckbox').prop("checked", false);
        // appName.resend.uncheckAll();
    }, //==============================================
    openEditEmailWindow: function() {
        $('.resendCheckbox').each(function(index) {
            if ($(this).is(':checked')) {
                $('#selectsomething').remove();
                certificate.order.resendEmailStart($(this).parent().parent().attr('record'));
                return false;
            }
        });
        appName.resend.buildCardID();
        setTimeout(function() {   $('#selectsomething button').attr( 'onclick', 'appName.resend.resendEmailSend();' );    }, 500);
        setTimeout(function() {   $('.close').attr( 'onclick', 'appName.resend.uncheckAll();$(\'#selectsomething\').remove();' );    }, 500);
        // setTimeout(function() {   $('#selectsomething button').attr( 'onclick', 'appName.resend.resendEmailSend();' );  $('#resendToEmail').val('scott.reph@gmail.com');  }, 500);
        // appName.resend.openEditEmailWindow();
    }, //==============================================
    showButtonPallet: function(order) {
        $('#selectsomething').remove();
        $('body').append('<div id="selectsomething"><span class="close" onClick="$(\'#selectsomething\').remove();appName.resend.uncheckAll();" style="padding:20px"> ( X ) </span><div id="selectsomethinginner"><span id="selectheader"> Click Here To Drag </span><hr /><div class="items"></div></div></div>');
        $('#selectsomething .items').append('<div onclick="location.href=\'copyCertificate.php?record='+order+'\';" style="margin:10px;" > Add Certificate </div>');
        $('#selectsomething').draggable({ handle: '#selectheader' });
        $('#selectsomething .items').append('<div onclick="appName.resend.uncheckAll(); $(\'#selectsomething\').remove();" style="margin:10px;" > Uncheck All </div>\
<div onclick="appName.resend.openEditEmailWindow();" style="margin:10px;" > Ready to edit send form </div>');




        $('#selectsomething').mouseup(function() { window.getSelection().removeAllRanges(); });
        $('#selectsomething').mouseover(function() { window.getSelection().removeAllRanges(); });
        // appName.resend.showButtonPallet();
    }, //==============================================
    buildCardID: function() {
        appName.resend.cardIDs = [];
        $('.resendCheckbox').each(function(index) {
            if ($(this).is(':checked')) {
                var order = $(this).attr('order');
                var card = $(this).attr('id');
                console.log('buildCardID:', order, card);
                appName.resend.extractCardID(order, card);
            }
        });
        appName.resend.cardIDs.sort();
        console.log('appName.resend.cardIDs:', appName.resend.cardIDs.join(','), appName.resend.cardIDs);

        // setTimeout(function(){ console.log( 'appName.resend.cardIDs:', appName.resend.cardIDs ); }, 500);

        // appName.resend.buildCardID();
    }, //==============================================
    extractCardID: function(order, card) {
        console.log('extractCardID:', order, card);
        var one = card.replace(order, '');
        var two = Number(one);
        var payload = two.toString();
        if (appName.resend.cardIDs.indexOf(payload) === -1) {
            appName.resend.cardIDs.push(payload);
        }
        // appName.resend.extractCardID();
    }, //==============================================
    resendEmailSend: function() {
        if (!certificate.order.resendEmailValidate()) {
            return false;
        }
        appName.resend.buildCardID();
        var data = certificate.v.resendEmailData;
        data['form'] = {};
        data.form['first'] = $('#resendToFirst').val();
        data.form['last'] = $('#resendToLast').val();
        data.form['email'] = $('#resendToEmail').val();
        data.form['from'] = $('#resendFrom').val();
        data.form['message'] = $('#resendMessage').val();
        data.form['cards'] = appName.resend.cardIDs.join(',');
        console.log(data);
        // console.log( data );
        $('#JSON').val(JSON.stringify(data));
        // console.log( JSON.stringify(data) );
        $('#selectsomething .items').html('');
        $('#selectsomething .items').append('<img src="images/loader.gif"> Sending ');


        $.post(
            "processEmailResendNew.php", {
                'JSON': JSON.stringify(data)
            },
            function(data) {


                console.log(data);
                $('#selectsomething .items').html('');
                if (data.indexOf('success') > -1) {
                    $('#selectsomething .items').append('<h4 id="resendResults">Success</h4>');
                    $('#resendResults').css('color', 'forestgreen');
                } else {
                    $('#selectsomething .items').append('<h4 id="resendResults">Failure</h4>');
                    $('#resendResults').css('color', 'crimson');
                }


            }
        );
        // appName.resend.resendEmailSend();
    }, //==============================================
    emailPreview: function() {

        if (!certificate.order.resendEmailValidate()) {
            return false;
        }
        appName.resend.buildCardID();
        var data = certificate.v.resendEmailData;
        data['form'] = {};
        data.form['first'] = $('#resendToFirst').val();
        data.form['last'] = $('#resendToLast').val();
        data.form['email'] = $('#resendToEmail').val();
        data.form['from'] = $('#resendFrom').val();
        data.form['message'] = $('#resendMessage').val();
        data.form['cards'] = appName.resend.cardIDs.join(',');
        console.log(data);
        // console.log( data );
        $('#JSON').val(JSON.stringify(data));
        // console.log( JSON.stringify(data) );
        $('#selectsomething .items').html('');
        $('#selectsomething .items').append('<img src="images/loader.gif"> Sending ');


        $.post(
            "emailPreview_resend.php", {
                'JSON': JSON.stringify(data)
            },
            function(data) {


                        var html = '<div id="loadsomething"><span class="close" onClick="$(\'#loadsomething\').remove();$(\'#loadCloseBackground\').remove();">X</span><div id="loadsomethinginner"><span id="loadheader"> Email Preview </span><hr /><div class="items"> [HERE] </div></div></div>';

                        $('body').append( html.replace( '[HERE]', data ) );

                        $('#loadsomething').css( 'width', '80%' );
                        $('#loadsomething').css( 'height', '80%' );
                        $('#loadsomething').css( 'overflow', 'auto' );

                        $('body').prepend('<div id="loadCloseBackground"></div>');
                        setTimeout(function() {
                            $('#loadCloseBackground').click(function() {
                                $('#loadsomething').remove();
                                $('#loadCloseBackground').remove();
                            });
                        }, 200);


            }
        );






        // appName.resend.emailPreview();
    }, //==============================================
    nullFunction: function() {
        try {} catch (err) {}
        setTimeout(function() {}, 500);
        // appName.resend.nullFunction();
    } //==============================================
};

$(document).ready(function() {

    setTimeout(function() {  appName.resend.load();  }, 10);



});

