//created by karin ludvish- functions for ajax 

var msgID;
var FORUM_PAGER_HEIGHT = 13;
var BASE_FORUM_PAGER_OFFSET_HEIGHT = 625;

var arrOpenPostsId = new Array();

function forumThread(id, srcImg){
	this.id = id;
	this.srcImg = srcImg;
}

function handleResponse_bringMsgTxtData (req)
{
    var div = $('txtImg' + msgID);
	
	div.innerHTML = data = req.responseText.replace(/&lt;/g,'<').replace(/&gt;/g,'>');
				
	data.evalScripts();	
	
	updateForumHeight();
	
}

function expandNode(e, elm)
{
	var $elm = $(elm);
	$subNode = $elm.up('div[id^="forum_header_div_"]').next('.subNode');
	if($subNode) 
	{
		$subNode.toggle();
		//$subNode.setStyle({border:'1px solid yellow'});
		
	}
	e = e || window.event;
	if(e) Event.stop(e);
	
	var imgSRC = $elm.src;
	
	if(imgSRC.indexOf('plus')>-1)
		$elm.src = imgSRC.replace('plus','minus');	
	else
		$elm.src = imgSRC.replace('minus','plus');	
}


function bringMsgTxtData(id)
{
		msgID=id;
		var obj = $('msgTxt'+id);
		var str=  obj.style.display;

		if (str.indexOf("non")!=-1)
		{
			$('bringMsgTxtFRM').postID.value=id;
		    obj.show();
		}
		else
		{
			 obj.style.display='none'; 
			 return;
		}
		
		new VignettePortal.AJAXClient().sendForm(document.forms['bringMsgTxtFRM'],handleResponse_bringMsgTxtData)
}

function showMessage(element, message_id, bHasContent)
{
	element.blur();
	// stop the active player (if existing)
	
	//stopActiveForumPlayer();
	KPlayer_stopAllPlayers();

	msgID = message_id;
	// update relevant div
	
	
	//var div = document.getElementById("forum_div_" + message_id);
	var div = $("forum_div_" + message_id);
	//alert(div.innerHTML);
	
	
	// update relevant image
	//var img = document.getElementById("forum_img_" + message_id);
	var img = $("forum_img_" + message_id);
	
	var srcImg;
	var selectedImg;
	
	if (bHasContent)
		srcImg = "side_bar_bullet_blue.gif";
	else
		srcImg = "side_bar_bullet_blue_2.png";	
	
	selectedImg = "side_bar_bullet_white.gif";
	
	if (div.style.display == 'none') // show div
	{
		div.style.display = 'block';
		$('bringMsgTxtFRM').postID.value = message_id;
		element.className = element.className.replace("forum_thread", "forum_opened_comment_header");
		img.src = img.src.replace(srcImg, selectedImg);
		initializeDIV("forum_div_" + message_id);// init lightbox for every msg that gets displayd (FF dose this automatically). works grate, alert Erez if it dosen't.
		

		arrOpenPostsId[arrOpenPostsId.length] = new forumThread(message_id, srcImg);
		
		if(navigator.userAgent.indexOf("Firefox") != -1) 
		{
			// hide pager white div is loading
			var pager_div = $('forum_pager_holder');
			if(pager_div) pager_div.hide();
		}
		
		if(!div.wasLoaded){
			
			new VignettePortal.AJAXClient().sendForm(document.forms['bringMsgTxtFRM'],handleResponse_bringMsgTxtData);
			div.wasLoaded = true;
		}else{
			updateForumHeight();
		}
		
	}
	else // hide div
	{
		div.style.display = 'none'; 
		//div.innerHTML="";
	/*	if(isExplorer()){
			stopVideo();
			}
		else{
			if (document.getElementById('video_waitscreen_'+playerID).style.display=="none"){
				document.getElementById('video_waitscreen_'+playerID).style.display="block";
				$('dPlayer_'+playerID).innerHTML="";
				$('player_play_image_'+playerID).src=play;
				}
		}
		*/
		element.className = element.className.replace("forum_opened_comment_header", "forum_thread");
		img.src = img.src.replace(selectedImg, srcImg);
		updateForumHeight();
	}
	
}

	function handleResponse_pager(req)
    {
    	$('forum_content_td').update(req);
		
		$$('#forum_content_td .forum_sticky').each( function(obj){ expandNode(null, obj); });
		
		//stopActiveForumPlayer();
			
		//KPlayer_stopAllPlayers();
		isPlayerActive = false;
		curAddMessageDivID = orgAddMessageDivID;
		
		updateForumHeight();
	}
	
	 // used at starAnswers.jsp
    function handleResponse_refreshStarAnswers(req)
    {
        $('starAnswers_td').innerHTML = req.responseText;    
    }
	
    function refreshStarAnswers()
    {
    	new VignettePortal.AJAXClient().sendForm(document.forms['starAnswersFRM'],handleResponse_refreshStarAnswers);
    }
    // used at hosefTguvaFRM.jsp
	

   function handle_hosef_tguva_frm(req)
   {
   	   //var status=req.activeElement.innerHTML;//gets the response text
   	   //var status=req.documentElement.innerHTML; //gets the response text
   	   //var res=req.responseText; //gets the response text
   	   //alert(res);
 
		forumMessageSent(req);  
    }
    
	function handle_hosef_talkback_frm(req)
	{
		var status=req.responseText;

		talkbackMessageSent(status);
	}

	function sendMessage()
	{
		new VignettePortal.AJAXClient().sendMultiPartForm(document.forms['hosefTguvaForm'],handle_hosef_tguva_frm,'text/html');
	}
	
	function sendTalkback()
	{
		new VignettePortal.AJAXClient().sendForm(document.forms['hosefTguvaForm'],handle_hosef_talkback_frm,function(){talkbackMessageSent('');});
	}
 //for save forum
	function saveForumtoKeshetIshi()
	{
    	new VignettePortal.AJAXClient().sendForm(document.forms['addForumtoKeshetIshi'],handleResponseMessageBox);
    }
	function recommandPost()
    {
    	new VignettePortal.AJAXClient().sendForm(document.forms['recommandPostFRM'],handleResponseMessageBox);
    }
    function saveMessageToKehetIshi()
    {
    	new VignettePortal.AJAXClient().sendForm(document.forms['addMsgToKeshetIshi'],handleResponseMessageBox);
    }
    function saveContenttoKeshetIshi()
	{
		var username = "";
		if (usernameOnPage != null)
		{
			username = usernameOnPage;
		}
		document.forms['statusBar_save_content'].user.value=username;		
    	new VignettePortal.AJAXClient().sendForm(document.forms['statusBar_save_content'],handleResponseMessageBox);
    }
    //written by karin ludvish- function for ajax

