/********************************************************************/
// Function to align the selected text
function alignText(frmElement, alignTo) {
	frmObject = eval("document.forms[0]." + frmElement);
	
	selText = document.selection.createRange().text;
	frmObject.focus();
	selObject = trimSpaces(document.selection.createRange());
	if(selObject.text.length > 0) {
		selObject.text = "<div align='" + alignTo + "'>" + selObject.text + "</div>";
	}
	else {
		if(frmObject.createTextRange && frmObject.caretPos) {
			var caretPos = frmObject.caretPos;
			caretPos.text = "<div align='" + alignTo + "'></div>";
		}
		else {
			frmObject.value = "<div align='" + alignTo + "'></div>";
		}
	}
}
/********************************************************************/


/********************************************************************/
// Function to insert BOLD, ITALIC, UNDERLINE tag in the selected text
function insertTag(frmElement, tagElement) {
	frmObject = eval("document.forms[0]." + frmElement);
	
	selText = document.selection.createRange().text;
	frmObject.focus();
	selObject = trimSpaces(document.selection.createRange());
	if(selObject.text.length > 0) {
		selObject.text = "<" + tagElement + ">" + selObject.text + "</" + tagElement + ">";
	}
	else {
		if(frmObject.createTextRange && frmObject.caretPos) {
			var caretPos = frmObject.caretPos;
			caretPos.text = "<" + tagElement + "></" + tagElement + ">";
		}
		else {
			frmObject.value = "<" + tagElement + "></" + tagElement + ">";
		}
	}
}
/********************************************************************/


/********************************************************************/
// Function to indent the text to left
function indentText(frmElement, indentPos) {
	frmObject = eval("document.forms[0]." + frmElement);

	selText = document.selection.createRange().text;
	frmObject.focus();
	selObject = trimSpaces(document.selection.createRange());
	if(indentPos == "right") {
		if(selObject.text.length > 0) {
			inputText = trimSpaces(selObject.text);
			outputText = "";
			lineArray = inputText.split("\n");
			for(lineNum = 0; lineNum < lineArray.length; lineNum++) {
				outputText += "#indent#" + lineArray[lineNum] + "\n";
			}
			selObject.text = outputText;
		}
		else {
			if(frmObject.createTextRange && frmObject.caretPos) {
				var caretPos = frmObject.caretPos;
				if(frmObject.value.length <= 0) {
					caretPos.text = "#indent#";
				}
				else {
					caretPos.text = "\n#indent#";
				}
			}
			else {
				if(frmObject.value.length <= 0) {
					frmObject.value = "#indent#";
				}
				else {
					frmObject.value = "\n#indent#";
				}
			}
		}
	}
	else if(indentPos == "left") {
		if(selObject.text.length > 0) {
			inputText = trimSpaces(selObject.text);
			outputText = "";
			lineArray = inputText.split("\n");
			for(lineNum = 0; lineNum < lineArray.length; lineNum++) {
				tabIndex = lineArray[lineNum].indexOf("#indent#");
				if(tabIndex == 0) {
					lineArray[lineNum] = lineArray[lineNum].substring(8);
				}
				outputText += lineArray[lineNum] + "\n";
			}
			selObject.text = outputText;
		}
		else {
			return;
		}
	}
}
/********************************************************************/


/********************************************************************/
// Function to insert #bul# or #num# tag at the begining of each line of the selected text
function insertBullet(frmElement, tagElement) {
	frmObject = eval("document.forms[0]." + frmElement);

	selText = document.selection.createRange().text;
	frmObject.focus();
	selObject = trimSpaces(document.selection.createRange());
	if(selObject.text.length > 0) {
		inputText = trimSpaces(selObject.text);
		outputText = "";
		bulTextArray = inputText.split("\n");
		for(lineNum = 0; lineNum < bulTextArray.length; lineNum++) {
			outputText += tagElement + bulTextArray[lineNum] + "\n";
		}
		selObject.text = outputText;
	}
	else {
		if(frmObject.createTextRange && frmObject.caretPos) {
			var caretPos = frmObject.caretPos;
			if(frmObject.value.length <= 0) {
				caretPos.text = tagElement;
			}
			else {
				caretPos.text = "\n" + tagElement;
			}
		}
		else {
			if(frmObject.value.length <= 0) {
				frmObject.value = tagElement;
			}
			else {
				frmObject.value = "\n" + tagElement;
			}
		}
	}
}
/********************************************************************/


