
// Script for popup window

var popup_data_array = new Array();

function popupData_to_string()
{
	return this.name + '=' + this.value;
}

function popupData( name, value )
{
	this.name = name;
	this.value = value;
	this.to_string = popupData_to_string;
}

function popupFind( name )
{
	var i;
	for ( i = 0 ; i < popup_data_array.length ; i++ )
	{
		if ( popup_data_array[i].name == name ) return popup_data_array[i].value;
	}
	return 0;
}

function popupUpdate( name, value )
{
	var i;
	for ( i = 0 ; i < popup_data_array.length ; i++ )
	{
		if ( popup_data_array[i].name == name )
		{
			popup_data_array[i].value = value;
			return;
		}
	}
	popup_data_array[i] = new popupData( name, value );
}

function popupParseItem( s )
{
	var t, name, value;
	t = s.indexOf( "=" );
	if ( t == -1 )
		return;
	popupUpdate( s.substring( 0, t ), parseInt( s.substring( t+1 ) ) );
}

function popupParse( s )
{
	var t;
	while ( s.length > 0 )
	{
		t = s.indexOf( "," );
		if ( t == -1 )
		{
			popupParseItem( s );
			break;
		}
		popupParseItem( s.substring( 0, t ) );
		s = s.substring( t+1 );
	}
}

function popupInit()
{
	popup_data_array = new Array();
	popupParse( popupGetCookie( "DREAMWIZ_POPUP" ) );
}

function popupMakeString()
{
	var s = "";
	for ( i = 0 ; i < popup_data_array.length ; i++ )
		s += (popup_data_array[i].to_string()+",");
	return s;
}

function popupPrint()
{
	var s = "";
	for ( i = 0 ; i < popup_data_array.length ; i++ )
		s += (popup_data_array[i].to_string()+"\n");
	alert( s );
}

function popupSetCookie( name, value )
{
	var argc = popupSetCookie.arguments.length;
	var argv = popupSetCookie.arguments;

	var path = (argc > 2) ? argv[2] : null;
	var domain = (argc > 3) ? argv[3] : null;
	var expires = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;

	if ( value.length == 0 )
	{
		expires = new Date();
		expires.setTime( 0 );
	}

	/* Cookie SET */
	document.cookie = name + "=" + escape( value ) +
		( (path == null) ? "" : ("; path=" + path) ) +
		( (domain == null) ? "" : ("; domain=" + domain) ) +
		( (expires == null) ? "" : ("; expires=" + expires.toGMTString()) ) +
		( (secure == true) ? "; secure" : "" );
}

function popupGetCookie( name )
{
	var search = name + "=";

	if ( document.cookie.length > 0 ) /* if there are any cookies */
	{
		offset = document.cookie.indexOf( search );
		if ( offset != -1 ) /* if cookie exists */
		{
			offset += search.length;
			/* set index of beginning of value */
			end = document.cookie.indexOf( ";", offset );
			/* set index of end of cookie value */
			if ( end == -1 ) 
				end = document.cookie.length;
			return unescape( document.cookie.substring( offset, end ) );
		} 
	}
	return "";
}

function popupIsDisabled( name, value )
{
	popupInit();
	return (popupFind( name ) >= value) ? true : false;
}

function popupDisable( name, value )
{
	var expire = new Date();
	expire.setTime( expire.getTime() + 30*24*60*60*1000 );
	popupInit();
	popupUpdate( name, value );
	popupSetCookie( "DREAMWIZ_POPUP", popupMakeString(), "/", ".dreamwiz.com", expire );
}

