var curPW = "joshua";
var demoPW = "demo";
var letter, id, index, searchString;
var fatherID, motherID, spouseID, selfSex, spouseSex;
var child1ID, child1Sex, child2ID, child2Sex, child3ID, child3Sex, child4ID, child4Sex;
var child5ID, child5Sex, child6ID, child6Sex, child7ID, child7Sex, child8ID, child8Sex;
var lastValidID, lastValidLetter, allDataArray, listOfNames, anchorText;

var fatherLastName, fatherNickName;
var motherLastName, motherNickName;
var selfLastName, selfNickName;
var spouseLastName, spouseNickName;
var child1LastName, child1NickName, child1YearChars;
var child2LastName, child2NickName, child2YearChars;
var child3LastName, child3NickName, child3YearChars;
var child4LastName, child4NickName, child4YearChars;
var child5LastName, child5NickName, child5YearChars;
var child6LastName, child6NickName, child6YearChars;
var child7LastName, child7NickName, child7YearChars;
var child8LastName, child8NickName, child8YearChars;

function getListNames(ltr,hbPKey)
{
	var listOfNames, i, iCounter, foundIndex, foundMatch, searchingAll;
	listOfNames = "";
	i = 0;
	iCounter = -1;
	foundIndex = -1;
	foundMatch = false;
	searchingAll = false;
	if (ltr.length == 3)	// "ALL"
	{
		searchingAll = true;
	}
	else
	{
		// Change ltr to correspond to letter associated with hbPKey
		for(i=0; i<allDataArray.length; i++)
		{
			if ( getField(allDataArray[i],0,"|")==hbPKey )
			{
				ltr = getField(allDataArray[i],4,"|").substring(0,1);
				break;
			}
		}
	}
	
	for(i=0; i<allDataArray.length; i++)
	{
		if ( searchingAll || getField(allDataArray[i],4,"|").substring(0,1)==ltr )
		{
			iCounter++;
			if ( getField(allDataArray[i],0,"|")==hbPKey )
			{
				foundIndex = iCounter;
			}
			foundMatch = true;
			listOfNames += "<option value='" 
			+ getField(allDataArray[i],0,"|") + "'>" 
			+ getField(allDataArray[i],4,"|") + ", "
			+ getField(allDataArray[i],5,"|") + "</option>\n"; 
		}
		else
		{
			if (foundMatch)
			{
				break;
			}
		}
	}
	setCookie("index", foundIndex, null, null, null, null);
	
	if (pw() != true)
	{
		listOfNames = "<option value='0'></option>\n";
	}
	
	return listOfNames;
}

function getField(arrayString, fieldIndex, delimiter)
{
	//example: "1|0|0|0|Aaker|Tanya|Tanya|F|0",
	
	var i, startPos, workStr, nextDelimiter;
	i = 0;
	startPos = 0;
	workStr = "";
	nextDelimiter = 0;
	
	for (i=0; i<fieldIndex; i++)
	{
		startPos = arrayString.indexOf(delimiter,startPos+1);
	}
	
	if (fieldIndex > 0)
	{
		workStr = arrayString.substring(startPos+1);
	}
	else
	{
		workStr = arrayString.substring(0,arrayString.indexOf("|",0));
	}
	
	nextDelimiter = workStr.indexOf("|",0);	
	
	if (nextDelimiter > 0)
	{
		workStr = workStr.substring(0, nextDelimiter);
	}
	
	return workStr;
}

