﻿var common = new Common();

function Common()
{
	this.submitted = false;
	this.scrollToElementId = null;
	//this.onDocumentLoaded = "";
	
	this.setScrollToElementId = function(id)
	{
		this.scrollToElementId = id;
	}

	this.initPage = function()
	{
		//if (this.onDocumentLoaded != "") eval(this.onDocumentLoaded);

		setTimeout("common.initScrollPos(); common.initFocus();", 50);
	}
	
	this.initScrollPos = function()
	{
		if (this.scrollToElementId != null)
		{
			var el = document.getElementById(this.scrollToElementId);
			if (el != null && el.scrollIntoView != null) el.scrollIntoView(true);
		}
	}
	
	this.initFocus = function()
	{
		var field = this.getDefaultField();
		if (field != null && //(field.value == null || field.value == "") &&
			(document.activeElement != null && document.activeElement.form == null))
		{
			try { field.focus() }
			catch (ex) {}
		}
	}
	
	this.getDefaultField = function()
	{
		var form;
		if (document.forms.length != 0)
		{
			form = document.forms[0];
			if (form.elements.length != 0)
			{
				var fields = form.elements;
				var count = fields.length;
				var field, fieldType;
				for (var i=0; i<count; i++)
				{
					field = fields[i];
					fieldType = field.type;
					if (fieldType != null && this.isTextField(field) && !field.disabled && fieldType != "hidden" && field.style.display != "none" && field.className != "Fine")
						return field;
				}
			}
		}
		return null;
	}
	
	this.isTextField = function(field)
	{
		if (field == null) return false;
		var fieldType = field.type;
		return (fieldType != null && (fieldType.indexOf("text") != -1 || fieldType.indexOf("password") != -1));
	}
	
	this.submitOnce = function(targetElement)
	{
		this.submitted = true;
		var fields = targetElement.form.elements;
		var count = fields.length;
		var field, fieldType;
		for (var i=0; i<count; i++)
		{
			var field = fields[i];
			var fieldType = field.type;
			if (fieldType == "submit") field.disabled = true;
		}
		__doPostBack(targetElement.name, "");
	}
	
	this.highlightColor = "#ffdd00";
	this.normalColor = null;
	
	this.highlight = function(element)
	{
		if (!this.submitted)
		{
			var row = element;
			while (row != null && row.tagName != "TR") row = row.parentElement;
			if (row != null)
			{
				this.normalColor = row.style.backgroundColor;
				row.style.backgroundColor = this.highlightColor;
			}
		}
	}
	
	this.unhighlight = function(element)
	{
		if (!this.submitted)
		{
			var row = element;
			if (row != null)// && row.style.backgroundColor == this.highlightColor)
			{
				row.style.backgroundColor = this.normalColor;
			}
		}
	}
	
	this.popupWin = null;
	this.popupEl = null;
	this.popupWidth = 300;
	
	this.showHelp = function(source, targetId, content)
	{
		if (content != null && content != "")
		{
			//var target = document.getElementById(targetId); // not doing anything with target now
			content = unescape(content);
			var el = this.popupEl;
			if (window.createPopup != null)
			{
				if (el == null)
				{
					el = this.popupEl = document.createElement('div');
					el.style.position = "absolute";
					el.style.width = this.popupWidth + "px";
					el.style.display = "none";
					document.body.appendChild(el);
					this.popupWin = window.createPopup();
				}
				else this.hideHelp();
				
				el.innerHTML = content;
				el.style.display = "block";
				var rect = this.getBoundingClientRect(el);
				el.style.display = "none";

				rect = this.getHelpPopupRect(source, this.popupWidth, rect.height);
				this.popupWin.document.body.innerHTML = content;
				this.popupWin.show(rect.left, rect.top, rect.width, rect.height, document.body);
			}
			else
			{
				if (el == null)
				{
					el = this.popupEl = document.createElement('div');
					el.style.position = "absolute";
					el.style.width = this.popupWidth + "px";
					el.style.display = "none";
					document.body.appendChild(el);
				}
				else this.hideHelp();
				
				el.innerHTML = content;
				el.style.display = "block";
				var height = el.offsetHeight;
				el.style.display = "none";
				
				var rect = this.getHelpPopupRect(source, this.popupWidth, height);
				el.style.left = rect.left + "px";
				el.style.top = rect.top + "px";
				el.style.display = "block";
			}
		}
	}
	
	this.getHelpPopupRect = function(source, width, height)
	{
		var scrollLeft, scrollTop, clientWidth;
		if (document.compatMode && document.compatMode != 'BackCompat')
		{
			scrollLeft = document.documentElement.scrollLeft;
			scrollTop = document.documentElement.scrollTop;
			clientWidth = document.documentElement.clientWidth;
		}
		else
		{
			scrollLeft = document.body.scrollLeft;
			scrollTop = document.body.scrollTop;
			clientWidth = document.body.clientWidth;
		}
		var rect = this.getBoundingClientRect(source);
		var left = rect.left;
		if (left + width > scrollLeft + clientWidth) left = rect.right - width;
		var top = rect.top - height - 4;
		if (top < scrollTop) top = rect.bottom + 4;
		return new Rect(left, top, width, height);
	}
	
	this.hideHelp = function()
	{
		if (this.popupWin != null) this.popupWin.hide(); // IE
		else if (this.popupEl != null) this.popupEl.style.display = "none"; // non-IE
	}
	
	this.openWin = function(url,winName,width,height,left,top)
	{
		left = (left ? left : 20);
		top = (top ? top : 20);
		width = (width ? width : 600);
		height = (height ? height : 550);
		var win = window.open(url, winName, "left=" + left + ",top=" + top + ",width=" + width + ",height=" + height + ",resizable,scrollbars"); 
		if (win.focus) win.focus();
	}
	
	this.getBoundingClientRect = function(el)
	{
		var left = (el.offsetLeft ? el.offsetLeft : 0);
		var top = (el.offsetTop ? el.offsetTop : 0);
		var parentEl = el.offsetParent;
		while (parentEl)
		{
			left += (parentEl.offsetLeft ? parentEl.offsetLeft : 0);
			top += (parentEl.offsetTop ? parentEl.offsetTop : 0);
			parentEl = parentEl.offsetParent;
		}
		return new Rect(left, top, el.offsetWidth, el.offsetHeight);
	}
	
	/// Shows properties for debugging
	this.showProperties = function(obj)
	{
		var props = "", val;
		for (var prop in obj)
		{
			try
			{
			if (typeof prop == "string")
			{
				val = obj[prop];
				props += (props == "" ? "" : "; ") + prop + " = " + val;
			}
			}
			catch (ex) { }
		}
		alert("Properties: " + props);
	}
}

function Rect(left,top,width,height)
{
	this.left = left;
	this.top = top;
	this.width = width;
	this.height = height;
	this.right = left + width;
	this.bottom = top + height;
}