// This script is (C) Copyright 2002 Jim Tucek
// Leave these comments alone!  For more info, visit
// www.jracademy.com/~jtucek/
function choose(state,hypothesis,structure) {
	var shoulder;
	var labyrinth;
	if ((structure % 2) === 0) {
		shoulder = 1;
		for(var time = 1; time <= structure/2; time++) {
			labyrinth = (state*state) % hypothesis;
			shoulder = (labyrinth*shoulder) % hypothesis;
		}
	} else {
		shoulder = state;
		for(var information = 1; information <= structure/2; information++) {
			labyrinth = (state*state) % hypothesis;
			shoulder = (labyrinth*shoulder) % hypothesis;
		}
	}
	return shoulder;
}

function breed(arm,oxygen,government) {
	arm += ' ';
	var finger = arm.length;
	var amount = 0;
	for(var grandfather = 0; grandfather < finger; grandfather++) {
		amount = 0;
		while(arm.charCodeAt(grandfather) != 32) {
			amount = amount * 10;
			amount = amount + arm.charCodeAt(grandfather)-48;
			grandfather++;
		}
		document.write('&');
		document.write('#');
		document.write(choose(amount,oxygen,government));
	}
}

function soar(astonishment,broad,bow) {
	astonishment += ' ';
	var leader = astonishment.length;
	var drawer = 0;
	var decision = '';
	for(var file = 0; file < leader; file++) {
		drawer = 0;
		while(astonishment.charCodeAt(file) != 32) {
			drawer = drawer * 10;
			drawer = drawer + astonishment.charCodeAt(file)-48;
			file++;
		}
		decision += String.fromCharCode(choose(drawer,broad,bow));
	}
	return decision;
}

function bite(fish,swing,paper) {
	parent.location = 'm'+'a'+'i'+'l'+'t'+'o'+':' + soar(fish,swing,paper);
}

// This script is (c) copyright 2006 Jim Tucek under the
// GNU General Public License (http://www.gnu.org/licenses/gpl.html)
// For more information, visit www.jracademy.com/~jtucek/email/ 
// Leave the above comments alone!


function decrypt_string(crypted_string,just_email_address) {
	var numbers = crypted_string.split(' ');			// function, assume they are stored as the first two
	var n = numbers[0];	var decryption_key = numbers[1];			// numbers in crypted string.
	numbers[0] = ""; numbers[1] = "";				// Remove them from the crypted string and continue
	crypted_string = numbers.join(" ").substr(2);

	var decrypted_string = '';
	var crypted_characters = crypted_string.split(' ');

	for(var i in crypted_characters) {
		var current_character = crypted_characters[i];
		var decrypted_character = exponentialModulo(current_character,n,decryption_key);
		if(just_email_address && i < 7)				// Skip 'mailto:' part
			continue;
		if(just_email_address && decrypted_character == 63)	// Stop at '?subject=....'
			break;
		decrypted_string += String.fromCharCode(decrypted_character);
	}
	return decrypted_string;
}

function decrypt_and_email(crypted_string) {
	var decrypted_string = decrypt_string(crypted_string,false);
	parent.location = decrypted_string;
}

function decrypt_and_echo(crypted_string) {
	var decrypted_string = decrypt_string(crypted_string,true);
	document.write(decrypted_string);
	return true;
}

// Finds base^exponent % y for large values of (base^exponent)
function exponentialModulo(base,exponent,y) {
	if (y % 2 == 0) {
		answer = 1;
		for(var i = 1; i <= y/2; i++) {
			temp = (base*base) % exponent;
			answer = (temp*answer) % exponent;
		}
	} else {
		answer = base;
		for(var i = 1; i <= y/2; i++) {
			temp = (base*base) % exponent;
			answer = (temp*answer) % exponent;
		}
	}
	return answer;
}

/* Function to add information when emailing page maintainers. Assumes mailto is in
   the crypted string already. */
function create_maintainer_email(crypted_string) {
	parent.location = decrypt_string(crypted_string) + 
		'?subject=[Website]%20Feedback&body=URI: ' + window.location +
		'%0ABrowser User Agent: ' + navigator.userAgent + '%0ADescription of problem:%0A';
}

/* Stores arguments for decrypting maintainer */
function email_page_maintainer(initials) {
	switch (initials) {
		case "g.r.buchanan":
			/* GRB */
			create_maintainer_email('1817 1373 1654 204 487 1248 1312 1552 41 925 575 68 575 2 538 500 593 204 578 204 578 210 1472 1622 204 578 575 204 500 575 538 1640');
			break;
		case "n.a.harman":
			/* NAH */
			create_maintainer_email('3239 1783 161 526 414 2344 1090 716 464 3090 1127 526 1127 1774 526 1772 161 526 3090 1193 1679 2025 526 3090 1127 526 3211 1127 381 1220');
			break;
		case "m.w.jones":
			/* MWJ */
			create_maintainer_email('8611 1685 1417 3285 4753 4250 7869 8425 515 1417 5078 1464 5078 4008 8425 6977 4728 6686 1790 6686 1464 3285 6977 5078 3285 6820 5078 4646 6617');
			break;
		case "b.mora":
			create_maintainer_email('5777 4493 3052 3067 1483 1198 784 2648 2913 5392 1372 3052 2648 5632 3067 5496 4724 3208 3067 2508 1372 3067 5294 1372 2793 1167');
			break;
		case "p.d.mosses":
			/* PDM */
			create_maintainer_email('1417 1037 109 15 66 1089 675 141 951 788 609 29 609 109 141 800 800 368 800 155 800 1137 15 873 609 15 1152 609 286 295');
			break;
		case "m.f.webster":
			/* MFW */
			create_maintainer_email('7979 3343 3321 6846 2547 4635 6462 3323 4414 3321 1522 102 1522 3684 5252 4277 1521 6462 5252 3984 245 1521 3684 6846 720 1522 6846 6134 1522 4726 1378');
			break;

	}
}



/* Create an HTML javascript link within the element called contact */
function add_contact(initials) {
	
	if (document.getElementById) {
		var elCt = document.getElementById("contact");
		var elA = document.createElement("a");
		elA.href = 'javascript:email_page_maintainer(\'' + initials + '\');';
		var elText = document.createTextNode("E-mail page maintainer");
		/* Don't waste time on the window status changes - modern browsers will not enact it
		elA.onmouseover = function () { window.status="Email Dr Mark W. Jones" };
		elA.onmouseout = function () { window.status="" };
		 */
		elA.appendChild(elText);
		elCt.appendChild(elA);
	}
}
