// Globals
var oEditImage;
var oEditRow;
var oEditCol;
var oEditCell;
var oEditTable;
var bAllowMergeRight;
var bAllowMergeLeft;
var bAllowSplit;
var oPopup;

var ContextSeparator = '<div  style="padding:3 5 3 5;  background:threedface;">\
	<div style="padding:0; border-bottom: 1px solid threedlightshadow; border-top: 1px solid threedshadow; background:threedface;"></div></div>';



function CheckClose ( )
{
	   event.returnValue = "Leaving the editor in this way will corrupt the editing session.\nPlease use the \'Save and Close\' button in the editor toolbar.\n\nCLICK CANCEL TO RESUME EDITING\n"
}

function CheckSaved ( sMode )
{

	if ( EditForm.sGoodSave.value == 'true' )
	{
		oMessage.innerText = ' Document saved successfully. Exiting. ';
		CloseForm.sCloseWell.value=true;
		this.onbeforeunload=null;
//		this.onunload=null;
		oMessage.innerText = CloseForm.sCloseWell.value;
		top.close();
		BE_EditWin=undefined;
	}
	else
	{
		if (sMode == 'News')
		window.open ( "BE_NewsAdmin.asp","BE_PageAdmin");
		else
		window.open ( "BE_PageAdmin.asp","BE_PageAdmin");
	}

}

function DoFinish ( sMode)
{
		CloseForm.submit();
		if (sMode == 'News')
		window.open ( "BE_NewsAdmin.asp","BE_PageAdmin");
		else
		window.open ( "BE_PageAdmin.asp","BE_PageAdmin");
}

function BE_ShowContext(event)
{

	var oWhatClicked = event.srcElement;

	oPopup = window.createPopup();
    var lefter = event.clientX+10;
    var topper = event.clientY+10;

	var sContextHTML = '<DIV style="cursor:default; font:8pt tahoma; background:threedface; border: 2px outset; color:menutext;">';

	if (BE_ImageSelected())
	{
		sContextHTML += top.bitingEdgeOnlineEditor.oContextImage.innerHTML;

		sContextHTML += BE_TableSubContext();
	}
	else
	{
		if (BE_TableSelected())
		{
			alert ( 'Table Only');
		}
		else
		{
			sContextHTML += top.bitingEdgeOnlineEditor.oContextTableAdd.innerHTML;

			sContextHTML += top.bitingEdgeOnlineEditor.oContextImageAdd.innerHTML;

			sContextHTML += BE_TableSubContext();
		}
	}

	sContextHTML += '</DIV>';
	
	oPopup.document.body.innerHTML = sContextHTML; 
	oPopup.show(0,0,160,0, document.body);

	var realHeight = oPopup.document.body.scrollHeight;
	oPopup.hide();
	oPopup.show(lefter, topper, 160, realHeight, document.body);

}


function BE_TableSubContext()
{
	var sTableSubContext = '';
	if (BE_InCellCheck())
	{
		var sTableSubContext = '';
		sTableSubContext += ContextSeparator;
		sTableSubContext += top.bitingEdgeOnlineEditor.oContextTable.innerHTML;
		if (bAllowMergeRight || bAllowMergeLeft || bAllowSplit)
			sTableSubContext += ContextSeparator;
		if (bAllowMergeRight)
			sTableSubContext += top.bitingEdgeOnlineEditor.oContextMergeRight.innerHTML;
		if (bAllowMergeLeft)
			sTableSubContext += top.bitingEdgeOnlineEditor.oContextMergeLeft.innerHTML;
		if (bAllowSplit)
			sTableSubContext += top.bitingEdgeOnlineEditor.oContextSplitCells.innerHTML;
	}
	return sTableSubContext;
}




function BE_HideContext()
{
	oPopup.hide();
}

function BE_Highlight(oControl)
{
	oControl.style.background = "highlight";
	oControl.style.color = "highlighttext";
}