/********************************************************************/
// Functions to insert the <font> tag with color attribute
function insertColor(frmElement, rgbOnly) {
	// Function to show the color pallette
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	sTop = (sHeight - 136) / 2;
	sLeft = (sWidth - 204) / 2;

	window.open("colorPicker.php?frmElement=" + frmElement + "&rgbOnly=" + rgbOnly, "colorpicker", "width=204,height=136,top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=0,resizable=0");
	return;
}
function addColor(frmElement, colorValue, rgbOnly) {
	frmObject = eval("window.opener.document.forms[0]." + frmElement);
	
	// Function to update the color values
	if(rgbOnly == 1) {
		frmObject.value = "#" + colorValue;
	}
	else {
		selText = window.opener.document.selection.createRange().text;
		frmObject.focus();
		selObject = trimSpaces(window.opener.document.selection.createRange());
		if(selObject.text.length > 0) {
			selObject.text = "<font color='#" + colorValue + "'>" + selObject.text + "</font> ";
		}
		else {
			if(frmObject.createTextRange && frmObject.caretPos) {
				var caretPos = frmObject.caretPos;
				caretPos.text = "<font color='#" + colorValue + "'></font>";
			}
			else {
				frmObject.value = "<font color='#" + colorValue + "'></font>";
			}
		}
	}
	frmObject.focus();
	window.close()
}
/********************************************************************/


/********************************************************************/
// Functions to add the image tag in the text
function insertImage(frmElement) {
	// Alerts the user if they have selected a text because the text will be replaced with the image tag
	frmObject = eval("document.forms[0]." + frmElement);
	selText = document.selection.createRange().text;
	frmObject.focus();
	selObject = trimSpaces(document.selection.createRange());
	if(selObject.text.length > 0) {
		if(!confirm("The selected text in the document will be replaced by the image information.\n\t\tDo you wish to continue?")) {
			return;
		}
	}

	// Function to show the image picker window
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	sTop = (sHeight - 280) / 2;
	sLeft = (sWidth - 400) / 2;

	window.open("chooseImage.php?frmElement=" + frmElement, "imagepicker", "width=400,height=280,top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=0,resizable=0");
	return;
}
function checkImageInput() {
	imageName = trimSpaces(document.forms[0].imageName.value);
	if(imageName.length <= 0) {
		alert("Please select an image");
		document.forms[0].imageName.focus();
		return false;
	}
	document.forms[0].frmAction.value = "update";
}
function addImage(frmElement, imgString) {
	frmObject = eval("window.opener.document.forms[0]." + frmElement);

	imgString += " ";
	selText = window.opener.document.selection.createRange().text;
	frmObject.focus();
	selObject = window.opener.document.selection.createRange();
	if(selObject.text.length > 0) {
		selObject.text = imgString;
	}
	else {
		if(frmObject.createTextRange && frmObject.caretPos) {
			var caretPos = frmObject.caretPos;
			caretPos.text = imgString;
		}
		else {
			frmObject.value = imgString;
		}
	}
}
/********************************************************************/


/********************************************************************/
function insertLink(frmElement) {
	// Function to show the link selector window
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	sTop = (sHeight - 170) / 2;
	sLeft = (sWidth - 400) / 2;
	window.open("chooseLink.php?frmElement=" + frmElement, "linkselector", "width=400,height=170,top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=0,resizable=0");
	return;
}
function checkLinkURL() {
	linkURL = trimSpaces(document.forms[0].linkURL.value);
	if(linkURL.length <= 0) {
		alert("Please enter the link URL");
		document.forms[0].linkURL.focus();
		return false;
	}
	document.forms[0].frmAction.value = "update";
}
function addLink(frmElement, linkString) {
	frmObject = eval("window.opener.document.forms[0]." + frmElement);

	selText = window.opener.document.selection.createRange().text;
	frmObject.focus();
	selObject = window.opener.document.selection.createRange();
	if(selObject.text.length > 0) {
		selObject.text = linkString + selObject.text + "</a> ";
	}
	else {
		if(frmObject.createTextRange && frmObject.caretPos) {
			var caretPos = frmObject.caretPos;
			caretPos.text = linkString + "</a>";
		}
		else {
			frmObject.value = linkString + "</a>";
		}
	}
}
/********************************************************************/


