
var MIN_PASSWORD_LENGTH = 4;

function trim( s )
{
	return s.replace( /^\s*(.*?)\s*$/, "$1" );
}

var focused_form_control = null;

function i_have_focus( e )
{
	focused_form_control = e;
}

function urlencode( str ) 
{
	str = escape( str );

	str = str.replace( '+', '%2B' );
	str = str.replace( '%20', '+' );
	str = str.replace( '*', '%2A' );
	str = str.replace( '/', '%2F' );
	str = str.replace( '@', '%40' );
	
	return str;
}

function urldecode( str ) 
{
	str = str.replace( '+', ' ' );
	str = unescape( str );
	
	return str;
}

function SubmitForm( e, xtra )
{
	if ( e==null )
		return false;
	
	if ( xtra!=null )
		e.action = e.action + '?cmd=' + xtra;
	
	e.submit();
}

function HashPassword( e, xtra )
{
	if ( e == null )			
	{
		alert( "HashPassword: The form object is missing." );
		return false;
	}

	if ( document.getElementById( "password" ) == null )
	{
		alert( "HashPassword: The id 'password' input element is missing." );
		return false;
	}

	if ( document.getElementById( "password_encoded" ) == null )			
	{
		alert( "HashPassword: The id 'password_encoded' input element is missing." );
		return false;
	}

	i = trim( e.password.value );
	
	if ( i.length < MIN_PASSWORD_LENGTH )
	{
		alert( "Please provide a password that is at least " +
					MIN_PASSWORD_LENGTH + " characters long." );
		
		return false;
	}	

	var theCookie = "" + document.cookie;

	var cookieName = "PHPSESSID";

	var ind = theCookie.indexOf( cookieName );

	if( ind == -1 || cookieName == "" )
	{
		RandomServerString = null;
	}
	else
	{
		var ind1 = theCookie.indexOf( ";", ind );

		if( ind1 == -1 ) 
		{			
			ind1 = theCookie.length; 
		}	

		RandomServerString = unescape( theCookie.substring( ind + cookieName.length + 1, ind1 ) );
	}	
	
	hash = calcMD5( RandomServerString + calcMD5( i ) );
	e.password_encoded.value = hash;
	e.password.value = "";
	
	return SubmitForm( e, xtra );
}

function HashPasswordWithoutKey( e, xtra )
{
	if ( e == null )			
	{
		alert( "HashPasswordWithoutKey: The form object is missing." );
		return false;
	}

	if ( document.getElementById( "password" ) == null )
	{
		alert( "HashPasswordWithoutKey: The id 'password' input element is missing." );
		return false;
	}

	if ( document.getElementById( "password_encoded" ) == null )			
	{
		alert( "HashPasswordWithoutKey: The id 'password_encoded' input element is missing." );
		return false;
	}

	if ( document.getElementById( "password_verify" ) == null )			
	{
		alert( "HashPasswordWithoutKey: The id 'password_verify' input element is missing." );
		return false;
	}	

	i = trim( e.password.value );
	j = trim( e.password_verify.value );

	if ( i.length < MIN_PASSWORD_LENGTH )
	{
		alert( "Please provide a password that is at least " +
					MIN_PASSWORD_LENGTH + " characters long." );
		return false;
	}

	if ( i != j )
	{
		alert( "The verification password did not match. Please " +
			"make sure the password and the verification password " +
			"are exactly the same." );
		return false;
	}

	hash = calcMD5( i );
	
	e.password_encoded.value = hash;
	e.password.value = "";
	e.password_verify.value = "";
	
	return SubmitForm( e, xtra );
}

function check_all_by_group( me, e )
{
	if ( me == null
			|| e == null )
	{
		alert( "One of these objects is missing: the checkbox " +
			"or the group name." );
		
		return false;
	}
		
	var elements = document.getElementsByTagName( "*" );
			
	for( s = 0 ; s < elements.length; s++ )
	{
		var c = elements[s];

		if( c.type == 'checkbox' )
		{
			var group = c.getAttribute( "group" );
			
			if ( group == e )
	      c.checked = me.checked;
		}
	}

}

function check_all_by_name( me, e )
{
	if ( me == null
			|| e == null )
	{
		alert( "One of these objects is missing: the checkbox " +
			"or the checkbox name." );
		
		return false;
	}
		
	var elements = document.getElementsByName( e );
			
	for( s = 0 ; s < elements.length; s++ )
	{
		var c = elements[s];

		if( c.type == 'checkbox' )
		{
      c.checked = me.checked;
		}
	}

}
