$( document ).ready( function()
{
  $( 'form' ).validationEngine(
  {
    inlineValidation: false
  } );


  $( '.tooltip' ).tooltip(
    {
      track: true,
      delay: 0,
      showURL: false,
      showBody: " - ",
      fade: 250
    } );

  $( '#postocde' ).keyup( function( event )
  {
    postcode = $( '#postocde' ).val();

    if( /^[0-9]{2}\-[0-9]{3}$/.test( postcode ) )
    {
      $.ajax({
        url: strBaseUrl + 'index.php/ajax/postcode/' + postcode,
        beforeSend: function()
        {
          $( '#city' ).val( 'Proszę czekać...' );
          $( '#city' ).attr( 'disabled', 'disabled' );
        },
        success: function( city )
        {
          $( '#city' ).val( city );
          $( '#city' ).removeAttr( 'disabled' );
        }
      });
    }
  });



  $( '#credit_amount' ).keyup( function( event )
  {
    value = $( '#credit_amount' ).val().replace( / /g, '' );

    if( value != '' )
    {
      $( '#credit_amount' ).val( number_format( value, 0, ' ', ' ' ) );
    }
  } );
} );

function vehicleChange()
{
    v_type = $('#vehicle_type option:selected').val();
    v_state = $('#vehicle_state option:selected').val();
    /*$.ajax({
        url: '/kalkulator/index.php/ajax/purpose_vehicle/' + purpose,
        success: function( html )
        {
            ('#hiddenVehicle').html(html);
        }
      });*/
      $.ajax({
        url: strBaseUrl + 'index.php/ajax/getBranches/' + v_type +'/' +v_state,
        beforeSend: function()
        {
          $( '#vehicle_branch' ).html( '<option value="">Proszę czekać...</option>' );
          $( '#vehicle_branch' ).attr( 'disabled', 'disabled' );
        },
        success: function( html )
        {
          $( '#vehicle_branch' ).html( html);
          $( '#vehicle_branch' ).removeAttr( 'disabled' );
          branchChange();
        }
      });
}
function branchChange()
{
    v_type = $('#vehicle_type option:selected').val();
    v_state = $('#vehicle_state option:selected').val();
      v_branch = $('#vehicle_branch option:selected').val()
      $.ajax({
        url: strBaseUrl + 'index.php/ajax/getModels/'+v_branch +'/' + v_type +'/' +v_state,
        beforeSend: function()
        {
          $( '#vehicle_model' ).html( '<option value="">Proszę czekać...</option>' );
          $( '#vehicle_model' ).attr( 'disabled', 'disabled' );
        },
        success: function( html )
        {
          $( '#vehicle_model' ).html( html);
          $( '#vehicle_model' ).removeAttr( 'disabled' );
        }
      });
}

function number_format(a, b, c, d) {
 a = parseInt( a );
 a = Math.round(a * 1) / 1;
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;

 temp = f[0] + c + f[1];

 if( temp != 0 )
 {
   return f[0] + c + f[1];
 }
 else
 {
   return '';
 }
}
