//----------------------------------------------------------------------------
// Generic cookie code

var expDays = 3000;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal(offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
		endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

function SetCookie(name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}

//----------------------------------------------------------------------------
// The function below build the setting string for the game,
// saves it as a cookie, and then goes to the page specified as the form
// action.

// Note: no data is transferred via POST or GET.  You must retrieve the
//       settings via the cookie for the game's Game ID, since you will not
//       always arrive at the game's page from here.

function BuildAndSubmit(game_id) {
	var settings = "";
	for (var i = 0; i < document.options.elements.length; i++) {
		var item = document.options.elements[i];
		if (item.type.toLowerCase() == 'radio') {
			if (item.checked) settings = settings + item.name + item.value;
		}
	}
	SetCookie(game_id, settings, exp);
	document.location.href = document.options.action;
}

//----------------------------------------------------------------------------
// the function below gets the saved options string,
// and updates the checked items to reflect what was saved

function ShowLastSettings(game_id) {
	var settings = GetCookie(game_id);
	for (var i = 0; i < document.options.elements.length; i++) {
		var item = document.options.elements[i];
		if (item.type.toLowerCase() == 'radio') {
			var tag = item.name + item.value;
			if (settings.indexOf(tag) != -1) item.checked = true;
		}
	}
}

