///////////////////////////////////
var aSearchNamesTemp = [];
var aSearchSurnames = [];
var aSearchNames = [];
var aSearchLinks = [];
var aSearchDesigList = [];
var aSearchDesigs = [];
var aSearchSurnames = [];//will be populated on clicking...
var openText = "start typing name";

function fnInit(){
	var aSearchNamesTemp = myData.split("&");

	//quantity of designations:
	var qtyD = aSearchNamesTemp[0].substring(5,aSearchNamesTemp[0].length);
	//quantity of people:
	var qtyP = aSearchNamesTemp[1].substring(5,aSearchNamesTemp[1].length);
	//alert(qtyD);
	
	for(i=2; i<(2+Number(qtyD)); i++){
		var myTemp = aSearchNamesTemp[i].split("=");
		var myTemp = myTemp[1].split("|");
		//alert(myTemp);
		if(i%2 == 0){
			aSearchDesigList.push(myTemp);
		}else{
			aSearchDesigList.push(myTemp);
		}
	}
	//alert(aSearchDesigList);

	for(i=(2+Number(qtyD)); i<((2+Number(qtyD))+Number(qtyP)); i++){
		var myTemp = aSearchNamesTemp[i].split("=");
		var myTemp = myTemp[1].split("|");
		//alert(myTemp[0]);

			aSearchLinks.push(myTemp[0]);
			aSearchDesigs.push(myTemp[1]);

			//aSearchNames.push(myTemp[2]);
			var myTemp2 = myTemp[2].split(", ");
			aSearchSurnames.push(myTemp2[0]);
			aSearchNames.push(myTemp2[1]);

	}
	//alert(aSearchDesigs[0] + " - " + aSearchLinks[0] + " - " + aSearchNames[0] + " " + aSearchSurnames[0]);
	
	aSearchNamesTemp = myTemp = myTemp2 = null;
	////////////////////
	fnDefaultText();
	
}




