
	/* This functions are used to work with the XML objects
	 * recieved from ActiveMQ and representing comments.
	 */

/* functions to get data elements from the XML object */
function getElementsByClassName(node, name) {
	var result = [];
	if (node.childNodes.length == 0 || name == '') {
		return result;
	}
	
	var index = 0;
	
	for (i = 0; i < node.childNodes.length; i++) {
		if (node.childNodes[i].className == name) {
			result[index] = node.childNodes[i];
			index = index + 1;
		}
	}
		
	return result;
}
	
function getXmlNodeValue(xmlObject, tagName) {
	if (xmlObject.getElementsByTagName(tagName).length > 0 && xmlObject.getElementsByTagName(tagName)[0].childNodes.length > 0)
		return stripXmlSpecialChars(xmlObject.getElementsByTagName(tagName)[0].childNodes[0].nodeValue);
	
	return null;
}
	
function getXmlNodeAttribute(xmlObject, tagName, attrName) {
	if (xmlObject.getElementsByTagName(tagName).length > 0)
		return stripXmlSpecialChars(xmlObject.getElementsByTagName(tagName)[0].getAttribute(attrName));
	
	return null;
}

/* creates comment{} hash map from the XML object */
function getCommentHashMapFromXml(m) {
	var comment = {};
		
	comment['id'] = m.getAttribute('id');
	comment['languageId'] = m.getAttribute('languageId');
		
	comment['summary'] = getXmlNodeValue(m, 'summary');
	comment['vip'] = getXmlNodeValue(m, 'vip');
	comment['createdDate'] = getXmlNodeValue(m, 'createdDate');
	comment['status'] = getXmlNodeValue(m, 'status');
	comment['author'] = getXmlNodeValue(m, 'author');
	comment['likesCount'] = getXmlNodeValue(m, 'likesCount');
				 
	if (m.getElementsByTagName('parentComment').length > 0) {
		var parentComment = {};
		parentComment['id'] = getXmlNodeAttribute(m, 'parentComment', 'id');
		parentComment['userSex'] = getXmlNodeValue(m, 'parentCommentUserSex');
		parentComment['summary'] = getXmlNodeValue(m, 'parentCommentSummary');
		parentComment['author'] = getXmlNodeValue(m, 'parentCommentAuthor');
			
		comment['parentComment'] = parentComment;
	}

	if (m.getElementsByTagName('user').length > 0) {
		var user = {};
		user['id'] = getXmlNodeAttribute(m, 'user', 'id');
		user['username'] = getXmlNodeValue(m, 'username');
		user['lname'] = getXmlNodeValue(m, 'lname');
		user['fname'] = getXmlNodeValue(m, 'fname');
		user['userPhoto'] = getXmlNodeValue(m, 'userPhoto');
		user['createdDate'] = getXmlNodeValue(m, 'userCreatedDate');
		user['updatedDate'] = getXmlNodeValue(m, 'userUpdatedDate');
			
		comment['user'] = user;
	}

	if (m.getElementsByTagName('news').length > 0) {
		var news = {};
		news['id'] = getXmlNodeAttribute(m, 'news', 'id');
		news['newsName'] = getXmlNodeValue(m, 'newsName');
		news['newsTransliteration'] = getXmlNodeValue(m, 'newsTransliteration');
		news['languageId'] = getXmlNodeAttribute(m, 'newsLanguage', 'id');
		
		if (m.getElementsByTagName('newsCategory').length > 0) {
			news['categoryId'] = getXmlNodeAttribute(m, 'newsCategory', 'id');
			news['categoryName'] = getXmlNodeValue(m, 'categoryName');
			news['categoryTransliteration'] = getXmlNodeValue(m, 'categoryTransliteration');
			news['categoryTypeName'] = getXmlNodeValue(m, 'categoryTypeName');
		}

		if (m.getElementsByTagName('newsSubcategory').length > 0) {
			news['subcategoryId'] = getXmlNodeAttribute(m, 'newsSubcategory', 'id');
			news['subcategoryName'] = getXmlNodeValue(m, 'subcategoryName');
			news['subcategoryTransliteration'] = getXmlNodeValue(m, 'subcategoryTransliteration');
		}

		comment['news'] = news;
	}
	else if (m.getElementsByTagName('tag').length > 0) {
		var tag = {};
		tag['id'] = getXmlNodeAttribute(m, 'tag', 'id');
		tag['tagName'] = getXmlNodeValue(m, 'tagName');
		tag['tagTransliteration'] = getXmlNodeValue(m, 'tagTransliteration');
		tag['languageId'] = getXmlNodeAttribute(m, 'tagLanguage', 'id');
		
		comment['tag'] = tag;
	}
	else if (m.getElementsByTagName('tagSubtype').length > 0) {
		var tagSubtype = {};
		tagSubtype['id'] = getXmlNodeAttribute(m, 'tagSubtype', 'id');
		tagSubtype['tagSubtypeName'] = getXmlNodeValue(m, 'tagSubtypeName');
		
		comment['tagSubtype'] = tagSubtype;
	}
		
	return comment;
}

