/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_showAddPostForm																									*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_showAddPostForm ()
{
	if (document.getElementById("addPost").style.display == "")
	{
		document.getElementById("addPost").style.display = "none";
		document.getElementById("addPostBtn").innerHTML = "<div>הוספת פוסט</div>";
	} 
	else 
	{
		picUpload_formName = "addPostForm";
		docUpload_formName = "addPostForm";

		oForm	= document.getElementById("addPostForm");

		oForm.filesNames.value = "";
		oForm.docName.value    = "";

		document.getElementById("addPost").style.display = "";
		document.getElementById("addPostForm").reset ();
		document.getElementById("addPostForm").addPostTitle.focus ();
		document.getElementById("addPostBtn").innerHTML = "<div>סגור</div>";
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_addNewPost																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_addNewPost (isReady)
{
	tinyMCE.triggerSave();

	var oForm 		  = document.getElementById("addPostForm");

	if (oForm.addPostTitle.value == "")
	{
		alert ("יש להזין כותרת לפוסט");
		oForm.addPostTitle.focus ();
		return false;
	}

	if (oForm.addPostContent.value == "")
	{
		alert ("יש להזין את תוכן הפוסט");
		return false;
	}

	if (picFileDoUpload)
	{
		alert ("יש להמתין לסיום טעינת התמונה");
		return false;
	}

	if (docFileDoUpload)
	{
		alert ("יש להמתין לסיום טעינת המסמך");
		return false;
	}

	var postContent = "<![CDATA[" + oForm.addPostContent.value + "]]>";

	var countChecked = 0;
	var addExpertiseTags = "";
	for (var i = 0; i < oForm.elements.length; i++)
	{
		if (oForm.elements[i].type == "checkbox" && oForm.elements[i].checked)
		{
			var field = oForm.elements[i].id;

			if (oForm.elements[i].className == "limited")
				countChecked++;

			addExpertiseTags += "<" + field + ">on</" + field + ">";
		}
	}

	if (countChecked > 3)
	{
		alert ("ניתן לשייך פוסט ל-3 קהילות לכל היותר");
		return false;
	}

	var xml = "<data>" +	
					"<command>privateBlog.addBlogPost</command>"		+
					"<title>"		+ oForm.addPostTitle.value 			+ "</title>" 		+
					"<content>"		+ postContent						+ "</content>" 		+
					"<isReady>"		+ isReady							+ "</isReady>" 		+
					"<filesDir>"	+ oForm.filesDir.value				+ "</filesDir>"		+
					"<fileName>"	+ oForm.filesNames.value			+ "</fileName>"		+
					"<docName>"		+ oForm.docName.value				+ "</docName>"		+
					addExpertiseTags +
			  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "blog_editPost_response");
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_editPost_response																								*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_editPost_response (i)
{
	xmlRequest.init(commonDecode(asyncHttpObjs[i].responseText));

	try
	{
		var returnCode = xmlRequest.getValue("returnCode");

		if (returnCode == "OK")
		{
			oForm = document.getElementById("searchPostsForm");
			window.location.href = "index2.php?id=32&numPosts=" + oForm.numPosts.value + 
								   "&searchText=" + oForm.searchText.value + "&isReady=" + oForm.isReady.value + "&lang=HEB";
		}
		else if (returnCode == "NOT_LOGIN")
		{
			alert("עליך להתחבר לאתר תחילה");
		}
	}
	catch (e)
	{
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_deletePost																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_deletePost (postId)
{
	if (confirm("האם אתה בטוח שברצונך למחוק את הפוסט"))
		window.location.href = "blogActions.php?action=deletePost&postId=" + postId + "&redirect=32";
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_updatePost																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_updatePost (isReady)
{
	tinyMCE.triggerSave();

	var oForm 		  = document.getElementById("editPostForm");

	if (oForm.editPostTitle.value == "")
	{
		alert ("יש להזין כותרת לפוסט");
		oForm.editPostTitle.focus ();
		return false;
	}

	if (oForm.editPostContent.value == "")
	{
		alert ("יש להזין את תוכן הפוסט");
		return false;
	}

	if (picFileDoUpload)
	{
		alert ("יש להמתין לסיום טעינת התמונה");
		return false;
	}

	if (docFileDoUpload)
	{
		alert ("יש להמתין לסיום טעינת המסמך");
		return false;
	}

	var postContent = "<![CDATA[" + oForm.editPostContent.value + "]]>";

	var addExpertiseTags = "";
	var countChecked = 0;
	for (var i = 0; i < oForm.elements.length; i++)
	{
		if (oForm.elements[i].type == "checkbox" && oForm.elements[i].checked)
		{
			var field = oForm.elements[i].id;

			if (oForm.elements[i].className == "limited")
				countChecked++;

			addExpertiseTags += "<" + field + ">on</" + field + ">";
		}
	}

	if (countChecked > 3)
	{
		alert ("ניתן לשייך פוסט ל-3 קהילות לכל היותר");
		return false;
	}

	var xml = "<data>" +	
					"<command>privateBlog.editBlogPost</command>"	+
					"<postId>"		+ oForm.postId.value			+ "</postId>" 		+
					"<title>"		+ oForm.editPostTitle.value 	+ "</title>" 		+
					"<content>"		+ postContent					+ "</content>" 		+
					"<isReady>"		+ isReady						+ "</isReady>" 		+
					"<filesDir>"	+ oForm.filesDir.value			+ "</filesDir>"		+
					"<fileName>"	+ oForm.filesNames.value		+ "</fileName>"		+
					"<docName>"		+ oForm.docName.value			+ "</docName>"		+
					addExpertiseTags +
			  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "blog_editPost_response");

	return false;
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_showEditPostForm																								*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_showEditPostForm (postId)
{
	oForm = document.getElementById("editPostForm");

	var postTitle	= document.getElementById("postTitle"   + postId).innerHTML;
	var postContent	= document.getElementById("postContent" + postId).innerHTML.replace(/#NL#/g, "\n");
	var postCats	= document.getElementById("postCats"    + postId).innerHTML;

	// first - unchecked all cats
	var dml    		= document.forms["editPostForm"];
	var noElements	= dml.elements.length;

	for(var i=0 ; i < noElements; i++) 
	{
		if (dml.elements[i].name.indexOf("expertise") != -1)
			dml.elements[i].checked = false;
	}

	// now - checked this post cats
	if (postCats != "")
	{
		var cats = postCats.split (" ");

		for (i = 0; i < cats.length; i++)
		{
			eval ("oForm.expertise" + cats[i]).checked = true;
		}
	}

	picUpload_formName = "editPostForm";
	docUpload_formName = "editPostForm";

	oForm.filesNames.value = "";
	oForm.docName.value    = "";

	oForm.postId.value 		  = postId;
	oForm.editPostTitle.value = postTitle;

	tinymce.EditorManager.get("editPostContent").setContent (postContent);

	document.getElementById("editPostTitle").innerHTML = "עדכון פוסט" + document.getElementById("postStatus" + postId).innerHTML;

	document.getElementById("myBlog").style.display   = "none";
	document.getElementById("editPost").style.display = "";
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_hideEditPostForm																								*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_hideEditPostForm ()
{
	document.getElementById("editPost").style.display = "none";
	document.getElementById("myBlog").style.display   = "";
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* collapseExpand																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function collapseExpand (oDiv)
{
	if (oDiv.className.indexOf("_expand") == -1)
	{
		oDiv.className += "_expand";

		oDiv.nextSibling.style.display = "";
	}
	else
	{
		oDiv.className = oDiv.className.substr(0,oDiv.className.length-7)

		oDiv.nextSibling.style.display = "none";
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* highlight																											*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function highlight (oDiv) 
{
	oDiv.className =  "over_" + oDiv.className
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* turnOff																												*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function turnOff (oDiv) 
{
	oDiv.className = oDiv.className.substr(5,oDiv.className.length-1)
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* showHideComments																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function showHideComments (col,i)
{
	oComments = document.getElementById("postComments" + i);

	if (oComments.style.display == "")
	{
		oComments.style.display 	= "none";
		col.childNodes[0].innerHTML = "הצגת תגובות";
	}
	else
	{
		oComments.style.display 	= "";
		col.childNodes[0].innerHTML = "הסתרת תגובות";
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* openCloseComment																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function openCloseComment (postId, i)
{
	var commentBody = document.getElementById("commentBody" + postId + "_" + i); 

   	if (commentBody.style.display == "") 
	   	commentBody.style.display = "none";
   	else
		commentBody.style.display = "";
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_showHideCommentForm																								*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_showHideCommentForm (isValidMember, postId)
{
	if (isValidMember == 0)
	{
			alert("עליך להתחבר לאתר תחילה כדי להגיב");
			return;
	}

	if (document.getElementById("addComment" + postId).style.display == "")
	{
		document.getElementById("addComment" + postId).style.display = "none";
		document.getElementById("postAddComment" + postId).innerHTML = "<div>הוספת תגובה</div>";
	} 
	else 
	{
		document.getElementById("addComment" + postId).style.display = "";
		document.getElementById("postAddComment" + postId).innerHTML = "<div>סגור</div>";
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_addNewComment																									*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_addNewComment (postId)
{
	var oForm 		  = document.getElementById("addCommentForm" + postId);

	if (oForm.commentTitle.value == "")
	{
		alert ("יש להזין כותרת");
		oForm.commentTitle.focus ();
		return false;
	}

	var xml = "<data>" +	
					"<command>privateBlog.addPostComment</command>"	+
					"<postId>"				+ postId		 				+ "</postId>" 			+
					"<title><![CDATA["		+ oForm.commentTitle.value 		+ "]]></title>" 		+
					"<content><![CDATA["	+ oForm.commentContent.value	+ "]]></content>" 		+
			  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "blog_addNewComment_response");
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_addNewComment_response																							*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_addNewComment_response (i)
{
	xmlRequest.init(commonDecode(asyncHttpObjs[i].responseText));

	try
	{
		var returnCode = xmlRequest.getValue("returnCode");

		if (returnCode == "OK")
		{
			window.location.reload ();
		}
		else if (returnCode == "NOT_LOGIN")
		{
			alert("עליך להתחבר לאתר תחילה כדי להגיב");
		}
	}
	catch (e)
	{
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_searchPosts																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_searchPosts ()
{
	document.getElementById("searchPostsForm").submit();
}

/* ------------------------------------------------------------------------------------------------ */
/* blog_closeAllComments																			*/
/* ------------------------------------------------------------------------------------------------ */
function blog_closeAllComments (postId)
{
	var i = 1;
	var oComment = document.getElementById("commentBody" + postId + "_" + i);

	while(oComment != null)
	{
		oComment.style.display = "none";
		i++;
		oComment = document.getElementById("commentBody" + postId + "_" + i);
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* blog_openAllComments																				*/
/* ------------------------------------------------------------------------------------------------ */
function blog_openAllComments (postId)
{
	var i = 1;
	var oComment = document.getElementById("commentBody" + postId + "_" + i);

	while(oComment != null)
	{
		oComment.style.display = "";
		i++;
		oComment = document.getElementById("commentBody" + postId + "_" + i);
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* deleteComment																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function deleteComment (pageId, commentId)
{
	htmlData	= "<div id='formTitle'><div>Confirm</div></div>"	+
				  "<div id='confirmDeleteMsg'>Are you sure you want to delete this post comment?</div>" +
				  "<div id='confirmDeleteBtns'>" +
					  "<div id='confirmDeleteYesBtn'>" + myContainer_getBtnHtml ("Yes", "doDeleteComment(" + commentId + ")") + "</div>" + 
					  "<div id='confirmDeleteNoBtn'>"  + myContainer_getBtnHtml ("No", "formWin.close()") + "</div>" +
				  "</div>";

	htmlData = myContainer_createForm (htmlData, 400, 359);
	formWin.create ("", "confirmDelete", htmlData, 420, 255);

	fades = new Array("mainTbl");

	fade ("mainTbl");
	
	var browser  = ((navigator.appName == "Netscape") ? "firefox" : "ie");

	if (browser == "ie")
	{
		fade ("listPageTitle");
		fade ("blogTop");
		fade ("blogBg");
		fade ("blogBottom");

		fades.push ("listPageTitle");
		fades.push ("blogTop");
		fades.push ("blogBg");
		fades.push ("blogBottom");
	}

	formWin.show (fades);
}

function doDeleteComment (commentId)
{
		window.location.replace("blogActions.php?action=deleteComment&commentId=" + commentId + "&redirect=71");
}

var commentDeleted = false;

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_showPostComments																								*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_showPostComments (postId)
{
	commentDeleted = false;

	var xml = "<data>" +	
					"<command>privateBlog.getPostComments</command>"	+
					"<postId>"	+ postId			+ "</postId>" 		+
			  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "blog_showPostComments_response");
}

function blog_showPostComments_response (i)
{
	xmlRequest.init(commonDecode(asyncHttpObjs[i].responseText));

	try
	{
		var returnCode = xmlRequest.getValue("returnCode");
		var htmlCode   = xmlRequest.getValue("htmlCode");

		if (returnCode == "OK")
		{
			document.getElementById("editPostComments_in").innerHTML 	= htmlCode;
	
			document.getElementById("myBlog").style.display   			= "none";
			document.getElementById("editPostComments").style.display 	= "";
		}
	}
	catch (e)
	{
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_hidePostComments																								*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_hidePostComments ()
{
	if (commentDeleted)
		window.location.reload ();
	else
	{
		document.getElementById("myBlog").style.display   			= "";
		document.getElementById("editPostComments").style.display 	= "none";
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_showHideCommentTxt																								*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_showHideCommentTxt (commentId)
{
	oTxt 	= document.getElementById("postComment_" + commentId);
	oAction = document.getElementById("postCommentTxt_" + commentId);

	if (oTxt.style.display == "none")
	{
		oTxt.style.display = "";
		oAction.innerHTML  = "הסתר";
	}
	else
	{
		oTxt.style.display = "none";
		oAction.innerHTML  = "הצג";
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_deletePostComment																								*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_deletePostComment (postId, commentId)
{
	abiloko_showPopupMsg ("אישור מחיקה", 
						  "האם אתה בטוח שברצונך למחוק תגובה זו?<br/>" + 
						  "<div onclick='blog_doDeletePostComment(" + postId + "," + commentId + ")'" 	+
						  "		onmouseout =\"this.childNodes[0].style.color='white'\" " +
						  "		onmouseover=\"this.childNodes[0].style.color='#eaff74'\" class='yesNoBtn' id='yesBtn'>" + 
						  	"<div style='color:white;'>כן</div></div>" + 
						  "<div onclick='abiliko_closePopup(\"popupMsg\")'" 	+
						  "		onmouseout =\"this.childNodes[0].style.color='white'\" " +
						  "		onmouseover=\"this.childNodes[0].style.color='#eaff74'\" class='yesNoBtn' id='noBtn'>" + 
						  	"<div style='color:white;'>לא</div></div>" + 
						  "<div style='clear:both'></div>");
}

function blog_doDeletePostComment (postId, commentId)
{
	commentDeleted = true;

	var xml = "<data>" +	
					"<command>privateBlog.deletePostComment</command>"	+
					"<postId>"		+ postId	+ "</postId>" 			+
					"<commentId>"	+ commentId	+ "</commentId>" 		+
			  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "blog_showPostComments_response");
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_subscribeToThisBlog																								*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_subscribeToThisBlog (blogId)
{
	var xml = "<data>" +	
					"<command>privateBlog.subscribeToBlog</command>"	+
					"<blogId>"		+ blogId	+ "</blogId>" 			+
			  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "blog_afterSubscribeToBlog");
}

function blog_afterSubscribeToBlog (i)
{
	xmlRequest.init(commonDecode(asyncHttpObjs[i].responseText));

	try
	{
		var returnCode = xmlRequest.getValue("returnCode");

		if (returnCode == "OK")
		{
			document.getElementById("blogJoinText").style.display = "none";
			document.getElementById("blogLeftText").style.display = "";
		}
		else if (returnCode == "NOT_LOGIN")
		{
			alert("עליך להתחבר לאתר תחילה");
		}
	}
	catch (e)
	{
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_unsubscribeToThisBlog																							*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_unsubscribeToThisBlog (blogId)
{
	var xml = "<data>" +	
					"<command>privateBlog.unsubscribeToBlog</command>"	+
					"<blogId>"		+ blogId	+ "</blogId>" 			+
			  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "blog_afterUnsubscribeToBlog");
}

function blog_afterUnsubscribeToBlog (i)
{
	xmlRequest.init(commonDecode(asyncHttpObjs[i].responseText));

	try
	{
		var returnCode = xmlRequest.getValue("returnCode");

		if (returnCode == "OK")
		{
			document.getElementById("blogJoinText").style.display = "";
			document.getElementById("blogLeftText").style.display = "none";
		}
		else if (returnCode == "NOT_LOGIN")
		{
			alert("עליך להתחבר לאתר תחילה");
		}
	}
	catch (e)
	{
	}
}


