var curPos = 0;

function initializePage(frmElement) {
	frmObject = eval("document.forms[0]." + frmElement);
	frmObject.focus();
	storePos(frmObject);
}

function storePos(frmObject){
	if(frmObject.createTextRange) {
		frmObject.caretPos = document.selection.createRange().duplicate();
		curPos = frmObject.caretPos;
	}
}

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) {
		startPos=selObject.text.indexOf("<div align=");
		endPos=selObject.text.indexOf("</div>");
		newString="<div align='" + alignTo + "'>";
		arr=selObject.text.split(">");
		oldString=arr[0]+">";
		if(startPos == 0 && endPos == selObject.text.length-6)
		{
		  if(oldString==newString)
			  selObject.text=selObject.text.substring(alignTo.length+14,selObject.text.length-6);
	      else
		    selObject.text=newString + selObject.text.substring(oldString.length,selObject.text.length);		  
		} 
		else
		{
		  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 insertTag(frmElement, tagElement) {
	frmObject = eval("document.forms[0]." + frmElement);
	selText = document.selection.createRange().text;
	frmObject.focus();
	selObject = trimSpaces(document.selection.createRange());
    elemen=tagElement.length+3;
	if(selObject.text.length > 0) {
	    startPos=selObject.text.indexOf("<" + tagElement + ">");
		endPos=selObject.text.indexOf("</" + tagElement + ">");
		if(startPos == 0 && endPos == selObject.text.length-elemen)
		{
		  selObject.text=selObject.text.substring(tagElement.length+2,selObject.text.length-elemen);
		} 
		else
		{
		        remStart=selObject.text.indexOf("<" + tagElement + ">");
				remEnd=selObject.text.indexOf("</" + tagElement + ">");
				if(remStart>0 && remEnd<selObject.text.length-elemen)
				{
				  A=selObject.text.substring(0,remStart);
				  A+=selObject.text.substring(remStart+tagElement.length+2,remEnd);
				  A+=selObject.text.substring(remEnd+tagElement.length+3,selObject.text.length);
				  selObject.text=A;
				}
				else
				{
		  	     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 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 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;
			}
		}
	}
}

function insertColor(frmElement, rgbOnly) {
	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);
	
	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()
}

function insertImage(frmElement) {
	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;
		}
	}

	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) {
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	sTop = (sHeight - 200) / 2;
	sLeft = (sWidth - 400) / 2;
	window.open("chooseLink.php?frmElement=" + frmElement, "linkselector", "width=400,height=200,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 modifyLinkType() {
	document.forms[0].url[0].checked = true;
	linkType = document.forms[0].linkType.options[document.forms[0].linkType.selectedIndex].value;
	if(linkType == "mailto:") {
		document.forms[0].linkURL.value = "";
	}
	else {
		document.forms[0].linkURL.value = "http://www.";
	}
}
function addLink(frmElement, linkString, linkUrl) {
	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 + linkUrl + "</a>";
		}
		else {
			frmObject.value = linkString + linkUrl + "</a>";
		}
	}
}


function insertTable(linkID, tableID) {
	selText = document.selection.createRange().text;
	document.forms[0].pageContents.focus();
	selObject = trimSpaces(document.selection.createRange());
	tblAction = "new";
	
	if(selObject.text.length > 0) {
		selectedText = selObject.text;
		fullText = trimSpaces(document.forms[0].pageContents.value);
		
		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);
				}
			}
		}
		
		tblAction = "new";
		if(tableID > 0) {
			if(!confirm("Do you want to edit the selected table?")) {
				return;
			}
			else {
				tblAction = "edit";
			}
		}
	}
