
var str = "";			// global search string variable
var foundindex = 0;
var keypressed = 0;

function newkeypress(e,opt) {
if (!e.which)
{
	e.which = event.keyCode;	// netscape uses e.which (e is the passed event)
}
//  IE doesn't, it uses event.keyCode - this line brings both browsers to the same result

key = String.fromCharCode(e.which);	// converts the keycode to the actual character

if (e.which == 47)		// "/" forwardslash - equal to Space Bar keypress
{
	key = " ";
}
if (e.which == 32)		// Space-bar : wildcard
{	
	key = ".*";
}
if (e.which == 92) 	// "\" backslash - resets the search string
{
	str = "";
	key = "";
}
if (e.which == 27)	// Esc, also resets the search string (won't work in Netscape)
{
	str = "";
	key = "";
}

if (e.which == 91) // "[" means search for the previous good match
{
	myRe = new RegExp (str, "i");
	for (i=(foundindex-1); i>0; i--)
	{
		if (myRe.test((opt[i].text)))
		{
			foundindex = i;
			
			break;
		}
	}
	e.which = 0;
	keypressed = 1;

}

if (e.which == 93)	//	] means search for the next good match
{
	myRe = new RegExp (str, "i");
	for (i=(foundindex+1); i<opt.length; i++)
	{
		if (myRe.test((opt[i].text)))
		{
			foundindex = i;
			
			break;
		}
	}
	e.which = 0;
	keypressed = 1;
}


if (e.which > 26 && e.which < 123)
{
str += key;				// adds the pressed key to the search string
keypressed = 1;

window.status = str;	// testing purpose step

myRe = new RegExp (str, "i");	// sets up a case-insensitive search

foundindex = 0;			
for (i=1; i<opt.length; i++) {	// loops through each element in the <select>

if (myRe.test((opt[i].text))) {	// tests for the search term in the text of the <option>
foundindex = i;					// if found, returns the index of this <option> 
break;					// stops at the first occurence of a found search string
}
}
}
}

function newkeyup(e) {	// used after the key has been pressed to counteract the browser's own crap seeking.
// window.status = str;
if (keypressed)			// needed else no key input resets the current value of the <option> to 0!
{
	e[foundindex].selected = true;	//   sets the selection to the index of the <option> that was located in the search loop
}
keypressed = 0;
}

function oldkeyup() {	// focus has been lost = right <option> found, so now must
str = "";				// reset the search string so there won't be relics for the
window.status = "";						// next <select>
foundindex = 0;
}

function putCookie() {	// sets the cookie full of the data in the main peal form, that would otherwise 
	var result = "";	// be lost when you submit a new ringer or tower etc in the side forms
	for ( var i = 0; i < document.pealsubmit.elements.length; i++)
	{
		result += document.pealsubmit.elements[i].value + "|#|";	// adds each input value in turn,
	}															// separated by a random separator |#|
	document.cookie = "myCookie=" + escape(result);		// sets the cookie, with dangerous characters converted
}

function resetCookie() {		// needed to blank the cookie values after a peal is submitted, so the next
	document.cookie = "myCookie=;";	// one is a clean slate
}

function getCookie() {		// decodes the cookie on page load, and fills the pealsubmit form with stored values
	var cookieString = document.cookie;		// nabs the cookie data
	var index = cookieString.indexOf("myCookie=");	// searches for the start of the cookie
	index = cookieString.indexOf("=", index) + 1;	// finds it
	var endstr = cookieString.indexOf(";", index);	// and searches for an end-of-string character, ";"

	if (endstr == -1)
	{
		endstr = cookieString.length;		// if not found, sets the end of the search to the end of string
	}

	splitstring = unescape(cookieString.substring(index,endstr)).split("|#|");	// splits up the separate values
	if (splitstring.length > 1)		// just tests in case there is no content to the cookie (it has been reset for example)
	{
		for ( var i=0; i < document.pealsubmit.elements.length ; i++)		// loops through each value it finds
		{
			document.pealsubmit.elements[i].value = splitstring[i];	// and sets the form input element to that value
		}
	}
}

function ClearAll() {
	document.inoptions.reset();




}
