<!--

function collapseShoutBox()
{
	var oElement = document.getElementById("divShoutBox");
	var oFade = null;
	var sBtnSrc = "";

	with (oElement.style)
	{
		//m_bShoutBoxOn = !(height == "0px")
		if (height == "0px")
		//if (m_bShoutBoxOn)
		{
			oFade = new Animator().addSubject(new NumericalStyleSubject(oElement, "height", "0px", "200px"));
			sBtnSrc = m_sCurSkinPath + "btn_power_on.gif";
			m_bShoutBoxOn = true;
		}
		else
		{
			oFade = new Animator().addSubject(new NumericalStyleSubject(oElement, "height", "200px", "0px"));
			sBtnSrc = m_sCurSkinPath + "btn_power.gif";
			m_bShoutBoxOn = false;
		}
	}
	
	if (m_bShoutBoxOn)
		refreshShoutBox();
	else
	{
		// The shoutbox widget is being closed. Ensure the smileys and BB code layers are hidden:
		document.getElementById("divSmileys").style.display = "none";
		document.getElementById("divBBCodes").style.display = "none";
	}

	// Blank and disable input boxes when closing the widget, enable them when opening:
	with (document.forms["frmShoutBox"])
	{
		txtNickname.disabled = !m_bShoutBoxOn;
		txtMsg.disabled = !m_bShoutBoxOn;
		if (m_bShoutBoxOn)
			txtNickname.focus();
		else
		{
			txtNickname.value = "";
			txtMsg.value = "";
		}
	}
		

	oFade.play();
	document.getElementById("imgPowerBtn").src = sBtnSrc;
	
	makeRequest(m_sSBScriptPathName + "?action=power&state=" + ((m_bShoutBoxOn)? "on" : "off"));
}

// Objective: Appends a message to the shoutbox table.
// Arguments: -sNickname, string; sender's nickname.
//			  - sMessage, string; sender's message.
function appendMessage(sNickname, sMessage)
{
	var oShoutTable = document.getElementById("tblShoutBox");
	var oRow = oShoutTable.insertRow(oShoutTable.rows.length);
	var oCell = document.createElement("td");
	//oCell.style.border = "1px solid silver"
	oCell.className = "shoutitem";

	oCell.innerHTML = "<span class=\"shout_nick\">" + sNickname + "</span>:<br>" + sMessage;
	oRow.appendChild(oCell);
	
}

// Objective: Checks the shoutbox form.
// Arguments: None.
// Returns:   boolean.
function checkShoutboxForm()
{
	with (document.forms["frmShoutBox"])
	{
		if (txtNickname.value.length == 0)
		{
			alert("Enter a nickname!");
			txtNickname.focus();
			return false
		}
		if (txtMsg.value.length == 0)
		{
			alert("Enter a message!");
			txtMsg.focus();
			return false
		}
		return true
	}
}

// Objective: Clears the shoutbox table.
// Arguments: None.
function clearBox()
{
	var oShoutTable = document.getElementById("tblShoutBox");
	var n = oShoutTable.rows.length;

	for (var i=0; i<n; i++)
	{
		oShoutTable.deleteRow(0);
	}
}

