//dostuff.js
//Copyright © 2007, Pall Thayer http://www.this.is/pallit. Sections of JavaScript were retrieved from Jim Ley's website, http://jibbering.com/2002/4/httprequest.html
//
//This file is part of CodeChat. CodeChat is a web-based chat system for generating public discussions around computer programming code.////CodeChat is free software; you can redistribute it and/or modify//it under the terms of the GNU General Public License as published by//the Free Software Foundation; either version 3 of the License, or//(at your option) any later version.////CodeChat is distributed in the hope that it will be useful,//but WITHOUT ANY WARRANTY; without even the implied warranty of//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the//GNU General Public License for more details.////You should have received a copy of the GNU General Public License//along with this program.  If not, see <http://www.gnu.org/licenses/>.

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
//when user expands comment box, retrieve related comments from the database
function getContent(divlinecnt) {
	if(document.getElementById(divlinecnt).style.display == 'none'){
		var splitinfo = divlinecnt.split(".");
		xmlhttp.open("GET", "getsql.php?lineno="+splitinfo[1],true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				document.getElementById(divlinecnt).innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
	}
 togdivs(divlinecnt);
}
//provides show/hide toggle function for comment boxes
 function togdivs(divlinecnt){
 	var whichDiv = document.getElementById(divlinecnt);
 	if(whichDiv.style.display == 'none')
 	whichDiv.style.display = 'block';
 	else
 	whichDiv.style.display = 'none';
 	return;
 }
//create input form when user clicks on "Post a new comment..." by rewriting comment box contents
 function makeCommForm(divlinecnt){
 	var whichDiv = document.getElementById(divlinecnt);
 	var splitinfo = divlinecnt.split(".");
 	var thisCont = whichDiv.innerHTML;
 	var formCont = '<form method="post" action="" name="newComm">name:<br /><input type="text" name="usrname"><br />comment:<br /><textarea cols="40" rows="5" name="comment"></textarea><input type="hidden" name="lineno" value="'+splitinfo[1]+'"><br /><INPUT TYPE="button" NAME="button" Value="Submit" onClick="doComm(this.form)"></form>';
 	whichDiv.innerHTML = formCont+'<br />'+thisCont;
 }
//insert new comments into database, refresh comment box contents when done
 function doComm(form){
 	var newComm = form.comment.value;
 	var lineNo = form.lineno.value;
 	var userName = form.usrname.value;
 	var divlinecnt = 'thisDIV.'+lineNo;    xmlhttp.open("POST", "putComm.php",true);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlhttp.send('comment='+newComm+'&name='+userName+'&line='+lineNo);
    xmlhttp.onreadystatechange=function() {
    	if (xmlhttp.readyState==4) {
    		document.getElementById(divlinecnt).innerHTML = xmlhttp.responseText;
		}
	}
}
//create input form when user clicks on "reply" by rewriting content of empty form embedded under each comment
function makeReplyForm(whichComm, divlinecnt){
	var FormID = 'replyform'+whichComm;
	var whichForm = document.getElementById(FormID);
	whichForm.innerHTML = 'name:<br /><input type="text" name="usrname"><br />reply:<br /><textarea cols="40" rows="5" name="comment"></textarea><input type="hidden" name="relation" value="'+whichComm+'"><input type="hidden" name="line" value="'+divlinecnt+'"><br /><INPUT TYPE="button" NAME="button" Value="Submit" onClick="doReply(this.form)">';
}
//insert new replies into database, refresh comment box contents when done
 function doReply(form){
 	var newComm = form.comment.value;
 	var related = form.relation.value;
 	var userName = form.usrname.value;
 	var lineNo = form.line.value;
 	var divlinecnt = 'thisDIV.'+lineNo;    xmlhttp.open("POST", "putReply.php",true);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlhttp.send('comment='+newComm+'&name='+userName+'&relation='+related+'&line='+lineNo);
    xmlhttp.onreadystatechange=function() {
    	if (xmlhttp.readyState==4) {
    		document.getElementById(divlinecnt).innerHTML = xmlhttp.responseText;
		}
	}
}