letters = new Array ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
numbers = new Array ( 1,   2,   3,   4,   5,   8,   3,   5,   1,   1,   2,   3,   4,   5,   7,   8,   1,   2,   3,   4,   6,   6,   6,   5,   1,   7 );

m10 = new Array(1, 1, 3, 1, 5, 6, 1, 3, 6, 1, 1, 6, 1, 6, 6, 1, 3, 6, 1, 1, 3, 1, 5, 6, 1, 3, 6, 7, 7, 3, 7);
m11 = new Array(5, 6, 6, 5, 6, 5, 5, 1, 9, 5, 6, 9, 5, 5, 5, 5, 1, 5, 5, 6, 6, 5, 6, 5, 5, 2, 9, 2, 5, 6, 2);

m20 = new Array(7, 2, 3, 7, 5, 6, 7, 3, 3, 7, 7, 6, 7, 5, 6, 2, 3, 3, 7, 2, 3, 7, 5, 6, 7, 3, 9, 7, 7);
m21 = new Array(2, 7, 6, 2, 6, 3, 2, 2, 6, 2, 5, 5, 2, 6, 3, 5, 2, 6, 2, 7, 6, 2, 6, 5, 2, 2, 6, 2, 6);

m30 = new Array(1, 2, 3, 1, 5, 6, 7, 3, 9, 1, 7, 6, 1, 7, 6, 2, 3, 3, 1, 2, 3, 1, 6, 6, 7, 3, 9, 5, 1, 3, 1);
m31 = new Array(7, 7, 6, 7, 7, 3, 2, 6, 6, 7, 1, 7, 7, 1, 3, 1, 6, 6, 7, 7, 6, 7, 7, 3, 2, 1, 1, 9, 7, 9, 7);

m40 = new Array(1, 1, 3, 1, 5, 3, 7, 3, 3, 1, 7, 1, 1, 5, 6, 2, 3, 3, 1, 7, 3, 1, 5, 3, 7, 3, 3, 2, 7, 3);
m41 = new Array(7, 7, 9, 7, 7, 5, 5, 1, 9, 7, 1, 9, 7, 7, 3, 1, 1, 1, 7, 2, 1, 7, 7, 5, 5, 2, 6, 7, 1, 6);

m50 = new Array(1, 2, 3, 1, 5, 6, 7, 3,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, 3, 2, 2, 3, 1, 5, 6, 7, 3, 9, 7, 7, 3, 1);
m101 = new Array(7, 7, 6, 7, 6, 3, 2, 6, 6, 7, 6, 2, 7, 6, 3, 1, 6, 6, 7, 7, 6, 7, 6, 3, 2, 6, 6, 2, 1, 6, 7);

m110 = new Array(1, 7, 3, 1, 5, 3, 7, 3, 9, 1, 7, 9, 1, 5, 3, 1, 3, 3, 1, 7, 3, 1, 5, 3, 7, 3, 9, 7, 1, 3);
m111 = new Array(7, 1, 9, 7, 7, 5, 1, 9, 3, 7, 1, 0, 7, 7, 5, 5, 9, 0, 7, 1, 9, 7, 7, 5, 1, 9, 3, 2, 7, 6);

m120 = new Array(1, 2, 3, 1, 3, 6, 7, 3, 9, 1, 7, 6, 1, 5, 6, 2, 3, 3, 1, 2, 3, 1, 5, 3, 7, 3, 6, 5, 1, 3, 1);
m121 = new Array(7, 7, 6, 7, 6, 3, 2, 2, 3, 7, 1, 9, 3, 6, 3, 1, 2, 6, 7, 7, 6, 7, 6, 6, 3, 0, 9, 6, 6, 6, 3);

match1 = new Array(1, 2, 4, 7);
match2 = new Array(2, 1, 7);
match3 = new Array(3, 6, 8, 9);
match4 = new Array(4, 1, 7);
match5 = new Array(5, 1, 6, 7, 9);
match6 = new Array(6, 3, 5, 9);
match7 = new Array(7, 1, 2, 4, 5);
match8 = new Array(8, 3, 9);
match9 = new Array(9, 3, 5, 6, 8);

notmatch5 = new Array(3, 8);

var name_whole = "";
var name_whole_res = 0;
var names_sub = new Array(null);
var names_sub_res = new Array(null);


var key = 0;
var key_sub = 0;

