function redirect_to(new_page_name) {
	window.location.replace(new_page_name);
	window.location = new_page_name;
}

function pop_it_up(page_ref, page_name, width, height) {
	window.open(page_ref,page_name,'toolbar=no,location=no,directories=no,status=yes,menubar=no,resizable=yes,copyhistory=no,scrollbars=yes,width='+width+',height='+height+',top=25,left=25');
}

function set_name(new_name) {
	window.name = new_name;
}

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function testObtained() {
	if (getCookie('gotMagnesium') == 'true' && 
		getCookie('gotNewt') == 'true' && 
		getCookie('gotFingerbone') == 'true' && 
		getCookie('gotRust') == 'true' && 
		getCookie('gotStardust') == 'true' && 
		getCookie('gotSulphur') == 'true' && 
		getCookie('gotMilk') == 'true') {
		redirect_to('PhilosopherStone.html');
	}
	else {
		alert('You haven\'t found everything yet.  Keep looking!');
	}
}

function randomize() {
	var currentid = '';
	var randomChance = 0;
	var textIter = 0;
	var anyShown = false;
	var redirectURL;
	for (textIter = 1; textIter < 23; textIter++) {
		randomChance = Math.floor(Math.random() * 7);
		currentid = 'text' + textIter;
		if (randomChance == 0) {
			document.getElementById(currentid).style.left = Math.floor(Math.random() * 500) + 'px';
			document.getElementById(currentid).style.top = ( (Math.floor(Math.random() * 14) + 50) + (26 * textIter)) + 'px';
			document.getElementById(currentid).style.visibility = "visible";
			anyShown = true;
		}
		else {
			document.getElementById(currentid).style.visibility = "hidden";
		}
	}
	if (! anyShown) {
		randomChance = Math.floor(Math.random() * 19);
		switch (randomChance) {
		case 0:
			redirectURL = '../../Center/Choices/index.html';
			break;
		case 1:
			redirectURL = '../../Center/Victoria/index.html';
			break;
		case 2:
			redirectURL = '../../Center/Watching/index.html';
			break;
		case 3:
			redirectURL = '../../Center/Yggdrasil/index.html';
			break;
		case 4:
			redirectURL = '../../East/Ages/index.html';
			break;
		case 5:
			redirectURL = '../../East/Celestine/index.html';
			break;
		case 6:
			redirectURL = '../../East/Devotion/index.html';
			break;
		case 7:
			redirectURL = '../../East/King/index.html';
			break;
		case 8:
			redirectURL = '../../North/Alchemy/index.html';
			break;
		case 9:
			redirectURL = '../../North/Dress/index.html';
			break;
		case 10:
			redirectURL = '../../North/Fools/index.html';
			break;
		case 11:
			redirectURL = '../../South/Beauty/index.html';
			break;
		case 12:
			redirectURL = '../../South/North/index.html';
			break;
		case 13:
			redirectURL = '../../South/Ophelia/index.html';
			break;
		case 14:
			redirectURL = '../../South/Together/index.html';
			break;
		case 15:
			redirectURL = '../../West/Families/index.html';
			break;
		case 16:
			redirectURL = '../../West/Revolution/index.html';
			break;
		case 17:
			redirectURL = '../../West/Solomon/index.html';
			break;
		case 18:
			redirectURL = '../../West/Troy/index.html';
			break;
		}
		redirect_to(redirectURL);
	}
}

function randomizeDeath() {
	var currentid = '';
	var randomChance = 0;
	var textIter = 0;
	var anyShown = false;

	for (textIter = 1; textIter < 17; textIter++) {
		randomChance = Math.floor(Math.random() * 4);
		currentid = 'text' + textIter;
		if (randomChance == 0) {
			document.getElementById(currentid).style.visibility = "visible";
			anyShown = true;
		}
		else {
			document.getElementById(currentid).style.visibility = "hidden";
		}
	}
	
	if (! anyShown) {
		redirect_to('death.html');
	}
}

function doRandomDeath() {
	randomizeDeath();
	setTimeout('doRandomDeath();', 4000);
}

function showDeeds() {
	document.getElementById('deedDiv').style.visibility = "visible";
}

