function setCookie( name, value )
{
	var expire_date = new Date(); 
  	expire_date.setTime( expire_date.getTime() + (1000 * 60 * 60 * 24 * 365 ) );

	if( value != null && value != "" )
		document.cookie = name + "=" + escape( value ) + "; expires=" + expire_date.toGMTString() + "; path=/";
}

function getCookie( sName )
{
	var aCookie = document.cookie.split("; ");
	for( var i = 0; i < aCookie.length; i++ )
	{
		var aCrumb = aCookie[i].split("=");
		if( sName == aCrumb[0] ) 
			if( typeof( aCrumb[1] ) == "undefined" )
				return null;
			else
				return unescape(aCrumb[1]);
	}
	
	return null;
}

function getexpirydate( nodays )
{
	var UTCstring;
	Today = new Date();
	nomilli=Date.parse(Today);
	Today.setTime(nomilli+nodays*24*60*60*1000);
	UTCstring = Today.toUTCString();
	return UTCstring;
}

function deleteCookie( name )
{ 
	document.cookie = name + "=;EXPIRES=" + getexpirydate(-2) + ";";
}

function deleteTab( index )
{
	setCookie( "tab" + index, "deleted" );
	setCookie( "tab_sources_res_time" + index, "deleted" );
}

function AddTab( tab_name, sources_str, num_results, time_out )
{
	var 	i,
		tab_i;
	//////////////
	
	if( tab_name == "" )
	{
		window.alert( "Cannot create tab with no name." );
		return;
	}
	
	// find tab index
	for( tab_i = 10; tab_i < 20; tab_i++ )
		if( getCookie( "tab" + tab_i ) == null || getCookie( "tab" + tab_i ) == "" || getCookie( "tab" + tab_i ) == "deleted" )
			break;
	
	if( tab_i == 18 )
	{
		windows.alert( "8 tab limit reached.");
		return;
	}
	
	if( sources_str == "" )
	{
		window.alert( "Cannot create tab with no sources." );
		return;
	}
	
	setCookie( "tab" + tab_i, tab_name );
	setCookie( "tab_sources_res_time" + tab_i, sources_str + "#" + num_results + "#" + time_out );
		
	location.href = "/index.php?tab=" + tab_i;
}
function AddTabCreate()
{
	var i,
		sources_str;
	////////////////
		
	sources_str = "";
	for( i = 0; i < name_form.name_source.length; i++ )
		if( name_form.name_source[ i ].checked )
		{
			sources_str += name_form.name_source[ i ].value;
			sources_str += ";"
		}

	AddTab( name_form.name_tab_name.value, sources_str, name_form.name_num_results.value, name_form.name_time_out.value );
}
