
function set_cookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function get_cookie(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;
				return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function goto_url(url)
{
	if (window.navigate)
		window.navigate(url);
	else
		window.location.href = url;
}

function dehash_url(url)
{
	var loc = new String(window.location.href);
	var hash = loc.indexOf('#');
	if (-1 != hash)
	{
		loc = loc.substr(0, hash);
	}
	return loc;
}

/*
init() is called in response to document.onload.
*/
function init()
{
	/*
	toplevel_showmenu('',1);
	*/
	simOrder(0,1);
	fitContentHeight();
	window.onresize = function () { fitContentHeight(); }
}

/******************************************************************************
  begin menu section
******************************************************************************/

//var designWidth = 1000;
var curmenu = null;
var curchild = null;
var minorder = 0;
var maxorder = 2;
var simordering = 0;
var sopanel = document.getElementById('sopanel0');
var firsttop = 1;
var firstsim = 1;

function topMenuOver (e)
{
	e.style.backgroundColor='#676767';
	//e.style.backgroundImage='url(./images/site/bgddm04.png)';
}
function topMenuOut (e)
{
	e.style.backgroundColor='#000000';
	//e.style.backgroundImage='';
}

function toplevel_showmenu (id, init)
{
	if (init && firsttop)
	{
		id = getTopMenuCookie();
	}
	//alert(id);
	firsttop = 0;

	if (curmenu != null)
	{
		toplevel_hidemenu(curmenu.id);
		curmenu = null;
	}
	if (id != '')
		curmenu = showmenu(id);

	setTopMenuCookie(id);
}
function toplevel_hidemenu(id)
{
	curmenu = hidemenu(id);
}

function parent_showmenu(id)
{
	if (curchild != null)
	{
		toplevel_hidemenu(curchild.id);
	}
	curchild = showmenu(id);
}
function parent_hidemenu(id)
{
	curchild = hidemenu(id);
}

function showmenu(id)
{
	hidemenu(curchild);
	var e = document.getElementById(id);
	if (!e) return null;
	e.style.display='block';
	return e;
}
function hidemenu(id)
{
	var e = document.getElementById(id);
	if (!e)
		return null;
	e.style.display='none';
	return null;
}

function simOrder (n, init)
{
	if (init && firstsim)
	{
		simordering = parseInt(getSimMenuCookie());
	}
	else
	{
		simordering += n;
		if (simordering < minorder)
			simordering = maxorder;
		else if (simordering > maxorder)
			simordering = minorder;
	}
	//alert(simordering);
	firstsim = 0;

	sopanel = document.getElementById('sopanel'+simordering);
	if (null == sopanel)
		return;

	sopanel.style.display = 'none';

	var e = document.getElementById('simmenu');
	if (null == e)
		return;

	e.innerHTML = sopanel.innerHTML;

	setSimMenuCookie(simordering);
}


/******************************************************************************
  begin cookie section
******************************************************************************/

function createCookie (name, value, days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie (name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0) == ' ')
		{
			c = c.substring(1, c.length);
		}
		if (c.indexOf(nameEQ) == 0)
		{
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function setTopMenuCookie (value)
{
	createCookie('isiTOPMenu', value, 365);
}
function getTopMenuCookie ()
{
	var item = readCookie('isiTOPMenu');
	if (item == null)
		return '';
	else
		return item;
}

function setSimMenuCookie (value)
{
	createCookie('isiSIMMenu', value, 365);
}
function getSimMenuCookie ()
{
	var item = readCookie('isiSIMMenu');
	if (null == item)
		return 0;
	else
		return item;
}


/******************************************************************************
  begin page menu section
******************************************************************************/

function showPageMenuContents(yes)
{
	e = document.getElementById('pageMenuContents');
	if (!e)
		return;
	else if (yes)
		e.style.display = 'block';
	else
		e.style.display = 'none';
}

/******************************************************************************
  other section
******************************************************************************/

/*
This function loads the currently selected newsletter page into contentFrame.
*/
function loadNewsletter(issue)
{
	window.open(
		'./interpretations/'+issue+'/', 'isinews',
		'location=0,resizeable=0,width=650,height=450,'
		+ 'left=100,top=100,titlebar=0,toolbar=1,menubar=0,'
		+ 'status=0,scrollbars=1'
	);
}
/* OLD VERSION
function loadNewsletter ()
{
	var e = document.getElementById('nlissue');
	if (!e) return;
	
	var item = e.options[e.options.selectedIndex].value;

	goto_url('index.php?pg=newsletter&content='+item);
}
*/

/*
This function reloads the page to the currently selected language.

QUESTION: HOW DO WE PRESERVE THE PAGE THE USER IS ON?

This is why I hate frames...
*/
function loadLanguage (lang)
{
	set_cookie('lang', lang, 90);
	goto_url(dehash_url(window.location.href));
}
function loadLanguageSelect()
{
	var e = document.getElementById('langbox');
	if (!e) return;
	
	var item = e.options[e.options.selectedIndex].value;

	loadLanguage(item);
}

/*
This function determines the proper height of the content frame and its container 
elements automatically based on the height of the document's client area.  The height
of the contentFrame element can never be less than 400px, but may grow as large as
it needs to.
*/
function fitContentHeight ()
{
return;
	var ch = document.body.clientHeight - 166;
	if (ch < 412)
		ch = 412;
	var e = document.getElementById('contentFrame');
	if (e)
		e.style.height = ch;
	e = document.getElementById('contentFrameContainer');
	if (e)
		e.style.height = ch;
	e = document.getElementById('midSectionContainer');
	if (e)
		e.style.height = ch + 8;
}


function MM_findObj(n, d)
{
	var p,i,x;
	if (!d)
		d=document;

	if((p=n.indexOf("?"))>0&&parent.frames.length)
	{
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}
	
	if(!(x=d[n])&&d.all)
		x=d.all[n];
	
	for (i=0;!x&&i<d.forms.length;i++)
		x=d.forms[i][n];
	
	for(i=0;!x&&d.layers&&i<d.layers.length;i++)
		x=MM_findObj(n,d.layers[i].document);
	
	if(!x && d.getElementById)
		x=d.getElementById(n);
	
	return x;
}

function MM_validateForm()
{
	var i,p,q,e,name,test,num,min,max,errors='',args=MM_validateForm.arguments;	
	var string='Dfdf';
	var re = /^[a-zA-Z]+$/;
	for (i = 0; i < (args.length - 3); i += 3)
 	{
 		test=args[i + 2];
		name = args[i];
 		e = MM_findObj(name);
		if (!e)
			continue;

		val = e.value;
		if (val != "")
		{
			if (test.indexOf('isEmail') != -1)
			{
				p=val.indexOf('@');
				if (p<1 || p==(val.length-1))
					errors+='    - '+name+' must contain an e-mail address.\n';

			}
			else if (test != 'R')
			{
				num = parseFloat(val);
				if (isNaN(val))
					errors+='    - '+name+' must contain a number.\n';
				if (test.indexOf('inRange') != -1)
				{
					p=test.indexOf(':');
					min=test.substring(8,p);
					max=test.substring(p+1);
					if (num<min || max<num)
						errors+='    - '+name+' must contain a number between '+min+' and '+max+'.\n';
				}
			}
		}
		else if (test.charAt(0) == 'R')
		{
			errors += '    - '+name+' is required.\n';
		}
	}
  	if (errors)
  		alert('The following error(s) have occurred:\n\n'+errors+'\n');
  	return (errors == '') ? true: false;
}

function change_image(id, url)
{
	var e = document.getElementById(id);
	if (!e || !e.src) return;
	e.src = url;
}

function refresh_captcha(tin, id, url)
{
	change_image(id, url+Math.random());
	var e = document.getElementById(tin);
	if (!e) return;
	e.value = "";
}

