bg_color = new Array("#000030","white","black");
fg_color = new Array("#c0c0c0","black","white");
font_sz = new Array("medium", "large", "x-large", "xx-large");

function newId(ni_id, limit)
{
	ni_id++;
	if (ni_id == limit) ni_id = 0;
	//alert("newId returning "+ni_id);
	return ni_id;
}

function newColorId(nci_id)
{
	return newId(nci_id, fg_color.length);
}

function newFontSize(nfsi_id)
{
	return newId(nfsi_id, font_sz.length);
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    c_str = document.cookie.substring(c_start,c_end);
    // alert(document.cookie);
    // alert("c_start="+c_start+" c_end="+c_end+"c_str="+c_str+" cookie="+document.cookie);
    return c_str;
    }
  }
return "";
}

function getNumericCookie(gnc_name)
{
	gnc_str = getCookie(gnc_name);
	if (gnc_str && (gnc_str != "")) gnc_id = parseInt(gnc_str, 10);
	else gnc_id = 0;
	return gnc_id;
}

function setCookie(c_name,c_value)
{
	sc_date = new Date();
	sc_date.setDate(sc_date.getDate()+1000);
	document.cookie = c_name+ "=" +c_value+";expires="+sc_date.toGMTString()+";path=/jjsha/";
}

function setColor(sc_id)
{
	setCookie("color_id", sc_id);
	document.body.style.backgroundColor = bg_color[sc_id];
	document.body.style.color = fg_color[sc_id];
}

function setFontSize(sfs_id)
{
	setCookie("font_sz", sfs_id);
	document.body.style.fontSize = font_sz[sfs_id];
}

function showColorButton()
{
	scb_id = newColorId(getNumericCookie("color_id"));
	if (scb_id) document.getElementById("chgp").innerHTML = "<input class=hmenu_button id=chgc type=button onclick=changeColor() value='change colors to "+fg_color[scb_id]+" on "+bg_color[scb_id]+"'>";
	else document.getElementById("chgp").innerHTML = "<input class=hmenu_button id=chgc type=button onclick=changeColor() value='change to original colors'>";
	document.getElementById("chgp").style.backgroundColor = bg_color[scb_id];
	document.getElementById("chgp").style.color = fg_color[scb_id];
	// alert("Used color id "+scb_id);
}

function showFontSizeButton()
{
	sfsb_id = newFontSize(getNumericCookie("font_sz"));
	document.getElementById("chgfs").innerHTML = "<input class=hmenu_button id=chgfs type=button onclick=changeFontSize() value='change font size to "+font_sz[sfsb_id]+"'>";
	document.getElementById("chgfs").style.fontSize = font_sz[sfsb_id];
	// alert("Used font size id "+sfsb_id);
}

/*
 * This actually sets the preserved color and font size.
 * It's called by the body onload event.
 */
function setPreservedColor()
{
	spc_id=getNumericCookie("color_id");
	// alert("in setPreservedColor spc_id="+spc_id);
	setColor(spc_id);
	showColorButton();
	spc_id=getNumericCookie("font_sz");
	// alert("in setPreservedColor spc_id="+spc_id);
	setFontSize(spc_id);
	showFontSizeButton();
}

function changeColor()
{
	cc_id = newColorId(getNumericCookie("color_id"));
	// alert("in changeColor, cc_id="+cc_id);
	setColor(cc_id);
	showColorButton();
}

function changeFontSize()
{
	cfs_id = newFontSize(getNumericCookie("font_sz"));
	// alert("in changeFontSize, cfs_id="+cfs_id);
	setFontSize(cfs_id);
	showFontSizeButton();
}