////////////////////////////////////////////////////////////////////
function fnList(amt) {
	document.getElementById('homePeopleNested').scrollTop = 0;

	aNames = [];
	aLinks = [];

	////////////
	fnClearOptions();
	var myCopy = "";

	
	if(amt == "firstNames"){
		//alert('fn');
		myCopy += '<a href="javascript:fnDefaultText(&#39;orAll&#39;);" class="homePalBold">list all by surname</a><br>';
		myCopy += '<a href="javascript:fnDefaultText(&#39;desigs&#39;);" class="homePalBold">list by designation</a><br><br>';

		for(i=0; i<aSearchNames.length; i++){
			//add SURNAMES to list of names:
			aNames.push(aSearchNames[i] + " " + aSearchSurnames[i]);
			//add Nos to list of links:
			aLinks.push(aSearchLinks[i]);		
		}
		//create subheader:
		myCopy += '<b class="home">listed by first name:</b><br>';
		
	}else if(amt == "all"){

		myCopy += '<a href="javascript:fnDefaultText(&#39;firstNames&#39;);" class="homePalBold">list all by first name&nbsp;</a><br>';
		myCopy += '<a href="javascript:fnDefaultText(&#39;desigs&#39;);" class="homePalBold">list by designation&nbsp;</a><br><br>';


		for(i=0; i<aSearchNames.length; i++){
			//add SURNAMES to list of names:
			aNames.push(aSearchSurnames[i] + ", " + aSearchNames[i]);
			//add Nos to list of links:
			aLinks.push(aSearchLinks[i]);		
		}
		//create subheader:
		myCopy += '<b class="home">listed by surname:</b><br>';
		

	}else{
		myCopy = '<a href="javascript:fnDefaultText(&#39;orAll&#39;);" class="homePalBold">list all names&nbsp;</a><br>';
		myCopy += '<a href="javascript:fnDefaultText(&#39;desigs&#39;);" class="homePalBold">list by designation&nbsp;</a><br><br>';

		if(document.forms['form1'].elements['txt'].value != ""){
			
			//if we are NARROWING THE NAMES DOWN:
			for(i=0; i<aSearchNames.length; i++){
	
				//test against NAMES:
				if(document.forms['form1'].elements['txt'].value.toLowerCase() == aSearchNames[i].substring(0,document.forms['form1'].elements['txt'].value.length).toLowerCase()){
					//add NAMES to list of names:
					aNames.push(aSearchNames[i] + " " + aSearchSurnames[i]);
					//add Nos to list of links:
					aLinks.push(aSearchLinks[i]);
			
				}			
	
				//test against SURNAMES:
				if(document.forms['form1'].elements['txt'].value.toLowerCase() == aSearchSurnames[i].substring(0,document.forms['form1'].elements['txt'].value.length).toLowerCase()){
					//add SURNAMES to list of names:
					aNames.push(aSearchSurnames[i] + ", " + aSearchNames[i]);
					//add Nos to list of links:
					aLinks.push(aSearchLinks[i]);		
						//alert(aSearchNames[i] + " - " + aSearchLinks[i]);
				
				}	
			}
	
			//create subheader with letters that have been entered:
			myCopy += '<b class="home">' + document.forms['form1'].elements['txt'].value.toUpperCase() + ':</b><br>';
			
		}
	
	
	}
	
	
	////////////////////////////////////
	//now we have both together,let's alphabeticise the array:
	// 1. join the two into a single array:
	aNamesAndLinks = [];
	for(i=0; i<aNames.length; i++){
		aNamesAndLinks[i] = aNames[i] + "|" + aLinks[i];
	}
	//alert(aNamesAndLinks);

	// 2. sort the new array (alphabetically):
	aNamesAndLinks.sort();
	//alert(aNamesAndLinks);
	
	// 3. split it into 2 again(turn into multiple arrays):
	for(i=0; i<aNamesAndLinks.length; i++){
		aNamesAndLinks[i] = aNamesAndLinks[i].split("|");
	}
	//4. pare the multiple array into two:
	for(i=0; i<aNamesAndLinks.length; i++){
		aNames[i] = aNamesAndLinks[i][0];
		aLinks[i] = aNamesAndLinks[i][1];
	}
	//alert(aNames);
	//alert(aLinks);

	//now we have them,write them to the field:
	searchOutput = "";
	for(i=0; i<aNames.length; i++){
		var rmNo = Math.floor(Math.random()*2)+1;	
		//myCopy += '<a href="http://www.ens.co.za/index.asp?pageID=people/Base1.asp&iID=' + aLinks[i] + '" class="homePal">' + aNames[i] + '</a><br>';	
		myCopy += '<a href="people/people' + rmNo + '.asp?iID=' + aLinks[i] + '" class="homePal">' + aNames[i] + '</a> <b>&#187;</b><br>';	
	}
	peopleList.innerHTML = myCopy;
	
}


//DESIGNATIONS:::::
//or list designations
function	fnListDesigs(){
	document.getElementById('homePeopleNested').scrollTop = 0;

	aNames = [];
	aLinks = [];

	////////////
	fnClearOptions();
	var myCopy = "";

	myCopy += '<a href="javascript:fnDefaultText(&#39;firstNames&#39;);" class="homePalBold">list all by first name</a><br>';
	myCopy += '<a href="javascript:fnDefaultText(&#39;orAll&#39;);" class="homePalBold">list all by surname</a><br><br>';

	//create subheader::
	myCopy += '<b class="home">click on a designation:</b><br>';

	for(i=0; i<aSearchDesigList.length; i++){
		//add SURNAMES to list of names:
		aNames.push(aSearchDesigList[i][1]);
		//
		aLinks.push(aSearchDesigList[i][0]);
	}
	for(i=0; i<aNames.length; i++){
		myCopy += '<a href="javascript:fnCompareDesigs(&#39;' + aLinks[i] + '&#39;);" class="homePal">' + aNames[i] + '</a> <b>&#187;</b><br>';	
	}
	peopleList.innerHTML = myCopy;
	//alert(aNames);
}

