// OpenWebTerm JavaScript support functions
// Dr. Klemens Waldhör / www.waldhor.com / dr.klemens.waldhoer@waldhor.com, December 2001
//
//
//	Cookie Functions - Second Helping	(21-Jan-96)
//	Written by:	Bill Dortch, hIdaho Design <bdortch@netw.com>
//	The following functions are released to the public domain.
//
//	The Second Helping version of the cookie functions dispenses with
//	my encode and decode functions, in favor of JavaScript's new built-in
//	escape and unescape functions, which do more complete encoding, and
//	which are probably much faster.
//
//	The new version also extends the SetCookie function, though in
//	a backward-compatible manner, so if you used the First Helping of
//	cookie functions as they were written, you will not need to change any
//	code, unless you want to take advantage of the new capabilities.
//
//	The following changes were made to SetCookie:
//
//	1.	The expires parameter is now optional - that is, you can omit
//			it instead of passing it null to expire the cookie at the end
//			of the current session.
//
//	2.	An optional path parameter has been added.
//
//	3.	An optional domain parameter has been added.
//
//	4.	An optional secure parameter has been added.
//
//	For information on the significance of these parameters, and
//	and on cookies in general, please refer to the official cookie
//	spec, at:
//
//			http://www.netscape.com/newsref/std/cookie_spec.html		
//
//
// "Internal" function to return the decoded value of a cookie
//

function getCookieVal (offset) 
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}


//
//	Function to return the value of the cookie specified by "name".
//		name - String object containing the cookie name.
//		returns - String object containing the cookie value, or null if
//			the cookie does not exist.
//

function GetCookie (name) 
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

//
//	Function to create or update a cookie.
//		name - String object object containing the cookie name.
//		value - String object containing the cookie value.	May contain
//			any valid string characters.
//		[expires] - Date object containing the expiration data of the cookie.	If
//			omitted or null, expires the cookie at the end of the current session.
//		[path] - String object indicating the path for which the cookie is valid.
//			If omitted or null, uses the path of the calling document.
//		[domain] - String object indicating the domain for which the cookie is
//			valid.	If omitted or null, uses the domain of the calling document.
//		[secure] - Boolean (true/false) value indicating whether cookie transmission
//			requires a secure channel (HTTPS).	
//
//	The first two parameters are required.	The others, if supplied, must
//	be passed in the order listed above.	To omit an unused optional field,
//	use null as a place holder.	For example, to call SetCookie using name,
//	value and path, you would code:
//
//			SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//	Note that trailing omitted parameters do not require a placeholder.
//
//	To set a secure cookie for path "/myPath", that expires after the
//	current session, you might code:
//
//			SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//



function SetCookie (name, value) 
{
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
		((path == null) ? "" : ("; path=" + path)) +
		((domain == null) ? "" : ("; domain=" + domain)) +
		((secure == true) ? "; secure" : "");
}

//	Function to delete a cookie. (Sets expiration date to current date/time)
//		name - String object containing the cookie name
//

function DeleteCookie (name) 
{
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);	// This cookie is history
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

