function HideShow(id)
{
	var element = document.getElementById(id);
	if ((element != null) && (element != 'undefined') && (element.style != 'undefined') && (element.style.display != 'undefined'))
	{
		if (element.style.display == 'none')
		{
			if (element.previousDisplay != 'undefined')
			{
				element.style.display = element.previousDisplay;
			}
			else
			{
				element.style.display = 'block';
			}
			traverseTree(true, element);
		}
		else
		{
			element.previousDisplay = element.style.display;
			element.style.display = 'none';
			traverseTree(false, element);
		}
	}
}

function Hide(id)
{
	Hide(id, true);
}

function Hide(id, traverse)
{
	var element = document.getElementById(id);
	if ((element != null) && (element != 'undefined') && (element.style != 'undefined') && (element.style.display != 'undefined'))
	{
		if (element.style.display != 'none')
		{
			element.previousDisplay = element.style.display;
			element.style.display = 'none';
		}
		if (traverse != false) { traverseTree(false, element); }
	}
}

function Show(id)
{
	Show(id, true);
}


function Show(id, traverse)
{
	var element = document.getElementById(id);
	if ((element != null) && (element != 'undefined') && (element.style != 'undefined') && (element.style.display != 'undefined'))
	{
		if (element.style.display == 'none')
		{
			if (element.previousDisplay != 'undefined')
			{
				element.style.display = element.previousDisplay;
			}
			else
			{
				element.style.display = 'block';
			}
		}
		if (traverse != false) { traverseTree(true, element); }
	}
}

function Expand(obj, id, trackerId, classNameMinimised, classNameExpanded)
{
	if ((obj != null) && (obj.className != 'undefined') && (id != null))
	{
		var element = document.getElementById(id);
		var tracker = document.getElementById(trackerId);
		if (element != null)
		{
			HideShow(id);
			if (element.style.display == 'none')
			{
				obj.className = classNameMinimised;
				if (tracker != null) { tracker.value = false; }
			}
			else
			{
				obj.className = classNameExpanded;
				if (tracker != null) { tracker.value = true; }
			}
		}
	}
}

function traverseTree(status, control)
{
	try
	{
		if(control == null)
		{
			for(var i = 0; i < Page_Validators.length; i++)
			{
				Page_Validators[i].enabled = status;    
				Page_Validators[i].style.display = 'none';
			}
		}
		else
		{ 
			if(isValidator(control))
			{
				control.enabled = status;
				control.style.display = 'none';
			}
			for( var i=0; i < control.childNodes.length; i++)
			{
				traverseTree(status, control.childNodes[i]);
			}
		}
	} catch (e) {}
}

function isValidator(control)
{
	try
	{
		for(var i = 0; i < Page_Validators.length; i++)
		{
			if (Page_Validators[i].id == control.id) { return true; }
		}
	} catch (e) {}
	return false;
}

function enableValidators(id)
{
	var control;
	if(id == null)
	{
		control = null;     
	}
	else
	{
		control = document.getElementById(id);  
	} 
	if( id == null || control != null )
		traverseTree(true,  control); 
}

function disableValidators(id)
{
	var control;
	if(id == null)
	{
		control = null;     
	}
	else
	{
		control = document.getElementById(id);  
	} 
	if( id == null || control != null )
		traverseTree(false,  control); 
}

function EnableDisable(id)
{
	var element = document.getElementById(id);
	if ((element != null) && (element != 'undefined') && (element.getAttribute('disabled') != null))
	{
		if (element.disabled)
		{
			element.disabled = false;
		}
		else
		{
			element.disabled = true;
		}
	}
}

function EnableDisableValidator(id, status)
{
	var element = document.getElementById(id);
	if ((element != null) && (isValidator(element)))
	{
		element.enabled = status;
	}
}

function SwapCheck(id, currentChecked)
{
	var element = document.getElementById(id);
	if ((element != null) && (element != 'undefined') && (element.type) && (element.type = 'checkbox'))
	{
		if (currentChecked)
		{
			element.checked = false;
		}
	}
}

	
function YesNoSelect(list, yesId, noId)
{
	Hide(yesId);
	Hide(noId);
	if ((list != null) && (list != 'undefined') && (list.selectedIndex != 'undefined') && (list.selectedIndex > -1))
	{
		var option = list.options[list.selectedIndex];
		if ((option != null) && (option != 'undefined') && (option.value != 'undefined'))
		{
			if ((option.value.toLowerCase() == 'true') || (option.value.toLowerCase() == 'yes'))
			{
				Show(yesId);
			}
			else if ((option.value.toLowerCase() == 'false') || (option.value.toLowerCase() == 'no'))
			{
				Show(noId);
			}
		}
	}
}

