///////////////////////////////////////////////////////////////
// File Name: 	public.js
///////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////
// Shared javascript functions for public site
///////////////////////////////////////////////////////////////

// Shortcut for very common function
function getEl(id) {
	return document.getElementById(id);
}

/////////////////////////////////////
// popupWindows
/////////////////////////////////////
function popupWindow2(windowName,options) {
	try {
		eval("var " + windowName + " = window.open('','" + windowName + "','" + options + "')");
		eval(windowName + ".focus()");
	} catch (e) {}
	return eval(windowName);
}
function popupDialog(windowName,width,height,options) {
	if (options == null) options = "status=no,toolbar=no,menubar=no,location=no";
	var top = (screen.availHeight - height) / 2;
	var left = (screen.availWidth - width) / 2;
	options = "height=" + height + ",width=" + width + ",top=" + top + ",left=" + left + "," + options;
	return popupWindow2(windowName,options);
}

function xAddEvent(obj, name, handler) {
	if (obj.attachEvent) {
		obj.attachEvent("on" + name, handler);
	} else {
		obj.addEventListener(name, handler, false);
	}
}

function generate_address(username,hostname,name) {
	var atsign = "&#64;";
	var linktext = username + atsign + hostname;
	document.write("<a href=" + "mail" + "to:" + username + atsign + hostname + ">")
	if (name != null) {
		document.write(name)
	} else {
		document.write(username + atsign + hostname)
	}

	document.write("</a>")
}

function formatAsMoney(mnt) {
	mnt -= 0;
	mnt = (Math.round(mnt*100))/100;
	return (mnt == Math.floor(mnt)) ? mnt + '.00'
			  : ( (mnt*10 == Math.floor(mnt*10)) ?
					   mnt + '0' : mnt);
}

function selectValue(selectObj,value) {
	for (var i=0; i < selectObj.options.length; i++) {
		if (selectObj.options[i].value==value) {
			selectObj.selectedIndex = i;
			break;
		}
	}
}

// This is contained in the janus.js file as well but we don't want to have to include janus.js in the front end pages
// Validate extension for file name
// Checks to be sure that the file extension is in the given list.
// To use, set the following parameters in the CustomValidator:
// 	extensions - a comma separated list of valid extensions
function ValidateExtension(source, arguments) {
	success = true;

	var extensions = source.getAttribute("extensions").toLowerCase().split(",");
	var extension = "";

	if (arguments.Value != "") {
		if (arguments.Value.lastIndexOf(".") == -1) {
			success = false;
		} else {
			extension = arguments.Value.substring(arguments.Value.lastIndexOf("."),arguments.Value.length).toLowerCase();

			var foundExt = false;
			for (var i = 0; i < extensions.length; i ++) {
				if (extension == extensions[i]) {
					foundExt = true;
					break;
				}
			}
			if (!foundExt) {
				success = false;
			}
		}
	}

	arguments.IsValid = success;
}

