var ROBERTB = {
	debug: false,
	init: function() {
		if(location.href.indexOf('test') !== -1) {
			ROBERTB.debug = true;
		}
		if(document.getElementById('page_blog')) {
			ROBERTB.addComments();
		}
	},
	addComments: function() {
		if(window.XMLHttpRequest && window.ActiveXObject) {
			return false;
		}
		var pdivs = document.getElementsByTagName('li'),
			pdivsl = pdivs.length,
			c = 0,
			cur = false,
			cur_class = '',
			cur_cont = false,
			cur_cont_c = 0,
			guid = false,
			mf = false;
		for(c=0; c < pdivsl; c++) {
			cur = pdivs[c];
			cur_class = cur.className;
			if(cur_class.indexOf('Shared') !== -1 && 
				cur_class.indexOf('hentry') !== -1) {
				cur_cont = cur.getElementsByTagName('div');
				guid = false;
				for(cur_cont_c = cur_cont.length-1; cur_cont_c > 0; cur_cont_c--) {
					if(cur_cont[cur_cont_c].className.indexOf('guid') !== -1) {
						guid = cur_cont[cur_cont_c].innerHTML;
					}
				}
				cur_cont = cur_cont[2];
				if(guid) {
					mf = document.createElement('form');
					mf.onsubmit = ROBERTB.addComment;
					mf.setAttribute('action', 'no-post.php');
					mf.innerHTML = '<div class="inline">Add Your Comment:<br><input type="text" class="f_name" name="name" results="5" placeholder="Your Name" maxlength="50"><input type="text" name="comment" placeholder="Your Comment (Up to 200 Characters)" maxlength="200"> <input type="hidden" name="guid" value="' + guid + '"> <input type="submit" name="sub" value="Add Comment"></div>';
					cur_cont.appendChild(mf);
				}
			}
		}
	},
	addComment: function(e) {
		e.preventDefault();
		var f = this,
			n = this.name,
			c = this.comment,
			g = this.guid,
			qs = 'name=' + escape(n.value) + '&comment=' + escape(c.value) + '&guid=' + escape(g.value) + '&url=' + escape(location.href),
			ajax = false;
		if(n.value.length > 0 && c.value.length > 0 && g.value.length > 0) {
			this.sub.disabled = true;
			this.name.disabled = true;
			this.comment.disabled = true;
			this.sub.value = 'Adding Comment';
			ajax = new XMLHttpRequest();
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 4) {
					if(ajax.status == 200) {
						if(ajax.responseText == '1') {
							alert('Your comment was added.  Please refresh the page to see it.  Please note that comments may be deleted or be unavailable at any time.');
							f.parentNode.removeChild(f);
						} else {
							alert(ajax.responseText);
							f.sub.disabled = false;
							f.name.disabled = false;
							f.comment.disabled = false;
							f.sub.value = 'Add Comment';
						}
					} else {
						alert('The comment failed to post.  Try again!');
						f.sub.disabled = false;
						f.name.disabled = false;
						f.comment.disabled = false;
						f.sub.value = 'Add Comment';
					}
				}
			};
			ajax.open('GET', 'comment.php?' + qs, true);
			ajax.send();
		} else {
			alert('You must enter your name and a comment!');
		}
	}
};

if(window.addEventListener) {
	window.addEventListener('load', ROBERTB.init, false);
} else if(window.attachEvent) {
	window.attachEvent('onload', ROBERTB.init);
} else {
	window.onload = ROBERTB.init;
}