//people who fall under a specific designation:
function	fnCompareDesigs(myDesig){
	document.getElementById('homePeopleNested').scrollTop = 0;

	//alert(myDesig);
	aNames = [];
	aLinks = [];

	////////////
	fnClearOptions();
	var myCopy = "";

	myCopy = '<a href="javascript:fnDefaultText(&#39;desigs&#39;);" class="homePalBold"><b>&#171;</b>&nbsp;return to designations</a><br><br>';

	myCopy += '<b class="home">' + myDesig.toLowerCase() +':</b><br>';

	//alert(aSearchDesigs);
	for(i=0; i<=aSearchDesigs.length; i++){
		if(aSearchDesigs[i] == myDesig){
			//add SURNAMES to list of names:
			aNames.push(aSearchSurnames[i] + ", " + aSearchNames[i]);
			//add Nos to list of links:
			aLinks.push(aSearchLinks[i]);		
			//alert(aSearchNames[i] + " - " + aSearchLinks[i]);
		}
	}
	
	////////////////////////////////////
	//now we have both together,let's alphabeticise the array:
	// 1. join the two into a single array:
	aNamesAndLinks = [];
	for(i=0; i<aNames.length; i++){
		aNamesAndLinks[i] = aNames[i] + "|" + aLinks[i];
	}
	//alert(aNamesAndLinks);

	// 2. sort the new array (alphabetically):
	aNamesAndLinks.sort();
	//alert(aNamesAndLinks);
	
	// 3. split it into 2 again(turn into multiple arrays):
	for(i=0; i<aNamesAndLinks.length; i++){
		aNamesAndLinks[i] = aNamesAndLinks[i].split("|");
	}
	//4. pare the multiple array into two:
	for(i=0; i<aNamesAndLinks.length; i++){
		aNames[i] = aNamesAndLinks[i][0];
		aLinks[i] = aNamesAndLinks[i][1];
	}

	//now we have them,write them to the field:
	searchOutput = "";
	for(i=0; i<aNames.length; i++){
		var rmNo = Math.floor(Math.random()*2)+1;	
		//myCopy += '<a href="http://www.ens.co.za/index.asp?pageID=people/Base1.asp&iID=' + aLinks[i] + '" class="homePal">' + aNames[i] + '</a><br>';	
		myCopy += '<a href="people/people' + rmNo + '.asp?iID=' + aLinks[i] + '" class="homePal">' + aNames[i] + '</a> <b>&#187;</b><br>';	
	}
	peopleList.innerHTML = myCopy;
		
}






////////////////////////////////////////////
//function to delete all options:
function fnClearOptions(){
	peopleList.innerHTML = '<a href="javascript:fnDefaultText(&#39;orAll&#39;);" class="homePalBold">list all names</a>';
	peopleList.innerHTML += '<br><a href="javascript:fnDefaultText(&#39;desigs&#39;);" class="homePalBold">list by designation</a>';
}




////////////////////////////////////////////
function fnSelected(item){
	//alert(item);
	
	var temp = item.split("|");
	document.forms['form1'].elements['txt'].value = temp[0];
	
	//document.location = 'http://www.ens.co.za/index.asp?pageID=people/Base2.asp&iID=' + temp[1];//alert(temp[1]);
	var rmNo = Math.floor(Math.random()*2)+1;	
	//alert(rmNo);
	parent.location = 'people/people' + rmNo + '.asp&iID=' + temp[1];//alert(temp[1]);
	///////////////////////////////

	fnClearOptions();
	//document.forms['form1'].elements['select'].size = 1;

}


////////////////////////////////////////////
function fnClearText(){
	if(document.forms['form1'].elements['txt'].value == openText){
		document.forms['form1'].elements['txt'].value = "";
	}
}

var lastClicked;
function fnDefaultText(whence){
	//alert("whence = " + whence);
	lastClicked = whence;
	
	//////////////////////
	document.forms['form1'].elements['txt'].value = openText;

	//////////////////////
	if(whence == 'orAll'){
		fnList('all');	
	}else if(whence == 'firstNames'){
		fnList('firstNames');	
	}else if(whence == 'desigs'){
		fnListDesigs();	
	}
}
function fnBG(){
	peopleList.innerHTML = '&nbsp;<br>';

}


///////////////////////////////////////////////////
//function for onKeyPress:
function fnCallPage(e, whereTo) {

   var key;

	if(window.event){
		//alert('IE');
   	key = window.event.keyCode;     //IE
	}else{
		//alert('FF');
      key = e.which;     					//firefox
	}

	if(key == 13){
		//alert('enter key');
		fnSelected(whereTo);
	}else{
		//alert('not enter key');
	}

} 


