//////////////////////////////////////////////////////////////////////////////
//
//	Method:  		f_save
//	
//	Arguments:		aa_dwArray[] 	dwo array		- an array of dwo to be updated
//					aa_parmName[]	string array	- array of parameter names
//					aa_parmVal[]	string array	- array of parameter values
//					ab_resetParms	boolean			- if true, reset existing params.
//
//	Returns:  		none
//						
//	Description:  	Creates and submits a form containing the action and context
//					from multiple datawindows. Allows parameters to be added or
//					replace or cleared from the current URL.
//
// 	Modifications	
//	2005.01.01		Cynergy Systems	Original function supplied in AdvEAF course.
//	2005.09.08		Herb			Allow the modification of href parms.
//	2005.11.29		Frank			Add a new function f_dw_submit to enable f_save
//                                    and f_insert_rows and share the code.
//
//////////////////////////////////////////////////////////////////////////////
function f_save(aa_dwArray, aa_parmName, aa_parmVal, ab_resetParms){
	// Call f_dw_submit(), pass Update as the action argument.
	f_dw_submit(aa_dwArray, aa_parmName, aa_parmVal, ab_resetParms, "Update")
}


//////////////////////////////////////////////////////////////////////////////
//
//	Method:  		f_dw_submit
//	
//	Arguments:		aa_dwArray[] 	dwo array		- an array of dwo to be actioned
//					aa_parmName[]	string array	- array of parameter names
//					aa_parmVal[]	string array	- array of parameter values
//					ab_resetParms	boolean			- if true, reset existing params.
//					as_action			            - action to be submit.
//                   
//	Returns:  		none
//						
//	Description:  	Creates and submits a form containing the action and context
//					from multiple datawindows. Allows parameters to be added or
//					replace or cleared from the current URL.
//
// 	Modifications	
//	2005.11.29		Frank           Extract from f_save().
//
//////////////////////////////////////////////////////////////////////////////
function f_dw_submit(aa_dwArray, aa_parmName, aa_parmVal, ab_resetParms, as_action){
	
	
	//************************************************************************
	// Check arguments.
	//************************************************************************
	var ll_dwCount = aa_dwArray.length;
	if ( ll_dwCount == 0 ){
		alert('Application Error (f_dw_submit) Datawindow array empty. ');
		return 0;
	}
	
	// Check if optional arg were supplied.
	// - If not then set the parms to empty arrays.
	if ( aa_parmName == null ){
		aa_parmName = new Array();
	}
	if ( aa_parmVal == null ){
		aa_parmVal = new Array();
	}

	// Ensure the parameters arrays are equal lengths.
	var ll_parmNameCount = aa_parmName.length;
	var ll_parmValCount = aa_parmVal.length;
	if ( ll_parmNameCount != ll_parmValCount ){
		alert('Application Error (f_dw_submit) Name, Value parameters must be in pairs. ');
		return 0;
	}
	
	//************************************************************************
	// Modify the URL.
	//************************************************************************
	// Get the current URL.
	var	ls_href = document.location.href;
	
	// Check is we should clear existing params.
	if ( ab_resetParms ){
		ls_href = f_removeURLParameters( ls_href )
	}
	
	if ( ll_parmNameCount > 0  ){
		
		var ls_url_plus_newparameters = '';
		
		// Loop through parameter list
		for ( i=0; i < ll_parmNameCount; i++ ){
			
			// Check for a current param value (returned if the param already exists). 
			var ls_parm_value = f_getURLParameter( ls_href, aa_parmName[i] );
			if ( ls_parm_value == aa_parmVal[i] ){
				// Param Exists and value hasn't changed.
				// - Continue. 
			}else {
				// ******************************
				// *** Replace existing param ***
				// ******************************
				if (ls_parm_value.length > 0 ){
					//  - Param exists but the value has changed. 
					ls_url_plus_newparameters =  f_replaceURLParameter(ls_href, aa_parmName[i], aa_parmVal[i] )
					ls_href = ls_url_plus_newparameters;
				}else{
					// *********************
					// *** Add new param *** 
					// *********************
					// Append replacement parameter.
					ls_url_plus_newparameters =  f_appendURLParameter(ls_href, aa_parmName[i], aa_parmVal[i] )
					ls_href = ls_url_plus_newparameters;
				} 
			}
		} // End for loop
	}
	//alert(ls_href);
	
	//************************************************************************
	// Create Form.
	//************************************************************************
	var formElement = document.createElement( "form" );
	
	formElement.setAttribute( "name", "eafSubmitForm" );
	formElement.setAttribute( "method", "POST" );
	formElement.setAttribute( "action", ls_href );
	
	// Add the form to the document.
	document.body.appendChild( formElement );
	
	// Loop through dw list
	for ( dwIndex=0; dwIndex< ll_dwCount; dwIndex++ ){

		aa_dwArray[dwIndex].AcceptText();
		
		dwName = aa_dwArray[dwIndex].name;

		// Create Action element.
		var actionElement = document.createElement("input");
		actionElement.setAttribute("type", "hidden" );
		actionElement.setAttribute( "value", as_action );
		actionElement.setAttribute( "name", dwName + "_action" );

		// Create Context element.
		var contextElement = document.createElement("input");
		contextElement.setAttribute( "type", "hidden" );
		contextElement.setAttribute( "value", aa_dwArray[dwIndex].GetFullContext() );
		contextElement.setAttribute( "name", dwName + "_context" );

		// Add action and context to the form.
		formElement.appendChild(actionElement);
		formElement.appendChild(contextElement);
	}
	
	formElement.submit();
}