function BE_Lowlight(oControl)
{
	oControl.style.background = "threedface";
	oControl.style.color = "menutext";
}

function AddLink()
{
	var sText = top.editPage.document.selection.createRange();
	var popLeft = (screen.availWidth - 450) / 2;
	var popTop = (screen.availHeight - 320) / 2;
	if (sText.text!="" && sText.parentElement().tagName.toUpperCase() != "A")

	{
		//sURL = "http://localhost/housing/_admin/editorlink.asp";	
		sURL = "../_admin/editorlink.asp";	
		BE_LinkWin = window.open ( sURL , "BE_LinkEdit", "status=no,width=450,height=320,left=" + popLeft + ",top=" + popTop );
	}
	else
	{
		if ( sText.parentElement().tagName.toUpperCase() == "A")
		{
			//sURL = "http://localhost/housing/_admin/editoramendlink.asp";	
			sURL = "../_admin/editoramendlink.asp";	
			BE_LinkWin = window.open ( sURL , "BE_LinkEdit", "status=no,width=450,height=320,left=" + popLeft + ",top=" + popTop );

		}
		else
		{ 
	    	alert("Create Hyperlink Error\nPlease select some text first!");
		}
	}   
}

function BE_AddTable(bFromContext)
{
	var popLeft = (screen.availWidth - 450) / 2;
	var popTop = (screen.availHeight - 320) / 2;
	if (top.editPage.document.selection.type == 'Control')
	{
		//Add check for Table control selected and alert user to select a suitable location

	}   
	else
	{
		if (top.editPage.document.selection.type == 'Text'||top.editPage.document.selection.type == 'None')
		{
			sURL = "" + (bFromContext ? '.' : '..' ) + "/_admin/editortable.asp";	
			BE_TableWin = window.open ( sURL , "BE_TableAdd", "status=no,width=450,height=320,scrollbars=yes,left=" + popLeft + ",top=" + popTop );
//			BE_TableWin = window.open ( sURL , "BE_TableAdd" );
		}
		else
		{
			alert("Please place the cursor within the page before clicking the button.");
		}
	}
}

function BE_DeleteRow()
{
	if (BE_InCellCheck())
	{
		oEditTable.deleteRow(oEditRow.rowIndex);
	}
}

function BE_InsertRow(bAbove, bFromContext)
{
	if (BE_InCellCheck())
	{
		var noCols = 0;
		var arrCells = oEditRow.cells;
		for (var i=0;i<arrCells.length;i++) 
		{
			noCols += arrCells[i].getAttribute('colSpan');
		}
		if (bAbove)
			var addRow = oEditTable.insertRow(oEditRow.rowIndex);
		else
			var addRow = oEditTable.insertRow(oEditRow.rowIndex + 1);

		for (i = 0; i < noCols; i++)
		{
			var addCell = addRow.insertCell();
		    addCell.innerHTML = '&nbsp;';

			if (document.showTableGuides == true)
			{
				addCell.style.border = '1px dotted #00FF00';
			}
		}
	}
}

function BE_DeleteCol()
{
	if (BE_InCellCheck())
	{
		var noCellsInRow;
		var backIndex = (oEditRow.cells.length-1) - (oEditCell.cellIndex);
		var arrRows = oEditTable.rows;
		for (i=0;i<arrRows.length;i++)
		{
			noCellsInRow = arrRows[i].cells.length - 1;
			var delCellIndex = noCellsInRow - backIndex;
			if (delCellIndex < 0)
				delCellIndex = 0;

			var arrCells = arrRows[i].cells;
    
			if (arrCells[delCellIndex].colSpan > 1)
				arrCells[delCellIndex].colSpan --;
			else
				arrRows[i].deleteCell(delCellIndex);
		} 
	} 
}

