function GetPostbackFunction(a)
{
	return $get(a).href.replace('javascript:','');
}
function SetPostbackFunction(a,z)
{
	if (z == null)
		z = 0;
	var postbackFunction = document.getElementById('hidEditClosePostback' + z);
	postbackFunction.value = GetPostbackFunction(a);
}

function GetStartPos(scroll, win_length, doc_length, pop_length) {
	var pos = scroll + (win_length - pop_length) / 2;
	if (pos < scroll)
		pos = scroll;
	if (pos + pop_length + 40 > doc_length) {
		pos = doc_length - pop_length - 40;
		if (pos < 0)
			pos = 0;
	}
	return pos;	
}
function DialogShow(width, height, tabWidth, z, url)
{
	var parentBlock = null;
	var divEdit = null;
	var divDrag = null;
	
	if (z == null)
		z = 0;
	if (z > 0)
		parentBlock = $get('divDialogBlock' + (z - 1));
	
	var divBackground = $get('divDialogBackground' + z);
	var divShadow = $get('divDialogShadow' + z);
	var divBlock = $get('divDialogBlock' + z);
	var iframe = $get('iframeEdit' + z);
	var postbackFunction = $get('hidEditClosePostback' + z);

	SetLoadingMessage(iframe);

	if (postbackFunction)
		postbackFunction.value = '';
	
	var winHeight = $(window).height();
	var winWidth = $(window).width();
	var docHeight = $(document).height();
	var docWidth = $(document).width();
	
	var top = GetStartPos($(document).scrollTop(), winHeight, docHeight, height);
	var left = GetStartPos($(document).scrollLeft(), winWidth, docWidth, width);
		
	pnlEdit[z].style.top = top + "px";
	pnlEdit[z].style.left = left + "px";
	pnlEditDrag[z].style.width = (width - tabWidth) + "px";
	
	divShadow.style.width = (24 + width) + "px";
	divShadow.style.height = (24 + height) + "px";	 

	iframe.width = width;
	iframe.height = height;	

	if (divBlock)
	{
		divBlock.style.width = divShadow.style.width;
		divBlock.style.height = divShadow.style.height;	 
	}
		
	if (divBackground) 
	{
		var maxHeight = Math.max(winHeight, docHeight);
		var maxWidth = Math.max(winWidth, docWidth);

		// Account for IE not implementing min-width & min-height styles
		if (navigator.appName == 'Microsoft Internet Explorer') {
			divBackground.style.filter = 'alpha(opacity=35)';

			divBackground.style.width = Math.max(maxWidth, screen.width) + "px";
			divBackground.style.height = Math.max(maxHeight, screen.height) + "px";
		}
		else {
			divBackground.style.minWidth = maxWidth + "px";
			divBackground.style.minHeight = maxHeight + "px";
		}
		$(divBackground).fadeIn('slow');
		
		//divBackground.style.visibility = 'visible';
		//divBackground.style.display = 'block';
	}
	if (parentBlock)
	{
		if (navigator.appName == 'Microsoft Internet Explorer')
		{
			$(parentBlock).css('padding-bottom', '3px');
			parentBlock.style.filter = 'alpha(opacity=35)';
		}
		$(parentBlock).fadeIn('slow');
		//parentBlock.style.visibility = 'visible';
		//parentBlock.style.display = 'block';
	}
	pnlEdit[z].style.visibility = 'visible';
	$(pnlEdit[z]).show();
}

function DialogSubmit(value, z)
{
	if (z == 0)
	{
		var result = $get($get('hidChildResult' + z).value);
		var submit = $get('hidChildSubmit' + z).value;
		
		result.value = value;
		ClickLink(submit);
	}
	else
	{
		var iframeParent = document.getElementById('iframeEdit' + (z - 1));
		
		var doc = iframeParent.contentDocument;
		if (doc == undefined || doc == null)
			doc = iframeParent.contentWindow.document;

		var result = doc.getElementById($get('hidChildResult' + z).value);
		var submit = $get('hidChildSubmit' + z).value;
		
		result.value = value;
		iframeParent.contentWindow.ClickLink(submit);
	}
	
	DialogClose(0, z);
}

function DialogClose(refresh, z)
{
	var divEdit = null;
	if (z == null)
		z = 0;
	
	var parentBlock = document.getElementById('divDialogBlock' + (z - 1));
	if (parentBlock)
		$(parentBlock).fadeOut('slow');
		//parentBlock.style.visibility = 'hidden';
	var divBackground = document.getElementById('divDialogBackground' + z);
	if (divBackground)
		$(divBackground).fadeOut('slow');
		//divBackground.style.visibility = 'hidden';

	pnlEdit[z].style.visibility = 'hidden';
	$(pnlEdit[z]).hide();
	//$(pnlEdit[z]).hide('slow');
	
	if (refresh) 
	{
		var refresh_func = document.getElementById('hidEditClosePostback' + z).value;
		if (refresh_func == '')
			window.location = window.location.href.split('#',2)[0];
		else
			eval(refresh_func);
	}
}