// Function SaveValuesInCoockie 
// Description	save the login values in OpenWebTerm cookie
// Parameter	Type	Comment
// theForm		document 	the document where to set the login values
//							the form in there should be called "login" (see below)
// username				input field
// password				input field
// email				input field
// defaultdb			input field
// defaultsearchlans	input field
// defaultdisplans		input field
// defaultdisplay		input field
//
// Typical usage in html document
// <form action="/cgi-bin/OpenWebTerm/loginstart.cgi" method="POST" name="login" onSubmit="return SaveValuesInCoockie(this, this.username, this.password, this.email, this.defaultdb, this.defaultsearchlans, this.defaultdisplans, this.display)" enctype="multipart/form-data">
// requires in <head>
//		<script language="JavaScript" src="/support_lib.js">
//					document.write("<h1>The javascript library support_lib.js is not present.</h1>");
//		</script>
// Returns true
// Annotation: 
function SaveValuesInCoockie(theForm, username, password, email, defaultdb, defaultsearchlans, defaultdisplans, defaultdisplay)
{

	var expires = new Date();
	expires.setTime (expires.getTime() + 24 * 60 * 60 * 365 * 1000);
	SetCookie("OPW_version",           "0.1",                   expires, "/");

	if (username != null)
		SetCookie("OPW_username",          username.value,          expires, "/");
	if (password != null)
		SetCookie("OPW_password",          password.value,          expires, "/");
	if (email != null)
		SetCookie("OPW_email",             email.value,             expires, "/"); 
	
	var selecteddbs = "";
	if (defaultdb != null)
	{
		for(var index = 0; index < defaultdb.options.length; index++)
		{
			if (defaultdb.options[index].selected)
				selecteddbs = selecteddbs + defaultdb.options[index].text + ";";
		}
		SetCookie("OPW_defaultdb",         selecteddbs,         expires, "/");
	} 
	
	var selecteddefaultsearchlans = "";
	if (defaultsearchlans != null)
	{
		for(var index = 0; index < defaultsearchlans.options.length; index++)
		{
			if (defaultsearchlans.options[index].selected)
				selecteddefaultsearchlans = selecteddefaultsearchlans + defaultsearchlans.options[index].text + ";";
		}
		SetCookie("OPW_defaultsearchlans",         selecteddefaultsearchlans,         expires, "/");
	} 

	var selecteddefaultdisplans = "";	
	if (defaultdisplans != null)
	{
		for(var index = 0; index < defaultdisplans.options.length; index++)
		{
			if (defaultdisplans.options[index].selected)
				selecteddefaultdisplans = selecteddefaultdisplans + defaultdisplans.options[index].text + ";";
		}	
		SetCookie("OPW_defaultdisplans",   selecteddefaultdisplans,   expires, "/"); 
	}
	
	if (defaultdisplay != null)
	{
		if (defaultdisplay.checked)
		{
			SetCookie("OPW_defaultdisplay", "Dictionary",   expires, "/");
		}
		else
		{
			SetCookie("OPW_defaultdisplay", "Edit",   expires, "/");
		}
	}
//	alert("SaveValuesInCoockie:" + username.value + " " + password.value + " " + email.value);
	
	return true;
}

// Function strstr 
// Description	searches for sub string in a string (like in C)
// Parameter	Type	Comment
// forsearch	string	string to search in
// tosearch		string	string to search for in forsearch
// Returns true if found, otherwise false
// Annotation: could be more simple done by using a regep ... :-)

function strstr (forsearch, tosearch)
{
	if (forsearch == null)
		return false;
	if (tosearch == null)
		return false;
			
	var ires = forsearch.indexOf(tosearch);
	if (ires == -1)
		return false;
	return true;
}


// Function SetSimpleSearch 
// Description	sets simple search type flag
// Parameter	Type	Comment
// theForm		document 	uses then theForm.add.simple
// Typical usage in html document
// 		<form action="/cgi-bin/OpenWebTerm/addtermtodb.cgi" method="POST" name="add" onSubmit="return SetSimpleSearch(this);" enctype="multipart/form-data">
// requires in <head>
//		<script language="JavaScript" src="/support_lib.js">
//					document.write("<h1>The javascript library support_lib.js is not present.</h1>");
//		</script>
// Returns nothing

function SetSimpleSearch(theForm)
{
	
	if (theForm != null)
	{
		if (theForm.simple != null)
		{
			theForm.simple.value = "Simple";
		}
	}
	
	return;
}

// Function SetUserAndPassword 
// Description	sets user and password hidden input fields
// Parameter	Type	Comment
// theForm		document 	the document where to set the login values
//							the form in there should be called "add", "createdb" or "editdelete" (see below)
// Typical usage in html document
// <body onload="SetUserAndPassword(this)" bgcolor=aliceblue>
// and then form is set with:
// 		<form action="/cgi-bin/OpenWebTerm/addtermtodb.cgi" method="POST" name="add" onSubmit="return CheckEmptyTerm(this.document);" enctype="multipart/form-data">
// requires in <head>
//		<script language="JavaScript" src="/support_lib.js">
//					document.write("<h1>The javascript library support_lib.js is not present.</h1>");
//		</script>
// Returns nothing

