﻿$I = function (id) { return document.getElementById(id); }
$N = function (name) { return document.getElementsByName(name); }
$T = function (tag, element)
{
	if (typeof element == 'undefined') element = window.document;
	return document.getElementsByTagName(tag); 
}
$C = function (className, element)
{
	if (typeof element == 'undefined') element = window.document;
	if (typeof element.getElementsByClassName == "function") return element.getElementsByClassName(className);

	var els = $T('*', element);
	var result = [];

	for (var i = 0; i < els.length; i++) {
		if (els[i].className == className || els[i].className.indexOf(' ' + className + ' ') != -1) {
			result.push(els[i]);
		}
	}

	return result;
}

$new = function (element) { return document.createElement(element); }

function toggle(el, show)
{
	if (typeof (el) == "string")
	{
		el = $I(el);
	}

	el.style.visibility = show ? 'visible' : 'hidden';
	el.style.display = show ? 'block' : 'none';
}


var isIE = navigator.appName.indexOf("Microsoft") != -1;
var isIE6 = false;

if (isIE)
{
	maxVersion = 0;
	reg = /MSIE\s*(\d+)/gi;
	while ((version = reg.exec(navigator.appVersion)) != null)
	{
		maxVersion = Math.max(parseInt(version[1]), maxVersion);
	}
	isIE6 = maxVersion <= 6;
}

var isFF2 = /firefox\/[1-2]/.test(navigator.userAgent.toLowerCase());

window.Request =
{
	create: function ()
	{
		try
		{
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e1)
			{

			}
		}

		if (!this.xmlhttp && typeof XMLHttpRequest != 'undefined')
		{
			this.xmlhttp = new XMLHttpRequest();
		}
	},

	get: function (url, params, async, func)
	{
		return this.req('GET', url, params, async, func);
	},

	post: function (url, params, async, func)
	{
		return this.req('POST', url, params, async, func);
	},

	req: function (method, url, params, async, func)
	{
		var that = this;

		if (url == '')
		{
			url = location.pathname;
		}
		if (!params)
		{
			params = null;
		}

		var answer;

		this.create();

		if (async == null)
		{
			async = true;
		}

		this.xmlhttp.open(method, url, async);

		if (method == "POST")
		{
			this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
		}
		if (async)
		{
			this.xmlhttp.onreadystatechange = function ()
			{
				if (that.xmlhttp.readyState == 4)
				{
					if (that.xmlhttp.status == 200)
					{
						if (func)
						{
							return func(that.xmlhttp);
						}
					}
				}
			};

			this.xmlhttp.send(params);
		}
		else
		{
			this.xmlhttp.send(params);

			if (this.xmlhttp.status == 200)
			{
				if (func)
				{
					return func(that.xmlhttp);
				}
			}
		}

		return this.xmlhttp;
	},

	load: function (url, params, async)
	{
		return this.get(url, params, async).responseText;
	},

	ask: function (url, params, async)
	{
		return this.post(url, params, async).responseText;
	},

	getValue: function(getKey, getName, async, func)
	{
		return this.post('/Controls/Utility/Get.aspx', "getKey=" + encodeURIComponent(getKey) + "&getName=" + encodeURIComponent(getName), async, func);
	},

	postAction: function (postKey, el, postName, urlName, isRefresh)
	{
		if (typeof (el) == "string")
		{
			el = $I(el);
		}

		if (!urlName)
		{
			urlName = location.pathname;
		}

		if (el)
		{
			holder = el.parentNode;
			txt = $new("textarea");
			el.textarea = txt;
			el.style.cursor = "pointer";
			el.title = "Double-click to update this";
			txt.el = el;
			txt.urlName = urlName;
			txt.postName = postName;
			txt.postKey = postKey;
			txt.isRefresh = isRefresh;
			txt.style.width = "100%";
			txt.style.height = "100%";
			txt.style.display = "none";
			txt.style.fontSize = "11px";
			txt.setAttribute("onkeypress", "if(event.ctrlKey&&(event.keyCode==10||event.keyCode==13)){ answer=Request.ask(this.urlName,'postKey='+postKey+'&postName='+this.postName+'&postValue='+encodeURIComponent(this.value),false);if(answer == '1'){this.el.innerHTML = this.value;this.style.display='none'; this.el.style.display = 'block'; if (this.isRefresh){window.refresh();}}else{this.style.backgroundColor='#FF8080';alert(answer == '0' || !answer ? 'Unable to change string. Try again.' : answer);this.style.backgroundColor='#FFFFFF';}}if (event.keyCode==27){this.style.display='none'; this.el.style.display = 'block';}");
			txt.innerHTML = el.innerHTML;
			txt.title = "Press Ctrl-Enter to approve";
			holder.appendChild(txt);
			el.setAttribute("ondblclick", "this.style.display = 'none'; this.textarea.style.display = 'block'; this.textarea.focus();");
		}

		return null;
	}
}

