var req = false;
var cwindow = false;
var now = new Date();
var nulldate = now.getTime();
var nullcount = 1;
var heading = "";
var hostdomain = "www.illinois-valley-news.com";
var wwwcheck = window.location.href.split('/');
var iswww = 0;
for (i=0; i<wwwcheck.length; i++) {
	if (wwwcheck[i].substr(0,3) == 'www') {iswww = 1;}
}
if (iswww == 0) {hostdomain = "illinois-valley-news.com";}

/* Serves the "Comments (#)" link onto the article page. */

function crequest(caller) {
	nullcount++;
	if (heading == "") {
		var heads = document.getElementsByTagName("h2");
		heading = heads[0].firstChild.data;
		heads = heading.split("<br");
		heading = heads[0];
	}
	url = ("http://" + hostdomain + "/scripts/comments/sendclink.pl?callerID=" + caller + "&pageURL=" + window.location.href + "&title=" + heading + "&enum=" + nullcount + "&ndate=" + nulldate);
	req = false;
	if (window.XMLHttpRequest) { req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {req = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			try {req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {}
		}
	}
	if (req) {
		req.onreadystatechange = function() {makelink();}
		req.open("GET", url, 1);
		req.send(null);
	} else {alert("Unfortunately, your browser does not support the Javascript functions used in the comments system.");}
}

function makelink() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			if (req.responseText != " ") {
				thetext = req.responseText.split('|');
				newlink = document.createElement("a");
				newlink.href = thetext[0];
				newlink.target = "_blank";
				newtext = document.createTextNode(thetext[1]);
				newlink.appendChild(newtext);
				thespan = document.getElementById('commentspan');
				thespan.appendChild(newlink);
			}
		}
	}
}


/* Called when user clicks to add or reply to a comment. */
function addcomment(reply) {
	if (cwindow) {cwindow.close();}
	document.add.rt.value = reply;
	var curl = ("http://" + hostdomain + "/internal/comments/addcomment.html");
	if (reply != '') {curl = ("http://" + hostdomain + "/internal/comments/replycomment.html");}
	cwindow = window.open(curl, "addwindow", "width=550,height=550,screenX=20,screenY=20,titlebar=yes,toolbar=no,status=no,scrollbars=yes");
}

/* Called by document loaded into add/reply-to comment window. */
function getvals() {
	document.addcomment.callerID.value = opener.document.add.callerID.value;
	document.addcomment.cid.value = opener.document.add.cid.value;
	document.addcomment.rt.value = opener.document.add.rt.value;
}

/* Displays in popup window the complete comment for which the first 250 chars were displayed */
function allof(commentID) {
	var showtarget = ('/scripts/comments/showall.pl?callerID=' + document.add.callerID.value + '&cid=' + document.add.cid.value + '&show=' + commentID);
	allwindow = window.open(showtarget, "showwindow", "width=620,height=500,screenX=30,screenY=30,titlebar=yes,toolbar=no,status=no,scrollbars=yes");
}

/* Manages display styles for showing and hiding text on comments pages. */
function showhide(cname) {
	var thing = document.getElementById(cname);
	if (thing.style.display == "none") {
		thing.style.display = "block";
	} else {thing.style.display = "none";}
}

/* Called by sethides. */
function getElementsByClass(elem, classname) {
    classes = new Array();
    alltags = document.getElementsByTagName(elem);
    for (i=0; i<alltags.length; i++)
        if (alltags[i].className == classname)
            classes[classes.length] = alltags[i];
    return classes;
}

/* Called at end of comments area to set CSS display parameters so they can be changed by JS */
function sethides() {
	things = getElementsByClass('div', 'cbody');
	for (i=0; i<things.length; i++) {
		if (things[i].id) {things[i].style.display = "none";}
	}
}