function SetUserAndPassword(theForm)
{
	var username          = GetCookie ("OPW_username");
	var password          = GetCookie ("OPW_password");
	var email             = GetCookie ("OPW_email");
	var defaultdb         = GetCookie ("OPW_defaultdb");
	var defaultsearchlans = GetCookie ("OPW_defaultsearchlans");
	var defaultdisplans   = GetCookie ("OPW_defaultdisplans");
	var defaultdisplay    = GetCookie ("OPW_defaultdisplay");
	var index = 0;
	var formnum = -1;
	
	allforms = document.forms;
	formnum = allforms.length;

	for(index = 0; index < formnum; index++)
	{
		var formname = allforms[index]; 
		if (formname != null) 
		{
			if (formname.user != null)
			{
				formname.user.value = username;
			}
			
			if (formname.password != null)
			{
				formname.password.value = password;
			}
			
			if (formname.email != null)
			{
				formname.email.value = email;
			}
		}
	}	
}

// Function SelectDatabase 
// Description	selects the default database in document
// Parameter	Type	Comment
// theForm		document 	the document where to set the login values
//							the form in there should be called "add" (see below)
// Typical usage in html document
// <body onload="SelectDatabase(this);GetValuesForAdd(this, 1)" bgcolor=aliceblue>\n";
// and then form is set with:
// 		<form action="/cgi-bin/OpenWebTerm/addtermtodb.cgi" method="POST" name="add" onSubmit="return CheckEmptyTerm(this.document);" enctype="multipart/form-data">
// requires in <head>
//		<script language="JavaScript" src="/support_lib.js">
//					document.write("<h1>The javascript library support_lib.js is not present.</h1>");
//		</script>
// Returns nothing
// Annotation: used in: 
// importdbgui.cgi	<body onload=\"SelectDatabase(this);GetValuesForAdd(this, 1)\" bgcolor=aliceblue>\n";


function SelectDatabase(theForm)
{
	var username          = GetCookie ("OPW_username");
	var password          = GetCookie ("OPW_password");
	var email             = GetCookie ("OPW_email");
	var defaultdb         = GetCookie ("OPW_defaultdb");
	var defaultsearchlans = GetCookie ("OPW_defaultsearchlans");
	var defaultdisplans   = GetCookie ("OPW_defaultdisplans");
	
	if (theForm.defaultdb != null)
	{
		var indexlen= theForm.defaultdb.options.length;
		for(var index = 0; index < indexlen; index++)
		{
			var opttext = theForm.defaultdb.options[index].text + ";"; 
			if (strstr(defaultdb, opttext))
			{
				theForm.defaultdb.options[index].selected = true;
			}
		}
	} 
	return;
		
}

// Function GetUser 
// Description	get the user from cookie
// Returns user

function GetUser ()
{
	var username = GetCookie ("OPW_username");
	return username;
}

// Function SetUserPassword 
// Description	sets user and password from cookie in documents (with form createdb)
// theForm		document 	the document where to set the user and password values
// Returns nothing

function SetUserPassword (theForm)
{
	var username = GetCookie ("OPW_username");
	if (theForm.createdb.user != null)
	{
		theForm.createdb.user.value = username;
	} 
	
	var password = GetCookie ("OPW_password");
	if (theForm.createdb.password != null)
	{
		theForm.createdb.password.value = password;
	} 
	return;
}

// Function returnDetails 
// Description	returns the details of a document in concatened form
//				details are stored in hidden input fields like
//				<input type="hidden" name="details0"> where details0-n
//				The format is:
//				"::opwdetail0=string::opwdetail2=string::opwdetail3=

