var _global 	= new Array();
var comflds		= new Array("req","lang","tbpids");
var newcomflds	= new Array("req","lang","commentdescription","commentstatus","tbpid","tbid");
var listcomflds	= new Array("req","lang","tbpid","commentstatus");
var loginflds	= new Array("req","lang","loginname","password");

var b;
var fieldBorderColor = "#ABADB3";
var fieldBorderWarningColor = "#FF0000";
/******************************	CONTROLLERS ******************************/
function initPage() {
	loadCommentInfo();
	loadComments();
}
function viewComments(id) {
}
function addComment(id) {
	xGetElementById("commentdescription").focus();
}
function loginStatus(isLoggedIn) {
	if ( isLoggedIn )
		loadComments()
	else
		display.showElement("loginfailure");
}
function toggleInfo(str,show,success) {
	if ( !show )
		display.hideElement("infobox");
	else {
		if ( success ) {
			display.hideElement("loader");
			display.hideElement("warning");
			if ( str.length )
				xGetElementById("imsg").innerHTML = str;
			display.showElement("info");
		}
		else {
			display.hideElement("loader");
			display.hideElement("info");
			if ( str.length )
				xGetElementById("wmsg").innerHTML = str;
			display.showElement("warning");
		}
		display.showElement("infobox");
	}
}
/******************************	AJAX ******************************/
function loadCommentInfo() {
	xGetElementById("req").value = 'GET_COMMENT_INFO';
	var loader = new net.ContentLoader(AJAX_PATH + "twinblog.cfm",onLoadCommentInfoResult,onLoadCommentInfoError,"POST",net.CreatePostString(null,comflds));
}
function onLoadCommentInfoResult() {
	response = net.CleanResponse(this.req.responseText);
	if ( response.indexOf('ERROR') == 0 )
		alert('error');
	else if ( response == 'NO_SESSION' )
		toggleInfo(_global['ST_NO_SESSION'],true,false);
	else {
		try {
			eval(response);
			parseCommentInfo(commentinfo);
		}
		catch (e) { };
	}
}
function onLoadCommentInfoError() {
	response = net.CleanResponse(this.req.responseText);
	xGetElementById("error").innerHTML = response;
}
function loadComments() {
	xGetElementById("req").value = 'LIST_COMMENTS';
	var loader = new net.ContentLoader(AJAX_PATH + "twinblog.cfm",onLoadCommentsResult,onLoadCommentsError,"POST",net.CreatePostString(null,listcomflds));
}
function onLoadCommentsResult() {
	response = net.CleanResponse(this.req.responseText);
	if ( response.indexOf('ERROR') == 0 )
		alert('error');
	else if ( response == 'NO_SESSION' )
		toggleInfo(_global['ST_NO_SESSION'],true,false);
	else
		xGetElementById("comments_block").innerHTML = response;
}
function onLoadCommentsError() {
	response = net.CleanResponse(this.req.responseText);
	xGetElementById("error").innerHTML = response;
}
function submitComment() {
	if ( comFrmValidated() ) {
		xGetElementById("req").value = 'ADD_COMMENT';
		var loader = new net.ContentLoader(AJAX_PATH + "twinblog.cfm",onSubmitCommentResult,onSubmitCommentError,"POST",net.CreatePostString(null,newcomflds));
		display.hideElement("comButton");
		display.showElement("comLoader");
	}
}
function onSubmitCommentResult() {
	response = net.CleanResponse(this.req.responseText);
	if ( response.indexOf('ERROR') == 0 )
		toggleInfo("",true,false);
	else if ( response == 'NO_SESSION' )
		toggleInfo(_global['ST_NO_SESSION'],true,false);
	else
		toggleInfo("",true,true);
	display.showElement("comButton");
	display.hideElement("comLoader");
	xGetElementById("commentdescription").value = "";
}
function onSubmitCommentError() {
	response = net.CleanResponse(this.req.responseText);
	xGetElementById("error").innerHTML = response;
	display.showElement("comButton");
	display.hideElement("comLoader");
	xGetElementById("commentdescription").value = "";
}
/******************************	VALIDATORS ******************************/
function comFrmValidated() {
	var isValid = true;
	//	Description
	if ( trim(xGetElementById("commentdescription").value) == '' ) isValid = elementValidator('commentdescription','',false,'form',fieldBorderWarningColor,fieldBorderColor);
	else b = elementValidator('commentdescription','',true,'form',fieldBorderWarningColor,fieldBorderColor);
	return isValid;
}

/******************************	AJAX DATA PARSERS ******************************/
function parseCommentInfo(arr) {
	if ( arr.length > 0 ) {
		for ( i = 0; i < arr.length; i++ ) {
			xGetElementById("comments_" + arr[i].postid).innerHTML = arr[i].html;
		}
	}
}
function finishLogin() {
	alert('logged in');
}
/************************** MANAGE TWINBLOG **************************/
function manageTwinblog(pid,tbid,fa) {
	xGetElementById("pid_blog").value = pid;
	xGetElementById("tbid_blog").value = tbid;
	xGetElementById("fuseaction").value = fa;
	document.blogFRM.target = "_blank";
	document.blogFRM.submit();
	document.blogFRM.target = "_self";
}
function addTwinblogPost(pid,tbid,fa) {
	xGetElementById("pid_blog").value = pid;
	xGetElementById("tbid_blog").value = tbid;
	xGetElementById("fuseaction").value = fa;
	document.blogFRM.target = "_blank";
	document.blogFRM.submit();
	document.blogFRM.target = "_self";
}