function BE_InsertCol(bLeft, bFromContext)
{
	if (BE_InCellCheck())
	{
		var noCellsInRow;
		var backIndex = (oEditRow.cells.length-1) - (oEditCell.cellIndex);
		var arrRows = oEditTable.rows;
		for (i=0;i<arrRows.length;i++)
		{
			noCellsInRow = arrRows[i].cells.length - 1;
			var newCellIndex = noCellsInRow - backIndex;
			if (newCellIndex < 0)
				newCellIndex = 0;
			if (!bLeft)
				newCellIndex ++;
				
			var addCell = arrRows[i].insertCell(newCellIndex);

			addCell.innerHTML = '&nbsp;'

			if (document.showTableGuides == true)
			{
				addCell.style.border = '1px dotted #00FF00';
			}
		} 
	}
}

function BE_MergeCells(bRight)
{
	if (BE_InCellCheck())
	{
		var CellSpan = oEditCell.getAttribute('colSpan');
		var arrCells = oEditRow.cells;
		
		if (bRight)
		{
			if ((oEditCell.cellIndex + 1) < arrCells.length)
			{
				var AddSpan = arrCells[oEditCell.cellIndex + 1].getAttribute('colSpan');
				oEditCell.colSpan = CellSpan + AddSpan;
				oEditRow.deleteCell(oEditCell.cellIndex + 1);
			}
		}
		else
		{
			if (oEditCell.cellIndex >=1)
			{
				var AddSpan = arrCells[oEditCell.cellIndex - 1].getAttribute('colSpan');
				oEditCell.colSpan = CellSpan + AddSpan;
				oEditRow.deleteCell(oEditCell.cellIndex - 1);
			}
		}
	}
}

function BE_SplitCell(bFromContext)
{
	if (BE_InCellCheck())
	{
		if (bAllowSplit)
		{
			var CellSpan = oEditCell.getAttribute('colSpan');
			oEditCell.colSpan = 1;
			for (var i=1;i<CellSpan;i++) 
			{
				var addCell = oEditRow.insertCell(oEditCell.cellIndex + 1);
				if (document.showTableGuides == true)
				{
					addCell.style.border = '1px dotted #00FF00';
				}
			}
		}
	}
}

function BE_InCellCheck()
{
	if (top.editPage.document.selection.type != 'Control') 
	{
	    var oSel = top.editPage.document.selection.createRange().parentElement();
	}
	else
	{
	    var oSel = top.editPage.document.selection.createRange()(0).parentElement;
	}

	while (oSel.tagName.toUpperCase() != 'TD' && oSel.tagName.toUpperCase() != 'TH')
	{
		oSel = oSel.parentElement;
		if (oSel == null)
			break;
		if (oSel.className == 'edit')
		{
			oSel = null;
			break;
		}
	}
	if (oSel) 
	{
		oEditCell= oSel;
		oEditRow = oEditCell.parentElement;
		oEditTable = (oEditRow.parentElement).parentElement;
		bAllowMergeRight = ((oEditCell.cellIndex + 1) < oEditRow.cells.length);
		bAllowMergeLeft = (oEditCell.cellIndex >=1);
		bAllowSplit = (oEditCell.getAttribute('colSpan')) > 1;
		return true;
	}
}


function BE_DoTableGuides()
{
	if (!document.showTableGuides)
	{
		document.showTableGuides = false;
	}

	if (document.showTableGuides == false)
	{
		var arrEditTables = top.editPage.edit.getElementsByTagName('TABLE');
		for (var i=0; i < arrEditTables.length; i++)
		{
			arrEditTables[i].style.border = '2px dashed #EFA765';

			var arrEditRows = arrEditTables[i].rows;
			for (var j=0; j < arrEditRows.length; j++)
			{
				var arrEditCells = arrEditRows[j].cells;
				for (var k=0; k < arrEditCells.length; k++)
				{
					arrEditCells[k].style.border = '1px dotted #00FF00';	
				}
			}
		}
		document.showTableGuides = true;
	}
	else
	{
		var arrEditTables = top.editPage.edit.getElementsByTagName('TABLE');
		for (var i=0; i < arrEditTables.length; i++)
		{
		    arrEditTables[i].style.border = 'none';
		    arrEditTables[i].removeAttribute('style');

			var arrEditRows = arrEditTables[i].rows;
			for (var j=0; j < arrEditRows.length; j++)
			{
				var arrEditCells = arrEditRows[j].cells;
				for (var k=0; k < arrEditCells.length; k++)
				{
					arrEditCells[k].style.border = 'none';	
					arrEditCells[k].removeAttribute('style');	
				}
			}
		}

		document.showTableGuides = false;
	}
}