function ShowOther(list, otherId)
{
	Hide(otherId);
	if ((list != null) && (list != 'undefined') && (list.selectedIndex != 'undefined') && (list.selectedIndex > -1))
	{
		var option = list.options[list.selectedIndex];
		if ((option != null) && (option != 'undefined') && (option.text != 'undefined') & (option.text.toLowerCase() == 'other'))
		{
			Show(otherId);
		}
	}
}

function changeCell(id, e)
{
	var code = e.keyCode;
	if (cells != null)
	{
		if (currentCell == null) { setCurrentCell(id); }
		if (currentCell != null)
		{
			//TODO: devise some way to find or keep track of the cursor position within a cell.
			//This can then be used to allow navigation of the text on left and right clicks
			//instead of just cell navigation.
			var focusElement = null;
			var atEndOfCell = true;
			if ((code == 38) && (currentCell.up != ''))
			{
				focusElement = document.getElementById(currentCell.up);
			}
			else if ((code == 39) && (currentCell.right != ''))
			{
				focusElement = document.getElementById(currentCell.right);
			}
			else if ((code == 40) && (currentCell.down != ''))
			{
				focusElement = document.getElementById(currentCell.down);
			}
			else if ((code == 37) && (currentCell.left != ''))
			{
				focusElement = document.getElementById(currentCell.left);
			}
			if ((atEndOfCell) && (focusElement != null) && (focusElement != 'undefined'))
			{
				focusElement.focus();
				return false;
			}
		}
	}
	return true;
}

function findCell(id)
{
	var i = 0
	while (i < cells.length)
	{
		var element = cells[i];
		if ((element != null) && (element.id == id))
		{
			return element;
		}
		i++;
	}
	return null;
}

function setCurrentCell(id)
{
	currentCell = findCell(id);
}

function setRelativePosition(objId, elementId)
{
	var obj = document.getElementById(objId);
	var element = document.getElementById(elementId);
	if ((obj != null) && (element != null) && (element.className != null) && (element.className != 'undefined') && (element.className.toLowerCase() == 'floatingitem'))
	{
		var coords = getPosition(obj);
		var dimensions = getDimension(obj);
		var parentPos = getPosition(obj.offsetParent);
		var parentDim = getDimension(obj.offsetParent);
		if ((element.style != null) && (element.style != 'undefined'))
		{
			element.style.position = 'absolute';
			element.style.top = coords.top;
			element.style.left = coords.left + dimensions.width + 5;
			element.style.width = (parentDim.width - ((coords.left - parentPos.left) + dimensions.width + 5)) + 'px';
		}
	}
}

