/**
* This Javascript file contains basic utility functions for handling HTML forms.
* It should be placed in the client/rw3 web directory.  To override this file,
* change $GLOBAL["CONFIG"]["JAVASCRIPT"]["GENERAL"] in the client's config.inc
* file and place the new file in the client's web directory.
*
* Where possible, these functions should be called using the methods in a PHP
* cJavascript object. Conversely, if a new function is added to this file, a
* new method should be added to the cJavascript class to output a Javascript
* function call.
*
* @see cJavascript
*
* @package Framework
* @author kandrews
* @version 1.0
* @copyright Copyright (C) 2000 - 2004 RW3 Technologies, Inc. All rights reserved.
*/

var vDebug = 0;

//------------------------------------------------------------------------------
/**
* Sets an optional FormAction field, then validates the form & submits if it passes validation
* @param  Form   pFormObj    Form object to be submitted
* @param  string pActionName Optional value to set the FormAction field
* @return mixed              Does not return if the form passes validation; otherwise returns true
*/
function fSubmit (pFormObj, pActionName)
{
   if (vDebug) { alert('fSubmit('+pFormObj+', '+pActionName+')'); }
   if (pActionName) { fSetField(pFormObj, 'hFormAction',pActionName); }
   pFormObj.submit();
}

//------------------------------------------------------------------------------
/**
* Sets an optional FormAction field, then validates the form & submits if it passes validation
* @param  Form   pFormObj    Form object to be submitted
* @param  string pActionName Optional value to set the FormAction field
* @return mixed              Does not return if the form passes validation; otherwise returns true
*/
function fSave (pFormObj, pActionName)
{
   if (vDebug) { alert('fSave('+pFormObj+', '+pActionName+')'); }
   if (fValidate(pFormObj))
   //if (true || fValidate(pFormObj))
   {
      fSetField(pFormObj, 'hValidate', 'true');
      fSubmit(pFormObj, pActionName);
   }
   else
   {
      return;
   }
}

function fCancel (pFormObj, pActionName)
{
   if (vDebug) { alert('fCancel('+pFormObj+', '+pActionName+')'); }
   fSubmit(pFormObj, pActionName);
}

//------------------------------------------------------------------------------
/**
* Sets an optional FormAction field, then validates the form on the server side
* This is the same as fSave expect is doesn't do client side validation 
* @param  Form   pFormObj    Form object to be submitted
* @param  string pActionName Optional value to set the FormAction field
* @return mixed              Does not return if the form passes validation; otherwise returns true
*/
function fSave_NOJS (pFormObj, pActionName)
{
   if (vDebug) { alert('fSave('+pFormObj+', '+pActionName+')'); }
      fSetField(pFormObj, 'hValidate', 'true');
      fSubmit(pFormObj, pActionName);
}

//------------------------------------------------------------------------------
/**
* Sets an optional FormAction field, then resets the form
* @param  Form   pFormObj    Form object to be submitted
* @param  string pActionName Optional value to set the FormAction field
* @return mixed              Does not return if the form passes validation; otherwise returns true
*/
function fReset (pFormObj, pActionName)
{
   if (vDebug) { alert('fReset('+pFormObj+', '+pActionName+')'); }
   if (pActionName) { fSetField(pFormObj, 'hFormAction',pActionName); }
   pFormObj.reset();
}

//------------------------------------------------------------------------------
/**
* Sets an optional FormAction field, then prints the page
* @param  Form   pFormObj    Form object to be submitted
* @param  string pActionName Optional value to set the FormAction field
* @return mixed              Does not return if the form passes validation; otherwise returns true
*/
function fPrint (pFormObj, pActionName)
{
   if (vDebug) { alert('fReset('+pFormObj+', '+pActionName+')'); }
   if (pActionName) { fSetField(pFormObj, 'hFormAction',pActionName); }
   window.print();
}

//------------------------------------------------------------------------------
/**
* Sets an optional FormAction field, then submits the form
* @param  Form   pFormObj    Form object to be submitted
* @param  string pActionName Optional value to set the FormAction field
* @return none               Does not return (submits form)
*/
function fLink (pFormObj, pActionName)
{
   if (vDebug) { alert('fLink('+pFormObj+', '+pActionName+')'); }
   fSubmit(pFormObj, pActionName);
}

//------------------------------------------------------------------------------
/**
* Sets a field to a given value
* @param Form   pFormObj   Form object containing the field to be set
* @param string pFieldName Name of the field to be set
* @param string pValue     The field's new value
*/
function fSetField (pFormObj, pFieldName, pValue)
{
   //vDebug=true;
   if (vDebug) { alert('fSetField('+pFormObj+', '+pFieldName+', '+pValue+')'); }
   //if (vDebug) { i=0; while (pFormObj.elements[i] != null) {alert(pFormObj.elements[i].name); i++;} }
   pFormObj.elements[pFieldName].value = pValue;
   //vDebug=false;
}

//------------------------------------------------------------------------------
/**
* Gives focus to a field.  Obviously, if this function gets called multiple times, the last call
* determines which field gets focus.
* @param Form   pFormObj   Form object containing the field
* @param string pFieldName Name of the field
*/
function fSetFocus (pFormObj, pFieldName)
{
   if (vDebug) { alert('fSetFocus('+pFormObj+', '+pFieldName+')'); }
   pFormObj.elements[pFieldName].focus();
}
