Utils = {

	pop: function( url, name, style, w, h ) { // style: "SELECT" || "DIALOG" || "VISTA"
		var w, h;
		var status, toolbar, locationbar, menubar, scrollbars, resizable = false;
		switch ( style ) {
			case "SELECT":
				w = 350;
				h = 500;
				resizable = true;
				scrollbars = true;
				status = true;
				break;
			case "DIALOG":
				w = 500;
				h = 350;
				resizable = true;
				status = true;
				break;
			case "VISTA":
				w = 900;
				h = 600;
				resizable = true;
				break;
		}
		window.open( 
			url,
			name,
			( w ? "width=" 	+ w + "," : "" ) +
			( h ? "height=" 	+ h + "," : "" ) +
			"status=" 		+ ( status 		? "yes" : "no" ) + "," +
			"toolbar=" 	+ ( toolbar 	? "yes" : "no" ) + "," +
			"locationbar=" 	+ ( locationbar 	? "yes" : "no" ) + "," +
			"menubar=" 	+ ( menubar 	? "yes" : "no" ) + "," +
			"scrollbars=" 	+ ( scrollbars 	? "yes" : "no" ) + "," +
			"resizable=" 	+ ( resizable 	? "yes" : "no" )
		);	
	},

	quote: function( str ) {
		return '"' + str.replace(/(["\\])/g, '\\$1') + '"';
	},
	
	serialize: function( data ) {
		if ( data == null ) {
			return "null";		
		} else if ( typeof data == "number" || data.constructor == Number ) {
			return data.toString();
		} else if ( typeof data == "string" ) {
			return Utils.quote( data );
		} else if ( typeof data == "object" ) {
			if ( data.constructor == Array ) {
				var buf = new String();
				buf += "[";
				for ( var i = 0; i < data.length; i++ ) {
					buf += Utils.serialize( data[ i ] );
					if ( i != ( data.length - 1 ) ) {
						buf += ",";						
					}
				}
				buf += "]";
				return buf;
			} else {
				var buf = new String();
				buf += "{";
				var first = true;
				for ( var x in data ) {
					if ( typeof data[ x ] != "function" ) {
						if ( !first ) {
							buf += ",";
						}
						buf += Utils.serialize( x );
						buf += ":";
						buf += Utils.serialize( data[ x ] );	
						first = false;
					}
				}
				buf += "}";
				return buf;
			}
		}
		throw "UnserializableData: " + ( typeof data ) + " " + data;
	},
	
	EXPANDED: "Utils.EXPANDED",
	COLLAPSED: "Utils.COLLAPSED",
	
	toggle: function( idPrefix ) {
		var c = document.getElementById( idPrefix + "Collapsed" );
		var e = document.getElementById( idPrefix + "Expanded" );
		var visibleStyle = c.style.display == "none" 
			? e.style.display 
			: c.style.display
		;							
		c.style.display = c.style.display == "none" 
			? visibleStyle 
			: "none"
		;
		e.style.display = e.style.display == "none" 
			? visibleStyle 
			: "none"
		;
		var cookieName = Utils.getToggleCookieName( idPrefix );
		if ( e.style.display == "none" )
			Utils.removeCookie( cookieName, "/" );
		else
			Utils.setCookie( cookieName, Utils.EXPANDED, null, "/" );
	},
	
	autoExpand: function( idPrefix ) {
		if ( Utils.getToggleCookieState( idPrefix ) == Utils.EXPANDED
		 	&& Utils.getToggleState( idPrefix ) != Utils.EXPANDED )
			Utils.toggle( idPrefix );
	},
	
	forceExpand: function( idPrefix ) {
		if ( Utils.getToggleState( idPrefix ) != Utils.EXPANDED )
			Utils.toggle( idPrefix );
	},
	
	getToggleState: function( idPrefix ) {
		return document.getElementById( idPrefix + "Collapsed" ).style.display == "none" 
			? Utils.EXPANDED
			: Utils.COLLAPSED
		;	
	},
	
	getToggleCookieState: function( idPrefix ) {
		return Utils.getCookie( Utils.getToggleCookieName( idPrefix ) ) == Utils.EXPANDED
			? Utils.EXPANDED
			: Utils.COLLAPSED
		;	
	},
	
	getToggleCookieName: function( idPrefix ) {
		return "_CNXACollapsibleContent_" + idPrefix;
	},	
	
	// -- Cookie Handling --
	
	setCookie: function( name, value, expiryDate, path, domain, secure ) {
		var cookie = name + "=" + escape( value )
			+ ( expiryDate ? ";expires=" + expiryDate.toGMTString() : "" )
			+ ( path ? ";path=" + path : "" )
			+ ( domain ? ";domain=" + domain : "" )
			+ ( secure ? "; secure" : "" )
		;	
		document.cookie = cookie;
	},
	
	getCookie: function( name ) {
		var cookies = document.cookie;
		var prefix = name + "=";
		var prefixPos = cookies.indexOf( prefix );
		if ( prefixPos != 0 ) {
			prefix = "; " + prefix;
			prefixPos = cookies.indexOf( prefix );
			if ( prefixPos < 0 )
				return null;
		}	
		var start = prefixPos + prefix.length;
		var endPos = cookies.substring( start ).indexOf( ";" )
		var end = endPos < 0 
			? cookies.length 
			: start + endPos
		;    
		return unescape( cookies.substring( start, end ) );
	},
	
	removeCookie: function( name, path, domain ) {
		Utils.setCookie( name, "", new Date( 0 ), path, domain );
	}

}