
//search.js
//*************************************************************************************************************

//This file contains all of the functions utilized in database.html. The functions culminate to allow a database 
//to be searched and the results to be output to the database.html file. 

// Created By: Dustin Littau
// Copyright 2007
//*************************************************************************************************************

// global variables
//************************************************************************************************************* 
var xmlDoc; 		//stores the cemetey.xml file
var xmlDocLocation; 	//stores the location.xml file
var position; 		//position of where in database the search currently is
var input_test; 	//stores the users search input
var database_test; 	//stores the current name from the database
var found; 		//boolean which keeps track of whether a match is found with the search
var closest_match; 	//boolean which keeps track of whether the closest match box is checked
var end_output; 	//used for closest match, its the position of the last match for the search
var start_output; 	//used for closest match, its the position of the first match for the search
var death_date, birth_date, maiden_name, plot, spouse, children, relatives; //output variables 
//*************************************************************************************************************


// loads location.xml and cemetery.xml into there respective variables for use later. Sends an alert if the 
// script can not be handled
//*************************************************************************************************************
function initialize () {
	//code for IE
	if (window.ActiveXObject) {
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.load("cemetery.xml");
		xmlDocLocation=new ActiveXObject("Microsoft.XMLDOM");
		xmlDocLocation.async=false;
		xmlDocLocation.load("location.xml");
	//code for Firefox, Opera etc.
	} else if (document.implementation && document.implementation.createDocument) {
		xmlDoc=document.implementation.createDocument("","",null);
		xmlDoc.load("cemetery.xml");
		xmlDocLocation=document.implementation.createDocument("","",null);
		xmlDocLocation.load("location.xml");
	} else {
		alert('Your browser cannot handle this script');
	}	
}
//*************************************************************************************************************



// changes the status bar color
//*************************************************************************************************************
function status_color(color) {
	
	if (color == "green") {
		document.getElementById('status_one').bgColor = "green";
		document.getElementById('status_two').bgColor = "green";
		document.getElementById('status_three').bgColor = "green";
		document.getElementById('status_four').bgColor = "green";
		document.getElementById('status_five').bgColor = "green";
	}
	
	if (color == "red") {
		document.getElementById('status_one').bgColor = "red";
		document.getElementById('status_two').bgColor = "red";
		document.getElementById('status_three').bgColor = "red";
		document.getElementById('status_four').bgColor = "red";
		document.getElementById('status_five').bgColor = "red";
	}

	if (color == "yellow") {
		document.getElementById('status_one').bgColor = "yellow";
		document.getElementById('status_two').bgColor = "yellow";
		document.getElementById('status_three').bgColor = "yellow";
		document.getElementById('status_four').bgColor = "yellow";
		document.getElementById('status_five').bgColor = "yellow";
	} 

}
//*************************************************************************************************************