/********************************************************************/
// Functions to initiate the table insertion/editing tool
function insertTable(linkID, tableID) {
	selText = document.selection.createRange().text;
	document.forms[0].pageContents.focus();
	selObject = trimSpaces(document.selection.createRange());
	
	if(selObject.text.length > 0) {
		selectedText = selObject.text;
		fullText = trimSpaces(document.forms[0].pageContents.value);
		
		// Checks whether the selected text contain the "#tab" entry
		tabStartPos = selectedText.indexOf("#tab");
		if(tabStartPos >= 0) {
			tabStartPos += 4;
			tabEndPos = fullText.indexOf("#", tabStartPos);
			tableID = fullText.substring(tabStartPos, tabEndPos);
		}
		else {
			selectPos = fullText.indexOf(selectedText);
			tabStartPos = fullText.lastIndexOf("#tab", selectPos);
			if(tabStartPos >= 0) {
				tabStartPos += 4;
				tabEndPos = fullText.indexOf("#", tabStartPos);
				if(selectPos < tabEndPos) {
					tableID = fullText.substring(tabStartPos, tabEndPos);
				}
			}
		}
		
		if(tableID > 0) {
			if(!confirm("Do you want to edit the selected table?")) {
				return;
			}
		}
	}
	else {
		frmObject = document.forms[0].pageContents;
		if(frmObject.createTextRange && frmObject.caretPos) {
			var caretPos = frmObject.caretPos;
			caretPos.text = "#tab" + tableID + "#\n";
		}
		else {
			frmObject.value = "#tab" + tableID + "#\n";
		}
	}
	
	// Function to show the window to get the number of rows and columns required
/*
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	
	winWidth = 750; //sWidth - 30;
	winHeight = 450; sHeight - 80;
	
	sTop = (sHeight - winHeight) / 2;
	sLeft = (sWidth - winWidth) / 2;
	window.open("getTableInfo.php?linkid=" + linkID + "&tablenumber=" + tableID, "tableselector", "width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=0");
*/
	document.contentManager.tablenumber.value = tableID;
	document.contentManager.action = "getTableInfo.php";
	document.contentManager.pageData.value = trimSpaces(document.contentManager.pageContents.value);
	document.contentManager.submit();
		
	return;
}
function checkTableProperties() {
	document.tableProperties.tableWidth.value = trimSpaces(document.tableProperties.tableWidth.value);
	if(document.tableProperties.tableWidth.value.length > 0) {
		if(!checkAllowedChars(document.tableProperties.tableWidth.value, 'N')) {
			alert("Only numbers are allowed for table width");
			document.tableProperties.tableWidth.focus();
			return false;
		}
	}
	document.tableProperties.tableCols.value = trimSpaces(document.tableProperties.tableCols.value);
	if(document.tableProperties.tableCols.value.length > 0) {
		if(!checkAllowedChars(document.tableProperties.tableCols.value, 'N')) {
			alert("Only numbers are allowed for table columns");
			document.tableProperties.tableCols.focus();
			return false;
		}
		tCols = parseInt(document.tableProperties.tableCols.value, 10);
		if(tCols > 10 || tCols <= 0) {
			alert("Only numbers ranging from 1 to 10 are permitted for table columns");
			document.tableProperties.tableCols.focus();
			return false;
		}
	}
	else {
		alert("You have to enter the number of columns");
		document.tableProperties.tableCols.focus();
		return false;
	}

	document.tableProperties.frmAction.value = "modify";
}
function cancelTable(linkid, tableID) {
	document.forms[0].action = "contentManager.php";
	pageData = trimSpaces(document.forms[0].pageData.value);
	
	tableIDStr = tableID.toString();
	tabPos = pageData.indexOf("#tab" + tableIDStr + "#");
	pageData1 = pageData.substring(0, tabPos);
	pageData2 = pageData.substring(tabPos + tableIDStr.length + 5);

	pageData = pageData1 + " " + pageData2;

	document.forms[0].pageData.value = trimSpaces(pageData);
	document.forms[0].submit();
}
/********************************************************************/


/********************************************************************/
// Function to display the preview with the current contents
function showPreview(frmElement, pageId, mainID) {
	frmObject = eval("document.forms[0]." + frmElement);

	// Checks whether any information is there on the text area
	if(frmObject.value.length <= 0) {
		alert("No contents are there to display");
		return;
	}
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	
	winWidth = sWidth; // - 50;
	winHeight = sHeight; // - 80;
	
	sTop = (sHeight - winHeight) / 2;
	sLeft = (sWidth - winWidth) / 2;
	winRef = window.open("showPreview.php?frmElement=" + frmElement + "&pageId=" + pageId + "&mainID=" + mainID, "previewWindow", "width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=0");
	return;
}
function getPageContents(frmElement) {
	frmObject = eval("window.opener.document.forms[0]." + frmElement);
	
	pageContents = frmObject.value;
	document.forms[0].pageContents.value = pageContents;
	document.forms[0].submit();
}
/********************************************************************/


/********************************************************************/
// Function to save the contents
function saveData(frmElement) {
	frmObject = eval("document.forms[0]." + frmElement);

	document.contentManager.frmAction.value = "update";
	document.contentManager.submit();
	return;
}
/********************************************************************/


/********************************************************************/
// Function to close the child window and set focus to the opener window
function cancelInput(frmElement) {
	frmObject = eval("window.opener.document.forms[0]." + frmElement);

	window.opener.focus();
	frmObject.focus();
	window.close();
}
/********************************************************************/


/********************************************************************/
// Functions used for controlling the toolbar button display
function mouseover(el) {
  el.className = "raised";
}

function mouseout(el) {
  el.className = "button";
}

function mousedown(el) {
  el.className = "pressed";
}

function mouseup(el) {
  el.className = "raised";
}
/********************************************************************/


/********************************************************************/
// Function to initialize the content management page
function initializePage(frmElement) {
	frmObjectMain = eval("document.forms[0]." + frmElement);
	frmObjectMain.focus();
	storePos(frmObjectMain);
}
/********************************************************************/


/********************************************************************/
// Function to store the cursor position within the text area
function storePos(frmObject){
	frmObjectMain = frmObject;
	if(frmObject.createTextRange) {
		frmObject.caretPos = document.selection.createRange().duplicate();
	}
}
/********************************************************************/

