// read ajax data into target div object
function read_ajax(textObject, id) {
	document.getElementById('comment_form'+id).innerHTML=textObject;
}

function save_comment(id, user, path) {
	// validate form
	if (document.getElementById('blog_comment'+id).value=='') { alert('Please enter a comment before saving'); return false; }
	if (document.getElementById('blog_comment_name'+id) && document.getElementById('blog_comment_name'+id).value=='') { alert('Please enter your name before saving'); return false; }
	
	// send information to the ajax script for updating
	if (window.XMLHttpRequest) {
		// code for all new browsers
		http=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		// code for IE5 and IE6
	  	http=new ActiveXObject("Microsoft.XMLHTTP");
	}

	var myRandom = parseInt(Math.random()*99999999);  // cache buster
	url=path+'includes/save_comment.php?';
	url+='dummyid='+myRandom;

	var params = "id="+id;	
	params += "&comment="+escape(document.getElementById('blog_comment'+id).value);
	if (user=='') { params += "&user="+escape(document.getElementById('blog_comment_name'+id).value); }
	else { params += "&user="+user; };
	
	http.open("GET", url+"&"+params, true); 
	
	document.getElementById('comment_form'+id).innerHTML = 'Saving...';
	
	http.onreadystatechange = function() { 
		if (http.readyState == 4) { read_ajax(http.responseText, id); } 
	} 
	http.send(null);
}

function show_comment_form(id) {
	document.getElementById('comment_form'+id).style.display='block';
	document.getElementById('comment_btn'+id).style.display='none';
}

function hide_comment_form(id) {
	document.getElementById('comment_form'+id).style.display='none';
	document.getElementById('comment_btn'+id).style.display='block';
	document.getElementById('blog_comment'+id).value='';
}

function expand_comments(id) {
	document.getElementById('expand_comments_btn'+id).style.display='none';
	document.getElementById('expanded_comments'+id).style.display='block';
}

function shrink_comments(id) {
	document.getElementById('expand_comments_btn'+id).style.display='block';
	document.getElementById('expanded_comments'+id).style.display='none';
}