function BE_RemoveGuides()
{
	if (document.showTableGuides == true)
	{
		BE_DoTableGuides();
		document.restoreTableGuides = true;
	}
}


function BE_AddImage(bFromContext)
{
	var popLeft = (screen.availWidth - 700) / 2;
	var popTop = (screen.availHeight - 420) / 2;
	if (BE_ImageSelected())
	{
		BE_ModifyImage()
	}
	else
	{
		if (top.editPage.document.selection.type == 'Text'||top.editPage.document.selection.type == 'None')
		{
			sURL = "" + (bFromContext ? '.' : '..' ) + "/_admin/editorimage.asp";	
			BE_ImageWin = window.open ( sURL , "BE_ImageAdd", "status=no,width=700,height=420,scrollbars=yes,left=" + popLeft + ",top=" + popTop );
		}
		else
		{
			alert("Please select an image or place the cursor within the page before clicking the button.");
		}
	}   
}

function BE_ImageSelected()
{
	if (top.editPage.document.selection.type == 'Control')
	{
		var oRange = top.editPage.document.selection.createRange();
		if (oRange(0).tagName.toUpperCase() == "IMG")
		{
			oEditImage = oRange(0);
			return true;
		}
	}
}

function BE_TableSelected()
{
	if (top.editPage.document.selection.type == 'Control')
	{
		var oRange = top.editPage.document.selection.createRange();
		if (oRange(0).tagName.toUpperCase() == "TABLE")
		{
			oEditTable = oRange(0);
			return true;
		}
	}
}

function BE_ModifyImage(bFromContext)
{
	if (BE_ImageSelected())
	{
		var popLeft = (screen.availWidth - 700) / 2;
		var popTop = (screen.availHeight - 420) / 2;
		sURL = "" + (bFromContext ? '.' : '..' ) + "/_admin/editoramendimage.asp";
		imgSrc = oEditImage.src;
		sURL += "?imgSrc=" + imgSrc;	
		BE_ImageWin = window.open ( sURL , "BE_ImageEdit", "status=no,width=700,height=420,left=" + popLeft + ",top=" + popTop );
	}
}

function GetIntLink()
{
	var popLeft = (screen.availWidth - 450) / 2;
	var popTop = (screen.availHeight - 320) / 2;

		sURL = "../_admin/getintlink.asp";	
		BE_IntLinkWin = window.open ( sURL , "BE_IntLinkBrowse", "status=no,width=450,height=320,left=" + popLeft + ",top=" + popTop );

}


function InitSub()
{
	var oLinkWin = window.opener;
	document.all.feedback.innerText = oLinkWin;

}



function RemoveLink()
{
top.editPage.document.execCommand("Unlink");
}


