<!--
/*******************************************
 *	BaseWebForm以及相关控件所用的脚本
 *******************************************/

var m_hbSubmitControl = false;
var m_hbSubmitControlCaption = "";

function onHBSubmitButtonClick()
{
	var btn = event.srcElement;

	m_hbSubmitControl = true;

	if (btn.popupCaption)
		m_hbSubmitControlCaption = btn.popupCaption;

	if (btn.disabled)
		event.returnValue = false;
	
	if (m_formID)	//页面从基类派生，才会有此变量
	{
		if (typeof(Page_BlockSubmit) == "undefined" || 
			(typeof(Page_BlockSubmit) != "undefined" && Page_BlockSubmit == false))	//检查ASP.Net 的Validator是否阻止提交
		{
			var form = document.all(m_formID);
			hbSetAllSubmitButtonsState(form, true);
			
			if (btn.popupCaption)
				generatePopupPad(btn.popupCaption);
		}
	}
}

function hbSetAllSubmitButtonsState(form, bDisabled)
{
	if (form)
	{
		var inps = form.getElementsByTagName("INPUT");

		for(var i = 0; i < inps.length; i++)
		{
			var elem = inps[i];

			if (elem.type == "submit")
				if (bDisabled)
					hbSetSubmitButtonDisabled(elem);
				else
					hbRestoreSubmitButtonState(elem);
		}

		var anchors = form.getElementsByTagName("A");
		
		for(var i = 0; i < anchors.length; i++)
		{
			var elem = anchors[i];

			var link = elem.href;
				
			if (link.indexOf("__doPostBack") > 0)
				if (bDisabled)
					hbSetSubmitButtonDisabled(elem);
				else
					hbRestoreSubmitButtonState(elem);
		}
	}		
}

function hbRestoreSubmitButtonState(btn)
{
	if (typeof(btn.originalColor) == "string")
		btn.style.color = btn.originalColor;

	if (btn.alreadyClicked)
		btn.alreadyClicked = false;

	if (typeof(btn.lastDisabled) != "undefined")
		btn.disabled = btn.lastDisabled;

	if (document.body.popupBar)
		document.body.removeChild(document.body.popupBar);

	if (document.body.framePad)
		document.body.removeChild(document.body.framePad);
	
	document.body.popupBar = null;
	document.body.slidingBar = null;
	document.body.framePad = null;
}

function hbSetSubmitButtonDisabled(btn)
{
	if (btn.tagName == "A")
	{
		btn.lastDisabled = btn.disabled;
		btn.disabled = true;
	}
	else
	{
		btn.lastDisabled = btn.disabled;

		if (!btn.alreadyClicked)
		{
			btn.alreadyClicked = true;
			btn.originalColor = btn.style.color;
			btn.style.color = "gray";
		}
		else
		{
			btn.disabled = true;
		}
	}
}

var m_hbStoppedFlag = false;

function onHBDocumentStopped()
{
	if (m_formID)	//页面从基类派生，才会有此变量
	{
		var form = document.all(m_formID);

		if (m_hbStoppedFlag)
		{
			hbSetAllSubmitButtonsState(form, false);
			m_hbStoppedFlag = false;

			m_hbSubmitControl = false;
			m_hbSubmitControlCaption = "";
		}
		else
			m_hbStoppedFlag = true;
	}
}

document.attachEvent("onstop", onHBDocumentStopped);

function onBarInterval()
{
	var sBar = document.body.slidingBar;

	if (sBar);
	{
		try
		{
			var percent = sBar.percent;

			if (percent < 100)
			{
				percent++;
				sBar.style.width = percent + "%";
				sBar.percent = percent;
			}
		}
		catch(e)
		{
		}
	}
}

function createInnerPopupTable(container, caption)
{
	var table = document.createElement("table");
	
	with(table)
	{
		style.width = "100%";
		style.height = "100%";
		style.zIndex = 2500;
	}

	container.appendChild(table);

	var oRow = table.insertRow();

	var oCellText = oRow.insertCell();
	oCellText.align = "center";
	oCellText.innerText = caption;
	oCellText.style.fontWeight = "bold";
	oCellText.style.fontSize = "16px";

	var divBarContainer = document.createElement("<div style='width:80%;height:16;border:1px solid silver'></div>");
	divBarContainer.innerHTML = "<table width='100%' height='100%' cellspacing=0 cellpadding=0></table>";

	oCellText.appendChild(divBarContainer);
	
	table.style.left = adjustScrollLeft((document.body.offsetWidth - table.offsetWidth) / 2);
	table.style.top = adjustScrollTop((document.body.offsetHeight - table.offsetHeight) / 2);

	var oBarRow = divBarContainer.firstChild.insertRow();
	var oBarCell = oBarRow.insertCell();
	
	var sBar = document.createElement("<div align='left' style='width:0%;height:100%;background-color:blue;filter:alpha(opacity=25, style=1, finishOpacity=100, startx=0, starty=0, finishx=0, finishy=24)'></div>");
	oBarCell.align = "left";
	oBarCell.appendChild(sBar);

	document.body.slidingBar = sBar;
	document.body.slidingBar.percent = 0;
	
	window.setInterval(onBarInterval, 200);
}

function generatePopupPad(caption, w, h)
{
	if (!w)
		w = 320;

	if (!h)
		h = 160;

	var frm = document.createElement("iframe");

	with(frm)
	{
		style.width = w;
		style.height = h;
		style.position = "absolute";
		style.zIndex = 1000;
		frameBorder = 0;
	}
	document.body.appendChild(frm);

	frm.style.left = adjustScrollLeft((document.body.offsetWidth - frm.offsetWidth) / 2);
	frm.style.top = adjustScrollTop((document.body.offsetHeight - frm.offsetHeight) / 2);

	var div = document.createElement("div");
	
	with(div)
	{
		style.position = "absolute";
		style.zIndex = 1500;
		style.width = w;
		style.height = h;					

		style.borderLeft = "1px solid #666666";
		style.borderTop = "1px solid #666666";
		style.borderRight = "2px solid gray";
		style.borderBottom = "2px solid gray";
	}
	
	document.body.appendChild(div);

	div.style.left = adjustScrollLeft((document.body.offsetWidth - div.offsetWidth) / 2);
	div.style.top = adjustScrollTop((document.body.offsetHeight - div.offsetHeight) / 2);
	
	createInnerPopupTable(div, caption);
	
	document.body.popupBar = div;
	document.body.framePad = frm;
}

function adjustScrollLeft(l)
{
	return l + document.body.scrollLeft;
}

function adjustScrollTop(t)
{
	return t + document.body.scrollTop;
}

//PostToIFrame and drop it
function formPostToIframe()
{
	if (m_formID)
	{
		var form = document.all(m_formID);

		document.all("_baseformEnvParams").value = "postToFrame";
		var oldTarget = form.target;
		form.target = "_innerPostTargetFrame";

		form.submit();
		form.target = oldTarget;
		
		document.all("_baseformEnvParams").value = "";
	}
}
//-->