/*	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";
		}
	}*/
	
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	
	winWidth = 750; //sWidth - 30;
	winHeight = 450; sHeight - 80;
	
	sTop = (sHeight - winHeight) / 2;
	sLeft = (sWidth - winWidth) / 2;
	if(tblAction == "edit") {
		pageUrl = "editTableInfo.php?linkid=" + linkID + "&tableid=" + tableID + "&tblAction=" + tblAction;
	}
	else {
		pageUrl = "getTableInfo.php?linkid=" + linkID + "&tablenumber=" + tableID + "&tblAction=" + tblAction;
	}
	window.open(pageUrl, "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;
		}
	}else {
		alert("You have to enter the 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 > 4 || tCols <= 0) {
			alert("Only numbers ranging from 1 to 4 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 = "update";
}
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 checkTableData() {
	totCols = document.forms[0].tableCols.value;
}
function checkMerging(mergeObject) {
}
function addTable(tableRef) {
	selText = window.opener.document.selection.createRange().text;
	window.opener.document.forms[0].pageContents.focus();
	selObject = window.opener.document.selection.createRange();
	tableRef = "#tab" + tableRef + "#"
	if(selObject.text.length > 0) {
		selObject.text = tableRef;
	}
	else {
		if(window.opener.document.forms[0].pageContents.createTextRange && window.opener.document.forms[0].pageContents.caretPos) {
			var caretPos = window.opener.document.forms[0].pageContents.caretPos;
			caretPos.text = tableRef;
		}
		else {
			window.opener.document.forms[0].pageContents.value = tableRef;
		}
	}
}

/*
function insertFont(frmElement)
{
	frmObject = eval("document.forms[0]." + frmElement);
	selText = document.selection.createRange().text;
	frmObject.focus();
	selObject = trimSpaces(document.selection.createRange());

	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	
	winWidth = sWidth - 400;
	winHeight = sHeight - 400;
	
	sTop = (sHeight - winHeight) / 2;
	sLeft = (sWidth - winWidth) / 2;
	winRef = window.open("chooseFont.php?frmElement=" + frmElement, "Fonts", "width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=0");
	return;

}
*/
function showPreview(frmElement) {
	frmObject = eval("document.forms[0]." + frmElement);

	if(frmObject.value.length <= 0) {
		alert("No contents are there to display");
		return;
	}
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	
	winWidth = sWidth - 20;
	winHeight = sHeight - 80;
	
	sTop = (sHeight - winHeight) / 2;
	sLeft = (sWidth - winWidth) / 2;
	winRef = window.open("showPreview.php?frmElement=" + frmElement, "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 saveData(frmElement) {
	frmObject = eval("document.forms[0]." + frmElement);

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

function cancelInput(frmElement) {
	frmObject = eval("window.opener.document.forms[0]." + frmElement);

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

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 findPos(frmElement, fString) {
}
function activate()
{
if(colorPanel.style.visibility=='visible')
 colorPanel.style.visibility='hidden';
else
 colorPanel.style.visibility='visible';
}

//function to check delete column
function chkDeleteColumn(Fval) {
	if(confirm("Are you sure to delete the column(s)?")) {
		document.forms[0].currentColumn.value = Fval;
		document.forms[0].frmAction.value = 'delete_column';
		document.forms[0].action = 'editTableData.php';
		document.forms[0].submit();
	} else {
		return false;
	}
}
//function to check insert column
function chkInsertColumn(Fval) {
	if(confirm("Are you sure to insert column(s) preceding to this cell?")) {
		document.forms[0].currentColumn.value = Fval;
		document.forms[0].frmAction.value = 'insert_column';
		document.forms[0].action = 'editTableData.php';
		document.forms[0].submit();
	} else {
		return false;
	}
}
//function to check delete row
function chkDeleteRow(Fval) {
	if(confirm("Are you sure to delete the row(s)?")) {
		document.forms[0].frmAction.value = 'delete_row';
		document.forms[0].rowNumber.value = Fval
		document.forms[0].action = 'editTableData.php';
		document.forms[0].submit();
	} else {
		return false;
	}
}
//function to check insert rows
function chkAddRows(Frow,Fcol) {
	if(confirm("Are you sure to insert row?")) {
		document.forms[0].rowNumber.value = Frow;
		document.forms[0].tableCols.value = Fcol;
		document.forms[0].action = 'getTableData.php';
		document.forms[0].submit();
	} else {
		return false;
	}
}
