$(document).ready(function() {
    //$('#sfSubmit').attr('disabled', 'disabled');

    $('#sfEmail').focus(function() {
        if ($(this).val() == 'alamat email anda')
            $(this).val('');
    });
    $('#sfEmail').blur(function() {
        if ($(this).val() == '')
            $(this).val('alamat email anda');
    });


    $('#supportForm').submit(function() {
        $('body').css('cursor', 'wait');

        var email = $('#sfEmail').val();
        $.get('/pledges/check_email', {email: email}, function(data) {
            data = parseInt(jQuery.trim(data));
            if (isNaN(data)) {
                $('#supportFormDiv').html('<p>tunggu sebentar ...</p>');
                $.post('/pledges/add', {"_method": 'POST', "data[Pledge][email]": email}, function(data) {
                    $('#supportFormDiv').html(data);
                    $.get('/pledges/total_pledges', function(data) {
                        $('#digitCounterBox').html(data);
                    });
                });
                
            } else if (data < 0) {
                $('#supportFormDiv').html('<p>tunggu sebentar ...</p>');
                $.post('/pledges/reactivate', {"_method": 'POST', "data[Pledge][email]": email}, function(data) {
                    $('#supportFormDiv').html(data);
                });
                
            } else {
                alert('email telah ada di sistem.');
            }
        });

        $('body').css('cursor', 'auto');
        return false;
    });

    $('#unsubscribe-link').click(function(e) {
        e.preventDefault();
        $('#unsubscribe-modalbox').modal(); 
    });
    
    $('#unsubForm').submit(function() {
        var email = $('#unsubEmail').val();

        $.get('/pledges/check_email', {email: email}, function(data) {
            data = jQuery.trim(data);
            if (data > 0) {
                $('#unsubscribe-modalbox').html('tunggu sebentar ...');
                $.post('/pledges/unsubscribe', {"_method": 'POST', "data[Pledge][email]": email}, function(data) {
                    $.modal.close();
                });
            } else {
                alert('email tidak ada di sistem.');
            }
        });

        return false;
    });


    $('#resendvalidation-link').click(function(e) {
        e.preventDefault();
        $('#resendvalidation-modalbox').modal();
    });

    $('#resendForm').submit(function() {
        var email = $('#resendEmail').val();

        $.get('/pledges/check_email', {email: email}, function(data) {
            data = jQuery.trim(data);
            if (data > 0) {
                $('#resendvalidation-modalbox').html('tunggu sebentar ...');
                $.post('/pledges/resend_validation', {"_method": 'POST', "data[Pledge][email]": email}, function(data) {
                    $.modal.close();
                });
            } else {
                alert('email tidak ada di sistem.');
            }
        });

        return false;
    });
});
