// forms should be registered here
var forms = new Object();

// adds the new form to forms
// if there is a form sith ID of the one, currently adding already
// it will be averriden
forms.add = function( formDescription )
{
  // adding the new form description
  this[formDescription['id']] = formDescription;
}

// -------------------------------------------------------
// returns the new form description (but does not register it,
// use forms.add to register a form)
function createFormDescription( id, requestUrl, preferredFieldId, caption, onSubmit, onResponse, submitButtonCaption )
{
  var output = new Array();
  
  output['id'] = id;
  output['requestUrl'] = requestUrl;
  output['caption'] = caption;
  output['preferredFieldId'] = preferredFieldId;
  output['onSubmit'] = onSubmit;
  output['onResponse'] = onResponse;
  output['submitButtonCaption'] = submitButtonCaption;
  output['fields'] = new Array();
  
  // adding fields management methods
  for ( var controlType in controlsTypes )
  {
    output[controlType+"ControlAdd"] = controlsTypes[controlType].addToForm;
  };
  
  //   Adds the new base control description to the form, given
  //   The form must be a valid form
  //   If there already is a control with ID, given, it will be replaced
  // with new
  //   This function should be called inside the corresponding controls
  // adding routines as a base control adding function
  output.addBaseControl = function( id, defaultValue )
  {
    var control = new Array();
    
    control['id'] = id;
    control['type'] = 'base';
    control['value'] = defaultValue;
    
    this['fields'][id] = control;
  };
  
  // generates the form text and returns it. 
  // The form container will be filled with the form description either
  output.generateHtml = function()
  {
    // generating controls
    var output = this['caption']+"<br />";
    
    for ( var field in this['fields'] )
    {
      output += controlsTypes[this['fields'][field]['type']].
          generateHtml( this['id'], this['fields'][field] );
    };
    
    output += "<div class=\"buttoninput\"><input onclick=\"javascript: submitForm('"+this['id']+"')\" type=\"button\" value=\""+this['submitButtonCaption']+"\" /></div>";
  
    return output;
  };

  return output;
}

// -------------------------------------------------------
// returns the control ID
function getInternalControlId( formId, controlDescription )
{
  return formId + "_" + controlDescription['id'];
}

// Saves the value from the control into the control values array (forms)
function saveControlValue( formId, controlId, value )
{
  forms[formId]['fields'][controlId]['value'] = value;
}

// Returns the control value
function getControlValue( formId, controlId )
{
  return forms[formId]['fields'][controlId]['value'];
}

// Sets the control value: to controls values and populates to the control itself then
function setControlValue( formId, controlId, value )
{
  saveControlValue( formId, controlId, value );
  
  controlsTypes[forms[formId]['fields'][controlId]['type']].
      populateProperties( formId, forms[formId]['fields'][controlId] );
}

// loads the values from form control values array (forms) into 
// the form items
function populateFormControls( formId )
{
  var form = forms[formId];
  if ( !form )
    return;

  for ( var field in form['fields'] )
  {
    controlsTypes[form['fields'][field]['type']].
        populateProperties( formId, form['fields'][field] );
  };
}

// focuses the preferred control of the mini form, container specified
function focusFormPreferredControl( formId )
{
  var form = forms[formId];
  if ( !form )
    return;

  // due to the delay in hints, to focus in IE, must wait the
  // subject to be shown
  if ( form['fields'][form['preferredFieldId']] )
    setTimeout
    ( 
      function()
      {
        focusElementById
        ( 
          form['id'] + "_" + form['fields']
            [form['preferredFieldId']]['id']
        );
      }, hintHidingDelay*2
    );
}

// asks the user to confirm the submitting an submitting the
// form for the button with the index, specified
function submitForm( formId )
{
  var form = forms[formId];
  if ( !form )
    return;

  if ( form['onSubmit']( form ) ) 
  {
    // saving the latest changes
    //storeFakeDocumentParams( form );
    // building the list of params
    var params = new Array();
      
    var requestUrl = form['requestUrl'];
  
    // filling params  
    for ( var fieldId in form['fields'] )
    {
      // the function is the parameter, which should be passed in the request URL
      //if ( form['fields'][fieldId]['id'] == PARAM_FUNCTION )
      //  requestUrl += "?" + PARAM_FUNCTION + "=" + form['fields'][fieldId]['value'];
      //else
        // storing a value
        params[fieldId] = form['fields'][fieldId]['value'];
    };

    // submitting
    sendUrlResponseTextToFunction
    ( 
      requestUrl, 
      function( response )
      {
        form['onResponse']( response );
      },
      params
    );
    //obtainElementById( getManagementButtonIdByIndex( buttonIndex ) ).submit();
  };
}

// this function might be used as the submit callback
// function for simple forms
function defaultFormSubmitCallbackFunction( form )
{
  return confirm( "Run command?\n(" + form["caption"] + ")" );
}

// this function might be used as the response callback
// function for simple forms
function defaultFormResponseCallbackFunction( response )
{
  if ( response != RESPONSE_REQUEST_PROCEEDED_SUCCESSFULLY )
    alert( response );
}

// dependency is loaded
dependencyLoaded('forms');
