﻿// Declare namespaces
Type.registerNamespace('iCompass.CivicWeb.Global.Controls');

iCompass.CivicWeb.Global.Controls.getFirstElementChild = function(node, tagName)
{
	var nChild = node != null ? node.firstChild : null;
	while (nChild != null && (nChild.nodeType != 1 || (nChild.nodeType == 1 && tagName != null && tagName.toLowerCase() != nChild.tagName.toLowerCase())))
	{
		nChild = nChild.nextSibling;
	}
	
	return nChild;
}

iCompass.CivicWeb.Global.Controls.getFirstElementParent = function(node, tagName)
{
	var parent = node != null ? node.parentNode : null;
	while (parent != null && tagName != null && tagName.toLowerCase() != parent.tagName.toLowerCase())
	{
		parent = parent.parentNode;
	}
	
	return parent;
}

iCompass.CivicWeb.Global.Controls.getPreviousElementSibling = function(node, tagName)
{
	var nSibling = node != null ? node.previousSibling : null;
	while (nSibling != null && (nSibling.nodeType != 1 || (nSibling.nodeType == 1 && tagName != null && tagName.toLowerCase() != nSibling.tagName.toLowerCase())))
	{
		nSibling = nSibling.previousSibling;
	}
	
	return nSibling;
}

iCompass.CivicWeb.Global.Controls.getNextElementSibling = function(node, tagName)
{
	var nSibling = node != null ? node.nextSibling : null;
	while (nSibling != null && (nSibling.nodeType != 1 || (nSibling.nodeType == 1 && tagName != null && tagName.toLowerCase() != nSibling.tagName.toLowerCase())))
	{
		nSibling = nSibling.nextSibling;
	}
	
	return nSibling;
}

iCompass.CivicWeb.Global.Controls.addEventListener = function(target, type, listener, useCapture)
{
	if (target != null)
	{
		if (target.addEventListener)
		{
			target.addEventListener(type, listener, useCapture);
		}
		else if (target.attachEvent)
		{
			target.attachEvent('on' + type, listener);
		}
	}
}

iCompass.CivicWeb.Global.Controls.cancelEvent = function(e)
{
	if (e && e.returnValue)
	{
		e.returnValue = false;
	}
	if (e && e.cancelBubble)
	{
		e.cancelBubble = true;
	}
	if (window.event && window.event.returnValue)
	{
		window.event.returnValue = false;
	}
	if (window.event && window.event.cancelBubble)
	{
		window.event.cancelBubble = true;
	}
	if (e && e.preventDefault)
	{
		e.preventDefault();
	}
	
	return false;
}

iCompass.CivicWeb.Global.Controls.round = function(number, decimals)
{
	var roundedNumber = number;
	if (!isNaN(parseFloat(roundedNumber)) && !isNaN(parseInt(decimals)))
	{
		var multiplier = Math.pow(10, decimals);
		roundedNumber = Math.round(roundedNumber * multiplier) / parseFloat(multiplier);
	}

	return roundedNumber;
}

iCompass.CivicWeb.Global.Controls.splitXml = function(text)
{
	var splitText = new Array();
	if (text)
	{
		text = text.toString();
		
		var position = 0;
		var count = 0;
		while (position < text.length)
		{
			var tag = text.indexOf('<', position);
			if (tag >= 0)
			{
				splitText[count] = text.substring(position, tag);
				count++;
				position = tag;

				tag = text.indexOf('>', position);
				if (tag >= 0)
				{
					splitText[count] = (tag + 1) < text.length ? text.substring(position, tag + 1) : text.substring(position);
					count++;
					position = tag + 1;
				}
				else
				{
					splitText[count] = text.substring(position);
					position = text.length;
				}
			}
			else
			{
				splitText[count] = text.substring(position);
				position = text.length;
			}
		}
	}
	
	return splitText;
}

iCompass.CivicWeb.Global.Controls.stripXml = function(text)
{
	var fixedText = '';
	if (text)
	{
		var tags = iCompass.CivicWeb.Global.Controls.splitXml(text);
		for (var index = 0; index < tags.length; index++)
		{
			var currentTag = tags[index];
			if (currentTag.toUpperCase().indexOf('<', 0) != 0 || currentTag.toUpperCase().lastIndexOf('>') != (currentTag.length - 1))
			{
				fixedText += currentTag;
			}
		}
	}
	
	return fixedText;
}

iCompass.CivicWeb.Global.Controls.getRadWindow = function ()
{
	var radWindow = null;
	if (window.radWindow)
	{
		radWindow = window.radWindow;
	}
	else if (window.frameElement.radWindow)
	{
		radWindow = window.frameElement.radWindow;
	}

	return radWindow;
}

iCompass.CivicWeb.Global.Controls.refreshParent = function ()
{
	var radWindow = iCompass.CivicWeb.Global.Controls.getRadWindow();
	if (radWindow)
	{
		radWindow.BrowserWindow.location.href = radWindow.BrowserWindow.location.href;
	}
	else if (window.opener)
	{
		window.opener.location.href = window.opener.location.href;
	}
}

iCompass.CivicWeb.Global.Controls.closeWindow = function ()
{
	var radWindow = iCompass.CivicWeb.Global.Controls.getRadWindow();
	if (radWindow)
	{
		radWindow.close();
	}
	else if (window.opener)
	{
		window.close();
	}
}