function Trim(str)
{
	str = str.replace(/^[ ]+(.*)$/, '$1'); // Trims leading spaces
	str = str.replace(/^(.*)[ ]+$/, '$1'); // Trims trailing spaces
	return str;
}

function CheckItemSelected( list )
{
	var selObj = document.getElementById( list );
	var index = selObj.selectedIndex;
	
	if ( index < 0 )
	{
		alert( "No item selected" );
		return false;
	}
	else
	{
		return true;
	}
}

function LimitTextLength( textbox, length )
{
	var objTextBox = document.getElementById( textbox );
	var sText = objTextBox.value;

	if ( sText.length >= 20 )
	{ 
		objTextBox.value = sText.substr( 0, length );
	}
}

function LimitTextWordCount( textbox, maxWordCount )
{
	var objTextBox = document.getElementById( textbox );
	var sText = objTextBox.value;

	words = sText.split(" ");

	if ( words.length > maxWordCount )
	{ 
		var newText = "";
		var start = true;
		
		for(var i = 0; i < maxWordCount; i++) {
			if (start) {
				newText = words[i] + " ";
				start = false;
			}else{
				newText = newText + words[i] + " ";
			}
		}

		var scrollTop = objTextBox.scrollTop;

		objTextBox.value = Trim( newText ) + " ";

		if (document.all) {
			objTextBox.focus(sText.length - 1);
		}else{
			objTextBox.setSelectionRange(sText.length - 1, sText.length - 1);
			objTextBox.focus();
			objTextBox.scrollTop = scrollTop;			
		}
		
	}
}

var previous_selection;

function SetupPrevious( name )
{
	previous_selection = SelectedValues( name );	
}

function ReplaceSelections( listBox )
{
	var listObj = document.getElementById( listBox );
	
	for ( var i = 0; i < listObj.length; i++ )
	{
		if ( in_array( listObj[ i ].value, previous_selection ) )
		{
			listObj[ i ].selected = true;
		}
		else
		{
			listObj[ i ].selected = false;
		}
	}
}

function LimitUnitsSelected( name, num_units )
{
	var selected_items = SelectedValues( name );
	
	if ( selected_items.length > num_units )
	{
		alert( "A maximum of " + num_units + " units can be selected" );
		ReplaceSelections( name );
		return false;	
	}
	else
	{
		previous_selection = selected_items;
		return true;
	}
}

function CheckEmpty( text )
{
	if ( document.getElementById( text ).value == "" )
	{
		return false;
	}
	else
	{
		return true;
	}
}

function ValidateUnit( t1, t2 )
{
	if ( ( CheckEmpty( t1 ) ) && ( CheckEmpty( t2 ) ) )
	{
		return true;
	}
	else
	{
		alert( "Required field empty" );
		return false;
	}  
}

function ValidateSuite( suitetext )
{
	if ( CheckEmpty( suitetext ) )
	{
		return true;
	}
	else
	{
		alert( "Required field empty" );
		return false;
	}  
}

function ValidateLoginForm( formid )
{
	oForm = document.getElementById( formid );
	
	if ( oForm.email.value != "" )
	{
		if ( oForm.password.value != "" )
		{
			return true;	
		}
	}	
	
	alert( "One or more required fields are empty" );
	
	return false;
}

function ValidateUserRegistrationForm( formid )
{
	oForm = document.getElementById( formid );

	if ( oForm.name.value != "" )
	{	
		if ( oForm.email.value != "" )
		{
			if ( oForm.password1.value != "" )
			{
				if ( oForm.password2.value != "" )
				{
					if ( oForm.password1.value == oForm.password2.value )
					{
						return true;
					}
				}
			}
		}
	}
	
	if ( oForm.password1.value == oForm.password2.value )
	{
		alert( "One or more required fields are empty" );
	}
	else
	{
		alert( "Passwords entered do not match" );		
	}
		
	return false;
}

function ValidateEventForm( formid )
{
	oForm = document.getElementById( formid );
	
	if ( oForm.name.value != "" )
	{
		if ( oForm.category[oForm.category.selectedIndex].value != "" )
		{
			if ( oForm.location.value != "" )
			{
				if ( oForm.organisation.value != "" )
				{
					if ( oForm.telno.value != "" )
					{
						if ( oForm.email.value != "" )
						{
							if ( ( oForm.ondemand.checked ) || ( ( parseInt( oForm.numDatesTracker.value ) > 0 ) && validateDates() ) )
							{
								return true;
							}
						}						
					}					
				}		
			}
		}
	}
	
	if ( parseInt( oForm.numDatesTracker.value ) < 1 )
	{
		if ( !oForm.ondemand.checked )
		{
			alert( "At least one date must be entered for the event" );
		}
	}
	else
	{
		if ( !validateDates() )
		{
			alert( "The second date in a range of dates must be after the first" );
		}
		else
		{
			alert( "One or more required fields are empty" );
		}
	}
	
	return false;
}

function NotEmpty( t1, t2 )
{
	if ( ( CheckEmpty( t1 ) ) && ( CheckEmpty( t2 ) ) )
	{
		return true;
	}
	else
	{
		return false;
	}  
}