var isDOM = (document.getElementById) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1);
var isIE  = (document.all) ? !isOpera : false;
var isMac = (navigator.appVersion.indexOf("Mac") != -1);
var isWinIE = isIE && !isMac;
var main_ViewPhoto = false;

function main_Init() {
	main_InitFormHovers();
	if (main_ViewPhoto) main_InitPhotoPage();
}
function main_InitFormHovers() {
	// Based on Niceforms implementation by Lucian Slatineanu
	// http://www.badboy.ro/articles/2005-07-23/index.php
	if (!isDOM) return;
	var inputs = document.getElementsByTagName("input");
	var textareas = document.getElementsByTagName("textarea");
	var fields = new Array();
	var buttons = new Array();
	var numFields = 0, numButtons = 0;
	var htmlOn, htmlOff, i;
	for (i = 0; i < inputs.length; i++)
		if (inputs[i].type == "text" || inputs[i].type == "password")
			fields[numFields++] = inputs[i];
		else if (inputs[i].type == "button" || inputs[i].type == "submit")
			buttons[numButtons++] = inputs[i];
	for (i = 0; i < textareas.length; i++)
		fields[numFields++] = textareas[i];
	for (i = 0; i < numFields; i++) {
		htmlOn = (fields[i].onfocus) ? fields[i].onfocus.toString().getFunctionBody() : "";
		htmlOff = (fields[i].onblur) ? fields[i].onblur.toString().getFunctionBody() : "";
		fields[i].onfocus = new Function("if (this.parentNode.className.indexOf(\"-hover\") == -1) this.parentNode.className += \"-hover\";" + htmlOn);
		fields[i].onblur = new Function("this.parentNode.className = this.parentNode.className.replace(\"-hover\", \"\");" + htmlOff);
	}
	for (i = 0; i < numButtons; i++) {
		htmlOn = (buttons[i].onmouseover) ? buttons[i].onmouseover.toString().getFunctionBody() : "";
		htmlOff = (buttons[i].onmouseout) ? buttons[i].onmouseout.toString().getFunctionBody() : "";
		buttons[i].onmouseover = new Function("if (this.className.indexOf(\"-hover\") == -1) this.className += \"-hover\";" + htmlOn);
		buttons[i].onmouseout = new Function("this.className = this.className.replace(\"-hover\", \"\");" + htmlOff);
	}
}
function main_InitPhotoPage() {
	var width = document.images[0].width + 50;
	var height = document.images[0].height + ((isIE) ? 125 : 145);
	if (width < 380) width = 380;
	if (height < 300) height = 300;
	if (height > screen.height - 50) height = screen.height - 50;
	window.resizeTo(width, height);
	if (width > 380)
		window.resizeBy(document.images[0].width - main_GetWindowWidth() + 30, 0);
	window.focus();
}
function main_ShowPhoto() {
	var args = main_ShowPhoto.arguments;
	var url = "viewphoto.aspx?member=" + args[0] + ((args.length > 1) ? ("&photo=" + args[1]) : "");
	var name = "photo_" + args[0];
	main_OpenWindow(url, name, 640, 480);
}
function main_OpenWindow(url, name, width, height) {
	var win = window.open(url, name, "top=10,left=15,width=" + width + ",height=" + height + ",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1");
	if (!win)
		alert("Please disable your pop-up blocker.");
	else
		win.focus();
}
function main_FocusField(id) {
	var field = (isDOM) ? document.getElementById(id) : (isIE) ? document.all[id] : null;
	if (!field) return;
	if (isDOM && field.parentNode.className.indexOf("-hover") == -1)
		field.parentNode.className += "-hover";
	field.focus();
	field.select();
}
function main_ClearLogin(form) {
	if (!form) return;
	if (form.txtEmail.value == "Email address")
		form.txtEmail.value = "";
	if (form.txtPassword.value == "Pa$$word")
		form.txtPassword.value = "";
}
function main_ResetLogin(form) {
	if (!form) return;
	if (form.txtEmail.value == "" && form.txtPassword.value == "") {
		form.txtEmail.value = "Email address";
		form.txtPassword.value = "Pa$$word";
	}
}
function main_ClearOptions(list) {
	if (!list[0].checked) return false;
	for (var i = 1; i < list.length; i++)
		list[i].checked = false;
	return true;
}
function main_LimitFieldSize(field, maxChars) {
	if (!field) return;
	if (field.value.length > maxChars)
		field.value = field.value.substring(0, maxChars);
}
function main_ConfirmDelete(item) {
	return confirm("Are you sure you want to delete " + item + "?");
}
function main_GoBack() {
	if (window.history.length > 1)
		window.history.back();
	else
		window.location = "./";
}
function main_HideElement(id) {
	var el = (isDOM) ? document.getElementById(id) : (isIE) ? document.all[id] : null;
	if (!el) return;
	el.style.display = "none";
}
function main_GetWindowWidth() {
	var width = 0;
	if (document.documentElement && document.documentElement.clientWidth)
		width = document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth)
		width = document.body.clientWidth;
	else if (window.innerWidth)
		width = window.innerWidth - 18;
	return width;
}
function main_GetWindowHeight() {
	var height = 0;
	if (document.documentElement && document.documentElement.clientHeight)
		height = document.documentElement.clientHeight;
	else if (document.body && document.body.clientHeight)
		height = document.body.clientHeight;
	else if (window.innerHeight)
		height = window.innerHeight - 18;
	return height;
}
String.prototype.getFunctionBody = function() {
	var str = this.toString();
	str = str.replace(/[^\{]+\{/, "");
	str = str.replace(/\n/gi, "");
	str = str.substring(0, str.length - 1);
	return str; 
}
window.onload = main_Init;

