var w = "altrow";
var highcommentid = 0;
var timervar;
function insRow(username,userid,takeid,commentid,photopath)
{
var x=document.getElementById('commentsTable').insertRow(1);
if(w == "altrow") {
	x.className = "";
	w = "";
} else {
	x.className = "altrow";
	w = "altrow";
}
var y=x.insertCell(0);
var z=x.insertCell(1);
var take=document.getElementById('comment').value;
// Update the table
y.innerHTML="<a href=\"/users/view/"+userid+"\"><img src=\"/files/"+photopath+"\" style=\"margin: 0px 10px 0px 0px; float: left;\" alt=\""+username+"\"></a><a href=\"/users/view/"+userid+"\">"+username+"</a> "+take+
"<div style=\"clear: both\"></div>"+
"<div id=\"preNewReply\" style=\"overflow: visible; display: none\">"+
                                                                                                                "<fieldset>"+
                                                                "<legend style=\"background-color: transparent;\">What's Your Reply?</legend>"+
                                                                "<textarea name=\"preData\" label=\"\" id=\"preReply\"></textarea><div class=\"submit\"><input firstget=\"javascript:get(this.parentNode,'"+username+"',"+userid+","+takeid+",'"+photopath+"')\" value=\"Post My Reply\" type=\"submit\" id=\"replySubmit\"></div>                                                        </fieldset>"+

                                                "</div>"+

"<div id=\"newReplyBox"+highcommentid+"\"></div>";

z.innerHTML="<div id=\"newActionDiv\"><a href=\"#\" style=\"font-size: 10pt\">Reply</a> | <a href=\"/takes/report/\" style=\"font-size: 10pt\">Report</a></div>";
}

function updateRow(username,userid,takeid,commentid,photopath)
{
var take=document.getElementById('reply'+commentid).value;
// Update the table row
x = document.getElementById('replyBox'+commentid);
y = document.getElementById('replyBox'+commentid).innerHTML;
x.innerHTML="<p style=\"margin-left: 150px; margin-top: 20px; clear: both;\"><a href=\"/users/view/"+userid+"\"><img src=\"/files/"+photopath+"\" style=\"margin: 0px 10px 0px 0px; float: left;\" alt=\""+username+"\"></a><a href=\"/users/view/"+userid+"\">"+username+"</a> "+take+"</p>"+y;
}

var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
                // set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
	    performUpdate(result);
         } else {
            //alert(http_request.responseText);
         }
      }
   }
   
   function get(obj,username,userid,takeid,photopath,replyto) {
      if(replyto > 0) {
	var poststr = "comment=" + encodeURI( document.getElementById("reply"+replyto).value );
      } else {
      	var poststr = "comment=" + encodeURI( document.getElementById("comment").value );
      }
      clearInterval(timervar);
      timervar = setInterval( "updateTakeVar()", 15000);
      makePOSTRequest('http://www.yourtakes.com/comments/add/user_id:'+userid+'/take_id:'+takeid+'/reply_to:'+replyto, poststr);
      if(replyto > 0) {
	updateRow(username,userid,takeid,replyto,photopath);
	document.getElementById('reply'+replyto).value = '';
      } else {
      	insRow(username,userid,takeid,replyto,photopath);
      	document.getElementById('comment').value = '';
      }
      return true;
   }

   function updateTakes(takeid, highcommentid) {
	poststr = "commentid="+highcommentid;
	makePOSTRequest('http://www.yourtakes.com/comments/view/'+takeid, poststr);
   }

   function performUpdate(responseText) {
	resultarray = responseText.split("<splittakes>");
	for(var i = 1; i < resultarray.length; i++) {
		newDiv = document.createElement("div");
		newDiv.innerHTML = resultarray[i];
		ps = newDiv.getElementsByTagName("p");
		if(ps[0]) {
			x = document.getElementById('replyBox'+ps[0].className);
			y = document.getElementById('replyBox'+ps[0].className).innerHTML;
			x.innerHTML=resultarray[i]+y;
			commentiddiv = newDiv.getElementsByTagName("div");
			if(commentiddiv[0].className > highcommentid) {
				highcommentid = commentiddiv[0].className;
			}
		} else {
			newDiv2 = document.createElement("div");
			newDiv2.innerHTML = resultarray[i];
			divs = newDiv2.getElementsByTagName("div");
			commentid = divs[0].className;
			var x=document.getElementById('commentsTable').insertRow(1);
			if(w == "altrow") {
			        x.className = "";
			        w = "";
			} else {
			        x.className = "altrow";
			        w = "altrow";
			}
			var y=x.insertCell(0);
			var z=x.insertCell(1);
			y.innerHTML = resultarray[i];
			z.innerHTML="<a href=\"#\" onclick=\"Effect.BlindDown('newReply"+commentid+"'); return false\" style=\"font-size: 10pt\">Reply</a> | <a href=\"/takes/report/"+commentid+"\" style=\"font-size: 10pt\">Report</a>";
			if(commentid > highcommentid) {
				highcommentid = commentid;
			}
		}
	}
		newDiv3 = document.createElement("div");
		newDiv3.innerHTML = responseText;
		spans = newDiv3.getElementsByTagName("span");
		if(spans[0]) {
			commentid = spans[0].className;
			if(commentid > highcommentid) {
				newReplyBox = document.getElementById("newReplyBox"+highcommentid);
				highcommentid = commentid;
				if(newReplyBox) {
					newReplyBox.setAttribute('id','replyBox'+highcommentid);
					newActionDiv1 = document.getElementById("newActionDiv");
	                                newActionDivAnchors = newActionDiv1.getElementsByTagName("a");
					link2 = newActionDivAnchors[1].getAttribute("href");
					newActionDivAnchors[0].setAttribute("onClick", 'Effect.BlindDown(\'newReply'+highcommentid+'\'); return false');
					newActionDivAnchors[1].setAttribute("href", link2+highcommentid);
					newReply = document.getElementById("preNewReply");
					newReply.setAttribute('id', 'newReply'+highcommentid);
					testitem = newReply.getAttribute('id');
					reply = document.getElementById("preReply");
					reply.setAttribute('id', 'reply'+highcommentid);
					replySubmit1 = document.getElementById("replySubmit");
					replySubmit1.setAttribute('id', 'replySubmit'+highcommentid);
					replySubmitOnClick = replySubmit1.getAttribute('firstGet').slice(0, -1);
					replySubmitOnClickNew = replySubmit1.setAttribute('onClick', replySubmitOnClick+","+highcommentid+"); Effect.BlindUp('newReply"+highcommentid+"'); return false");
				}
				
			}
		}
   }
