var prvreq;
if (window.XMLHttpRequest) {
	prvreq = new XMLHttpRequest();
} else if (window.ActiveXObject) {
	prvreq = new ActiveXObject("Microsoft.XMLHTTP");
}
var modreq;
if (window.XMLHttpRequest) {
	modreq = new XMLHttpRequest();
} else if (window.ActiveXObject) {
	modreq = new ActiveXObject("Microsoft.XMLHTTP");
}

var timeron = false;
var changes = 0;

function update_preview() {
	var sendauthor = document.getElementById('author').value;
	var sendlink = document.getElementById('link').value;
	var sendcomment = document.getElementById('comment').value;
	var thelocation = document.getElementById('blogurl').value;
	if (prvreq != undefined) {
		prvreq.abort();
		prvreq.onreadystatechange = update_preview_done;
		prvreq.open('POST', thelocation, true);
		prvreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
		prvreq.send( 'preview=1;author=' + encodeURIComponent(sendauthor) + ';link=' + encodeURIComponent(sendlink) + ';comment=' + encodeURIComponent(sendcomment) );
	}
}

function update_preview_done() {
  if (prvreq.readyState == 4) { // only if prvreq is "loaded"
    if (prvreq.status == 200 || prvreq.status == 304) {
    	if ( prvreq.responseText.indexOf("GOOD\n") == 0 ) {
	      document.getElementById('blogpreview').innerHTML = prvreq.responseText.substr(5);
  	    document.getElementById('blogpreviewsection').style.display = 'block';
  	  }
    }
  }
}

function checkchanges() {
	if ( changes > 0 ) {
		changes = 0;
		setTimeout("checkchanges()",200);
	} else {
		timeron = false;
		update_preview();
		
	}
}

function notechange() {
	if ( !timeron ) {
		timeron = true;
		setTimeout("checkchanges()",200);
	}
	changes = changes + 1;
	return true;
}

function request_moderation_done( blogcomment_id ) {
  if (modreq.readyState == 4) { // only if modreq is "loaded"
    if (modreq.status == 200 || modreq.status == 304) {
    	if ( modreq.responseText.indexOf("GOOD\n") == 0 ) {
	      document.getElementById('comment-' + blogcomment_id).innerHTML = modreq.responseText.substr(5);
  	  }
    }
  }
}

function request_moderation( blogcomment_id, theaction ) {
	if ( confirm("If this comment is offensive or inappropriate, please request that it be moderated.  Are you sure you want this comment removed?") ) {
		modreq.abort();
    modreq.onreadystatechange = function() {request_moderation_done( blogcomment_id );};
		modreq.open('POST', 'adjustcomment', true);
		modreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
		modreq.send( 'blogcomment_id=' + encodeURIComponent(blogcomment_id) + ';action='+theaction );
	}
}