function getXML(callback) {
    var allNode = $('onelinerDIV').getChildren()
    for (var i =0; i < allNode.length; i++) {
        allNode[i].remove()
    }
    
    var myDocTree = this.transport.responseXML
        
    var returnObj = myDocTree.getElementsByTagName('object')
    for(var i=0;i< returnObj.length; i++) {
        var perObj = returnObj[i]
        var fieldObj = perObj.getElementsByTagName('field')
                
        name = fieldObj[0].firstChild.nodeValue
        comment = fieldObj[1].firstChild.nodeValue
        date = fieldObj[2].firstChild.nodeValue
        
        var commentDIV = document.createElement("div")
        commentDIV.className = "commentBox"
        var insideComment = document.createElement("div")
        insideComment.className = "comment"        
        insideComment.appendChild(document.createTextNode(comment))
        commentDIV.appendChild(insideComment)
        var usernameSPAN = document.createElement("div")
        usernameSPAN.className = "username"
        usernameSPAN.appendChild(document.createTextNode(name))
        commentDIV.appendChild(usernameSPAN)
        var tail = document.createElement("div")
        tail.className = "tail"
        commentDIV.appendChild(tail)
        $('onelinerDIV').appendChild(commentDIV)
    }
    
    return false;
}

function init() {
    $('input_name').onfocus = function () {
        if (this.value == "Your Name")
            this.value = ""
        return false
    }
    $('input_name').onblur = function () {
        if (this.value.length == 0) {
            this.value = "Your Name"
        }
    }
    $('input_comment').onfocus = function () {
        if (this.value == "Comment Here")        
            this.value = ""
    }
    $('input_comment').onblur = function () {
        if (this.value.length == 0) {
            this.value = "Comment Here"
        } 
        
    }
    $('submit_comment').onclick = function (event) {
/*            $('comment_form').send({onSuccess: function () { alert(this.transport.responseText)}})*/
            //console.log($('comment_form').toQueryString())
            var tempAjax = new Ajax('http://www.withoutnetwork.com/oneliner/add/',{
                method: 'post', 
                postBody: $('comment_form'), 
                onSuccess: updateDisplay, 
                onFailure: function () { alert("Error " + this.transport.responseText)}, 
                update: 'comment_feedback'
                })
            
            tempAjax.request();
            event.stop()
    }
    updateDisplay()
}

function updateDisplay() {
    var myXHR = new Ajax('http://www.withoutnetwork.com/oneliner', 
        {   
            method: 'get', 
            onSuccess : getXML,
            onFailure : function() { alert("Error " + this.transport.responseText)}        
        })
    myXHR.request();
    return false;
}

window.addEvent('domready', function(){
    init()
});