/*********************************************************************
 *
 * The following routines support setting the height of the content
 * container to 100% of the available height
 *
 *********************************************************************/

function getWindowHeight() 
{
	var windowHeight = 0;
	
	// Most modern browsers will follow this path
	if (typeof(window.innerHeight)=='number')
	{
		windowHeight = window.innerHeight;
	}
	else 
	{
		// IE 6 will follow this path
		if (document.documentElement && document.documentElement.clientHeight) 
		{
			windowHeight = document.documentElement.clientHeight;
		}
		else
		{
			// IE 4+ will follow this path
			if (document.body && document.body.clientHeight)
			{
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function getWindowWidth() 
{
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
	return 0;
}

function getOffsetTop(e)
{
	var top = e.offsetTop;
	if (e.offsetParent != null)
	{
		top += getOffsetTop(e.offsetParent);
	}
	return top;
}

function getOffsetLeft(e)
{
	var left = e.offsetLeft;
	if (e.offsetParent != null)
	{
		left += getOffsetLeft(e.offsetParent);
	}
	return left;
}
	
function setFullHeight(elementId, padding)
{
    var e = document.getElementById(elementId);

	if (e) 
	{
		var windowHeight = getWindowHeight();
		var windowWidth = getWindowWidth();

		windowHeight -= getOffsetTop(e);
		windowWidth -= getOffsetLeft(e);


		if (padding != undefined && padding != null) windowHeight -= padding;

		if (windowHeight > 0)
		{
		    e.style.height = windowHeight + "px";
		}
		
		if (windowWidth > 0)
		{
		    // .sm. added on 7/11 
		    var isFirefox = /firefox/.test(navigator.userAgent.toLowerCase());
		    var isChrome = /chrome/.test(navigator.userAgent.toLowerCase());
		    if (isChrome) { return; }
		    if (isFirefox) { return; }

            e.style.width = windowWidth + "px"; 
        }
	}

}

function setHeight(elementId, divStack, topPadding, bottomPadding)
{
	if (document.getElementById) 
	{
		var windowHeight = getWindowHeight();
					
		if (topPadding != undefined && topPadding != null) windowHeight -= topPadding;
		if (bottomPadding != undefined && bottomPadding != null) windowHeight -= bottomPadding;
					
		// split elementId tabs to string array
		if (divStack != null && divStack != undefined)
		{
			var divs = divStack.split(",");
			var i = 0;
			while (i < divs.length)
			{
  				windowHeight -= (document.getElementById(divs[i]) == null) ? 0 : document.getElementById(divs[i]).offsetHeight;
				i++;
			}
		}

		if (windowHeight > 0 && document.getElementById(elementId))
		{
			document.getElementById(elementId).style.height = windowHeight + "px";
		}
	}
}

function setWidth(elementId, leftPadding, rightPadding)
{
	if (document.getElementById)
	{
		var windowWidth = getWindowWidth();
					
		if (leftPadding != undefined && leftPadding != null) windowWidth -= leftPadding;
		if (rightPadding != undefined && rightPadding != null) windowWidth -= rightPadding;
					
		if (windowWidth > 0 && document.getElementById(elementId))
		{
			document.getElementById(elementId).style.width = windowWidth;
		}
	}
}

/*********************************************************************
 *
 * Utility functions
 *
 *********************************************************************/

function URLencode(sStr) 
{
    return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}

// Use this function to enforce a maxlength property on a textbox rendered as a <textarea>
function TestMaxLength(Object, MaxLen)
{
	return (Object.value.length < MaxLen);
}

// This function gets the named cookie, if it exists
function GetCookie(name)
{
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length)))
	{
		return null;
	}
	if (start == -1) return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

/*********************************************************************
 *
 * New Window Functions
 *
 *********************************************************************/
 
function NewWindowShow(ChildForm, DialogHeight, DialogWidth)
{
	var winHeight = getWindowHeight();
	
	// Handle 100% height
	if (DialogHeight == '100%') DialogHeight = winHeight;
	
	// Handle 100% width
	if (DialogWidth == '100%')
	{
		DialogWidth = (window.innerWidth!=window.undefined) ? window.innerWidth : (document.compatMode=='CSS1Compat') ? document.documentElement.clientWidth : (document.body) ? document.body.clientWidth : 0;
	}
	
	// Set window size
	var height = (DialogHeight != null && DialogHeight > 0) ? DialogHeight : 580;
	var width = (DialogWidth != null && DialogWidth > 0) ? DialogWidth : 780;
	
	// Set window starting positions
	var winleft = (screen.width - width) / 2;
	var wintop = (winHeight - height) / 2;
	
	var args='width='+width+',height='+height+',left='+winleft+',top='+wintop+',toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1';
		
	var dialogWindow = window.open(ChildForm,'',args);
	if (dialogWindow)
		dialogWindow.focus();
}

function LegacyWindowShow(ChildForm, DialogHeight, DialogWidth)
{
	// Set window size
	var height = (DialogHeight != null && DialogHeight > 0) ? DialogHeight : 580;
	var width = (DialogWidth != null && DialogWidth > 0) ? DialogWidth : 780;
	
	// Set window starting positions
	var winleft = (screen.width - width) / 2;
	var wintop = (screen.height - height) / 2;
	
	var args='width='+width+',height='+height+',left='+winleft+',top='+wintop+',toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1';
		
	// Build the url for the legacy page
	var len = top.location.href.lastIndexOf('/') + 1;
	var url = top.location.href.substring(0, len);
	url += 'Legacy.aspx?ChildForm=' + URLencode(ChildForm);
	
	// Open the legacy page
	var dialogWindow=window.open(url,'',args);
	dialogWindow.focus();
}
