
// Are we logged in?
var CWlogin_status = null;
var CWlogin_url = '';

function init_comments()
{
	// check the login status
	// returns to the callback function setCWready() which calls loadCommentsWidget()
	loadjscssfile( "http://na710n.com/wor-comments/?callback=setCWready&page_url=" + escape(document.location + "#CWHEAD"), "js" );
}

function loadCommentsWidget()
{
	loadjscssfile( 'http://na710n.com/comments/wor/comments/view-comments?js&comment_page=' + escape( CW_page ), "js" );
}

function cw_post()
{

	var ct = $('cw-new-comment').value;
	var p_url = "http://na710n.com/comments/rpc/js-post-comment";

	if( ct == "" )
	{
		alert( "You haven't entered a comment yet!" );
		return;
	}

	$('cw-btn-post-comment').disabled = true;
	$('cw-new-comment').disabled = true;

	var post_url = p_url + "?comment_page=" + escape( CW_page ) + "&comment_text=" + escape( ct );
	loadjscssfile(post_url, "js");
}


function setCWlogin_status( li )
{
	CWlogin_status = li;
	return;
}

function setCWlogin_url( url )
{
	CWlogin_url = url;
	return;
}

//callback from JS
function setCWready()
{
	loadCommentsWidget();
}

function CWLogin()
{
	if( CWlogin_url == '' )
	{
		alert( "An error occurred with the comments widget." );
		return;
	}

	document.location = CWlogin_url;
}

function setCW_content( content )
{
	document.getElementById( "CW_container" ).innerHTML = content;
	if( !CWlogin_status )
		$('cw-login-div').innerHTML = "Login to post a comment... <input type='button' onClick='CWLogin();' value='Login' />";
	return;
}

function CW_postCB( res )
{
	// post callback
	$('cw-new-comment').value = "";
	$('cw-new-comment').disabled = false;
	$('cw-btn-post-comment').disabled = false;

	if( res == "FAIL" || res  == "" )
		alert( "An error has occurred. Try again later." );
	else
		$('comment-tool-comments').innerHTML = res;

}

function CW_errorCB( msg )
{
	alert( msg );
}

function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}