function randomCharacter() {
	var randomChance = Math.floor(Math.random() * 2);
	if (randomChance == 0) {
		redirect_to('prince.html');
	}
	else {
		redirect_to('witch.html');
	}
}

function turnTheWheel() {
	pop_it_up('Farming1.html', 'farming', 250, 250);
	pop_it_up('Artwork1.html', 'artwork', 250, 250);
	pop_it_up('Medicine1.html', 'medicine', 250, 250);
	pop_it_up('Worship1.html', 'worship', 250, 250);
	pop_it_up('Literature1.html', 'literature', 250, 250);
}

function startWheelChecker() {
	if (document.storageForm.artwork.value == '1' && document.storageForm.farming.value == '1' && document.storageForm.literature.value == '1' && document.storageForm.medicine.value == '1' && document.storageForm.worship.value == '1') {
		redirect_to('complete.html');
	}
	else {
		setTimeout('startWheelChecker();', 1500);	
	}
}

function opheliaHideShow(toHide, toShow) {
	var anyShown = false;
	var numLines = 11;
	var currentid, textIter;

	document.getElementById('text' + toShow).style.visibility = "visible";
	document.getElementById('text' + toHide).style.visibility = "hidden";
}

function opheliaStart() {
	var randomChance;
	var textIter;

	for (textIter = 1; textIter < 11; textIter++) {
		currentid = 'text' + textIter;
		document.getElementById(currentid).style.visibility = "hidden";
	}

	randomChance = Math.floor(Math.random() * 3);
	
	if (randomChance == 0) {
		document.getElementById('text1').style.visibility = "visible";
		document.getElementById('text4').style.visibility = "visible";
		document.getElementById('text7').style.visibility = "visible";
		document.getElementById('text10').style.visibility = "visible";
	}
	else if (randomChance == 1) {
		document.getElementById('text2').style.visibility = "visible";
		document.getElementById('text6').style.visibility = "visible";
		document.getElementById('text9').style.visibility = "visible";
	}
	else {
		document.getElementById('text3').style.visibility = "visible";
		document.getElementById('text5').style.visibility = "visible";
		document.getElementById('text8').style.visibility = "visible";
	}
}

function ifOnlyLastExit() {
	var lastShown = (document.getElementById('text10').style.visibility == "visible");
	var othersShown = false;
	var numLines = 11;
	var currentid, textIter;
	
	for (textIter = 1; textIter < (numLines - 1); textIter++) {
		currentid = 'text' + textIter;
		if (document.getElementById(currentid).style.visibility == "visible") {
			othersShown = true;
		}
	}
	
	if (lastShown && ! othersShown) {
		redirect_to('hidden.html');
		return true;
	}
	
	return false;
}

function getCurrentMonth() {
	var right_now = new Date();
	return right_now.getMonth() + 1;
}

function monthRedirect() {
	var newPage = getCurrentMonth() + '.html';
	redirect_to(newPage);
	return true;
}

function showTogetherStart() {
	var textIter = 0;

	document.getElementById('text0').style.visibility = "visible";
	document.getElementById('text1').style.visibility = "visible";
	for (textIter = 2; textIter < 18; textIter++) {
		currentid = 'text' + textIter;
		document.getElementById(currentid).style.visibility = "hidden";
	}
}

function randomTogether() {
	var currentid = '';
	var randomChance = 0;
	var textIter = 0;
	var anyShown = false;
	var thisLucas = 0;
	var thisMarion = 0;
	var thisDifference = 0;

	for (textIter = 0; textIter < 18; textIter++) {
		thisLucas = a_lucas[textIter];
		thisMarion = a_marion[textIter];
		thisDifference = Math.abs(thisLucas - c_lucas) + Math.abs(thisMarion - c_marion);
		randomChance = Math.random();
		currentid = 'text' + textIter;
		if (randomChance > (.08 * thisDifference)) {
			document.getElementById(currentid).style.visibility = "visible";
			anyShown = true;
		}
		else {
			document.getElementById(currentid).style.visibility = "hidden";
		}
	}
	
	if (! anyShown) {
		if ( (c_lucas + c_marion) > 0 ) {
			redirect_to('happy.html');
		}
		else if ( (c_lucas + c_marion) < 0 ) {
			redirect_to('sad.html');
		}
		else {
			randomTogether();
		}
	}
}