function setup()
{
    document.form1.textfield.focus();
}

function inArray(ar, n) {
	for (var i = 0; i < ar.length; i++) {
		if (ar[i] == n) return true;
	}
	return false;
}


function matchIt() {
	var main1 = document.getElementById('match_key');
	var main2 = document.getElementById('match_no_data');
	if ((key == 0)||(name_whole == "")) {
		main1.style.display = 'none';
		main2.style.display = 'block';
		return false;
	}

	main1.style.display = 'block';
	main2.style.display = 'none';

	var km = document.getElementById('match_key_main');
	var ks = document.getElementById('match_key_sub');
	
	km.innerHTML = '<a href="javascript:showText('+key+')" class="numLink">' + key + '</a>';
	ks.innerHTML = '<a href="javascript:showText('+key_sub+')" class="numLink">' + key_sub + '</a>';
	
	var obj1 = document.getElementById('name_whole');
	
	var one = getOneDigit(name_whole_res);
	
	obj1.innerHTML = '<div>' + name_whole + '=<a href="javascript:showText('+name_whole_res+')" class="numLink">' + name_whole_res + '</a>=<a href="javascript:showText('+one+')" class="numLink">' + one + '</a><div class="matchDesc">'+getMatching(one)+'</div></div>';
	var subHTML = '';
	var i = 0;
	while((nsub = names_sub[i]) != null) {
		one = getOneDigit(names_sub_res[i]);
		subHTML += '<div>' + names_sub[i] + '=<a href="javascript:showText('+names_sub_res[i]+')" class="numLink">' + names_sub_res[i] + '</a>=<a href="javascript:showText('+one+')" class="numLink">' + one + '</a><div class="matchDesc">'+getMatching(one)+'</div></div>';
		i++;
	}
	obj1.innerHTML += subHTML;
	
	var obj3 = document.getElementById('match_key_sub_display');
	if (key_sub == 0) {
		obj3.style.display = 'none';
	} else {
		obj3.style.display = 'inline';		
	}
		
}


function getMatching(n) {
	res = '';
	if (isMatch(n, key)) {
		res = "MATCHES WITH KEY";
	}
	if (isMatch(n, key_sub)) {
		if (res == '') {
			res = "MATCHES WITH SUB KEY";
		} else {
			res += " AND SUB KEY";
		}
	}
	if (isRepel(n, key)) {
		if (res != '') {
			res += ', ';
		}
		res += "REPELS WITH KEY";
	}

	if (isRepel(n, key_sub)) {
		if (res != '') {
			res += ', ';
		}
		res += "REPELS WITH SUB KEY";
	}
	
	if (res == '') res = "NO MATCHES";
	
	return res;
}

function isMatch(n, k) {
	if (k != 0) {
		var mmm1 = eval('match' + k);
		for(var inx = 0; inx < mmm1.length; inx++) {
			if (mmm1[inx] == n) {
				return true;
			}
		}
	}
	return false;
}

function isRepel(n, k) {
	return ((k == 5) && ((n==8)||(n==3)));
}

