//==================================================================================
// Form submission functions & page initialization functions
// Notes: 
// A) All functions in this file should take EITHER a form object or the form id.
// B) All form submission functions should attempt to call a function named pageSubmitForm.
//		pageSubmitForm must be attached to the window object.
//		1) This function will be called before the validation function is called.
//		2) This function will be also called by postBack functions.  Therefore, any code
//			 written in this function cannot assume that the user thinks this is a form
//			 submission.
// C) A function named initializePage is assigned to document.onload if it exists.
//		This function may be defined in javascript on the page or in an included js file.
//		Note that the body onload attribute cannot be defined in the page for this to 
//    work (winIE).
// D) window.isDoNotSubmit is checked each time a page is submitted.  If isDoNotSubmit
//		is true then the page is not submitted.  If the state of the page is invalid for 
//		submission, set window.isDoNotSubmit to true on the page to suppress submission 
//		until the state of the page is valid.
//==================================================================================

//==================================================================================
// submitForm( form, page, callerId, bSkipValidate, bSkipPageFunc, method, target ) - Submit the form 
// Parameters:
// form (string) - id of form or javascript representation of the form object.
// page (string) - the page this function will submit to.
// callerId (string, optional) - id of the caller button.
// bSkipValidate (boolean, optional) - determines whether validation is skipped (true = skip validation)
// bSkipPageFunc (boolean, optional) - determines whether the "PageSubmitForm" function is called (true = don't call).
// method (string, optional) - method form will be submitted (POST, GET)
// target (string, optional) - the target window that the form will be submitted to (for popups etc.)
// byState (boolean, optional) - if true, ignores the isDoNotSubmit variable and submits anyways.
//==================================================================================
function submitForm( form, page, callerId, bSkipValidate, bSkipPageFunc, method, target, byState ) {
	// Check if caller passed in the id instead of the form object.
	// If the method attribute is blank this check will fail.
	if( form && !form.method && document && document.getElementById ) {
		form = document.getElementById(form);
	}
	var bValid = prepareSubmit( form, page, callerId, bSkipValidate, bSkipPageFunc, method, target, byState );
	if( bValid ) {
		form.submit();
		return true;
	}
	return false;
}

function prepareSubmit( form, page, callerId, bSkipValidate, bSkipPageFunc, method, target, byState ) 
{
	return _prepareSubmit( form, page, callerId, bSkipValidate ? false : true, bSkipPageFunc ? false : true, method, target, byState, true );
}

//==================================================================================
// function _prepareSubmit( form, page, callerId, bSkipValidate, bSkipPageFunc ) - A 
// general function to handle submittal preparation. Hide viewstate if form is to be 
// submitted to another page.  This function (hideViewState)
// must be called when we could be submitting to another aspx page.
// Parameters:
// form (string) - id of form or javascript representation of the form object.
// page (string) - the page this function will submit to.
// callerId (string, optional) - id of the caller button.
// bSkipValidate (boolean, optional) - determines whether validation is skipped (true = skip validation)
// bSkipPageFunc (boolean, optional) - determines whether the "PageSubmitForm" function is called (true = don't call).
// method (string, optional) - method form will be submitted (POST, GET)
// target (string, optional) - the target window that the form will be submitted to (for popups etc.)
// byState (boolean, optional) - if true, ignores the isDoNotSubmit variable and submits anyways.
// bHideViewState (boolean, optional ) - if true, will hide viewstate if the page is not a postback.
//==================================================================================
function _prepareSubmit( form, page, callerId, bValidate, bCallPageFunc, method, target, byState, bHideViewState ) 
{
	// Indicates that there is a reason to not submit the form.
	// If so, fail out of the submit.
	if( window.isDoNotSubmit && !byState ) {
		return false;
	}
	
	// Check if caller passed in the id instead of the form object.
	// If the method attribute is blank this check will fail.
	if( form && !form.method && document && document.getElementById ) {
		form = document.getElementById( form );
	}
	
	if( form ) {

		var bValid = true;
		
		// Call "custom" function defined by page (if defined).
		// If not defined, ignore it and move on.
		if( bCallPageFunc && bValid && window.pageSubmitForm ) {
			bValid = window.pageSubmitForm( form, callerId );
		}

		// Call validator function defined by page (if defined).
		if( bValidate && bValid && typeof(validate) != 'undefined' ) {
			
			bValid = validate( callerId );
		}

		if( bValid ) {
			form.action = page;

			// Hide viewstate if form is to be submitted to another page.
			if( bHideViewState ) {
				hideViewState( form );
			} else {
				showViewState( form );
			}
			
			if( method ) {
				form.method = method;
			} else {
				form.method = "post";
			}
			if( target ) {
				form.target = target;
			} else {
				form.target = "";
			}
		}
		return bValid;
	}
}

//======================================================================
// postBack( form, callerId, bValidate, bCallPageFunc ) - submits page back to itself for server side processing.
// Parameters:
// form (string) - id of form or javascript representation of the form object.
// callerId (string, optional) - id of the caller button.
// bValidate (boolean, optional) - determines whether validation is skipped (true = perform validation)
// bCallPageFunc (boolean, optional) - determines whether the "PageSubmitForm" function is called (true = call func).
// byState (boolean, optional) - if true, ignores the isDoNotSubmit variable and submits anyways.
// formTarget (string, optional) - target to direct the form to.
//======================================================================
function postBack( form, callerId, bValidate, bCallPageFunc, bHideViewState, byState, formTarget ) {
	// Check if caller passed in the id instead of the form object.
	// If the method attribute is blank this check will fail.
	if( form && !form.method && document && document.getElementById ) {
		form = document.getElementById( form );
	}
	if (form && formTarget)
	{
		form.target = formTarget;
	}
	//var bValid = preparePostBack( form, callerId, bValidate, bCallPageFunc, bHideViewState, byState );
	var bValid = true;
	// submit the form
	if( bValid ) {
		form.submit();
		return true;
	}
	return false;
}

