// -----------------------------------------------------------------------------
// -
// - requires prototype to work
// - http://www.prototypejs.org/download
// - 
// -----------------------------------------------------------------------------


// -----------------------------------------------------------------------------
// - Observe window load event
// -----------------------------------------------------------------------------

Event.observe(window, 'load', init, false);

// -----------------------------------------------------------------------------
// - Constants
// -----------------------------------------------------------------------------

var WEB_SERVICE_URL = '/cgi-bin/pvc_council_webservice.cgi';
var FORM_NAME       = 'info_form';
var INPUT_NAME      = 'privacy_record_id';
var DEBUG           = 0;

// -----------------------------------------------------------------------------
// - Called on window load
// -----------------------------------------------------------------------------

function init()
  {
  
  // ---------------------------------------------------------------------------
  // - add new hidden element to hold webservice value
  // ---------------------------------------------------------------------------
    
  var new_element = document.createElement('input');
  new_element.writeAttribute( { type: 'hidden', value: -1, name: INPUT_NAME, id: INPUT_NAME });
  $( FORM_NAME ).insert( { top: new_element } );
    
  $( FORM_NAME ).observe('submit', function( event )
    {

    // -------------------------------------------------------------------------
    // - stop submit event
    // -------------------------------------------------------------------------
    
    event.stop();
    
    // -------------------------------------------------------------------------
    // - validate form
    // -------------------------------------------------------------------------
    
    if( !onSubmitForm('info_form') )
      {  
      return;
      }
      
    // -------------------------------------------------------------------------
    // - cache form so we don't have to repeatedly look for it
    // -------------------------------------------------------------------------
    
    var input_form = this;
    
    // -------------------------------------------------------------------------
    // - make request to web service, on success, insert new dom input
    // - element with value returned from webservice
    // -------------------------------------------------------------------------
    
    new Ajax.Request( WEB_SERVICE_URL,
      {
      method: 'post',
      parameters: input_form.serialize(true),
      onSuccess: function(transport)
        {
        if( DEBUG ) { console.log( transport ); }
	      if( DEBUG ) { console.log(transport.responseText); }
        $(INPUT_NAME).writeAttribute( "value", transport.responseText);
        
        if( !DEBUG ) { input_form.submit(); }
        },
      onFailure: function()
        {
        if( !DEBUG ) { input_form.submit();  } 
        }
      });

    });
  } 


