var waitImg = new Image();
waitImg.src = "http://hipermarketfinansowy.pl/images/wait_small.gif";


function paramsChange(obj, field_no) {
  var insurance_form = document.insurance_form;
  var Today = new Date();
  var rok = Today.getFullYear()-10;

/*
  if ( view_example == 1 ) {
    view_example = 0;
    insurance_form.cena_poj.value='';
    insurance_form.kier_wiek.value='';
    insurance_form.kier_rok_upr.value='';
    insurance_form.kier_bez_upr.checked=false;
    insurance_form.kier_kod_poczt.value='';
    insurance_form.kier_plec.value=-1;
    insurance_form.kier_reduction_oc.value=0;
    insurance_form.kier_reduction_ac.value=0;

    var obj = document.getElementsByTagName("div");
    for (var i = 0; i < obj.length; i++ ) {
      if (obj[i].getAttribute("field") == "acoc_value") {
        obj[i].innerHTML='';
      }
    }

    obj = document.getElementsByTagName("input");
    for (var i = 0; i < obj.length; i++ ) {
      if (obj[i].getAttribute("field") == "acoc_order") {
        obj[i].style.display='none';
      }
    }

  }
*/
  switch ( field_no ) {
    case 1: {
      insurance_form.rok_prd.value=-1; insurance_form.rok_prd.setAttribute('disabled','disabled');
      insurance_form.marka.value=-1; insurance_form.marka.setAttribute('disabled','disabled');
      insurance_form.model.value=-1; insurance_form.model.setAttribute('disabled','disabled');
      insurance_form.wersja.value=-1; insurance_form.wersja.setAttribute('disabled','disabled');
      insurance_form.pojemnosc.value=-1; insurance_form.pojemnosc.setAttribute('disabled','disabled');
      insurance_form.moc.value=-1; insurance_form.moc.setAttribute('disabled','disabled');
      if ( obj.value != -1 ) {
        //wybrany rodzaj pojazdu
//        document.getElementById('wait1').innerHTML='';
//        var image = document.createElement("img"); image.setAttribute("src",waitImg.src); image.setAttribute("alt","wait");
//        document.getElementById('wait1').appendChild(image);
        if ( onlyten_var ) {
          loadData( 'oper='+field_no+'&rodz_poj='+insurance_form.rodz_poj.value+'&onlyten='+onlyten_var );
        } else {
          loadData( 'oper='+field_no+'&rodz_poj='+insurance_form.rodz_poj.value );
        }
      }
      break;
    }

    case 2: {
      insurance_form.marka.value=-1; insurance_form.marka.setAttribute('disabled','disabled');
      insurance_form.model.value=-1; insurance_form.model.setAttribute('disabled','disabled');
      insurance_form.wersja.value=-1; insurance_form.wersja.setAttribute('disabled','disabled');
      insurance_form.pojemnosc.value=-1; insurance_form.pojemnosc.setAttribute('disabled','disabled');
      insurance_form.moc.value=-1; insurance_form.moc.setAttribute('disabled','disabled');
      if ( obj.value != -1 ) {
        //wybrany rocznik
        loadData( 'oper='+field_no+'&rodz_poj='+insurance_form.rodz_poj.value+'&rok_prd='+insurance_form.rok_prd.value );
        
/*
        if ( insurance_form.rok_prd.value<rok ) {
          insurance_form.ryzyko_asistance.setAttribute('disabled','disabled');
          insurance_form.ryzyko_asistance.checked=false;
          document.getElementById('if_skladki_1').checked=true;
          document.getElementById('if_skladki_3').setAttribute('disabled','disabled');
        } else {
          insurance_form.ryzyko_asistance.removeAttribute('disabled');
          document.getElementById('if_skladki_3').removeAttribute('disabled');
        }
*/
      }
      break;
    }

    case 3: {
      insurance_form.model.value=-1; insurance_form.model.setAttribute('disabled','disabled');
      insurance_form.wersja.value=-1; insurance_form.wersja.setAttribute('disabled','disabled');
      insurance_form.pojemnosc.value=-1; insurance_form.pojemnosc.setAttribute('disabled','disabled');
      insurance_form.moc.value=-1; insurance_form.moc.setAttribute('disabled','disabled');
      if ( obj.value != -1 ) {
        //wybrana marka
        loadData( 'oper='+field_no+'&rodz_poj='+insurance_form.rodz_poj.value+'&rok_prd='+insurance_form.rok_prd.value+'&marka='+insurance_form.marka.value )
      }
      break;
    }

    case 4: {
      insurance_form.wersja.value=-1; insurance_form.wersja.setAttribute('disabled','disabled');
      insurance_form.pojemnosc.value=-1; insurance_form.pojemnosc.setAttribute('disabled','disabled');
      insurance_form.moc.value=-1; insurance_form.moc.setAttribute('disabled','disabled');
      if ( obj.value != -1 ) {
        loadData( 'oper='+field_no+'&rodz_poj='+insurance_form.rodz_poj.value+'&rok_prd='+insurance_form.rok_prd.value+'&marka='+insurance_form.marka.value+'&model='+insurance_form.model.value )
      }
      break;
    }

    case 5: {
      insurance_form.pojemnosc.value=-1; insurance_form.pojemnosc.setAttribute('disabled','disabled');
      insurance_form.moc.value=-1; insurance_form.moc.setAttribute('disabled','disabled');
      if ( obj.value != -1 ) {
        loadData( 'oper='+field_no+'&rodz_poj='+insurance_form.rodz_poj.value+'&rok_prd='+insurance_form.rok_prd.value+'&marka='+insurance_form.marka.value+'&model='+insurance_form.model.value+'&wersja='+insurance_form.wersja.value )
      }
      break;
    }

    case 6: {
      insurance_form.moc.value=-1; insurance_form.moc.setAttribute('disabled','disabled');
      if ( obj.value != -1 ) {
        loadData( 'oper='+field_no+'&rodz_poj='+insurance_form.rodz_poj.value+'&rok_prd='+insurance_form.rok_prd.value+'&marka='+insurance_form.marka.value+'&model='+insurance_form.model.value+'&wersja='+insurance_form.wersja.value+'&pojemnosc='+insurance_form.pojemnosc.value )
      }
      break;
    }

    case 7: {
      if ( obj.value != -1 ) {
        loadData( 'oper='+field_no+'&rodz_poj='+insurance_form.rodz_poj.value+'&rok_prd='+insurance_form.rok_prd.value+'&marka='+insurance_form.marka.value+'&model='+insurance_form.model.value+'&wersja='+insurance_form.wersja.value+'&pojemnosc='+insurance_form.pojemnosc.value+'&moc='+insurance_form.moc.value )
      }
      break;
    }


  }
}