// Parameter	Type	Comment
// theForm		document 	the document where to get the details from
//							the form in there should be called "add" (see below)
// inum			integer		the number of the detail hidden input field
//							which will store the details
// Typical usage in html document
// <input type="button" name="buttondetails0" value="Save Details" onclick="returnDetails(this.document, 0)">
// and then form is set with:
// requires in <head>
//		<script language="JavaScript" src="/support_lib.js">
//					document.write("<h1>The javascript library support_lib.js is not present.</h1>");
//		</script>
// Returns nothing
// Annotation: used in: 

function returnDetails(theForm, inum)
{
//	if ((theForm == null) || (theForm.add == null))
//		return;
		
	var index;
				
	var retval = "";
	
	// create the string for the detail values by concatenating all fields			
	var indexlen; //= theForm.add.length;
	
	allforms = document.forms;
	formnum = allforms.length;

	for(index = 0; index < formnum; index++)
	{
		if (allforms[index].name == "add")
		{
			theForm = allforms[index];
			indexlen= theForm.length;
			break;
		}
	}
	
	for(index = 0; index < indexlen; index++)
	{
		obj = theForm.elements[index]; // theForm.add.elements[index];
		// name of opwdetail<nn> to compare
		var formname = "opwdetail";
		var regstring = /(opwdetail.*)/;
		var myregarray = regstring.exec(obj.name);
		if (myregarray != null)
		{	
			var value = "";
			if (obj.type == "select-multiple")
			{
				var optionslen = obj.options.length;
				for(var indexopt = 0; indexopt < optionslen; indexopt++)
				{
					if (obj.options[indexopt].selected == true)
					{
						var addvalue = "";
						if (obj.options[indexopt].value == "")
							addvalue = obj.options[indexopt].text;
						else
							addvalue = obj.options[indexopt].value;
						if (value == "")
							value = value + addvalue;
						else
							value = value + "|" + addvalue;
					}
				}
			}
			else if (obj.type == "select-one")
			{
				var selected = obj.selectedIndex;
				value = obj.options[selected].value;
				if (value == "")
					value = obj.options[selected].text;
			}
			else if (obj.type == "checkbox")
			{
				value = obj.value;
				if (obj.checked == true)
					value = value + "$true";
				else
					value = value + "$false";
				
			}
			else if (obj.type == "radio")
			{
				if (obj.checked == true)
					value = obj.value;
			}
			else if ((obj.type == "text") ||  (obj.type == "textarea"))
			{
				value = obj.value;
			}
			if (value != "")
				retval = retval + "::" + RegExp.$1 + "=" + value;
		}
	}	
	// terminate retval string
	retval = retval + "::";
	// alert(retval);
	
	// now manipulate the detailsnn field of the parent opener window
	// map over all elements of form add !!
	var indexlen= theForm.length;
	for(var index = 0; index < indexlen; index++)
	{
		var obj = theForm.elements[index];
		// name of detailnn to compare
		var formname = "details" + inum;
		if (obj.name == formname)
		{
			obj.value = retval; // if equal we have found it !!!
		}
		
		formname = "term" + inum;
		if (obj.name == formname)
		{
			obj.style.backgroundColor = "green"; // if equal we have found it !!!
		}
		
	}
//	window.close();
	return;
}

// Function returnConceptDetails 
// Description	returns the details of a concept in concatened form
//				details for concepr are stored in hidden an input field domain
//				<input type="hidden" name="domain">
//				The format is:
//				"::conceptdetail0=string::conceptdetail2=string::conceptdetail3=

// Parameter	Type	Comment
// theForm		document 	the document where to get the details from
//							the form in there should be called "add" (see below)
// Typical usage in html document
// Returns nothing
// Annotation: nearly identical to returnDetails