// sets default values before entering the updater() method. it uses the location.xml file to set the position 
// variable. If open search is set then the values for the starting and ending location of matches are also set.
//*************************************************************************************************************
function manager() {

	// function variables 
	var output_green;
	var first_char;
	var letter, still_letters, letters_matched, greatest_match; 
	

	// initialization of function and global variable
	closest_match = false;
	found = false;
	output_green = true;
	input_test = document.search.family_name.value.toUpperCase();
	first_char = input_test.charAt(0);
	start_output = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells").length + 1;
	end_output = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells").length + 1;

	//checks to see if the checkbox is checked. 
	if (document.search.closest_match.checked) {
		closest_match = true;
	}


	// set position variable based on first letter of user input	
	if (first_char == "A") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("a")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "B") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("b")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "C") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("c")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "D") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("d")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "E") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("e")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "F") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("f")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "G") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("g")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "H") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("h")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "I") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("i")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "J") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("j")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "K") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("k")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "L") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("l")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "M") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("m")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "N") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("n")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "O") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("o")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "P") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("p")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "Q") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("q")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "R") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("r")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "S") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("s")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "T") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("t")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "U") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("u")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "T") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("t")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "V") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("v")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "W") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("w")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "X") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("x")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "Y") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("y")[0].childNodes[0].nodeValue;
		position--;
	} else if (first_char == "Z") {
		position = xmlDocLocation.getElementsByTagName("Location")[0].getElementsByTagName("z")[0].childNodes[0].nodeValue;
		position--;
	} else {
		position  = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells").length;
		found = true;  //setting found = true avoids output message of not being found twice
		output_green = false;
		status_color("yellow");
		alert("Make sure your search begins with a character A-Z and try again. Do not leave a space for a first character.");
		timerId = setTimeout('updater("next")', 1000);
	}

	
	//output green status bar to show searching
	if (output_green) { 
		status_color("green");
		timerId = setTimeout('updater("next")', 1000);
	}

	//find range of matches in the database if it is an open search
	if (closest_match) {
		greatest_match = 0;
		for(x = (position + 1); x < xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells").length; x++) {
			database_test = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].getElementsByTagName							("Last_x0020_Name")[0].childNodes[0].nodeValue.toUpperCase();  
			
			letter = 0;
			still_letters = true;
			letters_matched = 0;
			while (still_letters) {
				if(database_test.charAt(letter) == input_test.charAt(letter)) {
					letter++;
					letters_matched++;
					if (letters_matched == input_test.length) {
						still_letters = false;
					}	
				} else {
					still_letters = false;
				}
							
			} 
			
			if (input_test === database_test) {
				position = x - 1;
				closest_match = false;
				return;
			}
			
			if (letters_matched >= greatest_match) {
				if (letters_matched == greatest_match) {
					end_output = x;	
				} else {
					end_output = x; //set start and finsh to the same spot
					start_output = x;
				}
				greatest_match = letters_matched;

			} else {
				break; //went to far in the database
			}
		}
		position = start_output - 1;
	}


}
//*************************************************************************************************************



//called from the Next and Prev buttons, used to tell updater() which direction to go through the database
//*************************************************************************************************************
function direction (x) {
	if (x == "next") {
		updater("next");
	} else {
		updater("prev");
	}
}
//*************************************************************************************************************




// used to initialize the output for the information at database position x
//*************************************************************************************************************
function init_output (x) {

	// function variables
	var y, z;
	
	// intialize output variables
	death_date = " ";
	birth_date = " ";
	maiden_name = " ";
	plot = " ";
	spouse = " ";
	children = " ";
	relatives = " ";
		
	// checks for tag to exist and if found then puts the information in the appropriate variable		
	for (y = 0; y < xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].childNodes.length; y++) {
		z = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].childNodes[y].nodeName;
		if (z === "Death_x0020_Date") {
			death_date = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].getElementsByTagName							("Death_x0020_Date")[0].childNodes[0].nodeValue;
		} else if (z === "Birth_x0020_Date") {
			birth_date = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].getElementsByTagName							("Birth_x0020_Date")[0].childNodes[0].nodeValue;
		} else if (z === "Madein_x0020_Name") {
			maiden_name = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].getElementsByTagName							("Madein_x0020_Name")[0].childNodes[0].nodeValue;
		} else if (z === "Plot_x0020__x0023_") {
			plot = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].getElementsByTagName								("Plot_x0020__x0023_")[0].childNodes[0].nodeValue;
		} else if (z === "Spouse") {
			spouse = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].getElementsByTagName							("Spouse")[0].childNodes[0].nodeValue;
		} else if (z === "Children") {
			children = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].getElementsByTagName							("Children")[0].childNodes[0].nodeValue;
		} else if (z === "Known_x0020_Relitives") {
			relatives = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].getElementsByTagName							("Known_x0020_Relitives")[0].childNodes[0].nodeValue;
		} else {

		}	
	}
}
//*************************************************************************************************************



// outputs the database at a particular position x
//*************************************************************************************************************
function output (x) {

	document.result.last_name.value = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")							[x].getElementsByTagName("Last_x0020_Name")[0].childNodes[0].nodeValue;
	document.result.first_name.value = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")							[x].getElementsByTagName("First_x0020_Name")[0].childNodes[0].nodeValue;
	document.result.death_date.value = death_date;
	document.result.birth_date.value = birth_date;
	document.result.maiden_name.value = maiden_name;
	document.result.plot.value = plot;
	document.result.spouse.value = spouse;
	document.result.children.value = children;
	document.result.relatives.value = relatives;

}
//*************************************************************************************************************



