/*	OSP Employee Directory Javascript
	Created by Ethan Trewhitt
	Fall 2006
 */

/* --- Global Variables --- */

var searchdelay = 250;			// number of milliseconds to delay after each key press

/* --- Global Variables --- */

var searchtimer;
var searchstring;

/* --- Events --- */

function searchType(obj) {
	if (!obj) return;
	if (searchtimer) clearTimeout(searchtimer);
	searchstring = obj.value;
	//searchNow();
	searchtimer = setTimeout("searchNow();", searchdelay);
}

function searchNow() {
	var foundmatch = false;

	if (!searchstring) {
		// Empty search string, so show all
		foundmatch = true;
		for (var i = 1; i < cards.length; i++) {
			showCard(i);
		}
	} else {
		// Search for the given text among all cards
		for (var i = 1; i < cards.length; i++) {
			if (cards[i].toLowerCase().search(searchstring.toLowerCase()) >= 0) {
				showCard(i);
				foundmatch = true;
			} else {
				hideCard(i);
			}
		}
	}
	var searchbox = gID("searchbox");
	if (foundmatch) {
		searchbox.className = "textinput";
	} else {
		// Turn the input box red to indicate no matches
		searchbox.className = "textinput nomatches";
	}
}

/* --- General Functions --- */

function showCard(id) {
	var obj = gID("card-" + id);
	obj.className = "card_visible";
}

function hideCard(id) {
	var obj = gID("card-" + id);
	obj.className = "card_hidden";
}

function gID(id, suppresserror) {
	var obj = document.getElementById(id);
	if (obj) {
		return obj;
	} else {
		if (!suppresserror) alert("Couldn't find ID '" + id + "'");
		return false;
	}
}

function getNumberFromID(id) {
	var dash = id.indexOf("-");
	if (dash == -1) return 0;
	return parseInt(id.substring(dash + 1));
}

function rot13(text) {
	var toreturn = "";
	var len = text.length;
	var chr, t;

	if (len > 0) {
		for(var i = 0; i < len; i++) {
			chr = text.charCodeAt(i);
			if (((chr > 64) && (chr < 78)) || ((chr > 96) && (chr < 110))) {
				chr = chr + 13;
			} else if (((chr > 77) && (chr < 91)) || ((chr > 109) && (chr < 123))) {
				chr = chr - 13;
			}
			t = String.fromCharCode(chr);
			toreturn += t;
		}
	}
	return toreturn;
}