/*
	If someone leaves their browser open for a long time (or uses the "save state" option
	that many browser offer), they can end up with many many ASPSESSION* cookies on their 
	machine. So many, in fact, that they exceed the maximum size of cookie information for
	a site, so preventing any future cookies from being saved.
	
	To get around that, if the browsers sends more than one ASPSESSION cookies to the server
	then delete it. We don't know which cookie is the current one, but they should be logged
	in again automatically courtesy of the PlayerId/LoginGUID cookie we set so the player
	can survive a server restart without logging in.
*/
function ClearASPCookies() {

	// List of cookie name/value pairs
	var cookieArray = document.cookie.split(";");
	// Go through them all
	var sessionCookieCount = 0;
	for( var i = 0; i < cookieArray.length; i ++ ) {
		// Get the individual cookie name/value pair
		var cookie = cookieArray[i];
		// Trim leading spaces
		while( cookie.charAt(0) == ' ' ) {
			cookie = cookie.substring( 1 );
		}
		// Get the cookie name (everything before the '=')
		var cookieName = cookie.substring( 0, cookie.indexOf('=') );
		// Is it an ASP session cookie?
		if( cookieName.indexOf('ASPSESSIONID') == 0 ) {
			sessionCookieCount ++;
		}
	}
	
	if( sessionCookieCount > 5 ) { // We need at least one per world for the current session
	
		//alert( "Deleting " + sessionCookieCount + " ASPSESSION cookies" );
		for( var i = 0; i < cookieArray.length; i ++ ) {
			// Get the individual cookie name/value pair
			var cookie = cookieArray[i];
			// Trim leading spaces
			while( cookie.charAt(0) == ' ' ) {
				cookie = cookie.substring( 1 );
			}
			// Get the cookie name (everything before the '=')
			var cookieName = cookie.substring( 0, cookie.indexOf('=') );
			var cookieValue = cookie.substring( cookie.indexOf('=')+1 );
			// Is it an ASP session cookie?
			if( cookieName.indexOf('ASPSESSIONID') == 0 ) {
				var expiryTime = new Date(); 
				// Set the date to the past
				expiryTime.setFullYear(1970,1,1);
				// And reset the cookie; effectively deleting it
				document.cookie = cookieName + "=; expires=" + expiryTime.toGMTString() +"; path=/";
			}
		}
	}
}