var getImageSize = function (imageSrc)
{
	answer = Request.getValue('imageSize', imageSrc, false);
	size = /(\d+)x(\d+)/.exec(answer);
	if (size.length == 3)
	{
		return size;
	}
	else
	{
		return false;
	}
}

var stopEvent = function (event)
{
	e = event ? event : window.event;

	if (!isIE6 && e.stopPropagation)
	{
		e.stopPropagation();
	}
	else
	{
		e.cancelBubble = true;
	}

	if (!isIE6 && e.preventDefault)
	{
		e.preventDefault();
	}
	else
	{
		e.returnValue = false;
	}

	return false;
}

function toggleElement(el, show)
{
	if (typeof (el) == "string")
	{
		el = $I(el);
	}

	el.style.visibility = show ? 'visible' : 'hidden';
	el.style.display = show ? 'block' : 'none';
}

var getTrueForm = function (getName)
{
	req = Request;
	form = document.forms[0];

	req.post('/Controls/Utility/Get.aspx', "getKey=formHash&getName=" + encodeURIComponent(getName), true, function (xmlhttp)
	{
		var formKey = xmlhttp.responseText;
		var inp = document.createElement("input");
		inp.type = "hidden";
		inp.name = "formKey";
		inp.value = formKey;

		if (oldFormKey = form.elements["formKey"])
		{
			oldFormKey.parentNode.removeChild(oldFormKey);
		}
		
		form.appendChild(inp);		
	});
}

var forceValidation = function ()
{
	if (Page_Validators)
	{
		Page_ClientValidate();

		for (i = 0; i < Page_Validators.length; i++)
		{
			if (Page_Validators[i] && !Page_Validators[i].isvalid)
			{
				return false;
			}
		}
	}

	return true;
};

var hideBackgroundIfNotEmpty = function (input, hideAlways)
{
	if ((input.value.length > 0 || hideAlways) && input.parentNode.style.background)
	{
		input.parentNode.style.background = '';
	}

	if (input.value.length == 0 && !hideAlways && input.parentNode.oldBackground)
	{
		input.parentNode.style.background = input.parentNode.oldBackground;
	}

	input.onblur = function () { hideBackgroundIfNotEmpty(this); };
};

var selectSearchId = function (searchId, searchTitle, listItemId) {

	$I('idSearchTitleSuffix').innerHTML = searchTitle;

	window.iframes = $T('iframe');
	for (x = 0; x < window.iframes.length; x ++) {
		if (window.iframes[x].className == "searchFrame") {
			window.frames[window.iframes[x].name].setSearchId(searchId);
		}
		if (window.iframes[x].className == "searchResultsFrame") {
			window.iframes[x].style.display = window.iframes[x].name == searchId ? "" : "none";
		}
	}

	var ul = $I('idSearchSelectorList');
	var li = $I(listItemId);

	for (x = 0; x < ul.childNodes.length; x ++) {
		if (ul.childNodes[x].tagName && ul.childNodes[x].tagName.toLowerCase() == "li") {
			if (ul.childNodes[x].id == li.id) {
				ul.childNodes[x].className = "roundedBoxHolder current noblHolder nobrHolder";
			}
			else {
				ul.childNodes[x].className = "roundedBoxHolder lightRoundedBoxHolder noblHolder nobrHolder";
			}
		}
	}
};