// fills in the blank fields with the info for the person. checks that tags exists and if tag exists then retrieves 
// info from XML file and places it in the appropriate field of the form in the database.html file
//*************************************************************************************************************
function updater (direction) {

	// function variables
	var x;
	
	if (closest_match == false) {
		// Next button was pressed, count forward and search through database, output if match found and there is a death_date 
		if (direction == "next") {
		
			//special case for last entry
			if (position == (xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells").length - 1)) {
				alert("This is the last person on record. Press Prev to continue searching.");
			}
		
			for(x = (position + 1); x < xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells").length; x++) {
			
				database_test = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].getElementsByTagName							("Last_x0020_Name")[0].childNodes[0].nodeValue.toUpperCase();
			
				if (database_test === input_test) {
					init_output(x);
					if (death_date != " ") {
						status_color("red");
						output(x);
						found = true; 
						position = x;
						return;
					}
				
				}

				if ((found === true) && ((input_test != database_test) || (x == xmlDoc.getElementsByTagName									("Grave_x005F_x0020_Yard_x005F_x0020_Cells").length))) {
					alert("This is the last person on record. Press Prev to continue searching.");
					break;
				}


				if(input_test.charAt(0) < database_test.charAt(0)) {
					break;
				}
			}
		// Prev button was pressed, count back and search through database, output if match found and there is a death_date	
		} else {

			//special case for first entry
			if (position == 0) {
				alert("This is the first person on record. Press Next to continue searching.");
			}
		
			for(x = (position-1); x >= 0; x--) {
				database_test = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].getElementsByTagName							("Last_x0020_Name")[0].childNodes[0].nodeValue.toUpperCase();

				if (database_test === input_test) {
					init_output(x);
					if (death_date != " ") {
						status_color("red");
						output(x);
						found = true; 
						position = x;
						return;
					}
				}

				if ((found === true) && ((input_test != database_test) || (x < 0))) {
					alert("This is the first person on record. Press Next to continue searching.");
					break;
				}

				if(input_test.charAt(0) < database_test.charAt(0)) {
					break;
				}
			}
		}

		if (found === false) {
			status_color("yellow");
			alert("No record could be found for this family name. Check the spelling of the name or use 'closest match' and try again.");
		}

	// closest match search 
	} else {
		// Next button was pressed, count forward and search through database, output if match found and there is a death_date
		if (direction == "next") {

			//special case for last entry
			if (position == (xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells").length - 1)) {
				alert("This is the last person on record. Press Prev to continue searching.");
			}

			for(x = (position + 1); x < xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells").length; x++) {  
			
				if ((position == end_output) && (found === true)) {
					alert("This is the last person on record. Press Prev to continue searching.");
					break;
				} 
		
				init_output(x);
				if (death_date != " ") {	
					status_color("red");
					output(x);
					found = true; 
					position = x;
					return;
				}

				if ((death_date == " ") && ((position+1) == end_output) && (found === true)) {
					position = x;
				} 
	
			
			}

		// Prev button was pressed, count back and search through database, output if match found and there is a death_date
		} else {

			//special case for first entry
			if (position == 0) {
				alert("This is the first person on record. Press Next to continue searching.");
			}
	
			for(x = (position-1); x >= 0; x--) {
				database_test = xmlDoc.getElementsByTagName("Grave_x005F_x0020_Yard_x005F_x0020_Cells")[x].getElementsByTagName								("Last_x0020_Name")[0].childNodes[0].nodeValue.toUpperCase();
				if ((position == start_output) && (found == true)) {
					alert("This is the first person on record. Press Next to continue searching.");
					break;
				}

				init_output(x);
				if (death_date != " ") {	
					status_color("red");
					output(x);
					found = true; 
					position = x;
					return;
				}

				if ((death_date == " ") && ((position-1) == start_output) && (found === true)) {
					position = x;
				} 
	
			}
			
		}
	
		if (found === false) {
			status_color("yellow");
			alert("No records match or are close to your search criteria. Contact the cemetery for more information.");
  		}

	} //end of closest match
 
}
//*************************************************************************************************************
