/*This is the code that is part of the source of an interactive website.
The Users are a member of a group,
groups provide the users with different permissions.
Groups have a hierachical structure.
A lot of functionlities are available some of these functionalities are
build using other GPL licensed programs.

Copyright (C) 2003  Claeys Petra

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*/
/*
	Adds an item to a dropdown list, the value of the item is the content of a text field
	iSource : the content of the text field
	ioOptionsList : the complete dropdown list
	ioOption : one item of the dropdown list
	iTargetLength : the current length of the dropdown list
	ioAddedOptions : the nr of options added untill now ( should be a global variable )
	ioArray : the content of all data in the dropdown list ( global var )
*/
function AddItem(iSource, ioOption, ioOptionsList, ioAddedOptions, ioArray)
{
	if (iSource.value.length > 0)
	{
		if (!(CheckUniqueValue(iSource.value, ioArray)))
		{
			ResetSource(iSource);
			return ioAddedOptions;
		}
		/*if ( ioAddedOptions <= 0 )
		{
			ioOptionsList.length=0;
		}*/
		theOption= new Option();
		var ShortValue=CheckValueLength(iSource.value);
		theOption.value=ShortValue;
		theOption.text=ShortValue;
		ioOptionsList[ioOptionsList.length]=theOption;
		++ioAddedOptions;
		ioArray[ioArray.length]= iSource.value;
		ResetSource(iSource);
               	ioOptionsList.selectedIndex=0;
	        //ShowItems(ioOptionsList, ioArray, iSource);
	}
	/*var testindex=0
	while (testindex < ioArray.length)
	{
		++testindex
	}*/
	return ioAddedOptions;
}

function LoadItem(inArraySource, ioOptionsList, ioAddedOptions)
{
	var count=0;
	while ( count < inArraySource.length)
	{
		theOption= new Option();
		var ShortValue=CheckValueLength(inArraySource[count]);
		theOption.value=ShortValue;
		theOption.text=ShortValue;
		ioOptionsList[++count]=theOption;
                ++ioAddedOptions;
	}
	ioOptionsList.selectedIndex=0;
	return ioAddedOptions;
}

function DeleteItem(iSource, ioOptionsList, ioAddedOptions, ioArray, iStdString)
{	
	var theIndex = ioOptionsList.selectedIndex
	if ( theIndex <= 0 )
	{
		/*theOption= new Option();
		theOption.value=iStdString;
		theOption.text=iStdString;
		ioOptionsList.length=0;
		ioOptionsList[0]=theOption;
		ioAddedOptions = 0;
		ioArray.length = 0;*/
	}
	else
	{
		var theOldIndex = theIndex;
		var theNewIndex = theOldIndex + 1;
		while (theNewIndex < ioOptionsList.length)
		{

			theOption= new Option();
	               	theOption.value=ioOptionsList[theNewIndex].value;
        	       	theOption.text=ioOptionsList[theNewIndex].text;	
			ioOptionsList[theOldIndex]=theOption;
			++theOldIndex;
			++theNewIndex;
		}

		--ioAddedOptions;
		ioOptionsList.length = ioOptionsList.length-1;
		DeleteItemFromArray(--theIndex, ioArray);
	}
	ioOptionsList.selectedIndex=0;
	/*var testindex=0
	while (testindex < ioArray.length)
	{
		++testindex
	}*/
	//ResetSource(iSource);

        /*if (navigator.appName == "Netscape")
	{
	       ioOptionsList.focus();
	}*/
	return ioAddedOptions;
}


/*
Deletes the item at a index
*/
function DeleteItemFromArray(inIndex, ioArray )
{
	var theOldIndex=inIndex;
	var theNewIndex=++inIndex;
	var ArrayLength = ioArray.length;
	while ( theNewIndex < ArrayLength )
	{
		ioArray[theOldIndex] = ioArray[theNewIndex];
		++theOldIndex;
		++theNewIndex;
	}
	ioArray.length = ioArray.length - 1;
}

function ResetSource(iSource)
{
	iSource.value="";
}

function ShowItems(inList, inArray, iSource)
{
	if (inArray.length >= 1)
	{
		var theIndex = inList.selectedIndex;
		iSource.value=inArray[--theIndex];
	}
	else
	{
		iSource.value="";
	}
        if (navigator.appName == "Netscape")
        {
               inList.focus();
        }
}

function CheckUniqueValue(inOptionValue, inArray)
{
	var count=0;
	var theString=inOptionValue;
	var theResult=true;
	while ( count < inArray.length )
	{
		if (theString == inArray[count])
		{
			theResult=false;
			return theResult;
		}
		++count;

	}
	return theResult;
}

