/**
 * Online TextEditor Functions.
 * Cookies.js library must be imported prior to this script!
 * @author	$Author: suraj $
 * @version	$Revision: 1.6 $, $Date: 2003/02/11 03:54:40 $
 */
//

//---------------------------------------------------------------------
// global variables

var MESSAGES = new Array();
MESSAGES["continue"] = "Do you still want to continue?";
MESSAGES["changes"] = "Any unsaved changes to the current file will be lost.\n"+ MESSAGES["continue"];
MESSAGES["upload"] = "Uploading of a new file will overwrite the current file.\n"+ MESSAGES["continue"];
MESSAGES["save"] = "Saving this file will overwrite the current file."+ MESSAGES["continue"];

var the_form = null; // must be SET before calling any functions otherwise error
var cookie_name = "(OnText)";

window.onresize = handleResize;

//---------------------------------------------------------------------
// functions

function init()
{
	the_form = document.forms[0];//document.getElementById("EditorForm");
	loadTextSize();
	updateTextSize();
	handleResize();
}

function handleResize()
{
	document.forms[0].file_contents.style.height = (document.body.clientHeight * 0.64) + "px";
	document.forms[0].file_contents.style.width = (document.body.clientWidth * 0.95) + "px";
}

function isActiveDocument()
{
	return (isActivePath() && isActiveContent());
}

function isActivePath()
{
	return (the_form.is_file.value == 1 ? true : false);
}

function isActiveContent()
{
	return (the_form.file_contents.value != the_form.file_contents.defaultValue);
}

function spellCheck()
{
	if(isActiveContent())
	{
		window.open('http://www.spellcheck.net/cgi-bin/spell.exe?action=CHECKPG&string='+ escape(the_form.file_contents.value), 'SpellCheck');
	}
	else
	{
		alert("There is no text to spell check.");
	}
}

function fileBrowser()
{
	var url = "browser.php?path="+ escape(getDirPath(the_form.path.value));
	
	if(!isActiveDocument())
	{
		window.location = url;
	}
	else
	{
		window.open(url);
	}
}

// reads values from cookie
function loadTextSize()
{
	var cookieData = parseCookie(WM_readCookie(cookie_name), '/', ':');
	
	if(cookieData["rows"] != null && cookieData["cols"] != null)
	{
		var r = parseInt(cookieData["rows"]), c = parseInt(cookieData["cols"]);
		if(r <= 0) r = 1;
		if(c <= 0) c = 1;

		// apply values to textarea
		the_form.file_contents.rows = r;
		the_form.file_contents.cols = c;
	}
}

// changes the size of the textarea
function resizeTextarea()
{
	var rows, cols, input;

	if(cols = prompt("Enter the WIDTH (cols) for the text area:", the_form.file_contents.cols))
	{
		if(cols != null && cols > 0)
			the_form.file_contents.cols = cols.valueOf();
	}
	if(rows = prompt("Enter the HEIGHT (rows) for the text area:", the_form.file_contents.rows))
	{
		if(rows != null && rows > 0)
			the_form.file_contents.rows = rows.valueOf();
	}

	updateTextSize();
}

function updateTextSize()
{
	the_form.textRows.value = the_form.file_contents.rows;
	the_form.textCols.value = the_form.file_contents.cols;

	// set cookie containing this info
	var expiryDate = new Date();
	expiryDate.setYear(expiryDate.getYear()+1); // expires one year from now
	var theCookie = "rows:"+ the_form.textRows.value + "/" + "cols:"+ the_form.textCols.value;

	WM_setCookie(cookie_name, theCookie, expiryDate.toGMTString());
}

function resetForm()
{
	the_form.action = "";
	the_form.target = "_self";
}

function downloadFile()
{
	the_form.action = "download.php";
	the_form.submit();

	// restore values
	resetForm();
}
function uploadFile(allow)
{
	if(confirm(MESSAGES["upload"]))
	{
		the_form.target = "_blank";
		the_form.action = "upload.php";
		the_form.submit();

		// restore values
		resetForm();
	}
}
function saveFile()
{
	if(isActivePath())
	{
		the_form.target = "_blank";
		the_form.action = "save.php";
		the_form.submit();

		// restore values
		resetForm();
	}
	else if(isActiveContent())
	{
		saveFileAs();
	}
}
function saveFileAs()
{
	the_form.target = "_blank";
	the_form.action = "save_as.php";
	the_form.submit();

	// restore values
	resetForm();
}
function openFile(allow)
{
	if(!isActivePath() || allow)
	{
		window.location = "open.php"; //?path="+ escape(the_form.path.value);
	}
	else if(confirm(MESSAGES["changes"]))
	{
		openFile(true);
	}
}
function newFile()
{
	closeFile();
}
function closeFile(allow)
{
	if(!isActiveDocument() || allow)
	{
		the_form.path.value = "";
		the_form.action = ""; // submits to SELF
		the_form.submit();
	}
	else if(confirm(MESSAGES["changes"]))
	{
		closeFile(true);
	}
}
function refreshFile()
{
	if(confirm(MESSAGES["changes"]))
	{
		window.location.reload();
	}
}

/**
 * Parses the path to a directory from a string.
 *
 * Ex: '/dir/file.html' will result in: '/dir'
 */
function getDirPath(path)
{
	var index = path.lastIndexOf('/');
	
	if(index == -1)
		index = path.lastIndexOf('\\');
	
	if(index != -1)
		return path.substring(0, index);
}