function returnConceptDetails(theForm)
{
//	if ((theForm == null) || (theForm.add == null))
//		return;
				
	var retval = "";
	
	// create the string for the detail values by concatenating all fields			
	// var indexlen= theForm.add.length;
	var indexlen; //= theForm.add.length;
	
	allforms = document.forms;
	formnum = allforms.length;

	for(index = 0; index < formnum; index++)
	{
		if (allforms[index].name == "add")
		{
			theForm = allforms[index];
			indexlen= theForm.length;
			break;
		}
	}
	
	for(var index = 0; index < indexlen; index++)
	{
		obj = theForm.elements[index];
		// name of opwdetail<nn> to compare
		var formname = "conceptdetail";
		var regstring = /(concept.*)/;
		var myregarray = regstring.exec(obj.name);
		if (myregarray != null)
		{	
			var value = "";
			if (obj.type == "select-multiple")
			{
				var optionslen = obj.options.length;
				for(var indexopt = 0; indexopt < optionslen; indexopt++)
				{
					if (obj.options[indexopt].selected == true)
					{
						var addvalue = "";
						if (obj.options[indexopt].value == "")
							addvalue = obj.options[indexopt].text;
						else
							addvalue = obj.options[indexopt].value;
						if (value == "")
							value = value + addvalue;
						else
							value = value + "|" + addvalue;
					}
				}
			}
			else if (obj.type == "select-one")
			{
				var selected = obj.selectedIndex;
				value = obj.options[selected].value;
				if (value == "")
					value = obj.options[selected].text;
			}
			else if (obj.type == "checkbox")
			{
				value = obj.value;
				if (obj.checked == true)
					value = value + "$true";
				else
					value = value + "$false";
				
			}
			else if (obj.type == "radio")
			{
				if (obj.checked == true)
					value = obj.value;
			}
			else if ((obj.type == "text") ||  (obj.type == "textarea"))
			{
				value = obj.value;
			}
			if (value != "")
				retval = retval + "::" + RegExp.$1 + "=" + value;
		}
	}	
	// terminate retval string
	retval = retval + "::";
	// alert(retval);
	
	// now manipulate the hidden domain field of the parent opener window
	// map over all elements of form add !!
	theForm.domain.value = retval;
//	window.close();
	return;
}

// Function CheckEmptyTerm 
// Description	checks if at least one term contains a string, displays warning
// Parameter	Type	Comment
// theForm		document 	the document where to get the details from
//							the form in there should be called "add" (see below)
// Typical usage in html document
// <form action="/cgi-bin/OpenWebTerm/addtermtodb.cgi" method="POST" name="add" onSubmit="return CheckEmptyTerm(this.document);" enctype=\"multipart/form-data">
// requires in <head>
//		<script language="JavaScript" src="/support_lib.js">
//					document.write("<h1>The javascript library support_lib.js is not present.</h1>");
//		</script>
// Returns true if one termnn field is not empty, false otherwise
// Annotation: should be made simpler and more general so that it is not limited to  9 term fields ...

function CheckEmptyTerm(theForm)
{
	// if (theForm == null)
	//	return false;
		
	// if (theForm.add == null)
	//	return false;

	allforms = document.forms;
	formnum = allforms.length;

	for(index = 0; index < formnum; index++)
	{
		if (allforms[index].name == "add")
		{
			theForm = allforms[index];
			indexlen= theForm.length;
			break;
		}
	}
		
	if (theForm.addnewfield != null)
	{
		if (theForm.addnewfield.value == "add")
			return true;
	}
	
	var indexlen= theForm.length;
	for(var index = 0; index < indexlen; index++)
	{
		obj = theForm.elements[index];
		// name of term<nn> to compare
		var regstring = /(term.*)/;
		var myregarray = regstring.exec(obj.name);
		if (myregarray != null)
		{
			if (obj.value != "")
				return true;			
		}	
	}
		
	alert("All term fields are empty ! Please fill in one at least !");
	return false;			
}

// Function AddNewInputField 
// Description	adds a new input table row in the add concept form; increase no of input elements theForm.add.noofinputfields + 1
// Parameter	Type	Comment
// theForm		document 	the document where to get the details from
//							the form in there should be called "add" (see below)
// Typical usage in html document
// <input type="button" name="addinputfield" value="New 'Term to add' Field" onclick="AddNewInputField(this)";>
// requires in <head>
//		<script language="JavaScript" src="/support_lib.js">
//					document.write("<h1>The javascript library support_lib.js is not present.</h1>");
//		</script>
// Returns nothing
// Annotation: 

