/****

		Generic functions 
		Made by Lars Johanson

***/
// Would like to test for --if (document.getElementById != undefined)-- instead of --if (document.getElementById)-- but IE5.0 returns an error in that case...


// Retrieves an element based on the form name and element name
function retrieveElement(formName, elementName)
{
	if (document.getElementById)	// W3C style
	{
		return document.getElementById(elementName);
	} else if (document.all)	// IE style
	{
		return eval("document.all." + elementName);
	} else if (document.layers)	// NN4 style
	{
		return eval("document." + formName + "." + elementName);
	} else
	{
		return null;
	}
}

		
// Retrieves all elements based on the form name and element name
function retrieveElementsByName(formName, elementName)
{
	if (document.getElementsByName)	// W3C style
	{
		return document.getElementsByName(elementName);
	} else if (document.all)	// IE style
	{
		return eval("document.all." + elementName);
	} else if (document.layers)	// NN4 style
	{	// Not sure if this works...
		return eval("document." + formName + "." + elementName);
	} else
	{
		return null;
	}
}


// Retrieves a string from a text object
function getTextValue(formName, elementName)
{
	return retrieveElement(formName, elementName).value;
}

		
// Sets a text object string
function setTextValue(formName, elementName, value)
{
	if (document.getElementById ||	// w3c
			document.all)	// IE
	{
		retrieveElement(formName, elementName).value = value;
	} else if (document.layers)	// NN4
	{
		eval("document." + formName + "." + elementName + ".value = '" + value + "'");
	} else	// Other (??)
	{
	}
}

		
// Retrieves the current selection (the value) from a select object (returns only the first)
function getSelectedValue(formName, elementName)
{
	if (document.getElementById ||	// W3C style
			document.all)	// IE style
	{
		return retrieveElement(formName, elementName).value;
	} else if (document.layers)	// NN4 style
	{
		var selElement = retrieveElement(formName, elementName);
		return selElement.options[selElement.selectedIndex].value;
	} else
	{
		return null;
	}
}

		
// Retrieves the current selection (the text) from a select object (returns only the first)
function getSelectedText(formName, elementName)
{
	var selElement;
	
	selElement = retrieveElement(formName, elementName);
	if (selElement.selectedIndex >= 0)
	{
		return selElement.options[selElement.selectedIndex].text;
	} else
	{
		return "";
	}
}


// Retrieves the current selection index
// Only tested on IE6 and NN4
function getSelectedIndex(formName, elementName)
{
	return retrieveElement(formName, elementName).selectedIndex;
}


// Clears the entire select box object
// Only tested on IE6 and NN4
function clearSelectBox(formName, elementName)
{
	var elem;
	
	elem = retrieveElement(formName, elementName);
	
	while (elem.length > 0)
	{
		elem.options[elem.length-1] = null;
	}
}


// Sets the specified select box option to the specified content
// Only tested on IE6 and NN4
function setSelectBoxOption(formName, elementName, pos, value, text)
{
	var elem;
	
	elem = retrieveElement(formName, elementName);
	
	elem.options[pos] = new Option(text, value, false, false);
}


// Finds an option in a select box. Returns the index of it or -1 if not found.
// Either value or text may be used, set the other to "null"
// If select is "true" then also selects it.
function findSelectBoxValue(formName, elementName, value, text, select)
{
	var i;
	var sel;
	var len;
	var item;
	
	sel = retrieveElement(formName, elementName);
	if (!sel || !sel.options)
	{
		return -1;
	}
			
	if (document.getElementById	// w3c
			|| document.all)	// IE
	{
		if (isNaN(sel.options.length))
		{
			len = 1000000;	// Special for Opera, they don't have any array.length property!!!???
		} else
		{
			len = sel.options.length;	// Correct
		}
		
		for ( i = 0 ; i < len ; i++ )
		{
			// The following if-construct is also a special for Opera
			if (sel.options.item(i))
			{
				item = sel.options.item(i);
			} else
			{
				break;
			}
			
			if (value != null && item.value == value ||
					text != null && item.text == text)
			{
				if (select)
					sel.selectedIndex = i;
				return i;
			}
		}
	} else if (document.layers)	// NN4
	{
		for ( i = 0 ; i < sel.options.length ; i++ )
		{
			if (value != null && sel.options[i].value == value ||
					text != null && sel.options[i].text == text)
			{
				if (select)
					sel.options[i].selected = true;
				return i;
			}
		}
	} else	// Other (??)
	{
	}
	return -1;
}


