var strToolbarColor = "";
var strCancelEdit = "";
var oSpellRange = null;
var oPos = null;
var oSaveFunction = "";
var boolMove = false;
var boolChange = false;
var startx = 0;
var starty = 0;
var oSelection = null;
var oToolbar = null;
var oToolbarTable = null;
var oCss = null;
var oCssDiv = null;
var oFontDiv = null;
var oSymbolDiv = null;
var oInfoDiv = null;
var strClass;
var oEditing = null;
var boolSelect = false;
var oWindow = null;

var maxRedos = 50;
var aTitle = new Array(maxRedos);
var aText = new Array(maxRedos);
var aUndo = new Array(2);
var aTitle2 = new Array(maxRedos);
var aText2 = new Array(maxRedos);
var aRedo = new Array(2);
var boolRedo = false;
var UndoCount = -1;	
var RedoCount = -1;
var oUndoTimer = null;
var strStoreText;
var boolAddOn = false;
var oDivUndoTop;
var oDivUndo;
var oDivRedoTop;
var oDivRedo;
var oSpanDos;
var oPostback = false;

function initializeToolbar() {
	oToolbar = document.getElementById("divToolbar");
	var oTool = oToolbar;
	oToolbarTable = document.getElementById("tblToolbar");
	oCss = document.getElementById("spnCss");
	oCssDiv = document.getElementById("divCss");
	oFontDiv = document.getElementById("divFont");
	oSymbolDiv = document.getElementById("divSymbol");
	oTool.style.display = 'none';
	aUndo[0] = aTitle;
	aUndo[1] = aText;
	aRedo[0] = aTitle2;
	aRedo[1] = aText2;
	
	if (getCookie("toolbar_pos_x") != null && getCookie("toolbar_pos_y") != null && getCookie("toolbar_pos_x") != "" && getCookie("toolbar_pos_y") != "" && getCookie("toolbar_pos_x") != "0" && getCookie("toolbar_pos_y") != "0")
		move(oTool, getCookie("toolbar_pos_x"), getCookie("toolbar_pos_y"));
	else
		dockTop();
}
function ToolbarMouseOver(oCell) {
    if (boolMove == true)
        return;
    strToolbarColor = oCell.style.border;
    oCell.style.border = "solid 1px #FF6600";
}
function ToolbarMouseOut(oCell) {
    if (boolMove == true)
        return;
    if (boolChange == false)
        oCell.style.border = strToolbarColor;
    else
        boolChange = false;
}
function ToolbarMouseUp(oCell) {
    if (boolMove == true)
        return;
    boolChange = true;
    oSelection = document.selection.createRange();
    callCommand(oCell.firstChild.alt, oCell.firstChild.title.toUpperCase());
}

function showCss(oCell) {
    if (boolMove == true)
        return;
	oSelection = document.selection.createRange();
	showMenuDown(oCssDiv, oCell, "RIGHT", 27);
}

function showFont(oCell) {
    if (boolMove == true)
        return;
	oSelection = document.selection.createRange();
	showMenuDown(oFontDiv, oCell, "RIGHT", 27);
}