function AddNewInputField(theForm)
{
//	alert("Not yet implemented !");
	if (theForm.add.addnewfield != null)
	{
		theForm.add.addnewfield.value = "add";
	}
	return;
}

// Function filldetails 
// Description	fills the value of the details with the values of the corresponding hidden detaiul field
//				Function is needed to update the details if the user clicks into a term (concept field)
// Parameter	Type	Comment
// theForm		document 	the document where to get the details from
//							the form in there should be called "add" (see below)
// inum			integer		the hidden input field with the details value reference
// Typical usage in html document
// <input type="text" size="30" name="term$val" maxlength="30" value="$term" onfocus="filldetails(this.document, $i)">
// term$val is the ith term input field
// requires in <head>
//		<script language="JavaScript" src="/support_lib.js">
//					document.write("<h1>The javascript library support_lib.js is not present.</h1>");
//		</script>
// Returns nothing
// Annotation: 


function filldetails(theForm, inum)
{
	var detailvalue = "";
	var obj;
	var detvalue = "";
	// if (theForm == null)
	// 	return;
		
	// if (theForm.add == null)
	//	return;
	
	datum = new Date();
	var month = datum.getMonth() + 1;
	var datumstring = datum.getFullYear() + "-" + month + "-" + datum.getDate() + " " + datum.getHours() + ":" + datum.getMinutes() + ":"  + datum.getSeconds();
	creationdate = datumstring;
	changedate = datumstring;

	allforms = document.forms;
	formnum = allforms.length;

	for(index = 0; index < formnum; index++)
	{
		if (allforms[index].name == "add")
		{
			theForm = allforms[index];
			indexlen= theForm.length;
			break;
		}
	}
		
	var indexlen= theForm.length;
	for(var index = 0; index < indexlen; index++)
	{
		obj = theForm.elements[index];
		// name of detailnn to compare
		var formname = "details" + inum;
		if (obj.name == formname)
		{
			detailvalue = obj.value; // if equal we have found it !!!
			break;
		}
	}		
			
	// Form of detailstring:
	// ::opwdetail1=[value]::opwdetail2=[value] ...
	
	// modify the value string			
	if ((detailvalue != null) && (detailvalue != ""))
	{
		var indexlen= theForm.length;
		for(var index = 0; index < indexlen; index++)
		{
			obj = theForm.elements[index];
			// name of opwdetail<nn> to compare
			var regstring = /(opwdetail.*)/;
			var myregarray = regstring.exec(obj.name);
			if (myregarray != null)
			{	
				// now we must extract the corresponding value from detailvalue
				var searchfor = obj.name + "=";
				var myvalue = finddetailvalue(detailvalue, searchfor);
				if (obj.type == "select-multiple")
				{
					var optionslen = obj.options.length;
					for(var indexopt = 0; indexopt < optionslen; indexopt++)
					{
						if (myvalue.indexOf(obj.options[indexopt].value) != -1)
						{
							obj.options[indexopt].selected = true;
						}
						else
						{
							obj.options[indexopt].selected = false;
						}
					}
				}
				else if (obj.type == "select-one")
				{
					var optionslen = obj.options.length;
					for(var indexopt = 0; indexopt < optionslen; indexopt++)
					{
						if (obj.options[indexopt].value == myvalue)
						{
							obj.options[indexopt].selected = true;
							break;
						}
						else
							obj.options[indexopt].selected = false;
					}
					
				}
				else if (obj.type == "checkbox")
				{
					var compvalue = obj.value + "$true";
					if (myvalue == compvalue)
						obj.checked = true;
					else
						obj.checked = false;
					
				}
				else if (obj.type == "radio")
				{
					if (myvalue == obj.value)
						obj.checked = true;
				}
				else if ((obj.type == "text") ||  (obj.type == "textarea"))
				{
					obj.value = myvalue;
				}
			}
		}	
	}
	return;
}

