/*
Ensures that an element exists and
optionally also if the value is correct.
Usage:		boolean elementExists('id');
			boolean elementExists('id', null);
			boolean elementExists('id', 'value');
Returns:	True of False.

Works with:
-------------------
* Opera 7
* Mozilla Firebird
* IE 6
* Netscape 6
*/
function elementExists(id, value) {
	var element = getElement(id);
	
	if(element == null) // Does the element exist?
		return false;
		
	if(value != null) { // Should we test the value too?
		if(element.value != value) // Is the elements value correct?
			return false;
	}

	// The element did exist (and if we tested the value, it matched).
	return true;
}

function getTopFrame() {
	var iterations = 0;
	var current = window;
	var ret = null;
	while(true) {
		if(current == null) { ret = null; break; }
		if(current.IsTopFrame) { ret = current; break; }
		if(current.parent == null) { ret = null; break; }
		if(current.parent == current) { ret = null; break; }
		if(iterations++ > 5) { ret = null; break; }
		current = current.parent;
	}

	return ret;
}

/*
Sets a set of form-element's values.
Usage:		void setFormVariables(new Array('id1', 'id2'), new Array('value1', 'value2'));
			void setFormVariables(new Array('id1', 'id2'), new Array('value1', 'value2'), true);
Returns:	nothing.

Works with:
-------------------
* Opera 7
* Mozilla Firebird
* IE 6
* Netscape 6
*/
function setFormVariables(ids, values, createIfNotExists) {
	for(i=0; i<ids.length; i++) {
		var element = getElement(ids[i]);
		if(element == null && createIfNotExists == true) {
			element = createElement(ids[i], ids[i], values[i]);
		} else {
			element.value = values[i];
		}
	}
}

/*
Gets the main form of the page (this form is
the one that the .NET framework generates).
Usage:		object getMainForm();
Returns:	null if it couldn't be identified, else the form-object.

Works with:
-------------------
* Opera 7
* Mozilla Firebird
* IE 6
* Netscape 6
*/
function getMainForm() {
	var element = getElement('__MAINFORMID');
	if(element == null) return null;
	var formObj = getElement(element.value);
	return formObj;
}

/*
Gets an element specified by an id.
Usage:		object getElement('id');
Returns:	null if not found, else the element-object.

Works with:
-------------------
* Opera 7
* Mozilla Firebird
* IE 6
* Netscape 6
*/
function getElement(id, doc) {
	if(typeof(doc) == 'undefined') doc = document;
	if(doc.getElementById) return doc.getElementById(id);
	else if(doc.all) return doc.all[id];
	else if(doc.layers) return doc.layers[id];
	return null;
}
/* REMOVE THIS IF THE ABOVE WORKS!
function getElement(id) {
	if(document.getElementById) return document.getElementById(id);
	else if(document.all) return document.all[id];
	else if(document.layers) return document.layers[id];
	return null;
}
REMOVE THIS IF THE ABOVE WORKS! */

/*
Creates a hidden element on the mainform.
Usage:		void createElement('id', 'name', 'value', 'type');
Note:		Any parameter in this list can be null. At least one of 'id' or 'name'
Returns:	the newly created element if successfully created, else false.

Works with:
-------------------
* Opera 7
* Mozilla Firebird
* IE 6
* Netscape 6
*/
function createElement(id, name, value, type) {
	if(id == null && name == null) return null;
	var mainForm = getMainForm();
	if(mainForm == null) return null;
	var element = document.createElement("INPUT");
	if(element == null) return null;
	
	if(type == null) type = 'hidden';
	element.type = type;
	if(id != null) element.id = id;
	if(name != null) element.name = name;
	if(value != null) element.value = value;

	mainForm.appendChild(element);

	return element;
}

/*
Removes a specified element from the form.
Actually replaces it with a misc. element.
Usage:		void removeElementByName('name');
Returns:	true if sucessfully removed, else false.

Works with:
-------------------
* Opera 7
* Mozilla Firebird
* IE 6
* Netscape 6
*/
function removeElementByName(name) {
	var outerNodes = document.getElementsByName(name);
	if(outerNodes == null) return false;
	if(outerNodes.length < 1) return false;

	for(var i=0; i<outerNodes.length; i++) {
		outerNodes[i].parentNode.replaceChild(document.createElement("I"), outerNodes[i]);
	}
		
	return true;
}