function getPosition(obj)
{
	var objTop = 0;
	var objLeft = 0;
	try
	{
		while (obj != null)
		{
			objTop += obj.offsetTop;
			objLeft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	catch (e) {}
	return {top:objTop, left:objLeft}
}

function getDimension(obj)
{
	return {width:obj.offsetWidth, height:obj.offsetHeight};
}

function isInteger(val)
{
	var regExp = /^[-+]?[0-9]*$/;
	var result = trim(val).match(regExp);
	if (result == null) return false;
	return result;
}

function isNumeric(val)
{
	var regExp = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
	var result = trim(val).match(regExp);
	if (result == null) return false;
	return result;
}

function trim(val)
{
	return val.replace( /^\s*|\s*$/g , ""); 
}

var __nonMSDOMBrowser = (window.navigator.appName.toLowerCase().indexOf('explorer') == -1);
function WebForm_AutoFocus(focusId)
{
	var targetControl;
	/*if (document.all)
	{
		targetControl = document.all[focusId];
	}
	else
	{*/
	targetControl = document.getElementById(focusId);
	//}
	if (targetControl != null)
	{
		try {
			targetControl.focus();
		}
		catch (e) {}
	}
}

var scrollXId, scrollYId;

function SmartScroller_GetCoords()
{
	var scrollX, scrollY;

	if (document.all)
	{
		if (!document.documentElement.scrollLeft)
			scrollX = document.body.scrollLeft;
		else
			scrollX = document.documentElement.scrollLeft;
               
		if (!document.documentElement.scrollTop)
			scrollY = document.body.scrollTop;
		else
			scrollY = document.documentElement.scrollTop;
	}   
	else
	{
		scrollX = window.pageXOffset;
		scrollY = window.pageYOffset;
	}

	document.getElementById(scrollXId).value = scrollX;
	document.getElementById(scrollYId).value = scrollY;
}
   
function SmartScroller_Scroll()
{
	var x = document.getElementById(scrollXId).value;
	var y = document.getElementById(scrollYId).value;
	window.scrollTo(x, y);
}


/*Tab View controls*/
function TabOnLoad()
{
	if (typeof(Tabs) == 'undefined')
		return;
	var i, tab;
	for (i = 0; i < Tabs.length; i++)
	{
		tab = Tabs[i];
		if (typeof(tab.getAttribute('selected')) == 'string')
		{
			if (tab.getAttribute('selected').toLowerCase() == 'false')
			{
				tab.selected = false;
			} 
			else
			{
				tab.selected = true;
				currentTab = tab;
			}
		}
		else
		{
			tab.selected = false;
		}
		if (typeof(tab.getAttribute('enabled')) == 'string')
		{
			tab.enabled = (tab.getAttribute('enabled').toLowerCase() != 'false');
		}
		TabHookupTabPage(tab.getAttribute('tabpage'), tab);
	}
	var result = ChangeTab(currentTab);
}

function TabHookupTabPage(tappageID, tab)
{
	if(typeof(tappageID) != 'string')
		return;
	if(typeof(tab) == 'undefined')
		return;
	var tapPage = document.getElementById(tappageID);
	if (typeof(tapPage) != 'undefined')
	{
		tab.tabpage = tapPage;
		tab.tabpage.style.display = 'none';
	}
		
}

function ChangeTab(tab)
{
	if (typeof(Tabs) == 'undefined')
		return false;
	if (typeof(tab) == 'undefined')
		return false;
	if ((typeof(tab.enabled) != 'undefined') && (!tab.enabled))
		return false;
	if (typeof(tab.tabpage) == 'undefined')
		return false;
		
	if (typeof(currentTab) != 'undefined')
	{
		currentTab.className = tabClass;
		if ((typeof(currentTab.tabpage) != 'undefined') && (typeof(currentTab.tabpage) != 'string') && (typeof(currentTab.tabpage.style) != 'undefined'))
		{
			currentTab.tabpage.style.display = 'none';
		}
	}
	
	tab.className = selectedTabClass;
	if ((typeof(tab.tabpage) != 'undefined') && (typeof(tab.tabpage) != 'string') && (typeof(tab.tabpage.style) != 'undefined'))
	{
		tab.tabpage.style.display = 'block';
	}
	
	currentTab = tab;
}

function Validate(control)
{
	var IsValid = true;
	IsValid = ValidateControl(control);
	if ((!IsValid) && (typeof(ValidationSummaryOnSubmit) == 'function'))
	{
		Page_IsValid = IsValid;
		ValidationSummaryOnSubmit();
	}
	return IsValid;
}
function ValidateControl(control)
{
	var IsValid = true;
	try
	{
		if(control == null)
		{
			for(var i = 0; i < Page_Validators.length; i++)
			{
				if ((Page_Validators[i].enabled != false) && (typeof(ValidatorValidate) == 'function'))
				{
					ValidatorValidate(Page_Validators[i]);
					if (IsValid) { IsValid = Page_Validators[i].isvalid; }
				}
			}
		}
		else
		{ 
			if(isValidator(control))
			{
				if ((control.enabled != false) && (typeof(ValidatorValidate) == 'function'))
				{
					ValidatorValidate(control);
					if (IsValid) { IsValid = control.isvalid; }
				}
			}
			for( var i=0; i < control.childNodes.length; i++)
			{
				var valid = ValidateControl(control.childNodes[i]);
				if (IsValid) { IsValid = valid; }
			}
		}
	} catch (e) {}
	return IsValid
}

function getGMTTime() {
	var dte = new Date();
	dte.setTime(dte.getTime() + (dte.getTimezoneOffset()*60*1000));
	return dte;
}
function getClientTime(id) {
	if (id != null) {
		var obj = document.getElementById(id);
		if ((obj != null) && (obj != 'undefined')) { obj.value = getGMTTime().toLocaleString(); }
		setTimeout('getClientTime(\'' + id + '\')', 1000);
	}
}

function SetText(id,value)
           { 
                if(id != null)
                {  
                    var obj = document.getElementById(id);
                    if((obj != null) && (obj != 'undefined'))
                    {
                       
                      if(value.length>80)
                        {
                            value = value.substring(0,80) + '\n' + value.substring(80)
                        }
                      if(value.length > 0)
                        {
                            value = '(' + value + ')\n\n' 
                        }  
                        obj.innerText = value; 
                     }
                }
            }