
/**
 * @author: Rahmin Pavlovic
 * @date: 1999
 * @version: 2010
 *
 */

function placeFocus(obj) {
	if(window.focus)obj.focus();
};

function isBlank(s) {
	if(s.length==0)return true;
	for(var i=0; i<s.length; i++) {
		if(s.charAt(i)!=" ")return false;
	}
	return true;
};

function setCursor(field,value) {
	if(field.value==value) {
		field.value="";
	}
};

function checkCursor(field,value) {
	if(isBlank(field.value)) {
		field.value=value;
	}
};

// Global temp variable
var keyWords = "";

// Setup accepted filename chars
var accepts = " abcdefghijklmnopqrstuvwxyz&._+-/1234567890";

// Handle Keystrokes
function keyPress(e) {

	// Handle cross-browser DOMs
	keycode = (window.event) ? window.event.keyCode : e.which;
	realkey = String.fromCharCode(keycode);
	keyWords = document.geography[this.name].value;
	field = this.name.substring(0, this.name.indexOf('_'));

	// Test for Backspace (NS only)
	if (window.Event && keycode == 8) {
		backSpace(field);
		return;
	}

	// Capture Delete button
	if (keycode == 46) {
		setDropDown('', field);
		return;
	}

	// Only run program if accepted characters entered
	if (accepts.indexOf(realkey.toLowerCase())!=-1) {
		setDropDown(realkey, field);
	}
};

// Populate drop-down as user types
function setDropDown(key, field) {
	indx = 0;
	txt = document.geography[field + '_searchStr'];
	file = document.geography[field];

	// Clear all existing entries
	clearEntries(field);

	// Check to see if what's being typed matches any of the entries
	for (var i=0; i<menu[field].length; i++)	{
		if (menu[field][i][1].toLowerCase().indexOf(keyWords.toLowerCase())!=-1) {
			file.options[indx] = new Option(menu[field][i][1], menu[field][i][0]);
			indx++
		}
	}
	
	// Need to add: Check to see if what's being typed matches any entries in 'Your List'. If so, add the attribute 'disabled'


	// No results found
	if(indx==0) {
		//alert('No results found for "'+keyWords+'".');
		//resetForm(field);
	}
};

// match the drop-down with whatever is in the field
function matchDropDown(field) {
	indx = 0;
	file = document.geography[field];
	searchStr = document.geography[field + '_searchStr'];
	searchStr.value = searchStr.value.trim();

	// Make there is something in the field to be tested
	if (!isBlank(searchStr.value)) {

		// Clear All Entries
		clearEntries();

		// Display entries matching what's in the field
		for (var i=0; i<menu[field].length; i++)	{
			if(menu[field][i][1].toLowerCase().indexOf(searchStr.value.toLowerCase())!=-1) {
				file.options[indx] = new Option(menu[field][i][1], menu[field][i][0]);
				indx++;
			}
		}

		// Store current keywords
		keyWords = searchStr.value;
	}
	else {
		// Make sure there are no blanks
		searchStr.value = "";

		// Clear stored keywords
		keyWords = "";
	}
};

// Handle Backspaces (NS Only)
function backSpace(field) {
	searchStr = document.geography[field + '_searchStr'];
	keyWords = searchStr.value.trim();

	if (searchStr.value.length>=1) {

		// Check the drop-down for matches
		matchDropDown(field);
	}
	else {

		// Last character, reset form
		resetForm(field);
	}
};

// Method to empty existing matched entries
function clearEntries(field) {
	if(document.geography[field]) {
		document.geography[field].options.length=0;
	}
};

// Restting the Form
function resetForm(field) {
	form=document.geography;

	// Clear All Entries
	clearEntries(field);

	document.getElementById('select_to').options.length=0;

	// Re-populate the form
	for (var i=0; i<menu[field].length; i++) {
		form[field].options[i] = new Option(menu[field][i][1], menu[field][i][0]);
	}

	// Clear stored keywords
	keyWords = "";

	// Set default search text
	form[field + '_searchStr'].value = form[field + '_search_text'].value;
};

// method to capture only the events we want
function trapEvents(f) {
	// Initialize the Keystroke Capturing
	if (window.Event) {
		document.captureEvents(Event.KEYUP);
	}
	document.getElementById('id_'+f+'_searchStr').onkeyup = keyPress;
};

function menuTrap() {
	for (var f in menu) {
		resetForm(f);
		trapEvents(f);
	}
};

function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
	{
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined')
	{
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined')
	{
		window.attachEvent('onload', fn);	
	}
	else
	{
		var oldfn = window.onload;
		if (typeof window.onload != 'function')
		{
			window.onload = fn;
		}
		else
		{
			window.onload = function() {
       			oldfn();
       			fn();
     		};
		}
 	}
}

addLoadListener(menuTrap);

