$(document).ready(function () {
    $("#subject").change(function () {
        if ($(this).children("option:selected").text() == 'Literature') {
            $("#getTitleContainer").show();
            $("#getTitle").removeAttr('disabled');
        } else {
            $("#getTitleContainer").hide();
            $("#getTitle").attr('disabled', true);
        }
    });

    $(':input[name=docfile]').change(function () {
        $('#file_details').css('display', 'block');
    });

    $(':input[name=isClassroomResource]').change(function () {
        $('#classroomOptions').toggle();
    });

    if ($('#subject').children('option:selected').text() == 'Literature') {
        $("#getTitleContainer").show();
        $("#getTitle").attr('disabled', true);
    }

    $('a.revokeFormDoc').click(function() {
        $(this).siblings("form").toggle();
        return false;
    });

    $('input.doRevokeDoc').click(function() {
        if (!confirm("Are you sure you want to revoke earnings for this document?")) {
            return false;
        }

        var comment = $(this).siblings("input.revokeReason").val();
        var documentID = $(this).siblings("input.documentID").val();

        $.get('/api/documents/1.0/revokeEarnings?documentID=' + documentID + '&comment=' + comment, function(response) {
            if ($('error', response).size() == 1) {
                alert($('error', response).text());
                return false;
            }

        });
        $("#revokeEarnings").html('Revoked');
        $("#bonusDocument").hide();
        return false;
    });
});

function rateDocument(score, element)
{
    var documentID = $('input[name=documentID]').val();

    if (!documentID) {
        return false;
    };

    $.get('/api/documents/1.0/rate?rating=' + score + '&documentID=' + documentID, function(response) {
        if ($(response).find('Result').text() == 0) {
            if ($(response).find('Errors').length) {
                $('small.vresults', element).html('');
                $(response).find('Error').each(function () {
                    $('small.vresults', element).append($(this).text());
                });
            } else {
                $('small.vresults', element).html('Sorry, a fatal error occurred.');
            }
        }
    }, 'xml');
    return false;
}

function flagDocument(documentID)
{
    $.get('/api/documents/1.0/flag?documentID=' + documentID, function(response) {
        if ($(response).find('Result').text() == 1) {
            $('#flagResult').html('Thank you.');
        } else {
            if ($(response).find('Errors').length) {
                $('#flagResult').html('');
                $(response).find('Error').each(function () {
                    $('#flagResult').append($(this).text());
                });
            } else {
                $('#flagResult').html('Sorry, a fatal error occurred.');
            }
        }
    }, 'xml');
    return false;
}

function bonusDocument(documentID)
{
    if (!confirm("Are you sure you want to bonus this document?")) {
        return false;
    }
    $.get('/api/documents/1.0/bonus?documentID=' + documentID, function(response) {
        if ($(response).find('Result').text() == 1) {
            $('#bonusDocument').html('Thank you.');
        } else {
             if ($(response).find('Errors').length) {
                $('#bonusDocument').html('');
                $(response).find('Error').each(function () {
                    $('#bonusDocument').append($(this).text());
                });
            } else {
                $('#bonusDocument').html('Sorry, a fatal error occurred.');
            }
        }
    }, 'xml');
    return false;
}

function setScribdStatus(documentID, status)
{
    $.get('/api/documents/1.0/setScribdStatus?documentID=' + documentID + '&status=' + status, function (response) {
        if ($(response).find('Result').text() == 1) {
            $('li.scribd').html('Done.');
        } else {
            if ($(response).find('Errors').length) {
                $('li.scribd').html('');
                $(response).find('Error').each(function () {
                    $('li.scribd').append($(this).text());
                });
            } else {
                $('li.scribd').html('Sorry, a fatal error occurred.');
            }
        }
    }, 'xml');
    return false;
}