//////////////////////////////////////////////////////////////////////////////
//
//	Method:  		f_getURLParameter
//	
//	Arguments:		as_href			string 	- url 
//					as_ParamName 	string  - A parameter name
//
//	Returns:  		parameter value; 
//						
//	Description:  	Gets a URL parameter value and returns it. 
//					For example if the current URL is 
//					"...?opendocument=yes&id=testid" then calling 
//					getURLParam("id") will return "testid". 
//
// 	Modifications	
//	2005.09.09		Herb			Adapted from Marvin's f_getURLParam.
//////////////////////////////////////////////////////////////////////////////
function f_getURLParameter(as_href, as_ParamName){
	
	var ls_Return = "";
	
	// Check for params in URL.
  	if ( as_href.indexOf("?") > -1 ){
	  
		// Get the string of trailing parameters.
    	var ls_ParamString = as_href.substr(as_href.indexOf("?") + 1 ).toLowerCase();
		// Create array of param expressions (name=val).
    	var la_ParamExpr = ls_ParamString.split("&");
	
		// Loop through expressions.
    	for ( var li_ndx = 0; li_ndx < la_ParamExpr.length; li_ndx++ ){
			// Look for the matching param name.
      		if ( la_ParamExpr[li_ndx].indexOf(as_ParamName + "=") > -1 ){
				// Create array with name at index [0], val at index [1].  
        		var la_ParamParts = la_ParamExpr[li_ndx].split("=");
        		ls_Return = la_ParamParts[1];
        		break;
      		}
   	 	} // end for
 	 }
  return ls_Return;
}

//////////////////////////////////////////////////////////////////////////////
//
//	Method:  		f_replaceURLParameter
//	
//	Arguments:		as_href			string 	- url
//					as_ParamName  	string  - A parameter name that may exist. 
//					as_ParamValue 	string  - The new parameter value to put into it. 
//
//	Returns:  		The revised URL. 
//						
//	Description:  	Searches for the parameter name and, if it finds it
//					replaces any existing parameter value with
//	 				the provided parameter value.  
//					For example if the ls_href is 
//					"...?opendocument=yes&id=woof" then calling 
//					f_replaceURLParameter(ls_href, "id","bark") will replace 
//					"woof" with "bark". 
// 
// 	Modifications	
//	2005.09.09		Herb			Adapted from Marvins f_replaceURLParam.
//////////////////////////////////////////////////////////////////////////////
function f_replaceURLParameter( as_href, as_ParamName, as_ParamValue ){
	var ls_Return = "";
	
  	// Check for params in URL.
  	if ( as_href.indexOf("?") > -1 ){
		
		// Get the URL without trailing parameters.
		var ls_originalURL = as_href.substr(0,as_href.indexOf("?") );
		
		// Get the string of trailing parameters.
    	var ls_ParamString = as_href.substr(as_href.indexOf("?") + 1 ).toLowerCase();
		// Create array of param expressions (name=val).
    	var la_ParamExpr = ls_ParamString.split("&");
		
		// Loop through expressions. Build new expression string.
		var ls_NewParamString = '';
    	for ( var li_ndx = 0; li_ndx < la_ParamExpr.length; li_ndx++ ){
     	 	if ( la_ParamExpr[li_ndx].indexOf(as_ParamName + "=") > -1 ){
				// *************************
				// Found Matching Param Name
				// *************************
				// Append concatenation operator.
				if ( ls_NewParamString.length  > 0 ) {
					ls_NewParamString += '&'
				}
				// Append replacement parameter.
				ls_NewParamString += as_ParamName + '=' + as_ParamValue
				
      		}else {
				// *************************
				// Param Name does not match
				// *************************				
				// Append concatenation operator.
				if ( ls_NewParamString.length  > 0 ) {
					ls_NewParamString += '&'
				}
				// Append current parameter expression.
				ls_NewParamString += la_ParamExpr[li_ndx]
			}
			
		} // end for
  	}
	ls_Return = ls_originalURL + '?' + ls_NewParamString
	return ls_Return; 
}

//////////////////////////////////////////////////////////////////////////////
//
//	Method:  		f_appendURLParameter
//	
//	Arguments:		as_href			string 	- url
//					as_ParamName  	string  - A parameter name that may exist. 
//					as_ParamValue 	string  - The new parameter value to put into it. 
//
//	Returns:  		The revised URL. 
//						
//	Description:  	Append a parameter name and value.
// 
// 	Modifications	
//	2005.09.09		Herb			Initial version.
//////////////////////////////////////////////////////////////////////////////
function f_appendURLParameter( as_href, as_ParamName, as_ParamValue ){
	var ls_Return = "";
	
  	// Check for params in URL.
  	if ( as_href.indexOf("?") > -1 ){
		// Parameters exist, append param.
		as_href = as_href + '&' + as_ParamName + '=' + as_ParamValue;
	}else{
		// No Parameters exist.
		as_href = as_href + '?' + as_ParamName + '=' + as_ParamValue;
	}

	ls_Return = as_href
	return ls_Return; 
}

//////////////////////////////////////////////////////////////////////////////
//
//	Method:  		f_removeURLParameters
//	
//	Arguments:		as_href			string 	- url
//					as_ParamName  	string  - A parameter name that may exist. 
//					as_ParamValue 	string  - The new parameter value to put into it. 
//
//	Returns:  		The URL without parmameters.
//						
//	Description:  	Clear the existing parameters.
// 
// 	Modifications	
//	2005.09.09		Herb			Initial version.
//////////////////////////////////////////////////////////////////////////////
function f_removeURLParameters( as_href ){
	var ls_Return = "";
	
  	// Check for params in URL.
  	if ( as_href.indexOf("?") > -1 ){
		// Get the URL without trailing parameters.
		as_href = as_href.substr(0,as_href.indexOf("?") );
	}
	
	ls_Return = as_href
	return ls_Return; 
}



