var xmlHttp;
// "Hit It" Functions
function hitit(id) {
    hitstatus = "hit" + id;
    hitamount = "hitamount" + id;
    theid = id;
    var url = "/hitit.php?id=" + id;
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = callbackhitit;
    xmlHttp.send(null);
}

function callbackhitit() {
    if (xmlHttp.readyState == 1 || xmlHttp.readyState == 2) {
        document.getElementById(hitstatus).style.color = "white";
        document.getElementById(hitstatus).style.background  = "green";
        document.getElementById(hitstatus).innerHTML = "Hitting!";
    }
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            parseresponsehitit();
        } else {
            document.getElementById(hitstatus).style.color = "white";
            document.getElementById(hitstatus).style.background  = "red";
            document.getElementById(hitstatus).innerHTML = "Communication Error!";
        }
    }
}

function parseresponsehitit() {
    var results = xmlHttp.responseXML;
    var hits = results.getElementsByTagName("hit");
    entry = hits[0];
    response = entry.getElementsByTagName("response")[0].firstChild.nodeValue;
    newhits = entry.getElementsByTagName("newhits")[0].firstChild.nodeValue;
    problem = entry.getElementsByTagName("problem")[0].firstChild.nodeValue;

    if (newhits != 0)
        document.getElementById(hitamount).innerHTML = newhits;
    if (problem == 1) {
        document.getElementById(hitstatus).style.color = "white";
        document.getElementById(hitstatus).style.background  = "red";
    } else {
        document.getElementById(hitstatus).style.color = "white";
        document.getElementById(hitstatus).style.background  = "green";
    }
    document.getElementById(hitstatus).innerHTML = response;
}

///////////////////////////////
// "Add Comment" Functions
//////////////////////////////
function encodeform(form) {
    var aParams = new Array();
    for (var i=0 ; i < form.elements.length; i++) {
        var sParam = encodeURIComponent(form.elements[i].name);
        sParam += "=";
        sParam += encodeURIComponent(form.elements[i].value);
        aParams.push(sParam);
    } 
    return aParams.join("&");
}

function addcomment() {
    var url = "/comments.php";
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    var input = encodeform(document.commentform);
    xmlHttp.open("POST", url);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-Length", input.length);
    xmlHttp.onreadystatechange = callbackaddcomment;
    xmlHttp.send(input);
}

function callbackaddcomment() {
    if (xmlHttp.readyState == 1 || xmlHttp.readyState == 2) {
        document.getElementById("commentstatus").innerHTML = "Status: Adding comment!";
    }
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            parseresponseaddcomment();
        } else {
            document.getElementById("commentstatus").innerHTML = "Status: Communication Error!";
        }
    }
}


function parseresponseaddcomment() {
    var results = xmlHttp.responseXML;
    var comments = results.getElementsByTagName("comment");
    entry = comments[0];
    comment = entry.getElementsByTagName("comment")[0].firstChild.nodeValue;
    count = entry.getElementsByTagName("count")[0].firstChild.nodeValue;
    user = entry.getElementsByTagName("user")[0].firstChild.nodeValue;
    datetime = entry.getElementsByTagName("datetime")[0].firstChild.nodeValue;
    status = entry.getElementsByTagName("status")[0].firstChild.nodeValue;
    problem = entry.getElementsByTagName("problem")[0].firstChild.nodeValue;

    if (problem == 1) {
        document.getElementById("commentstatus").innerHTML = "Status: " + status;
    } else {
        if (count == 1) {
            document.getElementById("commentamounttitle").innerHTML = count + " Comment";
        } else {
            document.getElementById("commentamountlink").innerHTML = count + " comments";
            document.getElementById("commentamounttitle").innerHTML = count + " Comments";
        }

            
        // Create info row
        var tr = document.createElement("tr");
        var cell = document.createElement("td");
        cell.className = "clipoptions";
        var textnode = document.createTextNode("by ");
        cell.appendChild(textnode);
        var linktext = document.createTextNode(user);
        var a = document.createElement("a");
        a.href='/profile/' + user;
        a.appendChild(linktext);
        cell.appendChild(a);
        var textnode = document.createTextNode(" at " + datetime);
        cell.appendChild(textnode);
        tr.appendChild(cell);
        document.getElementById("commenttable").tBodies[0].appendChild(tr);
        // Create content row

        comment = comment.replace(/\n/g, "<br>");
        var tr = document.createElement("tr");
        var cell = document.createElement("td");
        cell.className = "clipcontents";
        cell.innerHTML = comment;
        tr.appendChild(cell);
        document.getElementById("commenttable").tBodies[0].appendChild(tr);
        
        document.getElementById("commentstatus").innerHTML = "Status: " + status;
        document.commentform.comment.value = "";

    }
}

function bookmark(url, description) {
    netscape="Please press CTRL+D to bookmark the site."
    if (navigator.appName=='Microsoft Internet Explorer') {
        window.external.AddFavorite(url, description);
    } else if (navigator.appName=='Netscape') {
        alert(netscape);
    }
}