function setCookie(key, value, expires, path, domain, secure)
{
	document.cookie= escape(key) + "=" + escape(value) + 
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookie(keyValue)
{
	var key;
	var value;
	var valueOffset;
	var keyValueArray ; 
  
	if (document.cookie.length > 0)
	{
		keyValueArray = document.cookie.split(";");  // get all key-value pairs in an array
		for (i = 0; i < keyValueArray.length; i++)
		{  
			valueOffset = keyValueArray[i].indexOf("=");  // split a key-value pair in to key and value
			if (valueOffset != -1)	// not an error condition
			{
				key = keyValueArray[i].substring(0, valueOffset);
				// may have a leading space!
				if (key.charAt(0) == " ")
				{
					key = key.substring(1, key.length);
				}
				value = keyValueArray[i].substring(valueOffset + 1, keyValueArray[i].length); 
				key = unescape(key);
				if (key == keyValue)  // found our cookie!
				{
					return unescape(value);
				}
			}
		}
	}
	return null;
}

function setLetter(ltr)
{
	setCookie("letter", ltr, null, null, null, null);
	setCookie("index", 0, null, null, null, null);
	setCookie("lastValidID", id, null, null, null, null);
	if (lastValidLetter == null)
	{
		lastValidLetter = letter;
	}
	setCookie("lastValidLetter", lastValidLetter, null, null, null, null);
	setCookie("id", 0, null, null, null, null);
}

function setSelf(idx)
{
	var hbPKey = document.formList.selectList.value;
	if (hbPKey >= 0)
	{
		setCookie("id", hbPKey, null, null, null, null);
		setCookie("index", idx, null, null, null, null);
		letter = document.formList.selectList.options[idx].text.substring(0,1);
		lastValidLetter = letter;
		setCookie("letter", letter, null, null, null, null);
		setCookie("lastValidLetter", lastValidLetter, null, null, null, null);
	}
}

function refreshPage(bFromServer)
{
	window.location.reload(bFromServer);
}

function fixBirthYear(nYear)
{
	var fixedYear, lenYear;
	lenYear = nYear.length;
	
	if (lenYear == 4)
	{
		fixedYear = nYear;
	}
	else
	{
		if (lenYear == 2)
		{
			if (nYear > 20)
			{
				fixedYear = "19" + nYear;
			}
			else
			{
				fixedYear = "20" + nYear;
			}
		}
		else
		{
			fixedYear = "0000";
		}
	}
	
	return fixedYear;
}

function getRelativeImage(hbPKey,who)
{
	var rc, hbFather, hbMother, hbSpouse;
	var hbChild1, hbChild2, hbChild3, hbChild4, hbChild5, hbChild6, hbChild7, hbChild8; 
	
	// Find the who associated with hbPKey
	hbFather = 0;
	hbMother = 0;
	hbSpouse = 0;
	hbChild1 = 0;
	hbChild2 = 0;
	hbChild3 = 0;
	hbChild4 = 0;
	hbChild5 = 0;
	hbChild6 = 0;
	hbChild7 = 0;
	hbChild8 = 0;
	
	switch (who)
	{
		case "self" :
			if (hbPKey > 0)
			{
				rc = "<a href='index.html' onClick='setCookie(\"id\", "	
					+ hbPKey + ", null, null, null, null)'>\n";
				rc += "<img name='imgSelf' src='images/" 
					+ hbPKey + ".jpg" + "' width='100%' height='100%' border='0'" 
					+ "onError='this.onerror=null;this.src=\"images/gifs/Missing.gif\";'>\n";
				rc += "</a>\n";
			}
			else
			{
				rc = "<a href='index.html'>\n";
				rc += "<img name='imgSelf' src='images/gifs/clear.gif' width='100%' height='100%' border='0'>\n";
				rc += "</a>\n";
			}
			break;
		case "father" :
			if (fatherID > 0)
			{
				rc = "<a href='index.html' onClick='setCookie(\"id\", "	
					+ fatherID + ", null, null, null, null)'>\n";
				rc += "<img name='imgFather' src='images/" 
					+ fatherID + ".jpg" + "' width='100' height='100%' border='0'" 
					+ "onError='this.onerror=null;this.src=\"images/gifs/Missing.gif\";'>\n";
				rc += "</a>\n";
			}
			else
			{
				rc = "<a href='index.html'>\n";
				rc += "<img name='imgFather' src='images/gifs/clear.gif' width='100' height='100%' border='0'>\n";
				rc += "</a>\n";
			}
			break;
		case "mother" :
			if (motherID > 0)
			{
				rc = "<a href='index.html' onClick='setCookie(\"id\", "	
					+ motherID + ", null, null, null, null)'>\n";
				rc += "<img name='imgMother' src='images/" 
					+ motherID + ".jpg" + "' width='100' height='100%' border='0'" 
					+ "onError='this.onerror=null;this.src=\"images/gifs/Missing.gif\";'>\n";
				rc += "</a>\n";
			}
			else
			{
				rc = "<a href='index.html'>\n";
				rc += "<img name='imgMother' src='images/gifs/clear.gif' width='100' height='100%' border='0'>\n";
				rc += "</a>\n";
			}
			break;
		case "spouse" :
			if (spouseID > 0)
			{
				rc = "<a href='index.html' onClick='setCookie(\"id\", "	
					+ spouseID + ", null, null, null, null)'>\n";
				rc += "<img name='imgSpouse' src='images/" 
					+ spouseID + ".jpg" + "' width='100' height='100%' border='0'" 
					+ "onError='this.onerror=null;this.src=\"images/gifs/Missing.gif\";'>\n";
				rc += "</a>\n";
			}
			else
			{
				rc = "<a href='index.html'>\n";
				rc += "<img name='imgSpouse' src='images/gifs/clear.gif' width='100' height='100%' border='0'>\n";
				rc += "</a>\n";
			}
			break;
		case "child1" :
			if (child1ID > 0)
			{
				rc = "<a href='index.html' onClick='setCookie(\"id\", "	
					+ child1ID + ", null, null, null, null)'>\n";
				rc += "<img name='imgChild1' src='images/" 
					+ child1ID + ".jpg" + "' width='100' height='100%' border='0'" 
					+ "onError='this.onerror=null;this.src=\"images/gifs/Missing.gif\";'>\n";
				rc += "</a>\n";
			}
			else
			{
				rc = "<a href='index.html'>\n";
				rc += "<img name='imgChild1' src='images/gifs/clear.gif' width='100' height='100%' border='0'>\n";
				rc += "</a>\n";
			}
			break;
		case "child2" :
			if (child2ID > 0)
			{
				rc = "<a href='index.html' onClick='setCookie(\"id\", "	
					+ child2ID + ", null, null, null, null)'>\n";
				rc += "<img name='imgChild2' src='images/" 
					+ child2ID + ".jpg" + "' width='100' height='100%' border='0'" 
					+ "onError='this.onerror=null;this.src=\"images/gifs/Missing.gif\";'>\n";
				rc += "</a>\n";
			}
			else
			{
				rc = "<a href='index.html'>\n";
				rc += "<img name='imgChild2' src='images/gifs/clear.gif' width='100' height='100%' border='0'>\n";
				rc += "</a>\n";
			}
			break;
		case "child3" :
			if (child3ID > 0)
			{
				rc = "<a href='index.html' onClick='setCookie(\"id\", "	
					+ child3ID + ", null, null, null, null)'>\n";
				rc += "<img name='imgChild3' src='images/" 
					+ child3ID + ".jpg" + "' width='100' height='100%' border='0'" 
					+ "onError='this.onerror=null;this.src=\"images/gifs/Missing.gif\";'>\n";
				rc += "</a>\n";
			}
			else
			{
				rc = "<a href='index.html'>\n";
				rc += "<img name='imgChild3' src='images/gifs/clear.gif' width='100' height='100%' border='0'>\n";
				rc += "</a>\n";
			}
			break;
		case "child4" :
			if (child4ID > 0)
			{
				rc = "<a href='index.html' onClick='setCookie(\"id\", "	
					+ child4ID + ", null, null, null, null)'>\n";
				rc += "<img name='imgChild4' src='images/" 
					+ child4ID + ".jpg" + "' width='100' height='100%' border='0'" 
					+ "onError='this.onerror=null;this.src=\"images/gifs/Missing.gif\";'>\n";
				rc += "</a>\n";
			}
			else
			{
				rc = "<a href='index.html'>\n";
				rc += "<img name='imgChild4' src='images/gifs/clear.gif' width='100' height='100%' border='0'>\n";
				rc += "</a>\n";
			}
			break;
		case "child5" :
			if (child5ID > 0)
			{
				rc = "<a href='index.html' onClick='setCookie(\"id\", "	
					+ child5ID + ", null, null, null, null)'>\n";
				rc += "<img name='imgChild5' src='images/" 
					+ child5ID + ".jpg" + "' width='100' height='100%' border='0'" 
					+ "onError='this.onerror=null;this.src=\"images/gifs/Missing.gif\";'>\n";
				rc += "</a>\n";
			}
			else
			{
				rc = "<a href='index.html'>\n";
				rc += "<img name='imgChild5' src='images/gifs/clear.gif' width='100' height='100%' border='0'>\n";
				rc += "</a>\n";
			}
			break;
		case "child6" :
			if (child6ID > 0)
			{
				rc = "<a href='index.html' onClick='setCookie(\"id\", "	
					+ child6ID + ", null, null, null, null)'>\n";
				rc += "<img name='imgChild6' src='images/" 
					+ child6ID + ".jpg" + "' width='100' height='100%' border='0'" 
					+ "onError='this.onerror=null;this.src=\"images/gifs/Missing.gif\";'>\n";
				rc += "</a>\n";
			}
			else
			{
				rc = "<a href='index.html'>\n";
				rc += "<img name='imgChild6' src='images/gifs/clear.gif' width='100' height='100%' border='0'>\n";
				rc += "</a>\n";
			}
			break;
		case "child7" :
			if (child7ID > 0)
			{
				rc = "<a href='index.html' onClick='setCookie(\"id\", "	
					+ child7ID + ", null, null, null, null)'>\n";
				rc += "<img name='imgChild7' src='images/" 
					+ child7ID + ".jpg" + "' width='100' height='100%' border='0'" 
					+ "onError='this.onerror=null;this.src=\"images/gifs/Missing.gif\";'>\n";
				rc += "</a>\n";
			}
			else
			{
				rc = "<a href='index.html'>\n";
				rc += "<img name='imgChild7' src='images/gifs/clear.gif' width='100' height='100%' border='0'>\n";
				rc += "</a>\n";
			}
			break;
		case "child8" :
			if (child8ID > 0)
			{
				rc = "<a href='index.html' onClick='setCookie(\"id\", "	
					+ child8ID + ", null, null, null, null)'>\n";
				rc += "<img name='imgChild8' src='images/" 
					+ child8ID + ".jpg" + "' width='100' height='100%' border='0'" 
					+ "onError='this.onerror=null;this.src=\"images/gifs/Missing.gif\";'>\n";
				rc += "</a>\n";
			}
			else
			{
				rc = "<a href='index.html'>\n";
				rc += "<img name='imgChild8' src='images/gifs/clear.gif' width='100' height='100%' border='0'>\n";
				rc += "</a>\n";
			}
			break;
	}
	
	return rc;
}

function setSearchString(str)
{
	searchString = str;
	setCookie("searchString", searchString, null, null, null, null);
	lastValidID = id;
	lastValidLetter = document.formList.selectList.options[0].text.substring(0,1);
	setCookie("lastValidID", lastValidID, null, null, null, null);
	setCookie("lastValidLetter", lastValidLetter, null, null, null, null);
}

function getSearchNames(searchString)
{
	var listOfNames, i, UCaseSearchString, f4, f5, f6;
	listOfNames = "";
	i = 0;
	UCaseSearchString = searchString.toUpperCase();
	
	for(i=0; i<allDataArray.length; i++)
	{
		//example: "1|0|0|0|Aaker|Tanya|Tanya|F|0",
		f4 = getField(allDataArray[i],4,"|").toUpperCase();
		f5 = getField(allDataArray[i],5,"|").toUpperCase();
		f6 = getField(allDataArray[i],6,"|").toUpperCase();
		
		if ( f4.indexOf(UCaseSearchString,0) >= 0
		|| f5.indexOf(UCaseSearchString,0) >= 0
		|| f6.indexOf(UCaseSearchString,0) >= 0 )
		{
			listOfNames += "<option value='" 
			+ getField(allDataArray[i],0,"|") + "'>" 
			+ getField(allDataArray[i],4,"|") + ", "
			+ getField(allDataArray[i],5,"|") + "</option>\n"; 
		}
	}
	return listOfNames;
}

function setVariables(id)
{
	//example: "1|0|0|0|Aaker|Tanya|Tanya|F|0",
	
	var i, j, hbBirthYear, hbChild, hbChildSex, end, start, remainder;
	var foundSelf, foundChildFather, foundChildMother, currentID;
	i = 0;
	j = 0;
	
	var today, thisYear;
	today = new Date();
	thisYear = today.getYear();
	
	//fatherID, motherID, spouseID, spouseSex, selfSex
	//child1ID, child1Sex, child2ID, child2Sex, child3ID, child3Sex, child4ID, child4Sex
	//child5ID, child5Sex, child6ID, child6Sex, child7ID, child7Sex, child8ID, child8Sex
	
	var childArray = new Array();
	
	for(i=0; i<allDataArray.length; i++)
	{
		foundSelf = 0;
		foundChildFather = 0;
		foundChildMother = 0;

		foundSelf = getField(allDataArray[i],0,"|");
		foundChildFather = getField(allDataArray[i],1,"|");
		foundChildMother = getField(allDataArray[i],2,"|");
		
		if (foundSelf==id || foundChildFather==id || foundChildMother==id)
		{
			if (foundSelf==id)
			{
				//Found self
				//fatherID, motherID, spouseID, selfSex, spouseSex
				fatherID = getField(allDataArray[i],1,"|");
				motherID = getField(allDataArray[i],2,"|");
				spouseID = getField(allDataArray[i],3,"|");
				selfSex = getField(allDataArray[i],7,"|");
				selfLastName = getField(allDataArray[i],4,"|");
				selfNickName = getField(allDataArray[i],6,"|");
				if (selfSex == "M")
				{
					spouseSex = "F";
				}
				else
				{
					spouseSex = "M";
				}
			}
			else
			{
				//Found child who refers to self
				hbBirthYear = getField(allDataArray[i],8,"|");
				hbBirthYear = fixBirthYear(hbBirthYear);
				hbChildSex = getField(allDataArray[i],7,"|");
				hbChild = getField(allDataArray[i],0,"|");
				childArray[j++] = hbBirthYear + "|" + hbChild + "|" + hbChildSex;
			}
		}
	}
	
	childArray.sort();
	
	//child1ID, child1Sex, child2ID, child2Sex, child3ID, child3Sex, child4ID, child4Sex
	//child5ID, child5Sex, child6ID, child6Sex, child7ID, child7Sex, child8ID, child8Sex

	child1ID = 0;
	child1Sex = "";
	child2ID = 0;
	child2Sex = "";
	child3ID = 0;
	child3Sex = "";
	child4ID = 0;
	child4Sex = "";
	child5ID = 0;
	child5Sex = "";
	child6ID = 0;
	child6Sex = "";
	child7ID = 0;
	child7Sex = "";
	child8ID = 0;
	child8Sex = "";
	
	for(j=0; j<childArray.length; j++)
	{
		// Remove year
		end = childArray[j].indexOf("|");
		start = end + 1;
		remainder = childArray[j].substring(start);
		// Extract sex
		end = remainder.indexOf("|");
		start = end + 1;
		hbChildSex = remainder.substring(start);
		// Extract child id
		start = 0;
		hbChild = remainder.substring(start,end);
		// Populate variables
		switch (j)
		{
			case 0 :
				child1ID = hbChild;
				child1Sex = hbChildSex;
				break;
			case 1 :
				child2ID = hbChild;
				child2Sex = hbChildSex;
				break;
			case 2 :
				child3ID = hbChild;
				child3Sex = hbChildSex;
				break;
			case 3 :
				child4ID = hbChild;
				child4Sex = hbChildSex;
				break;
			case 4 :
				child5ID = hbChild;
				child5Sex = hbChildSex;
				break;
			case 5 :
				child6ID = hbChild;
				child6Sex = hbChildSex;
				break;
			case 6 :
				child7ID = hbChild;
				child7Sex = hbChildSex;
				break;
			case 7 :
				child8ID = hbChild;
				child8Sex = hbChildSex;
				break;
		}
	}
	
	//example: "1|0|0|0|Aaker|Tanya|Tanya|F|0",
	
	for(i=0; i<allDataArray.length; i++)
	{
		currentID = getField(allDataArray[i],0,"|");
		switch (currentID)
		{
			case fatherID :
				fatherLastName = getField(allDataArray[i],4,"|");
				fatherNickName = getField(allDataArray[i],6,"|");
				break;
			case motherID :
				motherLastName = getField(allDataArray[i],4,"|");
				motherNickName = getField(allDataArray[i],6,"|");
				break;
			case spouseID :
				spouseLastName = getField(allDataArray[i],4,"|");
				spouseNickName = getField(allDataArray[i],6,"|");
				break;
			case child1ID :
				child1LastName = getField(allDataArray[i],4,"|");
				child1NickName = getField(allDataArray[i],6,"|");
				hbBirthYear = getField(allDataArray[i],8,"|");
				child1YearChars = getYearChars(hbBirthYear,thisYear);
				break;
			case child2ID :
				child2LastName = getField(allDataArray[i],4,"|");
				child2NickName = getField(allDataArray[i],6,"|");
				hbBirthYear = getField(allDataArray[i],8,"|");
				child2YearChars = getYearChars(hbBirthYear,thisYear);
				break;
			case child3ID :
				child3LastName = getField(allDataArray[i],4,"|");
				child3NickName = getField(allDataArray[i],6,"|");
				hbBirthYear = getField(allDataArray[i],8,"|");
				child3YearChars = getYearChars(hbBirthYear,thisYear);
				break;
			case child4ID :
				child4LastName = getField(allDataArray[i],4,"|");
				child4NickName = getField(allDataArray[i],6,"|");
				hbBirthYear = getField(allDataArray[i],8,"|");
				child4YearChars = getYearChars(hbBirthYear,thisYear);
				break;
			case child5ID :
				child5LastName = getField(allDataArray[i],4,"|");
				child5NickName = getField(allDataArray[i],6,"|");
				hbBirthYear = getField(allDataArray[i],8,"|");
				child5YearChars = getYearChars(hbBirthYear,thisYear);
				break;
			case child6ID :
				child6LastName = getField(allDataArray[i],4,"|");
				child6NickName = getField(allDataArray[i],6,"|");
				hbBirthYear = getField(allDataArray[i],8,"|");
				child6YearChars = getYearChars(hbBirthYear,thisYear);
				break;
			case child7ID :
				child7LastName = getField(allDataArray[i],4,"|");
				child7NickName = getField(allDataArray[i],6,"|");
				hbBirthYear = getField(allDataArray[i],8,"|");
				child7YearChars = getYearChars(hbBirthYear,thisYear);
				break;
			case child8ID :
				child8LastName = getField(allDataArray[i],4,"|");
				child8NickName = getField(allDataArray[i],6,"|");
				hbBirthYear = getField(allDataArray[i],8,"|");
				child8YearChars = getYearChars(hbBirthYear,thisYear);
				break;
		}
	}
}

function getYearChars(hbBirthYear,thisYear)
{
	var fixedBirthYear, yearChars;
	
	fixedBirthYear = fixBirthYear(hbBirthYear);
	
	if (thisYear - fixedBirthYear <= 18)
	{
		yearChars = " '" + fixedBirthYear.substring(2,4);
	}
	else
	{
		yearChars = "";
	}
	
	return yearChars;
}

function checkPassword()
{
	var password, expireWhen;
	password = getCookie("password");
	
	if (password != null && (password.toLowerCase() == curPW || password.toLowerCase() == demoPW))
	{
		expireWhen = getExpire();
		setCookie('password', password, expireWhen, null, null, null);	// Refresh timeout if in active use
	}
	else
	{
		window.location = "login.html";
	}
}

function getExpire()
{
	var when;
	when = new Date;
	when.setMinutes(when.getMinutes()+10);		// Expire after 10 minutes of idle time
	return when;
}

function pw()
{
	var pw, retVal;
	retVal = false;
	pw = getCookie("password");
	if (pw != null && (pw == curPW || pw == demoPW))
	{
		retVal = true;
	}
	return retVal;
}