// Select or unselect this check box
function selectCheckBox(element, select)
{
	if (select)
	{
		element.checked = true;
	} else
	{
		element.checked = false;
	}
}
function selectCheckBox(form, element, select)
{
	if (select)
	{
		retrieveElement(form, element).checked = true;
	} else
	{
		retrieveElement(form, element).checked = false;
	}
}

		
// Enable or disable this object
function enableElement(element, enable)
{
	if (enable)
	{
		element.disabled = false;
	} else
	{
		element.disabled = true;
	}
}
function enableElement(form, element, enable)
{
	if (enable)
	{
		retrieveElement(form, element).disabled = false;
	} else
	{
		retrieveElement(form, element).disabled = true;
	}
}


// Submit named form
// Returns true on success and false on error
function submitForm(formName)
{
	if (document.getElementById)	// W3C style
	{
		document.getElementById(formName).submit();
	} else if (document.all)	// IE style
	{
		eval("document.all." + formName).submit();
	} else if (document.layers)	// NN4 style
	{
		eval("document." + formName + ".submit()");
	} else
	{
		return false;
	}
	return true;
}


// Returns the number of elements with the specified name, -1 on error
function getElemCount(formName, elementName)
{
	if (document.all)	// IE style
	{
		if (eval("document.all." + elementName))
		{
			if (eval("document.all." + elementName + ".length"))
				return eval("document.all." + elementName + ".length");
			else
				return 1;
		} else
		{
			return 0;
		}
	} else if (document.layers)	// NN4 style
	{
		if (eval("document." + formName + "." + elementName))
		{
			if (eval("document." + formName + "." + elementName + ".length"))
				return eval("document." + formName + "." + elementName + ".length");
			else
				return 1;
		} else
		{
			return 0;
		}
	} else
	{
		return -1;
	}
}


// Returns the value associated with the first element with the specified  
// name that is checked. Intended to be used on Radio button groups.
// Returns null if no such element is found.
function getCheckedRadioValue(formName, elementName)
{
	var num;
	
	num = getElemCount(formName, elementName);
	if (document.all)	// IE style
	{
		if (num == 1)
		{
			if (eval("document.all." + elementName + ".checked"))
				return eval("document.all." + elementName + ".value");
		} else
		{
			for (var i = 0; i < num; i++)
			{
				if (eval("document.all." + elementName + "[" + i + "]" + ".checked"))
					return eval("document.all." + elementName + "[" + i + "]" + ".value");
			}
		}
		return null;
	} else if (document.layers)	// NN4 style
	{
		if (num == 1)
		{
			if (eval("document." + formName + "." + elementName + ".checked"))
				return eval("document." + formName + "." + elementName + ".value");
		} else
		{
			for (var i = 0; i < num; i++)
			{
				if (eval("document." + formName + "." + elementName + "[" + i + "]" + ".checked"))
					return eval("document." + formName + "." + elementName + "[" + i + "]" + ".value");
			}
		}
		return null;
	} else
	{
		return null;
	}
}

