function GetRequestObject() {
	var X;
	try {
		X=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			X=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (oc) {
			X=null;
		}
	}
	if(!X && typeof XMLHttpRequest != "undefined")
		X = new XMLHttpRequest();
	if (!X)
		alert("Could not create connection object.");
	return X;
}

// global array to keep track of which comments are expanded
// and which are collapsed.
var expanded_comments = new Array;

function ToggleComment(cmid) {
	var div_element = document.getElementById('comment_'+cmid);
	var img_element = document.getElementById('comment_image_'+cmid);

	// If there is already HTML inside the div, then collapse
	// it instead of expanding.
	if (expanded_comments[cmid] == 'y') {
		// Hide this block of HTML
		div_element.style.display = 'none';
		if (img_element)
			img_element.src = '/images/plus_sign.gif';
		expanded_comments[cmid] = 'n';
	} else {
		if (div_element.innerHTML) {
			// HTML already fetched, just redisplay it
			div_element.style.display = '';
			if (img_element)
				img_element.src = '/images/minus_sign.gif';
			expanded_comments[cmid] = 'y';
		} else {
			if (img_element)
				img_element.src = '/images/minus_sign.gif';
			expanded_comments[cmid] = 'y';

			var req = GetRequestObject();
			var url = '/comment_view.php?cmid='+cmid;
			req.open("GET",url,false);
			req.send(null);

			if (req.responseText != null) {
				comment = req.responseText;
			} else {
				comment = 'Sorry, unable to connect to server.';
			}
		    div_element.innerHTML = comment;
		}
	}

	// Always prevent the browser from following the link
	return false;
}

// global array to keep track of which comment forms are expanded
// and which are collapsed.
var expanded_comment_forms = new Array;

// Toggle the add comment form on and off
function CommentForm(lid, cmid) {
	var div_element = document.getElementById('comment_form_'+cmid);

	// If there is already HTML inside the div, then collapse
	// it instead of expanding.
	if (expanded_comment_forms[cmid] == 'y') {
		// Hide this block of HTML
		div_element.style.display = 'none';
		expanded_comment_forms[cmid] = 'n';
	} else {
		// If the comment is not expanded, do so
		if (cmid && expanded_comments[cmid] != 'y')
			ToggleComment(cmid);
		if (div_element.innerHTML) {
			// HTML already fetched, just redisplay it
			div_element.style.display = '';
			expanded_comment_forms[cmid] = 'y';
		} else {
			expanded_comment_forms[cmid] = 'y';

			var req = GetRequestObject();
			var url = '/comment_form.php?lid='+lid+'&cmid='+cmid;
			req.open("GET",url,false);
			req.send(null);

			if (req.responseText != null) {
				html = req.responseText;
			} else {
				html = 'Sorry, unable to connect to server.';
			}
		    div_element.innerHTML = html;
		}
	}

	// Always prevent the browser from following the link
	return false;
}

// Expand/collapse this comment and all of it's children.
function ToggleChildren(cmid) {
	var i,j, img_element;
	var expand = true;

	if (expanded_comments[cmid] == 'y') {
		expand = false;
	}

	for (i = 0; i < arguments.length; i++) {
		j = arguments[i];
		img_element = document.getElementById('comment_child_image_'+j);

		if (expand && expanded_comments[j] != 'y') {
			// Expand the comment
			ToggleComment(j);
			if (img_element)
				img_element.src = '/images/big_minus_sign.gif';
		} else if (!expand && expanded_comments[j] == 'y') {
			// Hide the comment
			ToggleComment(j);
			if (img_element)
				img_element.src = '/images/big_plus_sign.gif';
		}
	}

	// Always prevent the browser from following the link
	return false;
}

// CheckBizCommentForm - Make sure the user has entered
// a rating before they submit the form.
function CheckBizCommentForm(is_owner) {
	var form = document.getElementById('MyCommentForm');
	var error = '';

	// Make sure the user has selected a rating
	var rating_checked = false;
	for (i = 0; i < form.elements.length; i++) {
		e = form.elements[i];
		if (e.name == 'rating' && e.checked) {
			// We have found a checked radio button
			// so we are happy, let the form submit.
			rating_checked = true;
		}
	}
	if (!rating_checked) {
	    error = 'Please choose a rating before submitting';
	}

	// If this user OWNS this biz listing, warn them that
	// rating their own listing is a tacky thing to do.
	if (is_owner) {
	    var msg = 'Note: Comments are NOT anonymous and rating\n';
		msg += 'your own business is considered tacky.\n\n';
		msg += 'Do you want to continue?';

	    if (!confirm(msg)) {
		  return false;
		}
	}
		

	if (error) {
		alert(error);
		return false;
	} else {
	    return true;
	}
}

