
var selPost;
var blogid;
var postid;

function showAddPost() {
    document.getElementById('contentitems').innerHTML = '<div id="heading"><h2>Add Post</h2></div>';
    document.getElementById('contentitems').innerHTML += '<div id="post_input"><form action="#" id="postfrm"> <input type="text" name="posttitle" id="posttitle" /> Title <br /><textarea cols="70" rows="25" id="post_txtarea"> </textarea> <br /><input type="button" name="submit" value="Submit" id="postsubmit" onmouseup="startRequest(\'Post\', \'postfrm\')" /></form></div>';
}


function showPost(blog_id, post_id) {
    blogid = blog_id;
    postid = post_id;
    document.getElementById('selected').innerHTML = '';
    document.getElementById('selected_ids').innerHTML = '';
    
	var xhrRec = xhrRequest('html');
	xhrRec.onreadystatechange = function() {
		if ((xhrRec.readyState == 4) && (xhrRec.status == 200)) {
			var postResponse = xhrRec.responseText;
			document.getElementById('contentitems').innerHTML = postResponse;
			commentedIds(blog_id, post_id);
		}
	}
	var url = "respond.php?call=ShowPost&args=" + post_id;
	
	selPost = blog_id + '.' + post_id;
	document.getElementById('selected_ids').value = selPost;
	document.getElementById('statusmsg').innerHTML = selPost;
	xhrRec.open("GET", url, true);
	xhrRec.send(null);
	
	//var call = "call=ShowPost&args=" + post_id;
	//xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	//xhrRec.open("POST", "respond.php?", true);
	//xhrRec.send(call);
}

function addComment() {
    startRequest('AddComment', 'commentfrm');
    setTimeout('showPost(blogid, postid)', 200);
}

function commentedIds(blog_id, post_id) {
	var xhrRec = xhrRequest('html');
	xhrRec.onreadystatechange = function() {
		if ((xhrRec.readyState == 4) && (xhrRec.status == 200)) {
			var commentIdResponse = xhrRec.responseText;
			var commentIds = new Array();
			commentIds = commentIdResponse.split(',');
            for (i = 0; i < commentIds.length; i++) {
                if (commentIds[i]) {    
                    document.getElementById('statusmsg').innerHTML = commentIds[i];
                    document.getElementById(commentIds[i]).style.color = "#552";
                    document.getElementById(commentIds[i]).style.textDecoration = "underline";
                }
            }
		}
	}
	var url = "respond.php?call=GetCommentIds&args=" + blog_id + "." + post_id;
	xhrRec.open("GET", url, true);
	xhrRec.send(null);
}

function showComment(obj) {
    	var xhrRec = xhrRequest('html');
        xhrRec.onreadystatechange = function() {
            if ((xhrRec.readyState == 4) && (xhrRec.status == 200)) {
                if (xhrRec.responseText) {
                    var commentResponse = xhrRec.responseText;
                    document.getElementById('commentblock').innerHTML = commentResponse;
                    document.getElementById('commentblock').style.top = (obj.offsetTop + 20) + 'px';
                    document.getElementById('commentblock').style.left = (obj.offsetLeft + 20) + 'px';
                    document.getElementById('commentblock').style.display = 'block';
                }
            }
        }
        var url = "respond.php?call=GetCommentItem&args=" + obj.id;	
        xhrRec.open("GET", url, true);
        xhrRec.send(null);
}

function hideComment() {
    document.getElementById('commentblock').innerHTML = '';
    document.getElementById('commentblock').style.display = 'none';
}

function xhrRequest(type) {
	var xhrSend;
	if (!type) {
		type = 'plain';
	}
	if (window.ActiveXObject) {
		try {
			xhrSend = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhrSend = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	} else if (window.XMLHttpRequest) {
		xhrSend = new XMLHttpRequest();
		if (xhrSend.overrideMimeType) {
			xhrSend.overrideMimeType('text/' + type);
		}
	}
	return (xhrSend);
}

function setSel(obj) {
	var spanArray = obj.getElementsByTagName('span');
	for (s=0; s < spanArray.length; s++) {
		spanArray[s].onmousedown = getStart;
	}
}

function remSel(obj) {
	var spanArray = obj.getElementsByTagName('span');
	for (s=0; s < spanArray.length; s++) {
		spanArray[s].onmousedown = null;
	}
}

function resetSel(obj) {
	var spanArray = obj.getElementsByTagName('span');
	for (s=0; s < spanArray.length; s++) {
		spanArray[s].style.background = '';
	}

}

var startNode = false;
function getStart(obj) {
    resetSel(obj.parentNode);
	if (obj.id) {
		startNode = obj.id;
	}
}

function getSel(obj) {
	var txt = '';
	var selected = document.getElementById('selected');
	selected.innerHTML = '';
	if (window.getSelection) {
		txt = window.getSelection();
		
		selected.appendChild(txt.getRangeAt(0).cloneContents());
		window.getSelection().collapse(obj,0);
	} else if (document.selection) {
		document.getElementById('selected').innerHTML = document.selection.createRange().htmlText;
		document.selection.empty();
	} else {
		return;
	}
	getSelId();
}

var setClr = false;

function getSelId() {
	var selectedArray = new Array();
	var selected = document.getElementById('selected');
	var spans = selected.getElementsByTagName('span');
	selectedArray[0] = startNode;
	startNode = false;
	for (i=0; i < spans.length; i++) {
        // RETRIEVES SPAN IDS
        if (spans[i].id) {
			selectedArray[i] = spans[i].id;
			spans[i].id = null;
		}
	}
	for (j=0; j < selectedArray.length; j++) {
        // LIST SPAN IDS AND HIGHLIGHT SELECTED SPANS
		if (selectedArray[j]) {
			document.getElementById(selectedArray[j]).style.background = '#CB9';
		}
	}
	if (spans) {
	   var tmp = "";
	   for (i=0; i< selectedArray.length; i++) {
	       if (i < (selectedArray.length - 1)) {
	           tmp += selectedArray[i] + "^";
	       } else {
	           tmp += selectedArray[i];
	       }
	   }
       document.getElementById('statusmsg').innerHTML = tmp;
       document.getElementById('selected_ids').value = tmp;
    } else {
        document.getElementById('selected_ids').value = selPost;
    }
}