// Function fillconcept 
// Description	fills the value of the concept related fields with the values of the corresponding hidden domain field
// Parameter	Type	Comment
// theForm		document 	the document where to get the details from
//							the form in there should be called "add" (see below)
// Typical usage in html document
// Returns nothing
// Annotation: very similar to filldetails


function fillconcept(theForm)
{
	var detailvalue = "";
	var obj;
	var detvalue = "";
	// if (theForm == null)
	//	return;
		
	// if (theForm.add == null)
	// 	return;

	allforms = document.forms;
	formnum = allforms.length;
	var index;
	
	for(index = 0; index < formnum; index++)
	{
		if (allforms[index].name == "add")
		{
			theForm = allforms[index];
			indexlen= theForm.length;
			break;
		}
	}
		
	datum = new Date();
	var month = datum.getMonth() + 1;
	var datumstring = datum.getFullYear() + "-" + month + "-" + datum.getDate() + " " + datum.getHours() + ":" + datum.getMinutes() + ":"  + datum.getSeconds();
	creationdate = datumstring;
	changedate = datumstring;
	
	detailvalue = theForm.domain.value; // if equal we have found it !!!
		
	// Form of detailstring:
	// ::conceptdetail1=[value]::conceptdetail2=[value] ...
	
	// modify the value string			
	if ((detailvalue != null) && (detailvalue != ""))
	{
		var indexlen= theForm.length;
		for(var index = 0; index < indexlen; index++)
		{
			obj = theForm.elements[index];
			// name of opwdetail<nn> to compare
			var regstring = /(conceptdetail.*)/;
			var myregarray = regstring.exec(obj.name);
			if (myregarray != null)
			{	
				// now we must extract the corresponding value from concept detailvalue
				var searchfor = obj.name + "=";
				var myvalue = finddetailvalue(detailvalue, searchfor);
				if (obj.type == "select-multiple")
				{
					var optionslen = obj.options.length;
					for(var indexopt = 0; indexopt < optionslen; indexopt++)
					{
						if (myvalue.indexOf(obj.options[indexopt].value) != -1)
						{
							obj.options[indexopt].selected = true;
						}
						else
						{
							obj.options[indexopt].selected = false;
						}
					}
				}
				else if (obj.type == "select-one")
				{
					var optionslen = obj.options.length;
					for(var indexopt = 0; indexopt < optionslen; indexopt++)
					{
						if (obj.options[indexopt].value == myvalue)
						{
							obj.options[indexopt].selected = true;
							break;
						}
						else
							obj.options[indexopt].selected = false;
					}
					
				}
				else if (obj.type == "checkbox")
				{
					var compvalue = obj.value + "$true";
					if (myvalue == compvalue)
						obj.checked = true;
					else
						obj.checked = false;
					
				}
				else if (obj.type == "radio")
				{
					if (myvalue == obj.value)
						obj.checked = true;
				}
				else if ((obj.type == "text") ||  (obj.type == "textarea"))
				{
					obj.value = myvalue;
				}
			}
		}	
	}
	return;
}

// Function finddetailvalue 
// Description	search for the value of a detail field "::opwdetail1=[value]::opwdetail2=[value] "
// Parameter	Type	Comment
// detvalue		string 	the detail value "::opwdetail1=[value]::opwdetail2=[value] "
// opwdetail	string	the hidden input field name opwdetail[nn]
// Typical usage in html document
// <input type="text" size="30" name="term$val" maxlength="30" value="$term" onfocus="filldetails(this.document, $i)">
// term$val is the ith term input field
// requires in <head>
//		<script language="JavaScript" src="/support_lib.js">
//					document.write("<h1>The javascript library support_lib.js is not present.</h1>");
//		</script>
// Returns the value of the detail (opwdetail)
// Annotation: 

function finddetailvalue(detvalue, opwdetail)
{
	// ::opwdetail1=[value]::opwdetail2=[value] ... ::
	var iposstart = detvalue.indexOf(opwdetail);
	if (iposstart != -1)
	{
		var ioplen = opwdetail.length;
		var iposend = detvalue.indexOf("::", iposstart+1);
		if (iposend == -1)
		{
			iposend = detvalue.length-1;
			return detvalue.substring(iposstart+ioplen, iposend);
		}
		else
		{	
			return detvalue.substring(iposstart+ioplen, iposend);
		}
	}
	
	return "";
}

