if (navigator.appName.toLowerCase() == "netscape")
{
	Window.prototype.attachEvent = function(sType, fHandler)
	{
		var shortTypeName = sType.replace(/on/, "");
		this.addEventListener(shortTypeName, function(e) { window.event = e; return fHandler(); }, false);
	}
}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g, "");
}

function writeHTML(html)
{
	document.write(html);
}

function printDocument()
{
	var filename = /\/([^\/]+$)/.exec(location.pathname);
	if (filename != null && filename.length > 1 && filename[1].substring(0, 12) == "content.aspx")
		openWindow("print.aspx" + location.search, 800, 600);
	else
		window.print();
}

function toggleComment(o)
{
	o = o.parentNode.parentNode.nextSibling;
	if (o.style.display == "")
		o.style.display = "none";
	else
		o.style.display = "";
}

function showTooltip(text, src, e)
{
	try
	{
		if (src.tooltip)
			return;
		var isIE = document.all ? true : false;
		var container = document.createElement("DIV");
		var rtl = (isIE ? src.currentStyle.direction : document.defaultView.getComputedStyle(src, null).direction).toLowerCase() == "rtl";
		var y;
		container.id = "t" + (new Date()).valueOf();
		container.sourceElement = src;
		container.className = "Tooltip";
		if (document.body.offsetHeight - e.clientY < 50)
			container.style.top = e.clientY + document.body.scrollTop + src.offsetHeight - (isIE ? e.offsetY : 0) - 40;
		else
			container.style.top = e.clientY + document.body.scrollTop + src.offsetHeight - (isIE ? e.offsetY : 0) + 10;
		if (document.body.offsetWidth - e.clientX < 200)
			y = e.clientX - 200 - (isIE ? e.offsetX : 0);
		else
			y = e.clientX - (isIE ? e.offsetX : 0);
		if (rtl)
			container.style.right = document.body.offsetWidth - y;
		else
			container.style.left = y;
		container.innerHTML = text;
		container.hide = function()
		{
			this.sourceElement.tooltip = null;
			document.body.removeChild(this);
		}
		src.tooltip = container;
		document.body.appendChild(container);
		src.onmouseout = function ()
		{
			if (this.tooltip)
				this.tooltip.hide();
		}
	}
	catch (e)
	{
		src.title = text;
	}
}

