﻿function initPageForm(form)
{
	if (window._formFunctions == null) {
		window._formFunctions = 
		{
			isPostBack: function()
			{
				return castBool(this.getAttribute("_isPostBack"));
			},

			/// <remarks>
			///		This is the standard .NET 1.1 postback handler.  It is here as a backup.
			///		If there are no standard ASP.NET controls on the page, the Page object 
			///		does not output this javascript.  All the related functions in the Page
			///		object are marked as "internal" so we can NOT officialy hook or patch
			///		this functionality.  Therefore we had to copy it.
			/// </remarks>
			/// <remarks>
			///		Some of our FirstNet pages actualy call this function directly through
			///		frmPage.  This is bad behaviour and should be discouraged.  *Always* call
			///		the global __doPostBack() handler, this will retain max compatibility 
			///		with future .NET versions.
			/// </remarks>
			doPostBack: function(eventTarget, eventArgument)
			{
				var theform;
				if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1)
					theform = document.frmPage;
				else
					theform = document.forms["frmPage"];

				theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
				theform.__EVENTARGUMENT.value = eventArgument;
				theform.submit();
			},

			doCallBack: function (eventTarget, eventArgument, completionFunction, suppressError, context)
			{
				try
				{
					if (this._callBack_IsSubmitted)
						{alert("Data has been already submitted."); return;}
					//
					this._callBack_CompletionFunction = completionFunction;
					this._callBack_SuppressError = suppressError;
					this._callBack_Context = context;
					//
					this.__SCRIPTCALLBACKID.value = castStr(eventTarget);
					if (eventArgument)
						this.__SCRIPTCALLBACKPARAM.value = castStr(eventArgument);
					//
					this._callBack_IsSubmitted = true;
					this.target = "fraPageCallBack";
					//
					window.postAction("frmPage.submit(); frmPage.target='';"); // !!! status bar progress indicator completion problem
				}
				catch (e) {displayErr("doCallBack", e);}
			},

			_onCallBackCompleted: function(status, data)
			{
				try
				{
					this._callBack_IsSubmitted = false;
					this.target = "";
					this.__SCRIPTCALLBACKID.value = "";
					this.__SCRIPTCALLBACKPARAM.value = "";
					//
					suppressError = this._callBack_SuppressError;			this._callBack_SuppressError = null;
					completionFunction = this._callBack_CompletionFunction;	this._callBack_CompletionFunction = null;
					//
					var result = new Object();
					result.status = status;
					result.data = data;
					result.context = this._callBack_Context;				this._callBack_Context = null;
					//
					if (!result.status && (suppressError == null || !suppressError))
						alert(result.data);
					//
					if (completionFunction != null)
						completionFunction(result);
					else if (window.window_OnCallBackComplete)
						window.window_OnCallBackComplete(eventTarget, result);
				}
				catch (e) {displayErr("_onCallBackCompleted", e);}
			},

			cleanUp: function()
			{
				window.__doPostBack = null;
			}
		};
	}
	
	//	
	form = extendElement(form);	
	form = Object.extend(form, window._formFunctions);

	// initialize
	if (window.__doPostBack == null)
		window.__doPostBack = form.doPostBack;

	//
	return form;
}