//======================================================================
// preparePostBack( form, callerId, bValidate, bCallPageFunc ) - prepare 
// page to be posted back to itself, but do not do the final submission.
// Parameters:
// form (string) - id of form or javascript representation of the form object.
// callerId (string, optional) - id of the caller button.
// bValidate (boolean, optional) - determines whether validation is skipped (true = perform validation)
// bCallPageFunc (boolean, optional) - determines whether the "PageSubmitForm" function is called (true = call func).
// bHideViewState (boolean optional) - determines whether you want to hide viewstate.
// byState (boolean, optional) - if true, ignores the isDoNotSubmit variable and submits anyways.
//======================================================================
function preparePostBack( form, callerId, bValidate, bCallPageFunc, bHideViewState, byState ) {
	return _prepareSubmit( form, "", callerId, bValidate, bCallPageFunc, "post", "", byState, bHideViewState );
}

//==================================================================================
// hideViewState( form ) - Hides the view state of the form if applicable (ASP.NET).
//	Do this so that we can submit to another aspx page without error.  This function 
// is usually called by prepareSubmit().
// Parameters:
// form - The form element.
//==================================================================================
function hideViewState( form ) {
	if( form ) {
		if( form.__VIEWSTATE ) form.__VIEWSTATE.name = "__HIDEVIEWSTATE";
		if( form.__EVENTTARGET ) form.__EVENTTARGET.name = "__HIDEEVENTTARGET";
		if( form.__EVENTARGUEMENT ) form.__EVENTARGUEMENT.name = "__HIDEEVENTARGUEMENT";
	}
}

//==================================================================================
// showViewState( form ) - Shows the view state of the form if applicable (ASP.NET)
// and if it has been previously hidden.  Hiding view state seems to be a problem
// for IE6.
// Parameters:
// form - The form element.
//==================================================================================
function showViewState( form ) {
	if( form ) {
		if( form.__HIDEVIEWSTATE ) form.__HIDEVIEWSTATE.name = "__VIEWSTATE";
		if( form.__HIDEEVENTTARGET ) form.__HIDEEVENTTARGET.name = "__EVENTTARGET";
		if( form.__HIDEEVENTARGUEMENT ) form.__HIDEEVENTARGUEMENT.name = "__EVENTARGUEMENT";		
	}
}

//========================================================================
// submitFormToPopup( width, height, name, form, page, callerId, bSkipValidate, bSkipPageFunc, method, byState)
// Submits form to a popup window.
// Please note: this function depends on clientfunctions.js
// Parameters:
//	width - width of popup window
//	height - height of popup window
//  name - name of the popup window
//	form - id of form or form object
//	page - page to submit to
//  callerId - button that was clicked
//	....
//========================================================================
function submitFormToPopup(width, height, name, form, page, callerId, bSkipValidate, bSkipPageFunc, method, resize, byState) 
{
	if( form && !form.method && document && document.getElementById ) {
		// form must be formId.
		form = document.getElementById( form );
	}
	
	if( form ) {
		var resizable = "yes";
		if (resize) resizable = resize;
		
		var bValid = true;
		if( !bSkipValidate ) {
			if( typeof( validate ) != 'undefined' )
				bValid = validate( callerId );
		}
			
		if( bValid ) {
			var origAction = form.action;
			var win = OpenNewWindow( "about:blank", name, width, height, 'yes', 'no', 'no', 'no', 'no', resizable);
			// Note: validation has already been executed - skip validation.
			var isSubmit = submitForm( form, page, callerId, true, bSkipPageFunc, method, name, byState );
			form.target = '';
			form.action = origAction;
		}
	}
}

//========================================================================
// submitFormToOpener( formId, bCloseWin, method, callerId, bSkipValidate, bSkipPageFunc, byState )
// Parameters:
// formId - 
//========================================================================
function submitFormToOpener( formId, bCloseWin, method, callerId, bSkipValidate, bSkipPageFunc, byState ) {
	if( window ) {
		if( formId && window.opener && document && document.getElementById ) {
			// submit popup to the opener page.
			var form = document.getElementById( formId );
			var target = window.opener.name;
			if( !target ) {
				target = window.opener.name = "pumpflo_main_win";
			}
			var action = window.opener.location.href;
			alert( action );
			submitForm( form, action, callerId, bSkipValidate, bSkipPageFunc, method, target );
			if( bCloseWin ) window.close();
		}
	}
}

//========================================================================
// closePopup( formId, bPostBackOpener ) - Closes a popup window and reloads
//									the opener page (if proper parameter passed).
// Parameters:
// formId - (string, optional) the id of the opener window's form.  If id is
//							not specified, then opener form is not submitted.
//========================================================================
function closePopup( formId, bPostBackOpener, byState ) {
	if( window ) {
		if( formId && window.opener && window.opener.document && window.opener.document.getElementById ) {
			var form = window.opener.document.getElementById( formId );
			// submit the opener page's form - hide the viewstate (in case language has been changed).
			if( bPostBackOpener ) postBack( form, null, false, false, true, byState );
		}
		// close the graphoptions window.
		window.close();
	}
}