function openWindow(url, width, height, name, title)
{
	if (!name)
		name = (new Date()).valueOf();
	if (typeof(url) == "object")
	{
		if (!title)
		{
			if (url.title)
				title = url.title;
			else if (url.firstChild && url.firstChild.title)
				title = url.firstChild.title;
		}
		url = url.href;
	}
	else if (typeof(url) == "number")
		url = "content.aspx?id=" + url;
	var top, left, i;
	if (!width)
	{
		width = screen.availWidth - 10;
		left = 0;
	}
	else
		left = (screen.availWidth - width) / 2;
	if (!height || screen.availHeight - height < 0)
	{
		height = screen.availHeight - 30;
		top = 0;
	}
	else
	{
		top = (screen.availHeight - height) / 2;
		if (height > screen.availHeight)
			height = screen.availHeight;
	}
	var options = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width + ",resize=no,scrollbars=auto,toolbar=no,location=no,menubar=no,status=no";
	if (popupWindow)
	{
		popupWindow.close();
		popupWindow = null;
	}
	if (/\.(gif|jpg|jpeg|png)$/i.test(url))
	{
		try
		{
			var body, img;
			popupWindow = window.open("", name, options);
			body = popupWindow.document.body;
			body.style.margin = "0px";
			body.style.textAlign = "center";
			body.scroll = "auto";
			body.onkeypress = function ()
			{
				if (this.document.parentWindow.event.keyCode == 27)
					this.document.parentWindow.close();
			}
			url = url.split("|");
			for (i=0; i<url.length; i++)
			{
				if (!/^[a-z]+:\/\//i.test(url[i]))
				{
					if (url[i].substring(0, 1) == "/")
						url[i] = location.protocol + location.port + "//" + location.hostname + url[i];
					else
						url[i] = location.protocol + location.port + "//" + location.hostname + /(.*\/)/.exec(location.pathname)[0] + url[i];
				}
				body.appendChild(img = popupWindow.document.createElement("IMG"));
				img.src = url[i];
			}
			if (title)
				popupWindow.document.title = title;
		}
		catch (e)
		{
			popupWindow = window.open(url, name, options);
		}
	}
	else
		popupWindow = window.open(url, name, options);
	return popupWindow != null;
}
var popupWindow = null;

/*
	Validate a form's elements according to varius attributes
*/
function validateForm(form, title, submit)
{
	var i, j, u, sum;
	var input;
	var valid;
	var sErrorMsg = "";

	for (i=0; i<form.elements.length; i++)
	{
		input = form.elements[i];
		// skip input when it's not rendered (ie. parent display:none)
		if (input.offsetHeight == 0) continue;
		valid = true;

		// Validate value according to element type and validation type
		switch (input.type.toLowerCase())
		{
		case "text":
		case "password":
		case "textarea":
		case "file":
			if (!input.getAttribute("validation")) continue;
			if (input.getAttribute("mandatory"))
			{
				if (input.getAttribute("mandatory").toLowerCase() != "true" && input.value.length == 0)
					continue;
			}
			else
				if (input.value.length == 0)
					continue;
			switch (input.getAttribute("validation").toLowerCase())
			{
			case "string":
			case "password":
				if (input.value.length == 0)
					valid = false;
				break;
			case "integer":
				if (!/\d+/.test(input.value))
					valid = false;
				break;
			case "email":
				if (!/^[\w\.\-]+@[\w\-]+(\.[\w\-]+)+$/.test(input.value))
					valid = false;
				break;
			case "phone":
				if (!/^\+?\d+(-\d+)*$/.test(input.value))
					valid = false;
				break;
			case "id":
				j = input.value.toString();
				input.value = j.replace(/\D/g, "");
				if (/\d+/.test(input.value))
				{
					sum = 0;
					for (j=0; j<input.value.length; j++)
					{
						u = (j % 2 ? 2 : 1) * parseInt(input.value.charAt(input.value.length - j - 1));
						sum += u > 9 ? Math.floor(u / 10) + u % 10 : u;
					}
					if (sum % 10) valid = false;
				}
				else
					valid = false;
				break;
			case "compare":
				// Check the compareInput attribute
				if (input.getAttribute("compareInput"))
					if (input.value != form.elements[input.getAttribute("compareInput")].value)
						valid = false;
			}

			// Validate max and min according to validation type
			if (valid == true)
			{
				switch (input.getAttribute("validation").toLowerCase())
				{
				case "integer":
					if (input.getAttribute("validmax"))
					{
						if (parseInt(input.value) > parseInt(input.getAttribute("validmax")))
							valid = false;
					}
					if (input.getAttribute("validmin"))
					{
						if (parseInt(input.value) < parseInt(input.getAttribute("validmin")))
							valid = false;
					}
					break;
				default:
					if (input.getAttribute("validmax"))
					{
						if (input.value.length > input.getAttribute("validmax")) valid = false;
					}
					if (input.getAttribute("validmin"))
					{
						if (input.value.length < input.getAttribute("validmin")) valid = false;
					}
					break;
				}
			}
			break;
		case "select-one":
			if (input.getAttribute("mandatory"))
				if (input.selectedIndex == 0)
					valid = false;
			break;
		case "select-multiple":
			if (input.getAttribute("mandatory"))
			{
				sum = 0;
				for (j=0; j<input.options.length; j++)
				{
					if (input.options[j].selected) sum++;
				}
				if ((!input.getAttribute("validmax")) && (!input.getAttribute("validmin")))
				{
					if (sum == 0) valid = false;
				}
				else
				{
					if (input.getAttribute("validmax"))
					{
						if (sum > input.getAttribute("validmax")) valid = false;
					}
					if (input.getAttribute("validmin"))
					{
						if (sum < input.getAttribute("validmin")) valid = false;
					}
				}
			}
			break;
		case "checkbox":
			if (input.getAttribute("mandatory"))
				if (!input.checked)
					valid = false;
			break;
		}

		if (!valid)
		{
			if (input.getAttribute("validationError"))
				sErrorMsg += input.getAttribute("validationError") + "\n";
			else
				sErrorMsg += "Error in field " + input.name + "\n";
		}
	}
	if (sErrorMsg.length)
	{
		alert((title ? title + "\n" : "") + sErrorMsg);
		return false;
	}
	else
	{
		if (submit)
			form.submit();
		return true;
	}
}