function addMsgToKeshetIshi(id,count)
{
	$('addMsgToKeshetIshi').postID.value = id;
	$('addMsgToKeshetIshi').count.value = count;
	saveMessageToKehetIshi();
}
function recommandThisPost(id)
{
	$('recommandPostFRM').postID.value = id;
	recommandPost();
}


function showLink(pId)
{
	var e = $(pId);
	if (navigator.appName=="Netscape")
	{
		e.click = function() 
		{
		var evt = this.ownerDocument.createEvent('MouseEvents');
		evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
		this.dispatchEvent(evt);
		}
	}	
	e.click();
}

function encodeString(objid,destId)
{
	var obj = $(objid);
	var string = obj.value;
	var text =  encodeURIComponent( string );
	var destObj = $(destId);
	destObj.value = text;
}

function setSelectedOpt(selectId,parameterId)
{
	var selectObj = $(selectId);
	var selectedItem = selectObj.selectedIndex;
	var selectedValue = '';
	// in case not selected
	if (selectedItem != -1)
	{
		selectedValue = selectObj.options[selectedItem].value;
	}
	var parameterObj = $(parameterId);
	parameterObj.value = selectedValue;
	
}
	
function commonCallBack()
{
	//alert('back from submition!!!');
}

function checkErrors()
{		
	//td.className = 	'lb_form_input lb_error_message';		
}
function createComplaintAjax(formName,callBackFunction)
{
	setSelectedOpt('complaintSelect','complaintId');
	encodeString('message1','messageCoded');
	submitAjaxForm(formName,callBackFunction);
}

function signInAjax(formName,callBackFunction)
{
	encodeString('firstName','firstName');
	encodeString('lastName','lastName');
	submitAjaxForm(formName,callBackFunction);
}

function submitAjaxForm(formName,callBackFunction)
{
	//checkErrors();
	new VignettePortal.AJAXClient().sendForm($(formName), callBackFunction);	
}
	
function submitMakoLogin(formName,callBackFunction)
{
	//checkErrors();
	new VignettePortal.AJAXClient().sendForm(document.forms[formName], callBackFunction);	
}
    function submitMakoLogin()
  {
    	new VignettePortal.AJAXClient().sendForm(document.forms['lb_submitMako_form'],closeLightBox);
    }
	
function startCallback() {return true;}

function showTheNewMessage(a,b)
{
	var show = "showAddNewMessage('" + a + "','" + b + "')";
	
	//setTimeout(show,0);		
	registrate(show,1); // 1 mean the activation number
}
function showTheRecommandation(a)
{
	var rec = "recommandThisPost('" + a + "')";
	registrate(rec,1); // 1 mean the activation number
}

function getElementChildren(element)
{
	var children = [];
	var child = element.firstChild;
	while (child)
	{
		if (child.nodeType == 1 /* Node.ELEMENT_NODE */)
			children.push(child);
		child = child.nextSibling;
	}
	return children;
};

function updateForumHeight(){
	if(navigator.userAgent.indexOf("Firefox") == -1)	return;

	// update new height for forum holder (height is not updated in firefox without it);
	var dForum = $('dFurum');
	var forum_main_div = $('forum_content_td');
	var newHeight = forum_main_div.style.height;
	if (dForum.offsetHeight > BASE_FORUM_PAGER_OFFSET_HEIGHT)
		forum_main_div.style.height = (dForum.offsetHeight + FORUM_PAGER_HEIGHT) + "px";
	
	var pager_div = $('forum_pager_holder');
	if (pager_div.style.display == 'none')
		pager_div.style.display = 'block';
}