var _loadingImage = null;
function SetLoadingMessage(iframe)
{
	if (!_loadingImage)
	{
		_loadingImage = new Image();
		_loadingImage.src = '/Images/Loading.gif';
	}

	var doc = iframe.contentDocument;
	if (doc == undefined || doc == null)
		doc = iframe.contentWindow.document;
	doc.open();
	doc.write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
	doc.write("<html xmlns='http://www.w3.org/1999/xhtml'><head><title></title></head><body>");
	doc.write("<div style='width: 150px; top: 50%; left: 50%; margin: -50px 0px 0px -75px; font-family: Sans-Serif; font-size: 24px; color: #999; position: absolute;'>");
	doc.write("<center><img id='imgLoading' style='padding-bottom: 5px' /><br />Loading...</center>");
	doc.write("</div></body></html>");
	doc.close();
	doc.getElementById('imgLoading').src = _loadingImage.src;
}

function ClickLink(link)
{
	eval(GetPostbackFunction(link));
}

function IncChildrenIDs(id)
{
	return '#' + id + ',#' + id + ' > *:not(script)';
}
function ChildrenIDs(id)
{
	return '#' + id + ' > *:not(script)';
}

function ToggleDiv(id)
{
	var div = $get(id);
	if (!div)
		return;
	
	if (div.MyDisplay)
		HideDiv(id);
	else
		ShowDiv(id);
}

function HideDiv(id)
{
	var div = $get(id);
	if (!div)
		return;
		
	if (!div.MyDisplay)
		return;
	div.MyDisplay = 0;

	$(IncChildrenIDs(div.id)).fadeOut('slow');
}

function ShowDiv(id)
{
	var div = $get(id);
	if (!div || div.MyDisplay)
		return;
		
	// Close visible divs with same clasname
	if (div.className != '')
		$('.' + div.className + ':not(#' + div.id + '):visible').each(function() { HideDiv(this.id); });

	$('#' + div.id + ' > *').css('display', 'none');
	div.MyDisplay = 1;
	$(IncChildrenIDs(div.id)).fadeIn('normal');
}

function ViewPage(page, rootID, lbtnID, hidID)
{
	var hid = document.getElementById(rootID + hidID);
	hid.value = page;
	
	eval(GetPostbackFunction(rootID + lbtnID));
}

function EditPage(iid,parent)
{
	EditGizmo(null, iid, 'page', 680, 400, parent);
}

function AddGizmo(refresh,gizmo,width,height,parent,area)
{
	EditGizmo(refresh, -1, gizmo, width, height, parent, area);
}

function EditGizmo(container,iid,gizmo,width,height,parent,area,misc)
{
	DialogShow(width, height, 220, 0);

	if (container) 
		SetPostbackFunction(container + ((iid == -1)? '' : '_lbtnRefresh'), 0);

	var now = new Date();
	var ts = "" + now.getDay() + now.getMonth() + now.getFullYear() + now.getHours() + now.getMinutes() + now.getSeconds();
	
	if (parent != null)
		parent = "&parent=" + parent;
	else
		parent = '';
	if (area != null && area != undefined)
		area = "&area=" + area;
	else
		area = '';
	if (misc != null && misc != undefined)
		misc = "&misc=" + misc;
	else
		misc = '';
		
	var iframe = document.getElementById('iframeEdit0');
	iframe.src = "/Popups/EditGizmo.aspx?tp=" + gizmo + "&iid=" + iid + "&ts=" + ts + parent + area + misc;
}

function MovingCalendar(sender, eventArgs)
{
	var id = sender._clientID;
	if (!id)
		id = sender._dateInput._clientID;
	
	var newID_Date = id.replace('dateInput', 'calendar_wrapper');
	$get(newID_Date).style.visibility = 'hidden';
	setTimeout("MoveCalendar('" + newID_Date + "',-210)", 0);
	
	var newID_Time = id.replace('dateInput', 'timeView_wrapper');
	var oTime = $get(newID_Time);
	if (oTime)
	{
		oTime.style.visibility = 'hidden';
		setTimeout("MoveCalendar('" + newID_Time + "',-262)", 0);
	}
}
function MoveCalendar(id, move)
{
	var div = $get(id);
	var parent = div.offsetParent;
	if (!parent)
		parent = div.parentNode;
	var newTop = parseInt(parent.style.top.replace('px')) + move;
	if (newTop > 30)
		parent.style.top = newTop + 'px';
	div.style.visibility = 'visible';
}

function GetQueryStringValue(name) 
{
	var q = window.location.search.substring(1);
	var qs = query.split('&');
	for (var i = 0; i < qs.length; i++) 
	{
		var pair = qs[i].split('=');
		if (pair[0] == name)
			return pair[1];
	}
	return null;
}
function SetQueryStringValue(QueryString, name, value) 
{
	var qNew = '';
	var qs = QueryString.split('&');
	var found = 0;
	for (var i = 0; i < qs.length; i++) 
	{
		var pair = qs[i].split('=');
		if (pair[0] == name)
		{
			pair[1] = value;
			found = 1;
		}
		qNew += '&' + pair[0] + '=' + pair[1];
	}
	if (!found)
		qNew += '&' + name + '=' + value;
	if (qNew.length > 0)
		return qNew.substring(1);
	return null;
}

function ErrorPopup(hidID)
{
	var hidMsg = $get(hidID);
	if (hidMsg && hidMsg.value != '')
		alert(hidMsg.value);
}

function PrinterFriendly() {
	var argChar = '?';
	if (window.location.search)
		argChar = '&';
	var pf = window.open(window.location + argChar + 'pf=1', "PrinterFriendly", "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=800,width=850");
}