String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

function loadComment(id) {
    new Ajax.Request('/photos/', {
        method: 'get',
        parameters: {
            ajax: 'true',
            comments: 'list',
            id : id
        },
        onLoading: function() {
            window.status = 'Loading comments...';
            document.body.style.cursor = 'wait';
        },
        onSuccess: function(t) {
            // parse our XML for the count and the data
            var xml = t.responseXML;
            document.getElementById('commentCounter_' + id).innerHTML = xml.getElementsByTagName('total')[0].firstChild.nodeValue;
            document.getElementById('commentsList_' + id).innerHTML = xml.getElementsByTagName('data')[0].firstChild.nodeValue;
        },
        onFailure: function() {
            alert('Unable to fetch comments. Please try again later.');
        },
        onComplete: function() {
            window.status = 'Comments loaded...';
            window.setTimeout('window.status=""', 5000);
            document.body.style.cursor = 'default';
        }
    });
}

function postComment(id) {
    var inputs = document.getElementById('commentPost_' + id).getElementsByTagName('input');
    var textareas = document.getElementById('commentPost_' + id).getElementsByTagName('textarea');
    var email = ""
    var author = "";
    var body = "";
    var email_box = "";
    var author_box = "";
    var body_box = "";

    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].getAttribute("name") == "author") {
            author_box = inputs[i];
            author = author_box.value;
        }
        if (inputs[i].getAttribute("name") == "email") {
            email_box = inputs[i];
            email = email_box.value;
        }
    }

    for (var i = 0; i < textareas.length; i++) {
        if (textareas[i].getAttribute("name") == "body") {
            body_box = textareas[i];
            body = body_box.value;
        }
    }

    // error check the values to make sure they contain values
    author.trim();
    if (author == "") {
        alert("You must enter your name so I can hold you accountable.");
        return;
    }

    body.trim();
    if (body == "") {
        alert("You must enter a comment if you want to post a comment.");
        return;
    }

    // post them to the server so that they end up in the database
    new Ajax.Request('/photos/', {
        method: 'post',
        parameters: {
            ajax: 'true',
            comments: 'post',
            id: id,
            email: email,
            author: author,
            body: body
        },
        onLoading: function() {
            window.status = 'Posting comment...';
            document.body.style.cursor = 'wait';
        },
        onSuccess: function(t) {
            // parse our XML for the count and the data
            var xml = t.responseXML;
            document.getElementById('commentCounter_' + id).innerHTML = xml.getElementsByTagName('total')[0].firstChild.nodeValue;
            document.getElementById('commentsList_' + id).innerHTML = xml.getElementsByTagName('data')[0].firstChild.nodeValue;

            // clear out boxes containing our old data
            email_box.value = "";
            author_box.value = "";
            body_box.value = "";
        },
        onFailure: function() {
            alert('Unable to post comments. Please try again later.');
        },
        onComplete: function() {
            window.status = 'Comment posted...';
            window.setTimeout('window.status=""', 5000);
            document.body.style.cursor = 'default';
        }
    });
}