/* creates news url (simple news and blogs) */
function getNewsUrl(translit, id, catTypeName) {
	var isBlog = false;
	if (catTypeName != null && catTypeName == "BLOG") {
		isBlog = true;
	}
		
	if (isBlog) {
		if (translit == null) {
			return "blogs/showBlog.do?objectId=" + id;
		}
		else {
			return "blogs/showBlog.do?" + translit + "&objectId=" + id;
		}
	}
	else {
		if (translit == null) {
			return "home/showSingleNews.do?objectId=" + id;
		}
		else {
			return "home/showSingleNews.do?" + translit + "&objectId=" + id;
		}
	}
}

/* creates tag url (ex. politicans) */
function getTagUrl(translit, id) {
	if (translit == null) {
		return "tags/showPolitician.do?tagId=" + id;
	}
	
	return "tags/showPolitician.do?" + translit + "&tagId=" + id;
}

/* creates tag subtype url */
function getTagSubtypeUrl(id) {
	return "tags/showRating.do?tagSubtypeId=" + id;
}

/* creates category url (and also blogs) */
function getCatUrl(translit, id, catTypeName) {
	var isBlog = false;
	if (catTypeName != null && catTypeName == "BLOG") {
		isBlog = true;
	}
		
	if (isBlog) {
		return "blogs/showByAuthors.do";
	}
	else {
		if (translit == null) {
			return "home/showSingleNews.do?objectId=" + id;
		}
		else {
			return "home/showNews.do?" + translit + "&categoryId=" + id;
		}
	}
}

/* ... subcategory url */
function getSubcatUrl(translit, id) {
	if (translit == null) {
		return "home/showSubNews.do?subcategoryId=" + id;
	}
	else {
		return "home/showSubNews.do?" + translit + "&subcategoryId=" + id;
	}
}

/* same as freemarker function */
function addLanguageToUrl(url, lang) {
	if (lang == "uk") {
		return url;
	}
	
	if (url.indexOf("?") != -1) {
		return url + "&lang=" + lang;
	}
	else {
		return url + "?lang=" + lang;
	}
}

/* get quoted comment summary */
function getSubComment(txt) {
	var stIn = txt.indexOf("[quote]");
	var fnIn = txt.indexOf("[/quote]");
	
	if (stIn != -1 && fnIn != -1 && (stIn < fnIn)) {
		return txt.substring(stIn, fnIn).replace("[quote]", "");
	}
	else {
		return "";
	}
}

/* returns comment summary without quotated part */
function getPlainComment(txt) {
	var fnIn = txt.lastIndexOf("[/quote]");
	if (fnIn != -1) {
		return txt.substring(fnIn).replace("[/quote]", "");
	}
	else {
		return "";
	}
}