/*
Redirects an ASPX-form to another page-url.
If we don't remove the __VIEWSTATE-, __EVENTARGUMENT-
and __EVENTTARGET-variables we will get errors such
as 'Invalid Viewstate' and IsPostBack-errors.
Usage:		void redirectForm('action', 'target');
			void redirectForm('action', null);
			void redirectForm('action');
Returns:	True or False.

Works with:
-------------------
* Opera 7
* Mozilla Firebird
* IE 6
* Netscape 6
*/
function redirectForm(action, target) {
	var mainForm = getMainForm(); // Get the main-form.
	if(mainForm == null) return false;
		
	if(action != null) {
		// Remove the illegal redirect-variables.
		removeElementByName('__VIEWSTATE');
		removeElementByName('__EVENTARGUMENT');
		removeElementByName('__EVENTTARGET');

		mainForm.action = action;
		if(target) mainForm.target = target; // Do we want to set the target too?

		mainForm.submit();
		return true;
	}
	
	return false;
}

function adjustTextAreaById(id, minRows, minCols, maxRows, maxCols, wrapEnabled) {
	var element = getElement(id);
	adjustTextArea(element, minRows, minCols, maxRows, maxCols, wrapEnabled);
}

// Guidelines from the article: http://www.flws.com.au/showusyourcode/codeLib/code/dynamicLength.asp?catID=2
// Adjusts a TextArea to fit the content.
// Works on: Mozilla, IE6
// Doesn't work on: Opera 7.03, Netscape 6
// Usage: <TEXTAREA NAME="area" ROWS="1" COLS="30" onFocus="intervalId = window.setInterval('adjustTextArea(\'area\', 1, 30, -1, 30);', 1);" onBlur="window.clearInterval(intervalId);"></TEXTAREA>
function adjustTextArea(element, minRows, minCols, maxRows, maxCols, wrapEnabled) {
	if(maxRows == -1) maxRows = 10000;
	if(maxCols == -1) maxCols = minCols;

	var txtLength = element.value.length;
	var numRows = 0;
	var lines = element.value.split("\n");
	
	for(var i=0; i<=lines.length-1; i++) {
		numRows++;
		if(wrapEnabled == false) {
			if(lines[i].length > maxCols-5)
				numRows += Math.floor(lines[i].length / maxCols);
		}
	}
	
	if(txtLength == 0) {
		element.cols = minCols;
		element.rows = minRows;
	} else {
		if(numRows <= 1) {
			element.cols = (txtLength % maxCols) + 1 >= minCols ? ((txtLength % maxCols) + 1) : minCols;
		} else {
			element.cols = maxCols;    
			element.rows = numRows > maxRows ? maxRows : numRows;
		}
	}
}

/*

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[IE6][MF][O7][NS6]
Code:
	getMainForm().submit();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*/

/*
function setForm(action, target) {
	if(action != null) {
		var formObj = getMainForm();
		if(formObj == null) return;
		
		formObj.action = action; // document.forms.item(0).action = action;
		if(target != null)
			formObj.target = target; // document.forms.item(0).target = target;
		formObj.submit(); // document.forms.item(0).submit();
	}
}
*/
/*
function setTextProperty(prefix, suffix) {
	if(window.getSelection) {
		//var str = window.getSelection();
		//str = prefix + str + suffix;
		return false;
	} else if(document.selection && document.selection.createRange) {
		var range = document.selection.createRange();
		var str = range.text;
	    range.text = prefix + str + suffix;
	} else
		return false;
	
	return true;
}
*/

function saveSelectedRange() {
	if(window.getSelection)
		savedRange = window.getSelection();
	else if(document.selection && document.selection.createRange)
		savedRange = document.selection.createRange().duplicate();
}

function surroundSavedRange(prefix, suffix) {
	if(window.getSelection)
		return false;
	else if(document.selection && document.selection.createRange)
		savedRange.text = prefix + savedRange.text + suffix;
	return true;
}
