var msgLoaderObj = null;

function IconClick(obj)
{
    if(obj==null || typeof(obj)=="undefined")
        return false;

    var msg = GetParElement(obj, "msg");
    var code = msg.getAttribute("code");
    if ("undefined" == typeof(msg.closed))
	    msg.closed = false;
    msg.closed = !msg.closed;
    
    //update: change class without deleting the other classes
    var cname = (msg.closed) ? "Hide" : "Show";
    var cNameMsg=msg.className;
	if(cname=="Hide")
           cNameMsg=cNameMsg.replace("Show","Hide");
       else 
           cNameMsg=cNameMsg.replace("Hide","Show");
   //end of update
   
    ToggleNode(code, cNameMsg)
    SetIcon(obj, cname);
    
    return false;	       
}

function ToggleNode(parCode, cname)
{
    var code;
	var coll = document.getElementsByName("childMsgOf_" + parCode);
	if (coll == null) return;
	for (var i=0; i<coll.length; i++)
	{
		var msg = coll[i];
		if (!msg.closed)
		{
		    code=msg.getAttribute("code");
			ToggleNode(code, cname);
		}
		msg.className = cname;
	}
}

function LoadBody(ev,obj)
{   
    StopBubble(ev); //stop the bubbling of the event IE/MOZ      
    var e = obj;
    var hiddenFormObj = document.getElementsByName("msgHiddenForm");
    if (hiddenFormObj.length > 0)
    {
        hiddenFormObj = hiddenFormObj[0];
        if (hiddenFormObj.elements["inline"] && hiddenFormObj.elements["inline"].value==1)
        {
            if(!e) return false;
            
            if (e.nodeName == "IMG") e = e.parentNode;
    		
		    var msg = GetParElement(e, "msg");
		    if (msg)
		    {		                    
                var c = msg.getAttribute("code");
                mbody = document.getElementById("mbody_" + c);
                if (mbody)
                {                    
                    SubjectClick(mbody, e);
                } 
		    }
		    FalseEventValue(ev);// Prevent default actions
        }           
    }
}

function HideMsgBody(ev)
{
        var e = ev; 
	    var mbody = GetParElement(e, "mbody");
	    if (mbody)
		    ShowHideMsgBody(mbody);
	
	return false;	
}