// Selects the specified radio button
// Note that the value of the radio button is used to specify which radio button to select
function setRadioChecked(formName, elementName, elementValue)
{
	var num;
	
	num = getElemCount(formName, elementName);
	if (document.all)	// IE style
	{
		if (num == 1)
		{
			if (eval("document.all." + elementName + ".value") == elementValue)
			{
				eval("document.all." + elementName + ".checked=true");
			}
		} else
		{
			for (var i = 0; i < num; i++)
			{
				if (eval("document.all." + elementName + "[" + i + "]" + ".value") == elementValue)
				{
					eval("document.all." + elementName + "[" + i + "]" + ".checked=true");
					break;
				}
			}
		}
	} else if (document.layers)	// NN4 style
	{
		if (num == 1)
		{
			if (eval("document." + formName + "." + elementName + ".value") == elementValue)
			{
				eval("document." + formName + "." + elementName + ".checked=true");
			}
		} else
		{
			for (var i = 0; i < num; i++)
			{
				if (eval("document." + formName + "." + elementName + "[" + i + "]" + ".value") == elementValue)
				{
					eval("document." + formName + "." + elementName + "[" + i + "]" + ".checked=true");
					break;
				}
			}
		}
	}
}

// Sets the specified style to the specified value
function setElemStyle(elem, name, value)
{
	if (elem && elem.style)
		eval("elem.style." + name + "=value");
}
function setStyle(formName, elemName, styleName, styleValue)
{
	setElemStyle(retrieveElement(formName, elemName), styleName, styleValue);
}

// Removes leading spaces, newlines and tabs
function LTrim(str)
{
	var i;
	var j;
	var s;
	var whitespace = new String(" \t\n\r");
	
  s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) 
	{
		// We have a string with leading blank(s)
		j = 0;
		i = s.length;
		// Iterate from the far left of string until we
		// don't have any more whitespace...
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;

		// Get the substring from the first non-whitespace
		// character to the end of the string...
		s = s.substring(j, i);
	}
	return s;
}

// Remove trailing spaces, newlines and tabs
function RTrim(str)
{
	var i;
	var s;
	var whitespace = new String(" \t\n\r");

	s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1)
	{
		// We have a string with trailing blank(s)
		i = s.length - 1;       // Get length of string
		// Iterate from the far right of string until we
		// don't have any more whitespace...
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		
		// Get the substring from the front of the string to
		// where the last non-whitespace character is...
		s = s.substring(0, i+1);
	}
	return s;
}

// Remove leading and trailing white space characters
function Trim(str)
{
	return RTrim(LTrim(str));
}

// Finds the absolute x position of the specified element
function findPosX(formName, elementName)
{
	var obj = retrieveElement(formName, elementName);;
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

// Finds the absolute x position of the specified element
function findPosY(formName, elementName)
{
	var obj = retrieveElement(formName, elementName);
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// Sets the specified style to specified value
// Returns true on success, false on error
function setStyle(formName, elementName, style, value)
{
	try
	{
		if (document.getElementById)	// W3C style
		{
			// Would like to test for --if (eval("document.getElementById('" + elementName + "').style." + style))-- but it may be "" and then returns "false"...
			eval("document.getElementById('" + elementName + "').style." + style + "='" + value + "'");
		} else if (document.all)	// IE style
		{
			// Would like to test for --if (eval("document.all." + elementName + ".style." + style))-- but it may be "" and then returns "false"...
			eval("document.all." + elementName + ".style." + style + "='" + value + "'");
		} else if (document.layers)	// NN4 style
		{
			if (eval("document." + formName + "." + elementName + ".style." + style) != undefined)
			{
				eval("document." + formName + "." + elementName + ".style." + style + "='" + value + "'");
			}
		}
	} catch (e)
	{
		return false;
	}
	return true;
}

// Retrieves the value of the specified style
function retrieveStyle(formName, elementName, style)
{
	if (document.getElementById)	// W3C style
	{
		// Would like to test for --if (eval("document.getElementById('" + elementName + "').style." + style))-- but it may be "" and then returns "false"...
		return eval("document.getElementById('" + elementName + "').style." + style);
	} else if (document.all)	// IE style
	{
		// Would like to test for --if (eval("document.all." + elementName + ".style." + style))-- but it may be "" and then returns "false"...
		return eval("document.all." + elementName + ".style." + style);
	} else if (document.layers)	// NN4 style
	{
		if (eval("document." + formName + "." + elementName + ".style." + style) != undefined)
		{
			return eval("document." + formName + "." + elementName + ".style." + style);
		}
	}
	return null;
}