function getOneDigit(num) {
	snum = "" + num;
	res = num;
	while (res > 9) {
		res = parseInt(snum.charAt(0)) + parseInt(snum.charAt(1));
		snum = "" + res;
	}
	return res;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function calculate() {
	var result = 0;
	var text = document.form1.textfield.value.toUpperCase();
	text = trim(text);
	name_whole = text;
	var details = "";
	var sub = 0;
	var sub_inx = 0;
	names_sub[sub_inx] = '';
	var spaces = false;
	for (inx = 0; inx < text.length; inx++) {
		num = getNumber(text.charAt(inx));
		if ((num == 0) && (!isNaN(text.charAt(inx))) && (text.charAt(inx) != " ")) {
			num = parseInt(text.charAt(inx));
		}
		
		if (text.charAt(inx) != " ") {
			names_sub[sub_inx] += text.charAt(inx);
		}
				
		if (details != "") details += " ";

		if ((text.charAt(inx) == " ") && (sub > 0)) {
			details += '<span style="color:#990000;"><strong>&nbsp;= <a href="javascript:showText('+sub+')" class="numLink">' + sub + '</a></strong></span><br />';
			names_sub_res[sub_inx++] = sub;
			names_sub[sub_inx] = '';
			sub = 0;
			spaces = true;
		} else {
			details += text.charAt(inx) + "=" + num;
		}
		result += num;
		sub += num;
	}

	if ((sub > 0) && spaces) {
		details += ' <span style="color:#990000;"><strong>&nbsp;= <a href="javascript:showText('+sub+')" class="numLink">' + sub + '</a></strong></span>';
		names_sub_res[sub_inx++] = sub;
	}
	
	names_sub[sub_inx] = null;
	

	var obj_result = document.getElementById("result");
	var obj_expl = document.getElementById("name_explanation");

	if (result!=0) {
		obj_result.style.display = "block";
		obj_expl.style.display = "none";
		obj_result.innerHTML = '<div id="name_result_number"><a href="javascript:showText('+result+')" class="numLink">' + result + '</a></div>' + details;
	} else {
		obj_result.style.display = "none";
		obj_expl.style.display = "block";
		obj_result.innerHTML = '';
	}
	
	name_whole_res = result;
	
	return false;
	
}

function trim0l(s) {
	while(s[0] == '0') {
		s = s.substr(1);
	}
	return s;
}

function calculate2() {
	var day = (parseInt(trim0l(document.form2.day.value)) == NaN) ? 0 : parseInt(trim0l(document.form2.day.value));
	var month = (parseInt(trim0l(document.form2.month.value)) == NaN) ? 0 : parseInt(trim0l(document.form2.month.value));
	
	date_expl = document.getElementById('date_explanation');
	date_res = document.getElementById('date_result');

	if ((day == 0)||(month == 0)||!validDate(day, month)) {
		date_res.style.display = "none";
		date_expl.style.display = "block";
		key = 0;
		key_sub = 0;
		return false;
	}
	
	var n_main = document.getElementById('n_main');
	var n_sub = document.getElementById('n_sub');
	
	main_n = eval('m' + month + '0['+(day-1)+']');
	sub_n = eval('m' + month + '1['+(day-1)+']');
	
	n_main.innerHTML = '<a href="javascript:showText('+main_n+')" class="numLink">' + main_n + '</a>';
	if (sub_n != 0) {
		n_sub.innerHTML = '<a href="javascript:showText('+sub_n+')" class="numLink">' + sub_n + '</a>';
	} else {
		n_sub.innerHTML = '';
	}
	
	key = main_n;
	key_sub = sub_n;
	
	date_expl.style.display = "none";
	date_res.style.display = "block";
	
	return false;
	
}

function validDate(day, month) {
	return ((day > 0) && (day <= maxDays(month)));
}

function maxDays(month) {
	switch (month) {
		case 1:
		return 31;
		case 2:
		return 28;
		case 3:
		return 31;
		case 4:
		return 30;
		case 5:
		return 31;
		case 6:
		return 30;
		case 7:
		return 31;
		case 8:
		return 31;
		case 9:
		return 30;
		case 10:
		return 31;
		case 11:
		return 30;
		case 12:
		return 31;
	}
}

function getNumber(ch) {
	for (iny = 0; iny < letters.length; iny++) {
		if (ch == letters[iny]) {
			return numbers[iny];
		}
	}
	return 0;
}

var displayedId = 'name_numerology';

function showText(n) {
	hideDisplayed();
	showById('txNoExist');
	unsetButtons();
}

function showMatching() {
	hideDisplayed();
	matchIt();
	showById('matching');	
}


function showName() {
	hideDisplayed();
	showById('name_numerology');
	document.form1.textfield.focus();
}

function showDate() {
	hideDisplayed();
	showById('date_numerology');
	document.form2.day.focus();
}

function showMatchTable() {
	hideDisplayed();
	showById('matches_table');
}

function hideDisplayed() {
	var disp = document.getElementById(displayedId);
	disp.style.display = 'none';
}

function showById(id) {
	var disp = document.getElementById(id);
	if (disp == null) {
		id = 'txNoExist';
		disp = document.getElementById(id);
	}
	disp.style.display = 'block';
	displayedId = id;
}

function unsetButtons() {
	document.images['im_date'].src = 'images/date.png';
	document.images['im_name'].src = 'images/name.png';
	document.images['im_matching'].src = 'images/matching.png';
	document.images['im_mtable'].src = 'images/table.png';
}


function setImage(name, src) {
	unsetButtons();
	document.images[name].src = src;
	swapImages[name] = src;	
}


function debug(str) {
	var dd = document.getElementById('debug')
	dd.innerHTML += str + '<br />';
}