SBCOM.namespace('comments');

SBCOM.comments.collectionId = 344;

SBCOM.comments.MSG_NO_COMMENT = 'msg_comments_error';
SBCOM.comments.MSG_SUCCESS = 'msg_comments_success';
SBCOM.comments.MSG_SERVER_LOGIN = 'msg_comments_login_error';

SBCOM.comments.gdcUrl = '/ajax/gdc/';

SBCOM.comments.doCommentPost = function(form) {
	var data = SBCOM.comments.getComment(form);
	doDisplayLoader();
	if (!SBCOM.comments.isCommentValid(data)) {
		doRemoveLoader();
		return;
	}
	SBCOM.utils.toggleButton('btnSaveComment', false);
	// gdc is performing a redirect after success -- convert it to an xml blob and pass by "xml" param...
	data = {showComments:'false', cid:data.cid, xml:SBCOM.utils.getGdcXml(data.cid, data)};

	try { // Catch browser security exceptions.
		getJSON(SBCOM.comments.gdcUrl, SBCOM.utils.getQueryString(data), SBCOM.comments.handleCommentPost);
		SBCOM.utils.doLog('doCommentPost: '+SBCOM.comments.gdcUrl);
	} catch(error) {
		SBCOM.comments.handleSubmitDataErrors(null, error, null);
		doRemoveLoader();
	}

	//SBCOM.utils.toggleButton('btnSaveComment', false);
}

SBCOM.comments.getComment = function() {
	return {
		cid:SBCOM.comments.collectionId,
		type: $("c_type").get("value"),
		itemid: $("c_itemId").get("value"),
		comment: $$("textarea").get('value'),
		workflow_stages:$("c_workflow_stages").get("value"),
		workflow_name:$("c_workflow_name").get("value"),
		status_url:$("c_status_url").get("value"),
		suspend_url:$("c_suspend_url").get("value"),
		resume_url:$("c_resume_url").get("value"),
		publish_url:$("c_publish_url").get("value"),
		membername:SBCOM.utils.getUsername()
	}
}

SBCOM.comments.isCommentValid = function(data){
	if(SBCOM.utils.getUsername() == null) {
		//SBCOM.utils.doMessage(SBCOM.comments.MSG_SERVER_LOGIN);
		doDisplayLogin();
		return false;
	}

	var valid = (data != null && data.comment != null && data.comment != "" && data.itemid != null && data.itemid != "");
	if(!valid) SBCOM.utils.doMessage(SBCOM.comments.MSG_NO_COMMENT)
	return valid;
}

SBCOM.comments.handleSubmitDataErrors = function(request, error, exception) {
	SBCOM.utils.doLog("handleSubmitDataErrors: "+error);
}

SBCOM.comments.handleCommentPost = function(response, callerFunc) {
	if(!response) {
		SBCOM.utils.toggleButton('btnSaveComment', false);
		SBCOM.utils.doMessage(SBCOM.comments.MSG_NO_COMMENT);
	} else if(response.errors) {
		SBCOM.utils.toggleButton('btnSaveComment', false);
		var errorMessage = '';
		for(var error in response.errors) {
			SBCOM.utils.doLog('handleCommentPost: error: '+error);
			if(error.toLowerCase() == 'membername') {
				//errorMessage = SBCOM.comments.MSG_SERVER_LOGIN;
				doDisplayLogin();
				return;
			}
			errorMessage += response.errors[error];
		}

		SBCOM.utils.doLog('handleComment: errors: '+errorMessage)
		errorMessage = SBCOM.comments.MSG_NO_COMMENT;
		SBCOM.utils.doMessage();
	} else {
		//SBCOM.utils.hideElement('commentsForm');
		SBCOM.utils.doMessage(SBCOM.comments.MSG_SUCCESS);
	}	
}
