$(document).ready(function() {

    var prices = {
        "Regular" : 90.00,
        "Sustaining" : 125.00,
        "Student" : 35.00,
        "Retiree" : 35.00 
    };
    var selected = $('select option:selected').val();
    var price = prices[selected];
    var old_exp = 'N/A';
    
    var date = new Date();
    var this_year = date.getFullYear();
    var username = window.location.search;
    username = username.replace('?username=','');
    username = username.replace('#','');

    $.ajax({
        url: '/include/paypal.php',
        data: 'a=getMemberInfo&user=' + username,
        dataType: 'json',
        type: 'POST',
        success: function(data) {
         if (data == 'false')
            alert('Member not found');
         else  {
           result = data;        
           old_exp = result.old_exp.split(' ');
           old_exp = old_exp[0];
           old_year = old_exp.split('/');
           old_year = old_year[2];
           $('select[name="membership_type"]').find('option[value="' + result.membership_type + '"]').attr("selected","selected");     
           $('.renew .input select').change();
           $('.renew .cost').fadeIn('200');           

         }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('Error: ' + errorThrown);
        }
    });

        $('.renew .input select').change( function() {
            var selected = $(this).val();
            var price = prices[selected];
            var new_exp = (old_year >= this_year) ? parseInt(old_year) + 1 : this_year;
            if (old_year >= this_year)
                exp_html = '<td class="old-exp current">' + old_exp + '&nbsp;&nbsp;&nbsp;<strong>**ACTIVE**</strong></td>'
            else
                exp_html = '<td class="old-exp expired">' + old_exp + '&nbsp;&nbsp;&nbsp;<strong>**NEEDS RENEWAL**</strong></td>'
            if (selected == 'Student')
                var note = '* You must be an actively enrolled full-time student to be eligible.';
            else if (selected == 'Retiree')
                var note = '* You must be at least 65 years old to be eligible.';
            else
                var note = '';            
            var cost_string = [ 
                '<div class="note">' + note + '</div>',
                '<table>',
                    '<tr>',
                        '<td><strong>Annual ' + selected + ' Membership Cost:</strong></td>',
                        '<td>$' + price + '</td>',
                    '</tr>',
                     '<tr>',
                        '<td><strong>Old Expiration:</strong></td>',
                        exp_html,
                    '<tr>',
                    '<tr>',
                        '<td><strong>New Expiration:</strong</td>',
                        '<td>12/31/' + new_exp + '</td>',
                    '</tr>'

            ].join('');            
            $('.cost').html(cost_string);
            $.ajax({
                url: '/include/paypal_session.php',
                data: 'membership_type=' + selected + '&price=' + price + '&type=renew',
                type: 'POST'
            });             
        });
        
        $('.apply .input select').change( function() {
            selected = $(this).val();
            price = prices[selected];
            
            // Add $20 for applicaton fee
            price += 20;
            
            if (selected == 'Student')
                var note = '* You must be an actively enrolled full-time student to be eligible.';
            else if (selected == 'Retiree')
                var note = '* You must be at least 65 years old to be eligible.';
            else
                var note = '';            
            var cost_string = [ 
                '<div class="note">' + note + '</div>',
                '<table>',
                    '<tr>',
                        '<td><strong>Annual ' + selected + ' Membership Cost:</strong></td>',
                        '<td>$' + price + '</td>',
                    '</tr>',
                    '<tr>',
                        '<td><strong>Valid Until:</strong</td>',
                        '<td>12/31/' + this_year + '</td>',
                    '<tr>'
            ].join('');            
            $('.cost').html(cost_string);
            $.ajax({
                url: '/include/paypal_session.php',
                data: 'membership_type=' + selected + '&price=' + price + '&type=renew',
                type: 'POST'
            });             
        });

        $('.paypal-button').live('click',  function() {
            $('.loading-bar').fadeIn('1000');
        });
                
        // Fire the event on load
        $('select').change();    
        
    });