// Executes commands depending on which button has been pushed
function BE_EditorCmd()
{

	var undefined;
	// get button label
	text = window.event.srcElement.id;
//	whatclicked.innerText=text;
	if (text == "tdSave" || text == "imgSave")
	{
		BE_RemoveGuides();

		EditForm.sEditedText.value = top.editPage.edit.innerHTML; // the main edit window
		if (top.editPage.newsHeadline)
		{
			EditForm.sNewsHeadlineText.value = top.editPage.newsHeadline.innerText; // the news headline area
		}
		EditForm.sSafeClose.value = undefined;					  // 
		this.onbeforeunload=null;
		this.onunload=null;
		EditForm.submit();


	}
	if (text == "tdClose" || text == "imgClose")
	{
		BE_RemoveGuides();


		EditForm.sEditedText.value = top.editPage.edit.innerHTML;
		if (top.editPage.newsHeadline)
		{
			EditForm.sNewsHeadlineText.value = top.editPage.newsHeadline.innerText; // the news headline area
		}
		EditForm.sSafeClose.value = true;
		this.onbeforeunload=null;
		this.onunload=null;

		EditForm.submit();
	}

	if (text == "tdWipeWordJunk" || text == "imgWipeWordJunk")
	{
		var Washing = top.editPage.edit.innerHTML;
		
		Washing = Washing.replace(/<\/?span([^>])*>/gi,"") //strip <SPAN>s
		Washing = Washing.replace(/<\/?font([^>])*>/gi, "") //strip <FONT>'s
		Washing = Washing.replace(/<([\w]+) class=([^ |>]*)([^>]*)/gi, "<$1$3") //strip classes
		Washing = Washing.replace(/<([\w]+) style="([^"]*)"([^>]*)/gi, "<$1$3") //strip styles
		Washing = Washing.replace(/<\/?\w+:[^>]*>/gi, "") //strip word <o:p> tags
		Washing = Washing.replace(/<p([^>])*>(&nbsp;)*\s*<\/p>/gi," ") //strip empty <P>s

		top.editPage.edit.innerHTML = Washing;


	}
		

	if (text == "tdBold" || text == "imgBold")	
		{
			top.editPage.document.execCommand("Bold");
		}

	else if (text == "tdItalic" || text == "imgItalic")
		top.editPage.document.execCommand("Italic");

	else if (text == "tdUnder" || text == "imgUnder")
		top.editPage.document.execCommand("Underline");

	else if (text == "tdCut" || text == "imgCut")
		top.editPage.document.execCommand("Cut");

	else if (text == "selBlockFormat")
	{
			var sBlockFormat = oBlockFormat.options[oBlockFormat.selectedIndex].value;

			top.editPage.document.execCommand("FormatBlock", false, sBlockFormat);
	}
		
	else if (text == "tdCut" || text == "imgCut")
		top.editPage.document.execCommand("Cut");

	else if (text == "tdCopy" || text == "imgCopy")
		top.editPage.document.execCommand("Copy");

	else if (text == "tdPaste" || text == "imgPaste")
		top.editPage.document.execCommand("Paste");

	else if (text == "tdSup" || text == "imgSup")
		top.editPage.document.execCommand("SuperScript");


	else if (text == "tdOlist" || text == "imgOlist")
		top.editPage.document.execCommand("InsertOrderedList");

	else if (text == "tdUlist" || text == "imgUlist")
		top.editPage.document.execCommand("InsertUnorderedList");



	else if (text == "tdOutdent" || text == "imgOutdent")
		top.editPage.document.execCommand("Outdent");

	else if (text == "tdIndent" || text == "imgIndent")
		top.editPage.document.execCommand("Indent");

	else if (text == "tdRight" || text == "imgRight")
		top.editPage.document.execCommand("JustifyRight");

	else if (text == "tdLeft" || text == "imgLeft")
		top.editPage.document.execCommand("JustifyLeft");

	else if (text == "tdCentre" || text == "imgCentre")
		top.editPage.document.execCommand("JustifyCenter");

	else if (text == "InsertImage")
	{
		theImg = document.all.imagepath.value;
		if (theImg != "")
			top.editPage.document.execCommand("InsertImage", false, theImg);
	}

	if (text == "tdUndo" || text == "imgUndo")
		top.editPage.document.execCommand('Undo');

	if (text == "tdRedo" || text == "imgRedo")
		top.editPage.document.execCommand('Redo');

//	oMessage.innerText = text;
}