function Trim( p_string ) {
	if (typeof p_string != "string") { 
		return p_string; 
	}

	var t_string = p_string;
	var t_ch = '';

	t_ch = t_string.substring( 0, 1 );
	while ( t_ch == " " ) {
		t_string = t_string.substring( 1, t_string.length );
		t_ch = t_string.substring( 0, 1 );
	}

	t_ch = t_string.substring( t_string.length-1, t_string.length );
	while ( t_ch == " " ) {
		t_string = t_string.substring( 0, t_string.length-1 );
		t_ch = t_string.substring( t_string.length-1, t_string.length );
	}

	return t_string;
}

function GetCookie( p_element ) {
	var t_cookie_name = "ROLLUP_HACK_" + p_element;
	var t_cookies = document.cookie;

	t_cookies = t_cookies.split( ";" );

	i = 0;
	while( i < t_cookies.length ) {
		var t_cookie = t_cookies[ i ];

		t_cookie = t_cookie.split( "=" );

		if ( Trim( t_cookie[ 0 ] ) == t_cookie_name ) {
			return( t_cookie[ 1 ] );
		}
		i++;
	}

	return -1;
}

function SetCookie( p_element, p_value ) {
	var t_cookie_name = "ROLLUP_HACK_" + p_element;
	var t_expires = new Date();

	t_expires.setTime( t_expires.getTime() + (365 * 24 * 60 * 60 * 1000));

	document.cookie = t_cookie_name + "=" + p_value + "; " + "path=/; " + "expires=" + t_expires + ";";
}


function GetViewSettings( p_element ) {
	var t_cookie = GetCookie( p_element );

	if ( -1 == t_cookie ) {
		t_cookie = 1;
	} else {
		t_cookie = parseInt( t_cookie );
	}

	return t_cookie;
}

function SetElementVisibility( p_element, t_view_settings, source ) {
	img_o = img_opened.src;
	img_c = img_closed.src;

	if (source == 0) {
		img_o = t_img_opened.src;
		img_c = t_img_closed.src;
	}

	if( t_view_settings ) {
		document.getElementById( p_element ).style.display = "";
		document.getElementById( p_element + "_img" ).src = img_o;
	} else {
		document.getElementById( p_element ).style.display = "none";
		document.getElementById( p_element + "_img" ).src = img_c;
	}
}

function Toggle( p_element, source ) {
	var t_view_settings = GetViewSettings( p_element );
	
	t_view_settings = ( t_view_settings == 1 ) ? 0 : 1;

	SetCookie( p_element, t_view_settings );
	SetElementVisibility( p_element, t_view_settings, source );
}

function ForceVisibilityMode( p_element, view_settings ) {
	SetElementVisibility( p_element,  view_settings );
}
