Event.onDOMReady( function(){
    if( $( 'saved-offers' ) )
    {
        $$( '.delete-offer' ).each( function( offer )
        {
            offer.observe( 'click', function( event ) {
                confirmresult = confirm( 'Napewno usunąć ofertę?' );
                if( confirmresult == true )
                { 
                    return true;
                }
                else
                {
                    Event.stop( event );
                    return false;
                }
            } );
        } );
    }
    
    if( $( 'leasing-calculator' ) )
    {
         new Insertion.After('moreoptions', '<div class="input"><label for="LeasingGap">Ubezpieczenie GAP</label><input style="text-align:right;" readonly="readonly" type="text" id="LeasingGap" value="0" name="data[Leasing][gap]"/></div>');
        
        
                              if( $( 'error' ) )
                              {
                                $('error').update('');
                                $('error').hide();
                              }
                              
                              new Insertion.After('calculateLeasingForm', '<div id="result" style="display: none;"></div>');
                              new Insertion.After('result', '<div id="loading" style="display:none;"><img src="/img/site/loading.gif" alt="...ładowanie" /></div>');
                              
                              if( $( 'LeasingObjectGroup' ) )
                              {
                                Event.observe( 'LeasingObjectGroup',
                                             'change',
                                             function(event){
                                                              var timestamp = new Date();
                                                              new Ajax.Updater( 'objectGroupSpecific',
                                                                                '/leasing/grupa/'+$('LeasingObjectGroup').value + '?' + timestamp.getTime(),
                                                                                { asynchronous:true,
                                                                                  evalScripts:true,
                                                                                  requestHeaders:['X-Update', 'objectGroupSpecific'],
                                                                                  onComplete:function(){
                                                                                	  								  Event.observe( 'LeasingPeriod',
                                                                                                                                     'change',
                                                                                                                                     function(event){
                                                                                                                                                      var timestamp = new Date();
                                                                                                                                                      new Ajax.Updater( 'moreoptions',
                                                                                                                                                                        '/leasing/wartosc_koncowa/'+$('LeasingPeriod').value + '?' + timestamp.getTime(),
                                                                                                                                                                        { asynchronous:true,
                                                                                                                                                                          evalScripts:true,
                                                                                                                                                                          requestHeaders:['X-Update', 'moreoptions']
                                                                                                                                                                        }
                                                                                                                                                                      );
                                                                                                                                                    }
                                                                                                                                   );
                                                                                                                    }
                                                                                }
                                                                              );
                                                            }
                                           );
                              }
                              
                              if( $( 'LeasingType' ) )
                              {
                                  Event.observe( 'LeasingType',
                                                 'change',
                                                 function(event){
                                                    if( $('LeasingType').value == 1 )
                                                    {
                                                        $( 'moreoptions' ).setStyle( { display: 'none' } );
                                                    }
                                                    else
                                                    {
                                                        $( 'moreoptions' ).setStyle( { display: 'block' } );
                                                    }
                                                 }
                                               );
                              }
                              
                              if( $( 'LeasingPeriod' ) )
                              {
                                  Event.observe( 'LeasingPeriod',
                                                 'change',
                                                 function(event){
                                                    var leasingAmount = parseInt( $( 'LeasingAmount' ).value );
                                                    if( !leasingAmount ) {
                                                        leasingAmount = 0;
                                                    }
                                                    var leasingPeriod = parseInt( $( 'LeasingPeriod' + $F( 'LeasingPeriod' ) ).text );
                                                    if( !leasingPeriod ) {
                                                        leasingPeriod = 0;
                                                    }
                                                    //$( 'LeasingGap' ).value = leasingAmount * parseFloat( 0.0005 ) *  leasingPeriod;
                                                    $( 'LeasingGap' ).value = leasingAmount * parseFloat( 0.0005 );
                                                                  var timestamp = new Date();
                                                                  new Ajax.Updater( 'moreoptions',
                                                                                    '/leasing/wartosc_koncowa/'+$('LeasingPeriod').value + '?' + timestamp.getTime(),
                                                                                    { asynchronous:true,
                                                                                      evalScripts:true,
                                                                                      requestHeaders:['X-Update', 'moreoptions']
                                                                                    }
                                                                                  );
                                                                }
                                               );
                              }
                              
                              if( $( 'LeasingAmount' ) ) {
                                Event.observe( 'LeasingAmount',
                                                 'change',
                                                 function(event){
                                                    var leasingAmount = parseInt( $( 'LeasingAmount' ).value );
                                                    if( !leasingAmount ) {
                                                        leasingAmount = 0;
                                                    }
                                                    var leasingPeriod = parseInt( $( 'LeasingPeriod' + $F( 'LeasingPeriod' ) ).text );
                                                    if( !leasingPeriod ) {
                                                        leasingPeriod = 0;
                                                    }
                                                    //$( 'LeasingGap' ).value = leasingAmount * parseFloat( 0.0005 ) *  leasingPeriod;
                                                    $( 'LeasingGap' ).value = leasingAmount * parseFloat( 0.0005 );
                                                 } );
                              }
                              
                              if( $( 'submitCalculateLeasingForm' ) )
                              {
                                  Event.observe( 'submitCalculateLeasingForm', 
                                                 'click',
                                                 function(event){
                                                                  if( $('LeasingAmount').value == '' )
                                                                  {
                                                                    $('error').update('Proszę wpisać kwotę');
                                                                    $('error').show();
                                                                  }
                                                                  else
                                                                  {
                                                                      $('error').hide();
                                                                      $('result').hide();
                                                                      $('loading').show();
                                                                        var timestamp = new Date();
                                                                        new Ajax.Updater( 'result',
                                                                                        '/leasing/oblicz' + '?' + timestamp.getTime(),
                                                                                        { asynchronous:true,
                                                                                          evalScripts:true,
                                                                                          onComplete:function(request, json){
                                                                                          	  								  $('result').show();
                                                                                          	  								  $('loading').hide();
                                                                                                                            },
                                                                                          parameters:Form.serialize(Event.element(event).form),
                                                                                          requestHeaders:['X-Update', 'result']
                                                                                        }
                                                                                       );
                                                                  }
                                                                }
                                               );
                                }
    }
} );


