/*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.*/
//Globals :
//*********
// This array contains 3 items for every project. ( 5 projects maximum )
var aProjects = new Array(0);
// The number of projects added
var AddedOptionsCount=0;

/*
	Adds an item to a dropdown list, the value of the item is the content of a text field
	document.Registration_Form.Projects.options[0] : the content of the text field
	document.Registration_Form.Projects.options : the complete dropdown list
	ioOption : one item of the dropdown list
	iTargetLength : the current length of the dropdown list
	AddedOptionsCount  : the nr of options added untill now ( should be a global variable )
	aProjects : the content of all data in the dropdown list ( global var )
*/
function CheckUniqueProject()
{
	var count=0;
	while( count <= aProjects.length - 3)
	{
		if (document.Registration_Form.ProjectName.value == aProjects[count] &&
			document.Registration_Form.ProjectContribution.value == aProjects[++count] &&
			document.Registration_Form.ProjectUrl.value == aProjects[++count])
				return false;
		++count;
	}
	return true;
}

function AddProject()
{
	if (document.Registration_Form.Projects.options.length < 6 && CheckUniqueProject() )
	{
		if (document.Registration_Form.ProjectName.value.length > 0)
		{
			/*if ( AddedOptionsCount  <= 0 )
			{
				document.Registration_Form.Projects.options.length=0;
			}*/
			theOption= new Option();
			var ShortValue=CheckValueLength(document.Registration_Form.ProjectName.value);
			theOption.value=ShortValue;
			theOption.text=ShortValue;
			document.Registration_Form.Projects.options[document.Registration_Form.Projects.options.length]=theOption;
			++AddedOptionsCount ;
			aProjects[aProjects.length]= document.Registration_Form.ProjectName.value;
			aProjects[aProjects.length]= document.Registration_Form.ProjectContribution.value;
			aProjects[aProjects.length]= document.Registration_Form.ProjectUrl.value;	
			ResetProjects();
			document.Registration_Form.Projects.options.selectedIndex=0;
		        if (navigator.appName == "Netscape")
	        	{
		                document.Registration_Form.Projects.focus();
			}
			ResetProjects();
		}
		/*var testindex=0
		while (testindex < aProjects.length)
		{
			++testindex
		}*/
	}
	
	if ( document.Registration_Form.Projects.options.length >= 6)
	{
		// Disable the Add button
		document.Registration_Form.AddProjectButton.disabled=true;
	}
}

function LoadProject()
{
	var ArrayCount=0;
	var OptionCount=0;
	while (ArrayCount < aProjects.length)
	{
		theOption= new Option();
		var StringValue=aProjects[ArrayCount];
		var ShortValue=CheckValueLength(StringValue);
		theOption.value=ShortValue;
		theOption.text=ShortValue;
		document.Registration_Form.Projects.options[++OptionCount]=theOption;
		ArrayCount += 3;
		++AddedOptionsCount ;
	}
	//document.Registration_Form.Projects.options.selectedIndex=0;
	if (navigator.appName == "Netscape")
        {
                document.Registration_Form.Projects.focus();
        }
        
	if (navigator.appName == "Netscape")
        {
        	document.Registration_Form.ProjectName.focus();
	}
}

function DeleteProject()
{	
	var theIndex = document.Registration_Form.Projects.options.selectedIndex
	if ( theIndex <= 0 )
	{
		// do nothing
	}
	else
	{
		var theOldIndex = theIndex;
		var theNewIndex = theOldIndex + 1;
		while (theNewIndex < document.Registration_Form.Projects.options.length)
		{
		
			theOption= new Option();
                	theOption.value=document.Registration_Form.Projects.options[theNewIndex].value;
                	theOption.text=document.Registration_Form.Projects.options[theNewIndex].text;	
			document.Registration_Form.Projects.options[theOldIndex]=theOption;
			++theOldIndex;
			++theNewIndex;
		}
	
		--AddedOptionsCount;
		document.Registration_Form.Projects.options.length = document.Registration_Form.Projects.options.length-1;
		DeleteArrayItem(--theIndex, aProjects);
	}
	var testindex=0
	while (testindex < aProjects.length)
	{
		++testindex
	}

	if (document.Registration_Form.Projects.options.length < 6 )
	{
		// enable the Add button
		document.Registration_Form.AddProjectButton.disabled=false;
	}

	if (navigator.appName == "Netscape")
        {
                document.Registration_Form.Projects.focus();
        }
	ResetProjects();
        if (navigator.appName == "Netscape")
        {
                document.Registration_Form.ProjectName.focus();
        }
	//ShowProject();
}

/*
Deletes the item at a index ( an item is 3 array items )
*/
function DeleteArrayItem(inIndex, inArray )
{
	var NewPlace = inIndex * 3;
	var OldPlace = NewPlace + 3;
	var ArrayLength = inArray.length;
	while ( OldPlace < ArrayLength )
	{
		inArray[NewPlace] = inArray[OldPlace];
		++NewPlace;
		++OldPlace;
	}
	inArray.length = ArrayLength - 3;
}

function ResetProjects()
{
	document.Registration_Form.ProjectName.value="";
	document.Registration_Form.ProjectContribution.value="";
	document.Registration_Form.ProjectUrl.value="";
}

function ShowProject()
{
	if (aProjects.length > 0)
	{
		var theIndex = document.Registration_Form.Projects.selectedIndex;
		if (theIndex > 0)
		{
			var ArrayIndex = (theIndex - 1) *3;
			document.Registration_Form.ProjectName.value=aProjects[ArrayIndex];
			document.Registration_Form.ProjectContribution.value=aProjects[++ArrayIndex];
			document.Registration_Form.ProjectUrl.value=aProjects[++ArrayIndex];
		}
		else
		{
			ResetProjects();
		}
	}
	else
	{
		ResetProjects();
	}
        if (navigator.appName == "Netscape")
        {
                document.Registration_Form.Projects.focus();
        } 
}


function CheckValueLength(iOptionValue)
{
	var theInString=iOptionValue;
	var theOutString;
	if ( iOptionValue.length > 15 )
	{
		theOutString=theInString.substring(0,14);
		return theOutString;
	}
	else
	{
		return theInString;
	}
}