function ToolbarDown() {
	startx = event.x - findPosX(oToolbar);
	starty = event.y - findPosY(oToolbar);
	boolMove = true;
	setUnselectable("on");
	document.attachEvent("onmouseup", ToolbarUp);
	document.body.attachEvent("onmouseleave", ToolbarUp);
}
function ToolbarUp() {
    if (boolMove == true) {
	    boolMove = false;
	    document.detachEvent("onmouseup", ToolbarUp);
	    oToolbar.style.removeExpression("left");
	    oToolbar.style.removeExpression("top");
	    setCookie("toolbar_pos_x", oToolbar.style.posLeft, 30);
	    setCookie("toolbar_pos_y", oToolbar.style.posTop, 30);
    	setUnselectable("off");
	}
}
function dockTop() {
	boolMove = false;
	document.detachEvent("onmouseup", ToolbarUp);
	move(oToolbar, 0, 0);
	setCookie("toolbar_pos_x", "0", 30);
	setCookie("toolbar_pos_y", "0", 30);
	oToolbar.style.setExpression("left", "scrollLeft-scrollLeft+document.body.scrollLeft", "JavaScript");
	oToolbar.style.setExpression("top", "scrollTop-scrollTop+document.body.scrollTop", "JavaScript");
	setUnselectable("off");
}
function moveToolbar() {
	if(boolMove == true) {
		if (event.y - starty < 0) {
			dockTop();
			return;
		}
	    move(oToolbar, event.x - startx, event.y - starty);
	}
}
function move(oTool, intLeft, intTop) {
	oTool.style.posLeft = intLeft;
	oTool.style.posTop = intTop;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
function setUnselectable(strSelect){
	for (ii=0; ii<document.all.length; ii++) {
	    if (strSelect == "off")
	        document.all(ii).removeAttribute("unselectable");
	    else
	        document.all(ii).setAttribute("unselectable","on",0);
	}
}
function getWidth(oTool) {
	var totalWidth = 0;
	var oRow = oToolbarTable.getElementsByTagName("tr").item(0);
	for (var yy=0; yy<oRow.children.length; yy++) {
		oCell = oRow.getElementsByTagName("td").item(yy);
		if (oCell.width != "") {
			totalWidth = totalWidth + parseInt(oCell.width);
		}
	}
	return totalWidth;
}
function callCommand(strCommand, strText) {
	addUndoAction(strText, oEditing.innerHTML);
	if (oSelection.text == "") {
	    oEditing.setActive();
	    oEditing.document.execCommand(strCommand);
	}
	else
		oSelection.execCommand(strCommand);
	setTimeout("updateToolbar()",20);
}
function pasteCommand(strValue, strText) {
	addUndoAction(strText, oEditing.innerHTML);
	if (oPos != null)
		oPos.innerHTML = strValue;
	else {
		if (oSelection.text == "") {
		    oEditing.setActive();
			oSelection = document.selection.createRange();
			oSelection.pasteHTML(strValue);
		}
		else
			oSelection.pasteHTML(strValue);
	}
	setTimeout("updateToolbar()",20);
	if ((oWindow != null) && (!oWindow.closed))
		oWindow.close();
}
function updateToolbar() {
    if (oEditing != null) {
	    oSelection = document.selection.createRange();
	    var oRow = oToolbarTable.getElementsByTagName("tr").item(0);
	    for (var yy=0; yy<oRow.children.length; yy++) {
		    oCell = oRow.getElementsByTagName("td").item(yy);
		    if (oCell.firstChild.tagName == "IMG" && oCell.firstChild.alt != "") {
		        if(oSelection.queryCommandValue(oCell.firstChild.alt) == true)
		            oCell.style.border = "solid 1px #FF6600";
		        else
		            oCell.style.border = "solid 1px #F4F3EA";
		    }
	    }
	    setTimeout("updateCss()",20);
	}
}
function updateCss() {
	var pElement;
	if (oSelection.length)
		pElement = oSelection[0]; 
	else
		pElement = oSelection.parentElement();
	// Get to the parent of the element
	while (pElement && !pElement.className)
		pElement = pElement.parentElement;
	strClass = pElement ? pElement.className.toLowerCase() : "";
	setTimeout("setCss()",20);
}
function setCss() {
	if (strClass != null && strClass != "") {
		oCss.className = strClass;
		oCss.innerHTML = strClass;
	}
	else {
		oCss.className = "default";
		oCss.innerHTML = "-- NONE --";
	}
}
function showMenuDown(oDiv, oFind, strAlign, intHeight) {
    hideMenus();
	if (strAlign == "RIGHT") {
		var strRight = "";
		if (oFind.style.width != null && oFind.style.width != "")
			strRight = oFind.style.width;
		else
			if (oFind.width != null && oFind.width != "")
				strRight = oFind.width;
			else
				return showMenuDown(oDiv, oFind, "LEFT", intHeight);
		oDiv.style.posLeft = (findPosX(oFind) + parseInt(strRight) - parseInt(oDiv.style.width));
	}
	else
		oDiv.style.posLeft = findPosX(oFind);
	
	if (parseInt(oDiv.style.height) + parseInt(event.clientY) + intHeight + 20 > parseInt(document.body.clientHeight))
	    oDiv.style.posTop = findPosY(oFind) - parseInt(oDiv.style.height) - intHeight;
	else
	    oDiv.style.posTop = findPosY(oFind) + intHeight;
	oDiv.style.display = "inline";
	setTimeout("setHideMenus()",200);
}
function showUndoDown(oDiv, oDivTop, oFind, strAlign, intHeight) {
    showMenuDown(oDiv, oFind, strAlign, intHeight);
    oDivTop.style.posLeft = parseInt(oDiv.style.posLeft);
    var intDivTop = parseInt(oDiv.style.posTop);
    var intButtonTop = parseInt(findPosY(oFind));
    if (intDivTop > intButtonTop) {
        oDivTop.style.posTop = intDivTop;
        oDiv.style.posTop = intDivTop + parseInt(oDivTop.style.height);
    }
    else {
        oDivTop.style.posTop = intDivTop;
        oDiv.style.posTop = intDivTop + parseInt(oDivTop.style.height);
    }
	oDivTop.style.display = "inline";
}
function setHideMenus() {
	document.attachEvent("onclick", hideMenus);
}
function hideMenus() {
	document.detachEvent("onclick", hideMenus);
	if (oCssDiv != null)
		oCssDiv.style.display = "none";
	if (oFontDiv != null)
		oFontDiv.style.display = "none";
	if (oDivUndoTop != null)
		oDivUndoTop.style.display = "none";
	if (oDivRedoTop != null)
		oDivRedoTop.style.display = "none";
	if (oDivUndo != null)
		oDivUndo.style.display = "none";
	if (oDivRedo != null)
		oDivRedo.style.display = "none";
	if (oSymbolDiv != null)
		oSymbolDiv.style.display = "none";
	if (oInfoDiv != null)
	    oInfoDiv.style.display = "none";
}
function CssOver(oCell) {
	oCell.style.border = 'solid 1px #0066FF';
}
function CssOut(oCell) {
	oCell.style.border = 'solid 1px #FFFFFF';
}
function CssSelect(strValue) {
	var oTemp = oSelection.parentElement();
	while (oTemp != null && oTemp != oEditing)
		oTemp = oTemp.parentElement;
	if (oTemp != null) {
	    addUndoAction("CSS FORMAT", oEditing.innerHTML);
	    if (oSelection.text == "" && oSelection.parentElement() != null) {
	        oSelection.parentElement().className = strValue;
	    }
        else if (oPos != null && (oSelection.text == "" || oPos.tagName == "A"))
            oPos.className = strValue;
        else {
	        oSelection.execCommand('RemoveFormat');
	        oSelection.execCommand('FontName',0,'TemporaryPlaceholder1234509876');
	        var oChildren = document.all.tags("FONT");
	        for (i=0; i<oChildren.length; i++) {
		        if (oChildren[i].face == 'TemporaryPlaceholder1234509876') {
			        oChildren[i].face = "";
			        oChildren[i].className = strValue;
			        oChildren[i].outerHTML = oChildren[i].outerHTML.replace("face=", "");
			        oChildren[i].outerHTML = oChildren[i].outerHTML.replace("??", "");
		        }
	        }
	    }
        oSelection.select();
	    hideMenus();
	    strClass = strValue;
	    setTimeout("setCss()",20);
	}
}
function FontSelect(strValue) {
    addUndoAction("FONT SIZE", oEditing.innerHTML);
    if (oSelection.text == "") {
        oEditing.setActive();
        oEditing.document.execCommand("FontSize", false, strValue);
    }
    else
	    oSelection.execCommand("FontSize", false,  strValue);
    hideMenus();
    oSelection.select();
}
function ChooseColor(oFunction) {
    oSaveFunction = oFunction;
    oSelection = document.selection.createRange();
    if ((oWindow != null) && (!oWindow.closed))
        oWindow.close();
    OpenWindow("/SiteSteam/support/color.html", "230", "168");
    return false;
}
function SelectColor(strColor) {
	var oTemp = oSelection.parentElement();
	while (oTemp != null && oTemp != oEditing)
		oTemp = oTemp.parentElement;
	if (oTemp != null) {
		addUndoAction("COLOR", oEditing.innerHTML);
		if (oSelection.text == "") {
		    oEditing.setActive();
			oSelection = document.selection.createRange();
			oSelection.execCommand(oSaveFunction, false, '#' + strColor);
		}
		else
			oSelection.execCommand(oSaveFunction, false, '#' + strColor);
		setTimeout("updateToolbar()",20);
	}
	if ((oWindow != null) && (!oWindow.closed))
		oWindow.close();
}

// ***************************************************************
// *******************  UNDO / REDO ******************************
// ***************************************************************
// TWO OPTIONS...
//		1) Update undo action for typing all at once until user makes another change (insert table, hr, etc...)
//		UNCOMMENT FOLLOWING FUNCTION
/*
function updateTypeUndo(oDiv) {
    if (oEditing != null) {
	    if ((UndoCount < maxRedos) && (LastUndoCount != UndoCount)) {
		    strStoreText = oDiv.innerHTML;
		    UndoCount = UndoCount + 1;
		    aUndo[0][UndoCount] = "Typing";
		    aUndo[1][UndoCount] = strStoreText;
		    LastUndoCount = UndoCount;
	    }
	}
}
*/

//		2) Update undo action for typing every second there is a pause
//		// UNCOMMENT FOLLOWING TWO FUNCTIONS
function updateTypeUndo(oDiv) {
    if (oEditing != null) {
	    clearTimeout(oUndoTimer);
	    if (boolAddOn == false) {			// User has stopped typing for one second - store new text
		    strStoreText = oDiv.innerHTML;	// Store text
		    boolAddOn = true;				// User is typing
	    }
	    // Call the function to save a redo after one second
	    oUndoTimer = setTimeout("updateTypeUndo2()",1000);
	}
}

// One second has passed - save the actions made as an undo action
function updateTypeUndo2() {
	clearTimeout(oUndoTimer);
	boolAddOn = false;				// User has stopped typing
	addUndoAction("TYPING", strStoreText);
}

// Change the background color for UNDO / REDO divs (and all the are before them)
function MouseOverUndo() {
	var oCell = event.srcElement;
	var tbody = oCell.parentElement.parentElement;
	for(var ii=0; ii<tbody.children.length; ii++) {
		// Change all that are before the selected item
		if (parseInt(tbody.children.item(ii).firstChild.id) >= parseInt(oCell.id))
			tbody.children.item(ii).firstChild.style.border='solid 1px #FF6600';
		else
			tbody.children.item(ii).firstChild.style.border='solid 1px #FFFFFF';
	}
	// Update the Label with number of actions
	oSpanDos.innerHTML = "<b>Last " + (tbody.children.length - oCell.id) + " Actions</b>";
}

// Change background back to color
function MouseOutUndo() {
	var oCell = event.srcElement;
	var tbody = oCell.parentElement.parentElement;
	for(var ii=0; ii<tbody.children.length; ii++) {
		tbody.children.item(ii).firstChild.style.border='solid 1px #FFFFFF';
	}
	// Reset the label with 0 actions
	oSpanDos.innerHTML = "<b>Last 0 Actions</b>";
}

// Open the UNDO div to select actions to undo
function ShowUndo(oButton) {
    if (boolMove == true)
        return;
	var tbody = document.getElementById("tblUndo").getElementsByTagName("TBODY")[0];
	// Reset table
	tbody.innerText = "";
	// Add actions in reverse order (100 -> 0)
	for(ii=UndoCount; ii>=0; ii--) {
		var row = document.createElement("TR");
		var td1 = document.createElement("TD");
		td1.appendChild(document.createTextNode(aUndo[0][ii]));
		td1.attachEvent("onmouseover", MouseOverUndo);
		td1.attachEvent("onmouseout", MouseOutUndo);
		td1.attachEvent("onclick", ClickUndo);
		td1.setAttribute("id", ii);
		td1.style.border = 'solid 1px #FFFFFF';
		row.appendChild(td1);
		tbody.appendChild(row);
	}
	// Check to see if there's only one action to undo (and do it)
	if ((UndoCount == 0) || (oDivUndo != null && oDivUndo.style.display == "inline")) {
		addRedoAction(aUndo[0][UndoCount], oEditing.innerHTML);
		oEditing.innerHTML = aUndo[1][UndoCount];
		tbody.innerText = "";
		UndoCount = UndoCount - 1;
		boolRedo = true;
	}
	else {
		// Multiple actions - show the DIV
		if (tbody.innerText != "") {
		    if (oDivUndoTop == null)
		        oDivUndoTop = document.getElementById("oUndoTop");
		    if (oDivUndo == null)
		        oDivUndo = document.getElementById("oUndo");
	        showUndoDown(oDivUndo, oDivUndoTop, oButton, "RIGHT", 27);
			setTimeout("setHideMenus()",200);
	        oSpanDos = document.getElementById("spnUndo");
		}
		// NO ACTIONS - DO NOTHING
	}
	return false;		// To prevent postback
}

// Click on an action to undo
function ClickUndo() {
	var oCell = event.srcElement;
	// Add previous UNDOs to REDO
	addRedoAction(aUndo[0][UndoCount], oEditing.innerHTML);
	for(var ii=UndoCount; ii>parseInt(oCell.id); ii--) {
		addRedoAction(aUndo[0][ii-1], aUndo[1][ii]);
	}
	// UNDO Changes
	oEditing.innerHTML = aUndo[1][oCell.id];
	oCell.parentElement.parentElement.parentElement.parentElement.style.display="none";
	UndoCount = parseInt(oCell.id - 1);
	boolRedo = true;
	setTimeout("updateToolbar()",20);
}

// Show the REDO div
function ShowRedo(oButton) {
    if (boolMove == true)
        return;
	if (boolRedo == true) {
    	var tbody = document.getElementById("tblRedo").getElementsByTagName("TBODY")[0];
		// Reset table
		tbody.innerText = "";
		// Populate the Table with actions
		for(ii=RedoCount; ii>=0; ii--) {
			var row = document.createElement("TR");
			var td1 = document.createElement("TD");
			td1.appendChild(document.createTextNode(aRedo[0][ii]));
			td1.attachEvent("onmouseover", MouseOverUndo);
			td1.attachEvent("onmouseout", MouseOutUndo);
			td1.attachEvent("onclick", ClickRedo);
			td1.setAttribute("id", ii);
			td1.style.border = 'solid 1px #FFFFFF';
			row.appendChild(td1);
			tbody.appendChild(row);
		}
		// Check to see if only one action and do it
		if ((RedoCount == 0) || (oDivRedo != null && oDivRedo.style.display == "inline")) {
			var intTempRedoCount = RedoCount;
			addUndoAction(aRedo[0][RedoCount], oEditing.innerHTML);
			oEditing.innerHTML = aRedo[1][intTempRedoCount];
			tbody.innerText = "";
			if (oDivRedo != null && oDivRedo.style.display == "inline")
				RedoCount = intTempRedoCount - 1;
			boolRedo = true;
		}
		else {
			// Multiple actions - show list
			if (tbody.innerText != "") {
		        if (oDivRedoTop == null)
		            oDivRedoTop = document.getElementById("oRedoTop");
		        if (oDivRedo == null)
		            oDivRedo = document.getElementById("oRedo");
    	        showUndoDown(oDivRedo, oDivRedoTop, oButton, "RIGHT", 27);
				setTimeout("setHideMenus()",200);
	            oSpanDos = document.getElementById("spnRedo");
			}
		}
	}
	return false;		// prevent postback
}

// Clicked on an action to redo
function ClickRedo() {
	var oCell = event.srcElement;
	var intRedos = RedoCount;
	// Add previous UNDOs to REDO
	addUndoAction(aRedo[0][intRedos], oEditing.innerHTML);
	for(var ii=intRedos; ii>parseInt(oCell.id); ii--) {
		addUndoAction(aRedo[0][ii-1], aRedo[1][ii]);
	}
	boolRedo = true;
	// REDO Changes
	oEditing.innerHTML = aRedo[1][oCell.id];
	oCell.parentElement.parentElement.parentElement.parentElement.style.display="none";
	RedoCount = parseInt(oCell.id - 1);
	setTimeout("updateToolbar()",20);
}

// Add an undo action
function addUndoAction(strTitle, strText) {
	clearTimeout(oUndoTimer);
	if (UndoCount + 1 >= maxRedos) {
		for(var ii=0; ii<UndoCount; ii++) {
			aUndo[0][ii] = aUndo[0][ii+1];
			aUndo[1][ii] = aUndo[1][ii+1];
		}
		UndoCount = UndoCount - 1;
	}
	UndoCount = UndoCount + 1;
	aUndo[0][UndoCount] = strTitle;
	aUndo[1][UndoCount] = strText;
	boolRedo = false;
	RedoCount = -1;
}

// Add a redo action
function addRedoAction(strTitle, strText) {
	clearTimeout(oUndoTimer);
	if (RedoCount + 1 >= maxRedos) {
		for(var ii=0; ii<RedoCount; ii++) {
			aRedo[0][ii] = aRedo[0][ii+1];
			aRedo[1][ii] = aRedo[1][ii+1];
		}
		RedoCount = RedoCount - 1;
	}
	RedoCount = RedoCount + 1;
	aRedo[0][RedoCount] = strTitle;
	aRedo[1][RedoCount] = strText;
}

function setCookie(name, value, expires, path, domain, secure)
{
    if (expires != null) {
        var today = new Date();
        today.setTime( today.getTime() );
        expires = new Date( today.getTime() + (expires * 1000 * 60 * 60 * 24));
    }
	document.cookie= name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function SelectOver(oCell) {
	boolSelect = true;
	oCell.style.border = 'solid 1px #0066FF';
}

function SelectOut(oCell) {
	boolSelect = false;
	oCell.style.border = 'solid 1px #FFFFFF';
}

function divDblClick() {
    if (oEditing != null) {
        if (event.srcElement.tagName == "A") {
            // LINKS
            oPos = event.srcElement;
	        OpenWindow("/SiteSteam/support/link.aspx?title=" + oPos.innerText + "&tip=" + oPos.getAttribute("title") + "&address=" + oPos.getAttribute("href") + "&target=" + oPos.getAttribute("target") + "&button=Update", "575", "200");
        }
        else if (event.srcElement.tagName == "IMG") {
            // IMAGE
            oPos = event.srcElement;
	        OpenWindow("/SiteSteam/support/image.aspx?Folder=/images&src=" + oPos.getAttribute("src") + "&border=" + oPos.getAttribute("border") + "&hspace=" + oPos.getAttribute("hspace") + "&vspace=" + oPos.getAttribute("vspace") + "&alt=" + oPos.getAttribute("alt") + "&align=" + oPos.getAttribute("align") + "&button=Update", "600", "500");
        }
        else
            oPos = null;
    }
}

function ShowImage(oCell) {
    if (boolMove == true)
        return;
	oSelection = document.selection.createRange();
    if (oPos != null && oPos.tagName == "IMG")
	    OpenWindow("/SiteSteam/support/image.aspx?Folder=/images&src=" + oPos.getAttribute("src") + "&border=" + oPos.getAttribute("border") + "&hspace=" + oPos.getAttribute("hspace") + "&vspace=" + oPos.getAttribute("vspace") + "&alt=" + oPos.getAttribute("alt") + "&align=" + oPos.getAttribute("align") + "&button=Update", "600", "500");
    else
	    OpenWindow("/SiteSteam/support/image.aspx?Folder=/images&src=/images/spacer.gif&button=Insert", "600", "500");
}

function InsertImage(strValue) {
	pasteCommand(strValue, "INSERT IMAGE");
}
function UpdateImage(strValue) {
	addUndoAction("UPDATE IMAGE", oEditing.innerHTML)
	oPos.outerHTML = strValue;
	if ((oWindow != null) && (!oWindow.closed))
		oWindow.close();
}


function ShowLink(oCell) {
    if (boolMove == true)
        return;
	oSelection = document.selection.createRange();
    if (oPos != null && oPos.tagName == "A")
	    OpenWindow("/SiteSteam/support/link.aspx?title=" + oPos.innerText + "&tip=" + oPos.getAttribute("title") + "&address=" + oPos.getAttribute("href") + "&target=" + oPos.getAttribute("target") + "&button=Update", "575", "200");
    else if (oPos != null && oPos.tagName == "IMG")
	    OpenWindow("/SiteSteam/support/link.aspx?button=OK&image=--IMAGE--", "575", "200");
    else
	    OpenWindow("/SiteSteam/support/link.aspx?button=OK&title=" + oSelection.text, "575", "200");
}
function InsertLink(strValue) {
	pasteCommand(strValue, "INSERT LINK");
}
function UpdateLink(strValue) {
	addUndoAction("UPDATE LINK", oEditing.innerHTML)
    oPos.outerHTML = strValue;
	if ((oWindow != null) && (!oWindow.closed))
		oWindow.close();
}

function UpdateImageLink(strValue) {
	addUndoAction("UPDATE LINK", oEditing.innerHTML)
	alert(strValue + oPos.outerHTML + "</a>");
// END HERE
    oSelection.pasteHTML(strValue + oPos.outerHTML + "</a>");
	alert(strValue + oPos.outerHTML + "</a>");
/*	try { 
	    var _temp = oPos.outerHTML;
	    oPos.outerHTML = strValue + _temp + "</a>";
    }
	catch(e) {
	}*/
	if ((oWindow != null) && (!oWindow.closed))
		oWindow.close();
}

function ShowSymbol(oCell) {
    if (boolMove == true)
        return;
	oSelection = document.selection.createRange();
	showMenuDown(oSymbolDiv, oCell, "RIGHT", 27);
}

function SelectSymbol(oCell) {
	if (boolSelect == true)
		InsertSymbol(oCell.innerText);
}

function InsertSymbol(strValue) {
	addUndoAction("CHARACTER", oEditing.innerHTML);
	oEditing.setActive();
	if (oSelection.text == "")
		oSelection = document.selection.createRange();
	oSelection.text = strValue;
}

function ShowTable(oCell) {
    if (boolMove == true)
        return;
	oSelection = document.selection.createRange();
	OpenWindow("/SiteSteam/support/table.aspx", "450", "250");
}

function InsertTable(intRows, intCols, intWidth, intHeight, intBorder, intSpacing, intPadding, strClass, strHorizontal, strVertical) {
	var strHtml = "";
	var strHeight = "";
	if (intHeight != "")
	    strHeight = " height=\"" + intHeight + "\"";
	var strWidth = "";
	if (intWidth != "")
	    strWidth = " width=\"" + intWidth + "\"";
	var strHor = "";
	if (strHorizontal != "")
	    strHor = " align=\"" + strHorizontal + "\"";
	var strVer = "";
	if (strVertical != "")
	    strVer = " valign=\"" + strVertical + "\"";
	strHtml = strHtml + "<TABLE" + strHeight + strWidth + " cellSpacing=" + intSpacing + " cellPadding=" + intPadding + " border=" + intBorder + " class=" + strClass + "><TBODY>";
	for (var xx=0; xx<intRows; xx++) {
		strHtml = strHtml + "<TR>";
		for (var yy=0; yy<intCols; yy++) {
        	if (intBorder == "0")
		    	strHtml = strHtml + "<TD" + strHor + strVer + " class=\"tableborder\"></TD>";
            else
		    	strHtml = strHtml + "<TD" + strHor + strVer + "></TD>";
		}
		strHtml = strHtml + "</TR>";
	}
	strHtml = strHtml + "</TBODY></TABLE>";
	if (intBorder == "0")
	    ResetBorders(false);
	pasteCommand(strHtml, "INSERT TABLE");
}

function ResetBorders(boolReset) {
    var oRule = document.styleSheets[0].rules.item(0);
    if (boolReset == true)
        oRule.style.border = '';
    else
        oRule.style.border = 'dotted 1px #CCCCCC';
}

function InsertHR(oCell) {
	if (boolMove == true)
		return;
    oSelection = document.selection.createRange();
	pasteCommand("<hr size=1 noshade />","HORIZONTAL");
}

function OpenWindow(strPath, strWidth, strHeight) {
    hideMenus();
	if ((oWindow != null) && (!oWindow.closed))
		oWindow.close();
	oWindow = window.open(strPath,"NONE","width=" + strWidth + ",height=" + strHeight + ",menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
}







function EditHTML(oDiv, oMenuEdit, oMenuSave, oRevision) {
    if (oEditing == null) {
        if (oPostback == false)
            oPostback = oRevision;
        initializeToolbar();
        strCancelEdit = oDiv.innerHTML;
	    oEditing = oDiv;
	    var oTables = oEditing.getElementsByTagName("TABLE");
	    ResetBorders(false);
	    oDiv.style.border = 'dashed 1px #666666';
	    oDiv.style.padding = '2';
	    oDiv.contentEditable = true;
	    if (oToolbar == null)
		    oToolbar = document.getElementById("divToolbar");
	    oToolbar.style.display = 'inline';
        oMenuSave.style.display = "inline";
        oMenuEdit.style.display = "none";
        HideFlash(true);
	    if (oDiv.innerText == "")
		    oDiv.focus();
    }
    else
        alert('You are currently editing another HTML control on this page.\n\nPlease Save or Cancel your changes before you continue...');
	return false;
}

function ApproveHTML(oApprove, strSchema, oSchema) {
    try {
        ApproveHtml_(strSchema);
        oApprove.style.display = "none";
        return false;
    }
    catch(e) {
        oSchema = document.getElementById(oSchema);
        oSchema.value = strSchema;
        return true;
    }
}

function SaveHTML(oDiv, oMenuEdit, oMenuSave, oMenuApprove, strSchema, oSchema, oHtml) {
    ResetBorders(true);
    try {
        AddHtml_(strSchema);
        oMenuApprove.style.display = "inline";
        return ResetHTML(oDiv, oMenuEdit, oMenuSave);
    }
    catch(e) {
        oSchema = document.getElementById(oSchema);
        oHtml = document.getElementById(oHtml);
        oSchema.value = strSchema;
        oHtml.value = escape(oEditing.innerHTML);
        return true;
    }
}

function CancelHTML(oDiv, oMenuEdit, oMenuSave) {
    if (oPostback == false) {
        try {
            ResetBorders(true);
            oEditing = null;
            oDiv.innerHTML = strCancelEdit;
            return ResetHTML(oDiv, oMenuEdit, oMenuSave);
        }
        catch(e) {
            return true;
        }
    }
    else
        return true;
}

function ResetHTML(oDiv, oMenuEdit, oMenuSave) {
	oDiv.style.border = '';
	oDiv.style.padding = '0';
	oEditing = null;
	oDiv.contentEditable = false;
    hideMenus();
	oToolbar.style.display = 'none';
    oMenuSave.style.display = "none";
    oMenuEdit.style.display = "inline";
    HideFlash(false);
	UndoCount = -1;
	RedoCount = -1;
    return false;
}

function ViewHTMLSource(oDiv, boolSweep) {
    if (boolSweep == true)
	    OpenWindow("/SiteSteam/support/HTMLSource.html", "400", "250");
    else
    	OpenWindow("/SiteSteam/support/HTMLSource.html", "625", "500");
	while (oWindow.frmHTML == null) {}
	oWindow.frmHTML.txtSource.innerText = oDiv.innerHTML;
	if (boolSweep == true)
		oWindow.SweepCode();
	else
		oWindow.ViewHTML();
	return false;
}

function HTMLUpdate(strHTML) {
	if (oWindow.divView.style.display == "inline") {
    	addUndoAction("EDIT SOURCE", oEditing.innerHTML);
		oEditing.innerHTML = strHTML;
	}
	else {
    	addUndoAction("CODE SWEEP", oEditing.innerHTML);
		oEditing.innerHTML = strHTML;
	}
	cleanUp(oEditing);
}

var URL_Host ='http://' + window.location.hostname + '/';
var URL_Dir = 'http://' + window.location.hostname +  window.location.pathname;
URL_Dir = URL_Dir.substr(0,URL_Dir.lastIndexOf("/"));
var URL = window.location.toString();
function fixurl(strRef)
{
	if (strRef.substr(0,URL_Host.length)==URL_Host)
	{
		if(strRef.substr(0,URL_Dir.length)==URL_Dir)
		{
			if(strRef.substr(0,URL.length)==URL)
				strRef =  strRef.substr(URL.length);
			else
				strRef =  strRef.substr(URL_Dir.length);
		}
		else
			strRef = '/'+ strRef.substr(URL_Host.length);
	}
	return strRef; 
}

function cleanUp(oObject)
{
	for(ii = 0; ii <= oObject.all.tags('A').length -1; ii++) {               
		oTemp = oObject.all.tags('A').item(ii);
		oTemp.href= fixurl(oTemp.href);
	}
	for(ii = 0; ii <= oObject.all.tags('IMG').length -1; ii++) {               
		oTemp = oObject.all.tags('IMG').item(ii);
		oTemp.src = fixurl(oTemp.src);
	}
}

function HideFlash(boolHide) {
    var oFlashShow = document.getElementsByName("divFlashShow");
    for (ii=0; ii<oFlashShow.length; ii++) {
        if (boolHide == true)
            oFlashShow.item(ii).style.display = "none";
        else
            oFlashShow.item(ii).style.display = "inline";
    }
    var oFlashHide = document.getElementsByName("divFlashHide");
    for (ii=0; ii<oFlashHide.length; ii++) {
        if (boolHide == true)
            oFlashHide.item(ii).style.display = "inline";
        else
            oFlashHide.item(ii).style.display = "none";
    }
}

function SpellCheck(oDiv, oText) {
    var strTemp = oDiv.innerHTML;
    strTemp = strTemp.replace(/<[^>]+>/g,'');
    var oSpellRange = document.createTextNode(strTemp);
    oDiv.appendChild(oSpellRange);
//    while (oSpellRange != "") {
        var oWord = oSpellRange.substringData(0, oSpellRange.data.indexOf(' '));
        alert(oWord);
//    }

    return false;
}

function SpellCheck2(oDiv) {
	var strTemp = oDiv.innerText;
	strTemp = strTemp.replace(/\r|\n|\r\n|\t/g, " ");
	strTemp = strTemp.replace(/\"/g, "");
	OpenWindow("support/spellCheck.asp", "425", "350");
	while (oWindow.frmSpell == null) {}
	oWindow.frmSpell.hdnHTML.value = oDiv.innerHTML;
	oWindow.frmSpell.hdnText.value = strTemp;
	oWindow.StartChecking();
	return false;
}

function SpellCheckUpdate(strHTML) {
	oEditing.innerHTML = strHTML;
}

function ShowCount(oButton, oDiv, oInfo, oWord, oCharNo, oCharWith, oParagraph) {
	var strText = oDiv.innerText;
	var intParagraphs = 0;

	// Total number of paragraphs
	var strText2 = oDiv.innerHTML;
	while (strText2.indexOf("</P>") > -1) {
		strText2 = strText2.replace("</P>","");
		intParagraphs = intParagraphs + 1;
	}
	oParagraph.innerText = intParagraphs;

	// Total Chars with space
	strText = strText.replace(/\r|\n|\r\n|\t/g, " ");
	while (strText.indexOf("  ") > -1) {
		strText = strText.replace("  ", " ");
	}	
	strText = strText.replace(/\r|\n|\r\n|\t/g, " ");
	var intWords = 0;
	oCharWith.innerText = strText.length;
	
	// Total chars without space
	strText2 = strText;
	while (strText2.indexOf(" ") > -1) {
		strText2 = strText2.replace(" ", "");
	}
	oCharNo.innerText = strText2.length;

	// Total number of words
	if (strText.charAt(strText.length - 1) == " ") {
		strText = strText.substring(0, strText.length - 1);
	}
	if (strText.length != "") {
		intWords = intWords + 1;
	}
	while (strText.indexOf(" ") > -1) {
		strText = strText.substring(strText.indexOf(" ") + 1);
		intWords = intWords + 1;
	}
	oWord.innerText = intWords;

	oInfo.style.posLeft = findPosX(oButton) - 180;
	oInfo.style.posTop = findPosY(oButton) + 20;
	oInfo.style.display = "inline";
	oInfoDiv = oInfo;
	
	setTimeout("setHideMenus()",200);
	return false;
}
function RemoveRevisions(intSchema) {
	OpenWindow("/SiteSteam/support/revisions.aspx?id=" + intSchema, "300", "250");
	return false;
}
function AddHtml_(strSchema) {
	cHTML.AddHtml(oEditing.innerHTML, strSchema,
		function(res) {
            if(res.error != null)
                alert(res.error);
            else
                alert('The content has been saved successfully!');
		}
	);
}

function ApproveHtml_(strSchema) {
	cHTML.ApproveHtml(strSchema,
		function(res) {
            if(res.error != null)
                alert(res.error);
            else
                alert('The content is approved and has been published!');
		}
	);
}

function GetRevision(oDDL, oRevision) {
    try {
        if (oDDL.options[oDDL.selectedIndex].value != '%') {
	        cHTML.GetRevision(oDDL.options[oDDL.selectedIndex].value,
                        function(res) {
                            if(res.error != null)
                                alert(res.error);
                            else
                                oEditing.innerHTML = res.value;
		        }
                );
        }
        return false;
    }
    catch(e) {
        oRevision = document.getElementById(oRevision);
        oRevision.value = oDDL.options[oDDL.selectedIndex].value;
        return true;
    }
}