function trim(obj) {
  return obj.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

function SubjectClick(mbody, hrefObj)
{
    
	if (!mbody || !hrefObj)
		return;
		
	if (trim(mbody.innerHTML) == "")
	{
				
		mbody.innerHTML = ("object" == typeof(Forum_Localization)) ? Forum_Localization.Loading : "Loading...";
				
		if (msgLoaderObj != null)
		{
			msgLoaderObj.innerHTML = "";
			msgLoaderObj.className = "Hide";
		}
		
		var cn = hrefObj.className;
		
		if (cn.indexOf("Seen") == -1)
		{
			cn +="Seen";
			hrefObj.className = cn;
		}
		
		msgLoaderObj = mbody;		
		
		var f=document.getElementsByName("msgHiddenForm")[0];
	    f.target="msgHiddenframe";
	    f.action=hrefObj.href;
	    f.submit();		
	}
	
	ShowHideMsgBody(mbody)
}

function ShowHideMsgBody(mbody)
{
    var cn=mbody.className;
    if(cn.indexOf("Show") != -1)
    {
        cn=cn.replace("Show","Hide");
        mbody.className=cn;
    }
    else if(cn.indexOf("Hide") != -1)
    {
        cn=cn.replace("Hide","Show");
        mbody.className="Show";
    }
}

function GetParElement(elem, type)
{
	while(elem)
	{
		if (elem.getAttribute("type")==type)
		{
			return elem;
		}
		elem = elem.parentNode;
	}
	return null;
}

function SetIcon(icon, cname)
{
	var src = icon.src;
	if (src.indexOf("_empty.gif") > 0) return;
	
	if (cname=="Show" && src.indexOf("closed") > 0)
	{
		src = src.replace("closed", "opened");
	}
	else if (cname=="Hide" && src.indexOf("opened") > 0)
	{
		src = src.replace("opened", "closed");
	}
	icon.src = src;
}

function CancelLoading(code, html)
{
	//var msg = document.all["mbody_" + code];
	var msg = document.getElementById("mbody_" + code);
	if (msg)
	{
		msg.innerHTML = html;
		msgLoaderObj = null;
		if (html.length > 0)
			LoadICQIcons();
	}
}

function LoadICQIcons()
{
	//var obj = document.all["icqIcon"];
	var obj = document.getElementsByName("icqIcon");
	if (obj)
	{
		if (obj.length)
		{
			for (var i=0; i < obj.length; i++)
			{
				SetICQIcon(obj[i]);
			}
		}
		else
		{
			SetICQIcon(obj);
		}
	}
}

function SetICQIcon(obj)
{
	if (!obj.state && obj.code)
	{
		obj.state = 1;
		obj.src = "http://online.mirabilis.com/scripts/online.dll?icq=" + obj.code;
	}
}

function ForumOnload()
{
	var html 	= "<iframe height=0 width=0 src='' id='msgHiddenframe' name='msgHiddenframe'></iframe>";
				//+ "<form action='' style='margin:0px' name=msgHiddenForm method=post><input type='hidden' name='inline' value='0'></form>"
	document.body.insertAdjacentHTML("afterBegin", html);
	LoadICQIcons();
}
ForumOnload();
//window.attachEvent("onload", ForumOnload);

//for message waiting during search

	function SrchSubmit()
	{
	    
		var f=document.getElementById("searchForm");
		if(f)
		{
		    setTimeout("ShowWaitMsg()", 1); // setTimeout is JS's mulit-thread. Fixes the fact that animated gif wasn't animated
			f.submit();
		}
		
		return false;
	}

function ShowWaitMsg()
{
	var cover=document.getElementById("idWaitMsgWinCover");
	var hol=document.getElementById("idWaitMsgConstruct");
	if(cover && hol)
	{
		var lWidth=hol.offsetWidth;
		var lHeight=hol.offsetHeight;
		//calculate display position
		var scrW=(document.body.offsetWidth-lWidth)/2;
		var scrH=(document.body.offsetHeight-lHeight)/2;
		scrH+=document.body.scrollTop - 40;
		scrW+=document.body.scrollLeft;
		//move message window
		if (hol.style) 
		{
		    hol.style.left=scrW;
		    hol.style.top=scrH;
		}
		if (cover.style)
		{
		    cover.style.left=scrW-20;
		    cover.style.top=scrH-20;
		    cover.style.width=lWidth + 40;
		    cover.style.height=lHeight + 40;
		}
		//show message window
		if (hol.style)
		    hol.style.visibility="visible";
		if (cover.style)
		    cover.style.visibility="visible";
	}
}

function HideMsgWait()
{
	var cover=document.getElementById("idWaitMsgWinCover");
	var hol=document.getElementById("idWaitMsgConstruct");
	if(cover && hol)
	{
			//show message window
		if (hol.style)
		    hol.style.visibility="hidden";
		if (cover.style)
		    cover.style.visibility="hidden";
    }
}

/* CHANGE UPDATE: (Lea) 10/12/2007 Decode encoded email address in order to send mail
 * when email link is clicked */

function sendEmail(encodedEmail)
{
    location.href = "mailto:" + decodeEmail(encodedEmail);     
}

function decodeEmail(encodedEmail)
{
    var email = "";
    for (i=0; i < encodedEmail.length;i+=2)
    {
        var letter = "";
        letter = encodedEmail.charAt(i) + encodedEmail.charAt(i+1)
        email += String.fromCharCode(parseInt(letter,16));
    }
    return email;
}
// on mouse over, show the real email address
function DecodeEmailMO(encodedEmail)
{
    setTimeout("DecodeEmailInStatus('"+encodedEmail+"')",1); // wait a little, othewise the automatic show in status bar shows 
}

function DecodeEmailInStatus(encodedEmail)
{
    window.status = decodeEmail(encodedEmail); 
}

function getParamCode(el) {
	var q = document.location.search;
	var qArr = q.split(el + "=");
	if (qArr.length > 1) {
		var val = qArr[1];
		if (val.indexOf("&") > -1) {
			val = val.slice(0,val.indexOf("&"));
		}
	}
	return val;
}
if (window.addEventListener) //DOM method for binding an event
 window.addEventListener("load", openMsgOnLoad, false);
else if (window.attachEvent) //IE exclusive method for binding an event
 window.attachEvent("onload", openMsgOnLoad);
else if (document.getElementById) //support older modern browsers
 window.onload=openMsgOnLoad;

////get paramter 'Item' from URL
function openMsgOnLoad() {
    var currentItem= getParamCode('item');
    if(currentItem)
    {
        openMsgItem(currentItem);
    }
}
 
////open chosen message 
// create by Yael (05/10/08)
function openMsgItem(currentItem) {
    var tr = document.getElementById('iCode_'+currentItem);
    if (tr) {
        var a = tr.getElementsByTagName("a")[0];
        var e = a;
        //code from LoadBody [altered]:
        if (msgHiddenForm && msgHiddenForm["inline"] && msgHiddenForm["inline"].value==1)
        {
            if (e.tagName == "IMG") e = e.parentElement;

            var msg = GetParElement(e, "msg");

            if (msg)
            {
                var c = msg.code;
                mbody = document.all["mbody_" + c];
                if (mbody)
                {
                    SubjectClick(mbody, e)
                    //cancel the delay
                    //end unterminated iframe request
                /*
                    var frame = document.getElementsByName("msgHiddenframe")[0];
                    if (frame) {
                        setTimeout(function(){frame.src="";},1000);
                    }
                    */
                }
            }
            e.returnValue = false;
        }
        //end code from LoadBody()
    }
}
 
// Prevents event bubble up only
// pE - event object
function StopBubble(pE)
{
	if (!pE) {
		if (window.event) pE = window.event;
		else return;
	}
	if (pE.cancelBubble != null) pE.cancelBubble = true;
	if (pE.stopPropagation) pE.stopPropagation();
}

//CHANGE (ZINA) 13/05/2010 -divide the StopEvent function to 2 main parts

// Prevent default actions
// pE - event object
function FalseEventValue(pE)
{
	if (!pE) {
		if (window.event) pE = window.event;
		else return;
	}
	if (pE.preventDefault) pE.preventDefault();
	if (window.event) pE.returnValue = false;
	if (pE.cancel != null) pE.cancel = true;
	return false;
}


