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;

function main_Init() {
	main_InitFormHovers();
	main_InitSiteLinks();
}
function main_InitFormHovers() {
	// Based on Niceforms implementation by Lucian Slatineanu
	// http://www.emblematiq.com/projects/niceforms/
	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/i, \"\");" + 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/i, \"\");" + htmlOff);
	}
}
function main_InitSiteLinks() {
	if (!isDOM) return;
	var links = document.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++)
		if (links[i].id && links[i].id.indexOf("site") != -1)
			links[i].target = "_blank";
}
function main_OpenWindow() {
	var args = main_OpenWindow.arguments;
	var url = (args[0]) ? args[0] : "./";
	var name = (args[1]) ? args[1] : "window";
	var width = (args[2] != null) ? args[2] : 800;
	var height = (args[3] != null) ? args[3] : 600;
	var centered = (args[4]) ? true : false;
	var scrollbars = (args[5] != null) ? args[5] : 1;
	var resizable = (args[6] != null) ? args[6] : 1;
	var top = (centered) ? Math.floor((screen.height - height - 80) / 2) : 10;
	var left = (centered) ? Math.floor((screen.width - width) / 2) : 15;
	var win = window.open(url, name, "top=" + top + ",left=" + left + ",width=" + width + ",height=" + height + ",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=" + scrollbars + ",resizable=" + resizable);
	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(name) {
	return confirm("Are you sure you want to delete " + name + "?");
}
function main_ConfirmEmpty(name) {
	return confirm("Are you sure you want to empty your " + name + " folder?");
}
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;
}
function main_GetTimestamp() {
	return parseInt(new Date().getTime() / 1000);
}
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; 
}
if (window.opener) {
	if (window.location.href.toLowerCase().indexOf("window=close") != -1) {
		window.opener.location = window.location.href.replace(/[?&]window=close/i, "");
		window.close();
		window.opener.focus();
	}
}
window.onload = main_Init;
