function setCookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
		expires = expires * 1000 * 60 * 60 * 24;

	var expires_date = new Date( today.getTime() + (expires) );

	path = "/";

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


// this function gets the cookie, if it exists
// don't use this, it's weak and does not handle some cases
// correctly, this is just to maintain legacy information
function getCookie( name ) 
{	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
		return null;
	if ( start == -1 ) 
		return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function addFormStateSaver( sForm, sSuffix )
{
	if( !sSuffix )
		sSuffix = "";
	
	var oForm = document.getElementById( sForm );
	if( oForm )
	{
		for( var i=0; i<oForm.length; i++)
		{
			if( oForm[i].className.indexOf('nostatestore') == -1 )
			{
				switch(oForm[i].type)
				{
					case "textarea":
					case "text":
						oForm[i].oldonkeyup = oForm[i].onkeyup ? oForm[i].onkeyup : function(e){;};
						oForm[i].onkeyup = function(e) {this.oldonkeyup(e); setCookie( sForm+"_"+sSuffix+"_"+this.name, this.value );};
						if( oForm[i].value == "" && getCookie( sForm+"_"+sSuffix+"_"+oForm[i].name ) )
							oForm[i].value = getCookie( sForm+"_"+sSuffix+"_"+oForm[i].name );
						break;
					case "radio":
						oForm[i].oldonclick = oForm[i].onclick ? oForm[i].onclick : function(e){;};
						oForm[i].onclick = function(e) {this.oldonclick(e); setCookie( sForm+"_"+sSuffix+"_"+this.name, this.value );};
						var bFilled = false;
						for( var j = 0; j < oForm[ oForm[i].name ].length; j++ )
							if( oForm[ oForm[i].name ][j].checked ) 
								bFilled = true;
						if( !bFilled && oForm[i].value == getCookie( sForm+"_"+sSuffix+"_"+oForm[i].name ) )
							oForm[i].checked = "checked";
						break;
					case "checkbox":
						oForm[i].oldonclick = oForm[i].onclick ? oForm[i].onclick : function(e){;};
						oForm[i].onclick = function(e) {this.oldonclick(e); setCookie( sForm+"_"+sSuffix+"_"+this.name, this.checked );};
						if( getCookie( sForm+"_"+sSuffix+"_"+oForm[i].name ) == "true" )
							oForm[i].checked = "checked";
						break;
					case "select-one":
						oForm[i].oldonchange = oForm[i].onchange ? oForm[i].onchange : function(e){;};
						oForm[i].onchange = function(e) {this.oldonchange(e); setCookie( sForm+"_"+sSuffix+"_"+this.name, this.value );};
						var bFilled = false;
						for( var j = 0; j < oForm[i].length; j++ )
							if( oForm[i][j].selected ) 
								bFilled = true;
						if( !bFilled )
							for( var j = 0; j < oForm[i].length; j++ )
								if(  oForm[i][j].value == getCookie( sForm+"_"+sSuffix+"_"+oForm[i].name ) )
									oForm[i][j].selected = true;
						break;
				}
			}
		}
	}
}