// Objective: Retrieves the position of an arbitrary page element. This function is needed because
//		      the offsetLeft and offsetTop properties are slightly different in IE and Mozilla, so
//			  using this function the position of the element is returned correctly.
// Arguments: oElement, object.
// Returns:   Array, returns an array containing the x and y pos as x and y props.
function getElementPosition(oElement)
{
	var posX = 0;
	var posY = 0;
	while (oElement != null)
	{
		posX += oElement.offsetLeft;
		posY += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return {x:posX,y: posY}
}

// Objective: Gets the selected test from the Msg edit box.
// Arguments: None.
// Returns:   string.  
function getMsgText()
{
	var oEdit = document.forms["frmShoutBox"].elements["txtMsg"];

	if (oEdit.createTextRange && oEdit.caretPos) {
		return oEdit.caretPos.text;
	} else if (typeof oEdit.selectionStart != 'undefined'){
		return oEdit.value.substr(oEdit.selectionStart,oEdit.selectionEnd-oEdit.selectionStart)
	}
	return "";
}

// Objective: Handles the server response.
// Arguments: - httpRequest, XMLHTTPRequest.
//			  - bIsPostRequest, boolean; true if we're handling a POST request. Defaults to false;
function handleResponse(httpRequest, bIsPostRequest)
{

	switch (httpRequest.readyState)
	{
		case 0:
		case 1:
		case 2:
		case 3:
			//alert("Peppe");
			document.getElementById("spanShoutboxStatus").innerText = "Loading...";
			break;

		case 4:
			document.getElementById("divShoutboxStatus").style.display = "none";

			if (httpRequest.status == 200)
			{
				//alert(httpRequest.responseText);

				// The shoutbox page returns ok when turning on/off the shoutbox: 
				if (httpRequest.responseText == "OK") return

				var xmldoc = httpRequest.responseXML;
				var sErrorMsg = "";

				// Safari and Chrome need to parse the response text in order to generate an XML document:
				if (navigator.appName == "Netscape" && navigator.appVersion.indexOf("AppleWebKit") > -1)
				{
					
					oParser = new DOMParser();
					xmldoc = oParser.parseFromString(httpRequest.responseText, "text/xml");
				}

				var oItemNodes = xmldoc.getElementsByTagName("Item");
				if (bIsPostRequest)
				{
					var oErrorNodes = xmldoc.getElementsByTagName("ErrorMessage");
					if (oErrorNodes.item(0).firstChild)
						sErrorMsg = oErrorNodes.item(0).firstChild.data
				}

				// Check last shout ID. If there aren't modification in the displayed list, leave it untouched:
				var lLastShoutID = oItemNodes.item(i).getElementsByTagName("ShoutID").item(0).firstChild.data;

				if (lLastShoutID == m_nLastShoutID)
				{
					if (sErrorMsg.length > 0)
						alert(sErrorMsg);
					return
				}
				else
					m_nLastShoutID = lLastShoutID

				// Clear the shoutbox contents:
				clearBox()
				// Records are sent in reverse order due to the SQL query layout:
				for (var i=oItemNodes.length - 1; i>=0; i--)
				{
					// The selectSingleNode function isn't inplemented in the Mozilla XML DOM. The following code works both on IE and FireFox:
					//alert(oItemNodes.item(i).getElementsByTagName("ShoutMessage").item(0).firstChild.data)
					appendMessage(oItemNodes.item(i).getElementsByTagName("ShoutNickname").item(0).firstChild.data + "<br>" + oItemNodes.item(i).getElementsByTagName("ShoutDate").item(0).firstChild.data, oItemNodes.item(i).getElementsByTagName("ShoutMessage").item(0).firstChild.data); 
				}
				scrollBox();
				
				// Reset message text:
				document.forms["frmShoutBox"].elements["txtMsg"].value = "";

				if (sErrorMsg.length > 0) alert(sErrorMsg)

			}
			else
			{
				alert("ShoutBox HTTP error: " + httpRequest.status);
				// document.getElementById("divDebugInfo").innerText = httpRequest.responseText;
			}	// --> Status isn't 200 OK
	}
}

// Objective: Initializes the shoutbox. Called at page load.
// Arguments: None.
function initShoutBox()
{
	m_bShoutBoxOn = document.getElementById('divShoutBox').style.height != '0px';
	refreshShoutBox()
}

// Objective: Encloses the selected text into a given BB code tag in the message edit box.
// Arguments: - sTag, string; Tag to insert. Can be "b" for bold, "i" for italic, "u" for underline or "s" for strikethru, or also "color". In that case, sColorCode must also be specified.
//			  - sColorCode, string. HTML color code in HTML hex form, or also as named color. This argument is only used if sTag is "color".
function insertBBFormatCode(sTag, sColorCode)
{
	var sText = getMsgText();
	var oEdit = document.forms["frmShoutBox"].elements["txtMsg"];
	var sAddText = "";

	if (m_bFormatBasic)
	{
		sAddText = sText
	}
	else
	{
		sAddText = prompt("Text to be formatted.", sText);
		if (sAddText == null) return
	}
	if (sTag == "color")
		sAddText = "[color=\"" + sColorCode + "\"]" + sAddText + "[/color]";
	else
		sAddText = "[" + sTag + "]" + sAddText + "[/" + sTag + "]";

	insertText(sAddText, oEdit);
}

// Objective: Inserts a text at caret pos if supported, else text is appended at the end.
// Arguments: - sText, string.
//			  - oEdit, object (can be either a text input or a textarea element).
function insertText(sText, oEdit) {
	if (typeof oEdit.selectionStart != 'undefined')
	{ // if it supports DOM2
		start = oEdit.selectionStart;
		end = oEdit.selectionEnd;
		oEdit.value = oEdit.value.substr(0,oEdit.selectionStart) + sText + oEdit.value.substr(oEdit.selectionEnd);
		oEdit.focus();
		oEdit.selectionStart = ((start - end) == 0) ? start + sText.length : start;
		oEdit.selectionEnd = start + sText.length;
	}
	else
	{
		if (oEdit.createTextRange && oEdit.caretPos)
		{
			var caretPos = oEdit.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? sText + ' ' : sText;
		}
		else
		{
			oEdit.value += sText;
		}
		//oEdit.focus(caretPos);
	}
}

// Objective: Posts a shout.
// Arguments: None.
function postShout()
{	
	var sName = encodeURI(document.forms["frmShoutBox"].elements["txtNickname"].value);
	var sMsg = encodeURI(document.forms["frmShoutBox"].elements["txtMsg"].value);
	//strhp=encodeURI(document.forms["frmShoutBox"].elements["URL"].value);
	
	var sParam = "name=" + sName + "&message=" + sMsg //+ "&URL=" + strhp;
	makeRequest(m_sSBScriptPathName + "?action=post", "POST", sParam);
	showShoutboxStatusLayer();
}

// Objective: Refreshes the shoutbox contents and restarts the timer if the shoutbox widget is on.
// Arguments: none.
function refreshShoutBox()
{
	if (m_bShoutBoxOn)
	{
		makeRequest(m_sSBScriptPathName + '?action=get', "GET");
		self.setTimeout("refreshShoutBox()", 60000)
	}
}

// Objective: Scrolls all way down the contents of the ShoutBox layer.
// Arguments: None.
function scrollBox()
{
	with (document.getElementById("divShoutBox"))
	{
		scrollTop = scrollHeight;
	}
}

// Objective: Shows or hides an element, typically a DIV layer.
// Arguments: - sId, string; DHTML id of the element to show/hide. If the element's display style is block, then the element is hidden, otherwise it gets faded in.
//			  - bHide, boolean; forces hiding the element regardless of its current display state.
function showHideElement(sId, bHide)
{
	if (!m_bShoutBoxOn) return

	var oElement = document.getElementById(sId);

	if ((bHide) || (oElement.style.display == "block"))
	{
		oElement.style.display = "none";

		if (sId == "divBBCodes")
			document.getElementById("imgBtnBBCode").src = m_sCurSkinPath + "btn_bbcode.gif";
		else
			document.getElementById("imgBtnSmileys").src = m_sCurSkinPath + "btn_smileys.gif";

		return
	}

	var oBox = document.getElementById("divShoutBox");
	var aPos = getElementPosition(oBox);

	var oFade = new Animator();
	oFade.addSubject(new NumericalStyleSubject(oElement, "opacity", "0.25", "1"));
	oFade.addSubject(new DiscreteStyleSubject(oElement, "display", "none", "block"));
	oElement.style.top = aPos.y + oBox.offsetHeight - 91 + "px"; //"100px";
	oElement.style.zIndex = 1;

	if (sId == "divBBCodes")
	{
		document.getElementById("divSmileys").style.display = "none"; //document.getElementById("divSmileys").style.zIndex = 0;
		document.getElementById("imgBtnSmileys").src = m_sCurSkinPath + "btn_smileys.gif";
		document.getElementById("imgBtnBBCode").src = m_sCurSkinPath + "btn_bbcode_on.gif";
	}
	else		
	{
		document.getElementById("divBBCodes").style.display = "none"; //document.getElementById("divBBCodes").style.zIndex = 0;
		document.getElementById("imgBtnSmileys").src = m_sCurSkinPath + "btn_smileys_on.gif";
		document.getElementById("imgBtnBBCode").src = m_sCurSkinPath + "btn_bbcode.gif";
	}

	oFade.play();
}

// Objective: Positions the status layer element dynamically and displays it.
// Arguments: None.
function showShoutboxStatusLayer()
{
	var oBox = document.getElementById("divShoutBox");
	var aPos = getElementPosition(oBox);

	with (document.getElementById("divShoutboxStatus").style)
	{
		left = aPos.x + 24 + "px";
		top = aPos.y + 90 + "px";
		display = "block"
	}
}

// Objective: Stores the caret pos of a text box adding a caretPos property to the box object itself. This function must be changed on the
//			  onselect, onclick, onkeyup and onchange even handlers of the target box in order for the insertText function to work properly.
// Arguments: oTextBox, object; can be either a TextArea or text Input element.
function storeCaret(oTextBox)
{
	if (oTextBox.createTextRange)
		oTextBox.caretPos = document.selection.createRange().duplicate();
}

// Init some global vars:
var m_nLastShoutID = 0;
var m_bFormatBasic = true;
var m_bShoutBoxOn = false;
var m_sCurSkinPath = "shoutbox/skins/default/"
var m_sSBScriptPathName = "shoutbox/shoutbox.asp"

//-->