function paramsCheck() {
  var insurance_form = document.insurance_form;
  var last_refresh = false;
  var Today = new Date();
  var rok = Today.getFullYear()-10;



//  insurance_form.ryzyko_asistance.removeAttribute('disabled');
//  document.getElementById('if_skladki_3').removeAttribute('disabled');

  if ( insurance_form.rodz_poj.value == -1 ) {
    last_refresh = true;
    paramsChange(insurance_form.rodz_poj, 1);
  }

  if ( !last_refresh && insurance_form.rodz_poj.value != -1 && insurance_form.rok_prd.value == -1 ) {
    last_refresh = true;
    paramsChange(insurance_form.rodz_poj, 1);
  }

  if ( !last_refresh && insurance_form.rok_prd.value != -1 && insurance_form.marka.value == -1 ) {
    last_refresh = true;
    paramsChange(insurance_form.rok_prd, 2);

/*
    if ( insurance_form.rok_prd.value<rok ) {
      insurance_form.ryzyko_asistance.setAttribute('disabled','disabled');
      insurance_form.ryzyko_asistance.checked=false;
      document.getElementById('if_skladki_1').checked=true;
      document.getElementById('if_skladki_3').setAttribute('disabled','disabled');
    } else {
      insurance_form.ryzyko_asistance.removeAttribute('disabled');
      document.getElementById('if_skladki_3').removeAttribute('disabled');
    }
*/

  }

  if ( !last_refresh && insurance_form.marka.value != -1 && insurance_form.model.value == -1 ) {
    last_refresh = true;
    paramsChange(insurance_form.marka, 3);
  }

  if ( !last_refresh && insurance_form.model.value != -1 && insurance_form.wersja.value == -1 ) {
    last_refresh = true;
    paramsChange(insurance_form.model, 4);
  }

  if ( !last_refresh && insurance_form.wersja.value != -1 && insurance_form.pojemnosc.value == -1 ) {
    last_refresh = true;
    paramsChange(insurance_form.wersja, 5);
  }

  if ( !last_refresh && insurance_form.pojemnosc.value != -1 && insurance_form.moc.value == -1 ) {
    last_refresh = true;
    paramsChange(insurance_form.pojemnosc, 6);
  }
}


function loadData( params ) {
  advAJAX.get({
      url: "ocac_params.php",
      timeout : 3000,
      retryCount: 2,
      retryDelay: 1000,
      queryString : params,
      onSuccess : function(obj) {
        var response = obj.responseText;
        eval(response);
      },
      onError : function(obj) {
        var _this = obj;
        this._timerRepeat = window.setTimeout(function() {
            _this.run();
        }, obj.repeatDelay);
      },
      method: "POST",
      async: true
  });
}

function foblicz() {
  document.insurance_form.submit();
}
