// function to GET the comments for a given blog entry...
function getCommentList(id,param)
{	
	var sURL = "rpc-comments-list-sp.cfm?blogid="+id;
	
	var request = new ajaxObj();	
	if (!ajaxObj)
		return null;
	
	var targetDiv = document.getElementById('iCommentsBox');
		targetDiv.style.display = "";
		targetDiv.innerHTML = '<img src="/images/loadcirc_pink2.gif" width="16" height="16" border="0" alt="Loading..." />';
		
	var requestComplete = function (oXML) {
		targetDiv.innerHTML = oXML.responseText;
	};
	
	if (param)
		request.connect(sURL, "GET", "&sort="+param , requestComplete);
	else
		request.connect(sURL, "GET", "" , requestComplete);		
}	

// function to GET the comment form, add to the page...
function writeCommentForm(id)
{
	if (!id)
		return false;
	
	var request = new ajaxObj();	
	if (!ajaxObj)
		return null;
			
	var sURL = "rpc-comments-form-hg.cfm?blogid="+id;
	var targetDiv = document.getElementById('iCommentForm');
		targetDiv.style.display = "";
		targetDiv.innerHTML = '<p><img src="/images/loadcirc_pink2.gif" width="16" height="16" border="0" alt="Loading..." /></p>';
	
	var requestComplete = function (oXML) {
		targetDiv.innerHTML = oXML.responseText;
	};
	
	request.connect(sURL, "GET", "" , requestComplete);
	//soundManager.play('mechopen');
}	

// validate that peepz actually enter shit in the comment form...
function validateComment() 
{	
    var formName = document.getElementById('cN');
    var formEmail = document.getElementById('cE');
    var formURL = document.getElementById('cU');
    var formCountry = document.getElementById('cCt');
    var formComment = document.getElementById('cComments');
    
    if (formName.value == "") {
        alert('Hey dipshit... got a Name?');
        return false;
    }
    if (formEmail.value == "") {
        alert('Give me your email, else i\'ll spam ya.');
        return false;
    }
    
    if (formCountry.value == "") {
        alert('What country do you hail from, amigo?');
        return false;
    }
    
    if (formComment.value == "") {
        alert('Leaving blank comments just isn\'t kewl, man.');
        return false;
    }
    
    postComment();
	return false;      
}

// function to POST the comment...
function postComment()
{			
    var str = "";
	var oForm = document.getElementById("myCommentForm");
	
	if (!oForm)
	  return false;
  
	with (oForm)
	{
	  for (var i=0; i<elements.length-1; i++)
	  {
	    if (str != "")
		  str += "&";
		str += elements[i].name+"="+encodeURIComponent(elements[i].value);
	  }
	}
		
    if (oForm == "")
	  return false;		  
	
	
	var request = new ajaxObj();	
	if (!ajaxObj)
		return null;
			
	var sURL = "rpc-comments-insert-new.cfm";
	var cID = document.getElementById("commentBlogID").value;
	var cCnt = document.getElementById('commentCount').value;
	
	var targetDiv = document.getElementById('iCommentForm');
		targetDiv.style.display = "none";
	
	var requestComplete = function (oXML) {
		var newCC = "commentcount" + cID;		  
		var newCB = "ccb" + cID;
		document.getElementById(newCC).innerHTML = parseInt(cCnt)+1;
		document.getElementById(newCB).innerHTML = parseInt(cCnt)+1;
		  
		getCommentList(cID);
		targetDiv.innerHTML = "";
		//soundManager.play('ahh');
	};
	
	request.connect(sURL, "POST", str, requestComplete);
}


// Close FooComments
function closeFooComments()
{
	document.getElementById('iCommentForm').style.display = "none";
	document.getElementById('iCommentForm').innerHTML = "";
}
// Close ContactForm
function closeContact()
{
	document.getElementById('iContactForm').style.display = "none";
	document.getElementById('iContactForm').innerHTML = "";
}
// Toggle the emoticon bar
function toggleEmoBar()
{
	var emoBar = document.getElementById('emos');
	var emoLink = document.getElementById('emos_link');
	
	if (emoBar.style.display == "none")
	{
		emoBar.style.display = "";
		emoLink.innerHTML = "Hide Emoticons";		
		emoLink.title = "Hide Emoticons";
	}
	else
	{
		emoBar.style.display = "none";
		emoLink.innerHTML = "Display Emoticons";
		emoLink.title = "Display Emoticons";
	}
}


// function to add the code to the message for the smileys
function AddSmileyIcon(iconCode) {	
	document.myCommentForm.cComments.value += iconCode + " ";
	document.myCommentForm.cComments.focus();
}

// Simple HTML tags addition to form
function getSelectStart(s) {
	return s.selectionStart;
}
function getSelectEnd(s) {
	return s.selectionEnd;
}
function getTextLength(s) {
	return s.textLength;
}
function getMozSelection(s) {
	return (s.value).substring(getSelectStart(s), getSelectEnd(s))
}
function setMozSelection(a,z) {
	s.selectionStart = a;
	s.selectionEnd = z;
}
function wrapSelection(v) {
if (navigator.appVersion.indexOf("MSIE")!=-1) {
    if (!document.selection) return;
    var str = document.selection.createRange().text;
    if (!str) return;
    document.selection.createRange().text = '<' + v + '>' + str + '</' + v + '>';}
else {
	var s = document.getElementById('cComments'); //document.forms['myCommentForm'].cComments
	var s1 = (s.value).substring(0,getSelectStart(s))
	var s2 = (s.value).substring(getSelectEnd(s),getTextLength(s))
	s.value = s1 + '<' + v + '>' + getMozSelection(s) + '</' + v + '>' + s2
}}

function insertMozLink() {
if (navigator.appVersion.indexOf("MSIE")!=-1) {
    if (!document.selection) return;
    var str = document.selection.createRange().text;
    if (!str) return;
    var my_link = prompt('Enter the URL of the hyperlink', 'http://');
    if (my_link != null)
        document.selection.createRange().text = '<a href="' + my_link + '" title="' + my_link + '" target="_blank">' + str + '</a>';}
else {
	var s = document.getElementById('cComments'); //document.forms['myCommentForm'].cComments
	var s1 = (s.value).substring(0,getSelectStart(s))
	var s2 = (s.value).substring(getSelectEnd(s),getTextLength(s))
	var my_link = prompt('Enter the URL of the hyperlink...', 'http://')
	if (my_link != null)
		s.value = s1 + '<a href="' + my_link + '" title="' + my_link + '" target="_blank">' + getMozSelection(s) + '</a>' + s2
}}