
var osago=0;
var kasko=0;

function setStyle(obj){

 if(obj.value!=''){
  obj.style.backgroundColor = '';
 }

 if(osago!=null && osago==1){
  calcOsago();
 }

 if(kasko!=null && kasko==1){
  getKaskoFunc(obj);
 }

}


function calcOsago(){

 var d = document;
 var f = d.forms['module_anketa'];
 var amount = 0;

 var tstype = [0,2375,395,2965,2025,3240,810,1620,2025,1620,1010,1215,305,1215];
 var tspower = [0, 0.5, 0.7, 1, 1.3, 1.5, 1.7];
 var ubnames = [0, 2.45, 1.55, 1, 0.95];

 amount = tstype[f.tip_ts.selectedIndex];
 if(f.lico.selectedIndex==0){amount = 0;}
 if(f.lico.selectedIndex==1 && f.tip_ts.selectedIndex==1){amount = 1980;}

 amount = amount * tsregion[f.rayon.selectedIndex];

 if(f.lico.selectedIndex==1){

  if(f.drivers_qty.selectedIndex==1){amount = amount*1;}
  if(f.drivers_qty.selectedIndex==2){amount = amount*1.5;}

  if(f.drivers_age.selectedIndex==1){

   if(f.drivers_rights.selectedIndex==1){amount = amount*1.3;}
   if(f.drivers_rights.selectedIndex==2){amount = amount*1.2;}

  }

  if(f.drivers_age.selectedIndex==2){

   if(f.drivers_rights.selectedIndex==1){amount = amount*1.15;}
   if(f.drivers_rights.selectedIndex==2){amount = amount*1;}

  }

 }

 if(f.tip_ts.selectedIndex==1 || f.tip_ts.selectedIndex==3){
  amount = amount*tspower[f.horse_power.selectedIndex];
 }

 amount = amount*ubnames[f.ubytki.selectedIndex];

 d.getElementById("amount").innerHTML = MoneyFormat(amount);

}


function roundEx(x, dgt){
	var p, n;
	p=Math.floor(Math.pow(10,dgt));
	(x>0) ? n = Math.floor(x) : n = Math.ceil(x);
	return(n + Math.round((x-n)*p)/p);
};

function MoneyFormat(x){
	var p,i,j;
	var s="", s2="";
	p=roundEx(x,2);	s=p.toString();
	p=s.lastIndexOf(".");
	if (p>0) {s2="." + s.substring(p+1, s.length); s = s.substring(0,p);} else {s2=".00";};
	if(s2.length<3)s2+="0";	j=1; i=s.length;
	for((i%3)?i=Math.floor(i/3):i=Math.floor(i/3)-1; i>0;--i)
	{
		p=s.length-(3*j)-(j-1);j++;
		s=s.substring(0,p) + " "+ s.substring(p,s.length);
	}
	return(s+s2);
};


function checkForm(){

 var frm = document.forms[0];
 var mandat = frm.mandat_fields;
 var val = add_flds;
 if(mandat!=null){
  val += mandat.value;
 }

 flds = val.split('|'); ret=false;
 for(f in flds){
  if(!trim(flds[f])){continue;}
  ob = document.getElementById(flds[f]);
  if(ob!=null && !trim(ob.value)){
   ob.style.backgroundColor = 'pink'; ret=true;
  } //else if(ob==null){alert(flds[f]);}
 }
 if(ret){
  alert('Заполните, пожалуйста, необходимые поля!');
  return false;
 } else {
  return true;
 }

}

function trim(str){
 if(str!=null){
  while(str.substring(0,1)==' '){str=str.substring(1);}
  while(str.substring(str.length-1,str.length)==' '){str=str.substring(0,str.length-1);}
 }
 return str;
}


function getBrandsList(){

 var lst = document.getElementById("marka");
 lst.length = 0;
 lst.options[0] = new Option('-->', ' ', false, false);

 for(var b=0;b<brands.length;b++){
  lst.options[b+1] = new Option(brands[b], brands[b], false, false);
 }

 setDrivers(1);

}

function setDrivers(h){

 var place =document.getElementById("drivers_info");

 var str = '<table cellspacing=0 cellpadding=0 border=0>';

 var f = document.forms['module_anketa'];
 for(var i=1;i<=4;i++){
  f.mandat_fields.value = f.mandat_fields.value.replace('|driver'+i+'_age', '');
  f.mandat_fields.value = f.mandat_fields.value.replace('|driver'+i+'_stazh', '');
 }

 for(var i=1;i<=h;i++){

  str += '<tr><td><strong>Водитель '+i+':</strong> &nbsp;</td><td>';
  str += 'возраст: <input type=hidden name="txt_driver'+i+'_age" value="Водитель '+i+', возраст">';
  str += '<input type=text name="driver'+i+'_age" id="driver'+i+'_age" onkeyup="setStyle(this);">';
  str += ' стаж: <input type=hidden name="txt_driver'+i+'_stazh" value="Водитель '+i+', стаж">';
  str += '<input type=text name="driver'+i+'_stazh" id="driver'+i+'_stazh" onkeyup="setStyle(this);">';
  str += '</td></tr>';

  f.mandat_fields.value += '|driver'+i+'_age|driver'+i+'_stazh';

 }

 str += '</table>';

 place.innerHTML = str;
// alert(f.mandat_fields.value);

}


function getKaskoFunc(obj){

 if(obj.getAttribute("id")=='marka'){

  var ind = obj.selectedIndex;

  var lst = document.getElementById("model");
  lst.length = 0;
  var val = '-->'; if(ind==0){val = 'Выберите марку машины';}
  lst.options[0] = new Option(val, ' ', false, false);
  if(ind==0){return;}

  for(var b=0;b<models[ind-1].length;b++){
   lst.options[b+1] = new Option(models[ind-1][b], models[ind-1][b], false, false);
  }

 }

}
