function isDefined(el){return typeof(el)!='undefined' && el != null;}
function isInnerTextSupported(){return isDefined(document.getElementsByTagName("BODY")[0].innerText);}
function getElementText(el){if(isInnerTextSupported())return el.innerText;else return el.textContent;}
function write(el,text){if(isInnerTextSupported())el.innerText = text;else el.textContent = text;}
function toggle(el){el.style.display=el.style.display=="none"?"":"none";}
function sanitize(str){return str.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;").replace("\"","&quot;");}
function desanitize(str){return str.replace("&lt;","<").replace("&gt;",">").replace("&quot;","\"").replace("&amp;","&");}
function trim(str){return str.replace(/^\s\s*/,"").replace(/\s\s*$/,"");}
function getDdlTextByValue(ddl,value)
{
	for(var i=0;i<ddl.options.length;i++)
		if(ddl.options[i].value == value)
			return ddl.options[i].text;
	return value;
}
function emptyDdl(ddl,addEmptyOption)
{
	for(var i=ddl.options.length-1;i>=0;i--)ddl.options[i] = null;
	if(addEmptyOption)addOptionToDll(ddl,"--","");
}
function addOptionToDll(ddl,text,value)
{
	var opt = document.createElement("OPTION");
	opt.text = text;
	opt.value = value;
	ddl.options.add(opt);
}
function selectDdlOptionByValue(ddl,value){selectDdlOption(ddl,value,true);}
function selectDdlOptionByText(ddl,text){selectDdlOption(ddl,text,false);}
function selectDdlOption(ddl,comparison,isValue)
{
	if(ddl.options.length == 0)return;
	var selected = false;
	for(var i=0;i<ddl.options.length;i++)
	{
		if((!isValue && ddl.options[i].text == comparison) || (isValue && ddl.options[i].value == comparison))
		{
			ddl.selectedIndex = i;
			selected = true;
			break;
		}
	}
	if(!selected)ddl.selectedIndex = 0;
}
function fillNumericDdl(ddl,start,limit,addLeadingZero)
{
	for(var i=start-1;i<limit;i++)
	{
		var n = i+1;
		var text = addLeadingZero && n < 10 ? "0"+n : n;
		addOptionToDll(ddl,text,n);
	}
}
function getClientCenter()
{
	return{x:parseInt(document.documentElement.clientWidth/2) + document.documentElement.scrollLeft, y:parseInt(document.documentElement.clientHeight/2) + document.documentElement.scrollTop};
}
function getClientBottomRight()
{
	return{x:parseInt(document.documentElement.clientWidth) + document.documentElement.scrollLeft, y:parseInt(document.documentElement.clientHeight) + document.documentElement.scrollTop};
}
function centerElement(el)
{
	var coords = getClientCenter();
	var b = Sys.UI.DomElement.getBounds(el);
	var w = isDefined(el.style.width) && el.style.width.length > 0 ? parseInt(el.style.width.substr(0,el.style.width.length-2)) : b.width;
	var h = isDefined(el.style.height) && el.style.height.length > 0 ? parseInt(el.style.height.substr(0,el.style.height.length-2)) : b.height;
	el.style.top = coords.y - h/2 + "px";
	el.style.left = coords.x - w/2 + "px";
}
function getParentByTagName(el,tagName,val)
{
	if(!isDefined(val))val = 0;
	if(val > 0 && el.tagName && el.tagName == tagName)return el;
	if(isDefined(el.parentNode))
		return getParentByTagName(el.parentNode,tagName,++val);
	return null;
}
function getChildrenByTagName(el,tagName,includeSubChildren)
{
	var children = [];
	var node;
	for(var i=0;i<el.childNodes.length;i++)
	{
		node = el.childNodes[i];
		if(node.tagName && node.tagName == tagName)children.push(node);
		if(includeSubChildren && node.childNodes.length > 0)
		{
			var c = getChildrenByTagName(node,tagName,includeSubChildren);
			for(var j in c)children.push(c[j]);
		}
	}
	return children;
}
function getParentByAttribute(el,attribute,value)
{
	if(el.getAttribute && isDefined(el.getAttribute(attribute)) &&
		(!isDefined(value) || el.getAttribute(attribute) == value))
			return el;
	if(isDefined(el.parentNode))
		return getParentByAttribute(el.parentNode,attribute,value);
	return null;
}
function getChildrenByAttribute(el,attribute,value)
{
	var children = [],node,attr;
	for(var i=0;i<el.childNodes.length;i++)
	{
		node = el.childNodes[i];
		if(node.tagName && node.getAttribute)
		{
			attr = node.getAttribute(attribute);
			if(attr && (!isDefined(value) || attr == value))children.push(node);
		}
		if(node.childNodes.length > 0)
		{
			var c = getChildrenByAttribute(node,attribute,value);
			for(var j in c)children.push(c[j]);
		}
	}
	return children;
}
function highlight(el){el.style.backgroundColor = "#ffff91";}
function dim(el){el.style.backgroundColor = "";}
function getSource(url,callback)
{
	var r = new Sys.Net.WebRequest();
	r.set_httpVerb("GET");
	r.set_url(url);
	r.add_completed(function(e){if(e.get_responseAvailable())callback(e.get_responseData());});
	var e = new Sys.Net.XMLHttpExecutor();
	r.set_executor(e); 
	e.executeRequest();
}
function globalize(container,labels)
{
	var child,txt;
	for(var i=0;i<container.childNodes.length;i++)
	{
		child = container.childNodes[i];//avoid too much looping though collection index
		if(child.tagName)
		{
			switch(child.tagName)
			{
				case "TD":
				case "TH":
				case "SPAN":
				case "A":
					txt = getElementText(child);
					if(txt.length == 4)
						write(child,getLabel(txt,labels));
					setTitle(child,labels);
					break;
				case "IMG":
					setTitle(child,labels);
					break;
				case "INPUT":
					if(child.type == "button" || child.type == "submit")
					{
						if(child.value.length == 4)
							child.value = getLabel(child.value,labels);
						setTitle(child,labels);
					}
					break;
				case "OPTION":
					if(child.text && child.text.length == 4)
						child.text = getLabel(child.text,labels);
					break;
			}
		}
		if(isDefined(child.childNodes) && child.childNodes.length > 0)
			globalize(child,labels);
	}
	function setTitle(el,ls){if(el.title && el.title.length == 4)el.title = getLabel(el.title,ls);}
	function getLabel(tg,ls){return isDefined(ls[tg])?ls[tg]:tg;}
}
function getUTCDate()
{
	var d = new Date();
	return new Date(d.getUTCFullYear(),d.getUTCMonth(),d.getUTCDate(),d.getUTCHours(),d.getUTCMinutes(),d.getUTCSeconds());
}
function getLocalDate(utcDateString)
{
	var d = new Date(utcDateString);
	var m = Date.UTC(d.getFullYear(),d.getMonth(),d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds()) - new Date().getTimezoneOffset();
	return new Date(m);
}
function registerLoadEvent(method)
{
	var ol = window.onload;
	if(typeof window.onload != "function")window.onload = method;
	else window.onload = function(e){if(ol)ol();method();}
}