// Function BrowserDetails 
// Description	return a string describing the browser
// Parameter	Type	Comment
// Returns a browser description string
// Annotation: 

function BrowserDetails()
{
	var retval = "Browser: " + navigator.appName + "\nCodename: " + navigator.appCodeName + "\nVersion: " + navigator.appVersion + "\nPlatform: " + navigator.platform  + "\nUser Language: " + navigator.userLanguage;
	retval = retval + "\nJava enabled: " + navigator.javaEnabled() + "\nUser Agent: " + navigator.userAgent;;
	return retval;
}

// Function SetBrowserDetails 
// Description	set the value of the browser details in a special text area
// Parameter	Type	Comment
// Returns always true
// Annotation:

function SetBrowserDetails(theForm)
{
	var browserdetails = BrowserDetails();
	if (navigator.appName != "Microsoft Internet Explorer")
	{
		if (theForm != null)
		{
			formnum = theForm.document.forms.length;
			
			// alert("SetBrowserDetails " + theForm.name + " " + formnum);
			for(index = 0; index < formnum; index++)
			{
				if (theForm.document.forms[index].name == "createdb")
				{
					theForm.document.forms[index].browserdetails.value = browserdetails;
					return;
				}
			}
		}
	}
	
	if (theForm.createdb != null)
	{
		if (theForm.createdb.browserdetails != null)
		{
			theForm.createdb.browserdetails.value = browserdetails;
		}
	}	
	return true;
}

// Function SetInputStyle 
// Description	sets style of an input field
// Parameter	Type	Comment
// input		input element
// language		language of input elements
// styles		string 	language related styles
// Returns  true if set otherwise false
// Annotation:	"languagestyles" "EL|symbol!!RU|arial unicode ms!!AR|arial unicode ms">

function SetInputStyle(language, input, styles)
{
	if (input == null)
		return false;
	if (language == null)
		return false;
	if (styles == null)
		return false;
	var option = language.options[language.selectedIndex];
	var value = option.text;
	// alert(value + "$$$$" + styles + "###" + input.style.fontFamily);
	// search for the language related style
	// regular expression: lan\|(.+)//
	var regstring = /([A-Z][A-Z]) .+/;
	var myregarray = regstring.exec(value);
	if (myregarray != null)	
	{
		var lancode = RegExp.$1 + "|";
		var index1 = styles.indexOf(lancode, 0);
		if (index1 != -1)
		{
			var index2 = styles.indexOf("!!", index1);
			var style = styles.substring(index1+3, index2);
			// alert(style);
			input.style.fontFamily = style;
			return true;
		}
	}
	// if nothing found check if a "$$DEFAULT$$|<font-family>!!" exists !
	var index1 = styles.indexOf("$$DEFAULT$$", 0);
	if (index1 != -1)
	{
		var index2 = styles.indexOf("!!", index1);
		var style = styles.substring(index1+12, index2);
		// alert(style);
		input.style.fontFamily = style;
		return true;
	}	
	input.style.fontFamily = "arial";
	return true;
}


function SetAllStyles(theForm, styles, noofinputfields)
{
	var indexlen= theForm.add.length;
	var inum = 0;
	var lanobj = null;
	var termobj = null;
	for (var inum = 0; inum < noofinputfields; inum++)
	{
		var lanname = "language" + inum;
		var termname = "term" + inum;
		for(var index = 0; index < indexlen; index++)
		{
			var obj = theForm.add.elements[index];
			// name of language to compare
			if (obj.name == lanname)
				lanobj = obj;
			else if (obj.name == termname)
				termobj = obj;
			if ((termobj != null) && (lanobj != null))
			{
				SetInputStyle(lanobj, termobj, styles)
			}
		}
	}
	return true;
}