$(document).ready(

  function()
  {
    $(".representations tr:nth-child(odd)").addClass("odd");
  }
)

  function checkSimple()
  {
  	var fid=parseInt($("#town_from").val());
  	var tid=parseInt($("#town_to").val());
	  var fid_type = $("#town_from").find('option[value=' + fid + ']').attr('class');
	  var tid_type = $("#town_to").find('option[value=' + tid + ']').attr('class');
	  var flag_simple = true;

      var weight = replaceComa($("#weight"));
      var volume = replaceComa($("#volume"));

      var v1 = replaceComa($("#v1"));
      var v2 = replaceComa($("#v2"));
      var v3 = replaceComa($("#v3"));

      volume2 = v1 * v2 * v3;
      if(volume2 > volume)
      {
        volume = volume2;
      }

    if(fid_type == "big_simple" || tid_type == "big_simple")
    {
      if(weight > 30 || volume > 0.1)
      {
          alert("Легкий тариф действует для грузов весом менее 30кг и объемом менее 0,1м3! Для расчета отправки большего груза выберите другое представительство.");
          flag_simple = false;
      }
      else
      {
        flag_simple = true;
      }
    }
    else
    {
      flag_simple = true;
    }
    return flag_simple;
  }

	function checkform(){
		var flag = true;
    type = $("input[name='type']:checked").val();
    if(type==3)
    {
    flag=true;
    calculate();
    }
    else
    {
		var weight=$("#weight");
		if(weight.val()=="")
		{
		  $("#weight").html("1");
			flag=false;
			weight.css("background-color","#FDE0E0").one('focus', function() {$(this).css("background-color","#FFFFFF");});
			weight.css("border","1px solid #CD4040").one('focus', function() {$(this).css("border","1px solid #AAAAAA");});
		}
		var volume=$("#volume");
		var v1=$("#v1");
		var v2=$("#v2");
		var v3=$("#v3");
		if(volume.val()=="" && (v1.val()=="" || v2.val()=="" || v3.val()==""))
		{
			flag=false;
			volume.css("background-color","#FDE0E0").one('focus', function() {$(this).css("background-color","#FFFFFF");});
			v1.css("background-color","#FDE0E0").one('focus', function() {$(this).css("background-color","#FFFFFF");});
			v2.css("background-color","#FDE0E0").one('focus', function() {$(this).css("background-color","#FFFFFF");});
			v3.css("background-color","#FDE0E0").one('focus', function() {$(this).css("background-color","#FFFFFF");});
			volume.css("border","1px solid #CD4040").one('focus', function() {$(this).css("border","1px solid #AAAAAA");});
			v1.css("border","1px solid #CD4040").one('focus', function() {$(this).css("border","1px solid #AAAAAA");});
			v2.css("border","1px solid #CD4040").one('focus', function() {$(this).css("border","1px solid #AAAAAA");});
			v3.css("border","1px solid #CD4040").one('focus', function() {$(this).css("border","1px solid #AAAAAA");});
		}
		var region_from=$("#region_from");
		if(region_from.val()==0)
		{
			flag=false;
			region_from.css("background-color","#FDE0E0").one('focus', function() {$(this).css("background-color","#FFFFFF");});
			region_from.css("border","1px solid #CD4040").one('focus', function() {$(this).css("border","1px solid #AAAAAA");});
		}
		var region_to=$("#region_to");
		if(region_to.val()==0)
		{
			flag=false;
			region_to.css("background-color","#FDE0E0").one('focus', function() {$(this).css("background-color","#FFFFFF");});
			region_to.css("border","1px solid #CD4040").one('focus', function() {$(this).css("border","1px solid #AAAAAA");});
		}
		var town_from=$("#town_from");
		if(town_from.val()==0)
		{
			flag=false;
			town_from.css("background-color","#FDE0E0").one('focus', function() {$(this).css("background-color","#FFFFFF");});
			town_from.css("border","1px solid #CD4040").one('focus', function() {$(this).css("border","1px solid #AAAAAA");});
		}
		var town_to=$("#town_to");
		if(town_to.val()==0)
		{
			flag=false;
			town_to.css("background-color","#FDE0E0").one('focus', function() {$(this).css("background-color","#FFFFFF");});
			town_to.css("border","1px solid #CD4040").one('focus', function() {$(this).css("border","1px solid #AAAAAA");});
		}
		   
    var pack_block=$("#pack_block");
		var pack=$("input[name='pack']:checked").val();
		if(pack == undefined)
		{
			flag=false;
      pack_block.css("border","1px solid #CD4040");
      pack_block.css("border-radius","5px 5px 5px 5px");
			pack_block.css("background-color","#FDE0E0");
			$("input[name='pack']").one('change', function() {pack_block.css("border","0px none");});
			$("input[name='pack']").one('change', function() {pack_block.css("border-radius","0px 0px 0px 0px");});
			$("input[name='pack']").one('change', function() {pack_block.css("background-color","#FFFFFF");});
		}
	  if(!flag) alert("Заполните, пожалуйста, корректно все обязательные поля");
		return flag;
    }
	}

var tariff = 1;
var terms_of_delivery = 1;

function changeRegion_from()
{
	rid_from=parseInt($("#region_from :selected").val());
	$('#load2').show();
  $.post(
  "/calc/",
   {
   region_from : rid_from
   },
   function(response){

    var towns_from = eval('(' + response + ')');
    var select = $("#town_from").html('');
    select.append('<option value="0">--------------</option>');
    for(var k in towns_from) {
      for (var kk in towns_from[k])
         select.append('<option class="'+ k +'" value="'+ kk +'">' + towns_from[k][kk] + '</option>');
    }

    $("#container_from").show();
  $('#load2').hide();
  });
  if(rid_from == '0')
  {
    $("#container_from").hide();
  }
}


function changeRegion_to()
{
	rid_to=parseInt($("#region_to :selected").val());
	$('#load3').show();
  $.post(
  "/calc/",
   {
   region_to : rid_to
   },
   function(response){

    var towns_to = eval('(' + response + ')');
    var select = $("#town_to").html('');
    select.append('<option value="0">--------------</option>');
    for(var k in towns_to) {
      for (var kk in towns_to[k])
         select.append('<option class="'+k+'" value="'+ kk +'">' + towns_to[k][kk] + '</option>');
    }

    $("#container_to").show();
  $('#load3').hide();
  });
  if(rid_to == '0')
  {
    $("#container_to").hide();
  }
}

function changeTown()
{
	fid=parseInt($("#town_from").val());
	tid=parseInt($("#town_to").val());
	fid_type = $("#town_from").find('option[value=' + fid + ']').attr('class');
	tid_type = $("#town_to").find('option[value=' + tid + ']').attr('class');
  $("#type1").attr("checked",true).change();
	if((tariffe[fid]!=null) && (tariffe[fid][tid]!=null))
	{
		$("#is_econom1").removeAttr("disabled");
	}
	else
	{
		$("#is_econom1").removeAttr("checked");
		$("#is_econom1").attr("disabled","true");
		$("#is_econom0").attr("checked","true").change();
	}

	if((fid!=21)&&(tid!=21))
	{
		$("#type4").removeAttr("checked").attr("disabled",true);
	}
  	$("#containerSuper").hide();
  	$("#containerPoshtuchno").hide();
  	$("#supermarket").attr("disabled",true);
  	$("#supermarket").removeAttr("checked");
    $("#poshtuchno").removeAttr("checked");
    $("#etaj").attr("disabled",true);
    $("#etaj").removeAttr("checked");

      if((fid_type == "small") && (tid_type != "small"))
  {
    $("#type3").attr("disabled",true);
		$("#type21").removeAttr("checked").attr("disabled",true);
		$("#type22").removeAttr("checked").attr("disabled",true);
		$("#type23").removeAttr("disabled");
		$("#type24").attr("checked",true).change();
		$("#supermarket").attr("disabled",true);
  	$("#supermarket").removeAttr("checked");
    $("#poshtuchno").removeAttr("checked");
    $("#etaj").attr("disabled",true);
    $("#etaj").removeAttr("checked");
  }
  if((tid_type == "small") && (fid_type != "small"))
  {
    $("#type3").attr("disabled",true);
 		$("#type21").removeAttr("checked").attr("disabled",true);
		$("#type22").removeAttr("disabled");
		$("#type23").removeAttr("checked").attr("disabled",true);
		$("#type24").attr("checked",true).change();
		$("#supermarket").attr("disabled",true);
  	$("#supermarket").removeAttr("checked");
    $("#poshtuchno").removeAttr("checked");
    $("#etaj").attr("disabled",true);
    $("#etaj").removeAttr("checked");
  }

  if((fid_type == "small") && (tid_type == "small"))
  {
    $("#type3").attr("disabled",true);
		$("#type21").removeAttr("checked").attr("disabled",true);
		$("#type22").removeAttr("checked").attr("disabled",true);
		$("#type23").removeAttr("checked").attr("disabled",true);
		$("#type24").attr("checked",true).change();
		$("#supermarket").attr("disabled",true);
  	$("#supermarket").removeAttr("checked");
    $("#poshtuchno").removeAttr("checked");
    $("#etaj").attr("disabled",true);
    $("#etaj").removeAttr("checked");
  }
  if((fid_type != "small") && (tid_type != "small"))
  {
    $("#type3").removeAttr("disabled");
		$("#type21").removeAttr("disabled").attr("checked",true).change();
		$("#type22").removeAttr("disabled");
		$("#type23").removeAttr("disabled");
		$("#type24").removeAttr("disabled");
  	$("#supermarket").removeAttr("disabled");
    $("#etaj").removeAttr("disabled");
  }
}

function changeEconom()
{
	iseconom = $("#is_econom1").attr("checked");
	if(iseconom)
	{
		$("#type2").removeAttr("checked").attr("disabled",true);
		$("#type5").removeAttr("checked").attr("disabled",true);
		$("#type3").removeAttr("checked").attr("disabled",true);
		$("#type4").removeAttr("checked").attr("disabled",true);
		$("#type1").attr("checked",true).change();

  	$("#containerSuper").hide();
  	$("#containerPoshtuchno").hide();
  	$("#supermarket").attr("disabled",true);
  	$("#supermarket").removeAttr("checked");
    $("#poshtuchno").removeAttr("checked");
    $("#etaj").attr("disabled",true);
    $("#etaj").removeAttr("checked");

		$("#type22").removeAttr("checked").attr("disabled",true);
		$("#type23").removeAttr("checked").attr("disabled",true);
		$("#type24").removeAttr("checked").attr("disabled",true);
		$("#type21").attr("checked",true).change();
	}
	else
	{
		fid=parseInt($("#town_from").val());
		tid=parseInt($("#town_to").val());

		$("#type2").removeAttr("disabled");
		$("#type5").removeAttr("disabled");
		$("#type3").removeAttr("disabled");

		if((fid==21)||(tid==21))
		{
			$("#type4").removeAttr("disabled");
		}

		$("#type22").removeAttr("disabled");
		$("#type23").removeAttr("disabled");
		$("#type24").removeAttr("disabled");
	}
}

function changeEtaj()
{
	etaj = $("input[name='etaj']:checked").val();
	if(etaj==5)
	{
		$("#containerEtaj").show();
		$("#supermarket").attr("disabled",true);
		$("#supermarket").removeAttr("checked");
  }
  else
  {
  	$("#containerEtaj").hide();
  	$("#supermarket").removeAttr("disabled");
  }
}

function changeSupermarket()
{
	supermarket = $("input[name='supermarket']:checked").val();
	if(supermarket==100)
	{
		$("#containerSuper").show();
		$("#etaj").attr("disabled",true);
		$("#etaj").removeAttr("checked");
  }
  else
  {
  	$("#containerSuper").hide();
  	$("#containerPoshtuchno").hide();
  	$("#supermarket").removeAttr("checked");
    $("#poshtuchno").removeAttr("checked");
  	$("#etaj").removeAttr("disabled");
  }
}

function changePoshtuchno()
{
	poshtuchno = $("input[name='poshtuchno']:checked").val();
	if(poshtuchno==6)
	{
		$("#containerPoshtuchno").show();
  }
  else
  {
  	$("#containerPoshtuchno").hide();
  }
}

function smallCargo()
{
 	var type2 = $("input[name='type2']:checked").val();
	var type = $("input[name='type']:checked").val();
	var v1 = replaceComa($("#v1"));
	var v2 = replaceComa($("#v2"));
	var v3 = replaceComa($("#v3"));
  var weight = replaceComa($("#weight"));
  var volume = replaceComa($("#volume"));
  volume2 = v1 * v2 * v3;
  volume2 = parseFloat(volume2.toFixed(5));

  if(volume == NaN && volume2 != NaN)
  {volume = volume2;}
  else if(volume < volume2)
  {volume = volume2;}

  if((weight>0 && volume>0) && (weight<10.1 && volume <= 0.05))
  {
    if(type == 1)
    {
      if(type2==1)
    	{
    	  $("input[name='pack']").removeAttr("checked");
        $("input[name='pack']").removeAttr("disabled");
      }
     	if(type2==2)
    	{
        $("input[name='pack']").removeAttr("checked");
        $("input[name='pack']").attr("disabled",true);
        $("#pack1").removeAttr("disabled");
        $("#pack5").removeAttr("disabled");
        $("#pack6").removeAttr("disabled");
    	}
    	if(type2==3)
    	{
        $("input[name='pack']").removeAttr("checked");
        $("input[name='pack']").attr("disabled",true);
        $("#pack1").removeAttr("disabled");
        $("#pack5").removeAttr("disabled");
        $("#pack6").removeAttr("disabled");
      }
    	if(type2==4)
    	{
    	  $("input[name='pack']").removeAttr("checked");
    	  $("input[name='pack']").attr("disabled",true);
        $("#pack1").removeAttr("disabled");
        $("#pack5").removeAttr("disabled");
        $("#pack6").removeAttr("disabled");
      }
    }
  }
  else
  {
 	  $("input[name='pack']").removeAttr("checked");
    $("input[name='pack']").removeAttr("disabled");
  }
}



function changeType2()
{
  smallCargo();
	var type2 = $("input[name='type2']:checked").val();
	
	if(type2==1)
	{
  	$("#containerSuper").hide();
  	$("#containerPoshtuchno").hide();
  	$("#containerEtaj").hide();
  	$("#supermarket").attr("disabled",true);
  	$("#supermarket").removeAttr("checked");
    $("#poshtuchno").removeAttr("checked");
    $("#etaj").attr("disabled",true);
    $("#etaj").removeAttr("checked");
	}
	if(type2==2)
	{
    $("#supermarket").removeAttr("disabled");
    $("#etaj").removeAttr("disabled");
	}
	if(type2==3)
	{
  	$("#containerSuper").hide();
  	$("#containerPoshtuchno").hide();
  	$("#containerEtaj").hide();
  	$("#supermarket").attr("disabled",true);
  	$("#supermarket").removeAttr("checked");
    $("#poshtuchno").removeAttr("checked");
    $("#etaj").attr("disabled",true);
    $("#etaj").removeAttr("checked");
	}
	if(type2==4)
	{
    $("#supermarket").removeAttr("disabled");
    $("#etaj").removeAttr("disabled");
	}
}

function changeType1()
{
  fid=parseInt($("#town_from").val());
	tid=parseInt($("#town_to").val());
	type = $("input[name='type']:checked").val();
	if(type==1)
	{
		$("#container3").hide();
		$("#container4").hide();
		$("#container2").show();
		$("#container1").show();
		$("#weight").removeAttr("readonly").css("background-color","");
		$("#volume").removeAttr("readonly").css("background-color","");
		if((fid != 124) && (fid != 125) && (tid != 124) && (tid != 125))
	  {
		$("#type22").removeAttr("disabled");
		$("#type23").removeAttr("disabled");
		}
		$("#type24").removeAttr("disabled");
		$("input[name='pack']").removeAttr("disabled");

	}
	if(type==2)
	{
		$("#container2").hide();
		$("#container3").hide();
		$("#container4").hide();
		$("#container1").show();
		$("#weight").val("1").attr("readonly",true).css("background-color","#cccccc");
		$("#volume").val("0.004").attr("readonly",true).css("background-color","#cccccc");
		if((fid != 124)&&(fid!=125)&&(tid!=124)&&(tid!=125))
		{
		$("#type22").removeAttr("disabled");
		$("#type23").removeAttr("disabled");
		}
		$("#type24").removeAttr("disabled");
        $("input[name='pack']").removeAttr("checked").attr("disabled",true);
        $("#pack6").removeAttr("disabled");
		$("#pack7").removeAttr("disabled");
		$("#pack5").removeAttr("disabled").attr("checked",true).change();
	}
	if(type==5)
	{
		$("#container2").hide();
		$("#container3").hide();
		$("#container4").hide();
		$("#container1").show();
		$("#weight").val("1").attr("readonly",true).css("background-color","#cccccc");
		$("#volume").val("0.004").attr("readonly",true).css("background-color","#cccccc");
		$("#type22").removeAttr("checked").attr("disabled",true);
		$("#type23").removeAttr("checked").attr("disabled",true);
		$("#type24").removeAttr("checked").attr("disabled",true);
		$("#type21").attr("checked",true).change();
		$("input[name='pack']").removeAttr("checked").attr("disabled",true);
		$("#pack7").removeAttr("disabled").attr("checked",true).change();
	}
	if(type==3)
	{
		$("#container1").hide();
		$("#container4").hide();
		$("#container3").show();

		$("#type22").removeAttr("checked").attr("disabled",true);
		$("#type23").removeAttr("checked").attr("disabled",true);
		$("#type24").removeAttr("checked").attr("disabled",true);
		$("#type21").attr("checked",true).change();
		$("input[name='pack']").removeAttr("checked").attr("disabled",true);
	}
	if(type==4)
	{
		$("#container1").hide();
		$("#container3").hide();
		$("#container4").show();
		if((fid != 124)&&(fid!=125)&&(tid!=124)&&(tid!=125))
		{
		$("#type22").removeAttr("disabled");
		$("#type23").removeAttr("disabled");
		$("#type24").removeAttr("disabled");
		$("#type21").attr("checked",true).change();
		}
		$("input[name='pack']").removeAttr("checked").attr("disabled",true);
		$("#pack1").removeAttr("disabled").attr("checked",true);
		$("#pack8").removeAttr("disabled");
	}
}

function getTariffs()
{
	var tid_from = parseInt($("#town_from :selected").val());
	var tid_to = parseInt($("#town_to :selected").val());
  fid_type = $("#town_from").find('option[value=' + fid + ']').attr('class');
	tid_type = $("#town_to").find('option[value=' + tid + ']').attr('class');
 	if(fid_type == "small")
	{
    fid = parseInt(tariff_small[tid_from].tr);
    tid_from = fid;
  }
	else if(tid_type == "small")
	{
    tid = parseInt(tariff_small[tid_to].tr);
    tid_to = tid;
  }

	$('#load1').show();
  $.post(
  "/calc/",
   {
   tid_from : tid_from,
   tid_to : tid_to
   },
   function(response){
    var tarifs = eval('(' + response + ')');
    tariff = tarifs.tariff;
    terms_of_delivery = tarifs.terms_of_delivery;
    calculate();
    $('#load1').hide();
  });
}

function replaceComa(k)
{
		var v1str = $("#v1");
		var k2 = k.val();
		k2 = k2.replace(",",".");
		k2 = k2.replace(/[^0-9.]/,"");
		if(k.val()!=k2)
			k.val(k2);
		k2 = parseFloat(k2);
    return k2;
}

function calculate()
{

  $("#report").hide();
	$("#error").hide();
   	$("#report #vol").hide();
   	$("#report #wei").hide();
   	$("#report #whe").hide();
  // 	$("#report #tim").hide();
   	$("#report #tar1").hide();
   	$("#report #aa").hide();
   	$("#report #a1").hide();
   	$("#report #a2").hide();
   	$("#report #a3").hide();
   	$("#report #obl_from").hide();
   	$("#report #obl_to").hide();
   	$("#report #sale").hide();
   	$("#report #package").hide();
    var pack=0;

	//город отправитель и поулчатель


	$("#report #t1").html($("#town_from option:selected").html());
	$("#report #t2").html($("#town_to option:selected").html());

	//эконом доставка true
	iseconom = $("#is_econom1").attr("checked");

	//тип груза
	var type = $("input[name='type']:checked").val();

	//тип доставки
	var type2 = $("input[name='type2']:checked").val();
	var addressee =  false;
	var sender =  false;
	var supermarket =  false;
	var poshtuchno =  false;
	var etaj =  false;
	if(type2==2) {sender=false;  addressee=true;}
	if(type2==3) {sender=true;  addressee=false;}
	if(type2==4) {sender=true;  addressee=true;}

	if($("#supermarket").attr("checked"))
		supermarket=true;
	if($("#poshtuchno").attr("checked"))
		poshtuchno=true;
	if($("#etaj").attr("checked"))
		etaj=true;


    pack = parseFloat($("input[name='pack']:checked").val());
    pack_type = $("input[name='pack']:checked").attr("id");
    pakage = pack;

	weight = replaceComa($("#weight"));
	volume = replaceComa($("#volume"));

	var fid=parseInt($("#town_from").val());
	var tid=parseInt($("#town_to").val());

  fid_type = $("#town_from").find('option[value=' + fid + ']').attr('class');
	tid_type = $("#town_to").find('option[value=' + tid + ']').attr('class');

	region_from = parseInt($("#region_from").val());
  region_to = parseInt($("#region_to").val());
  s_small_from = 0;
  s_small_to = 0;

  if(type!=2)
  {
    v1 = replaceComa($("#v1"));
    v2 = replaceComa($("#v2"));
    v3 = replaceComa($("#v3"));

		volume2=v1*v2*v3/1000000;
		if(!volume)
			volume = parseFloat(volume2);

		if(volume2>volume)
			volume = volume2;
		if(weight<(volume*200))
		weight=volume*200;
	}
  if((fid_type == "small") || (tid_type == "small"))
  {
  		  if(type == 3)
		  {
		  alert('При  заборе/доставке до двери автомобильных  шин и дисков в населенные пункты где нет Представительств "Ин-Тайм" - стоимость уточняйте на складах!');
		  return false;
      }
    var timing_show = true;
    if(weight<(volume*200))
		weight=volume*200;
    type2 = 1;
    if(fid_type == "small")
    {
      fid_type = "big";
      sender =  false;
      fid_small = fid;
      fid = parseInt(tariff_small[fid_small].tr);
      zone_from = parseInt(tariff_small[fid_small].z);
      for(k in tariffz[zone_from])
      {
        if((weight <= k) && (weight >= parseInt(tariffz[zone_from][k].w)))
        {
          s_small_from = parseInt(tariffz[zone_from][k].price);
        }
      }
      $("#report #s_obl_from").html(s_small_from);
		  $("#obl_from").show();
		  $("#report #timing").html('срок  доставки  груза  в  населенный  пункт, где нет представительств "Ин-Тайм",  с момента  отправки – в течение 5 рабочих дней');
    }

    if(tid_type == "small")
    {
      tid_type = "big";
      addressee =  false;
      tid_small = tid;
      tid = parseInt(tariff_small[tid_small].tr);
      zone_to = parseInt(tariff_small[tid_small].z);
      for(k in tariffz[zone_to])
      {
        if((weight <= k) && (weight >= parseInt(tariffz[zone_to][k].w)))
        {
          s_small_to = parseInt(tariffz[zone_to][k].price);
        }
      }
      if(region_to == 11)
      {
      	small_kiev_to_wei = Math.round(weight * 0.39 * 100)/100;
        small_kiev_to_vol = Math.round(volume * 89 * 100)/100;
        if(small_kiev_to_wei < small_kiev_to_vol)
        {
          s_small_to = s_small_to + volume * 89;
        }
        else
        {
          s_small_to = s_small_to + weight * 0.39;
        }
      }
      $("#report #s_obl_to").html(s_small_to);
		  $("#obl_to").show();
		  $("#report #timing").html('срок  доставки  груза  в  населенный  пункт, где нет представительств "Ин-Тайм",  с момента  отправки – в течение 5 рабочих дней');
    }
    if(fid == tid)
    {
      type2 = 0;
    }
  }

    if(fid>tid)
		{
			tmp=fid;
			fid=tid;
			tid=tmp;
		}

	cstr = $("#cost").val();
	cstr = cstr.replace(",",".");
	cstr = cstr.replace(/[^0-9.]/,"");
	if($("#cost").val()!=cstr)
		$("#cost").val(cstr);
	cost = 0;
	p_cost = 0;
	if(cstr!="")
	cost = parseFloat(cstr);
	if(cost<50){cost=50;$("#cost").val(cost);}
    p_cost=cost/100;

    if(p_cost<1)
    p_cost=1;

    //Расчет доставки
    //Груз или документы
    if(type == 1 || type == 2 || type == 5)
    {
    	if(iseconom)
    	{
    		var ac1=0;
    		rep_ac1 = ac1;
		    var ac2=0;
		    rep_ac2 = ac2;
		    var as=0;
		    rep_as = as;
		    if((fid>0) && (tid>0) && (weight>0) && (volume>0) && (cost>0))
			{
    	var text_day = '';
      day_week = Date.today().getDay();
if(tariffe[fid][tid].timing != undefined)
{
    if(timing_show != true)
    {
    	if(day_week == 0)
    	{
    	text_day = 'При отправке в понедельник: ';
    	day_add = tariffe[fid][tid].timing;
      }
    	else
      {
    	text_day = '';
    	day_add = tariffe[fid][tid].timing;
      }
          var n = 1;
          new_today = Date.today().days();
          while(n < day_add)
          {
        	new_today = new_today.add(1).days();
        	n++;
        	}
        	day = new_today.getDate();
          month = new_today.getMonth();
          if(month == '0')
          {
          month = 'января';}
          if(month == '1')
          {
          month = 'февраля';}
          if(month == '2')
          {
          month = 'марта';}
          if(month == '3')
          {
          month = 'апреля';}
          if(month == '4')
          {
          month = 'мая';}
          if(month == '5')
          {
          month = 'июня';}
          if(month == '6')
          {
          month = 'июля';}
          if(month == '7')
          {
          month = 'августа';}
          if(month == '8')
          {
          month = 'сентября';}
          if(month == '9')
          {
          month = 'октября';}
          if(month == '10')
          {
          month = 'ноября';}
          if(month == '11')
          {
          month = 'декабря';}

          year = new_today.getFullYear();
	$("#report #timing").html(text_day+day+' '+month+' '+year);
	       }
        }
				s1= Math.round(weight * tariffe[fid][tid].kg * 100)/100;
				s2= Math.round(volume * tariffe[fid][tid].m3 * 100)/100;
				if(s2>s1)
				{
					s1=s2;
					$("#report #tariff").html(tariffe[fid][tid].m3+" грн/м<sup>3</sup>");
					s2 = parseFloat(s2.toFixed(2));
					$("#report #volumecost").html(s2+" грн");
          var day = 0;
          var month = 0;
          var year = 0;

      if(fid>tid)
				{
					tmp=fid;
					fid=tid;
					tid=tmp;
				}
        	$("#wei").hide();
					$("#vol").show();
				}
				else
				{
					$("#report #tariff").html(tariffe[fid][tid].kg+" грн/кг");
					s1 += calc_order;
					s1 = parseFloat(s1.toFixed(2));
     			$("#report #weightcost").html(s1+" грн");
					var day = 0;
					day = Date.today().add(4).days();
				//	$("#report #timing").html(day);
					//$("#report #timing").html(tariffe[fid][tid].timing);
					$("#wei").show();
					$("#vol").hide();
				}

				s1+=calc_order;
				$("#report #orderprice").html(calc_order);

				s1+=p_cost;

				$("#report #pcost").html(p_cost+" грн");

				if(pack>0)
				{
					s1+=pack;
					$("#pack").html(pack+" грн");
					pakage = pack;
					$("#report #package").show();
				}

				s1=  Math.round(s1 * 100)/100;
				s1 = parseFloat(s1.toFixed(2));

				$("#report #total").html(s1+" грн");
				$("#report").show("slow");
			}


    	}
    	else
    	{
    	//сроки
    	//tariff[fid][tid].timing
      if(fid>tid)
				{
					tmp=fid;
					fid=tid;
					tid=tmp;
				}

    	var text_day = '';
      day_week = Date.today().getDay();

if((terms_of_delivery[fid] != undefined) && (terms_of_delivery[fid][tid] != undefined))
{
  if(timing_show != true)
  {
      var reg=/(\d+);(\d+);(\d+);(\d+);(\d+);(\d+)/;
      var days=reg.exec(terms_of_delivery[fid][tid].t);
      terms_of_delivery[fid][tid].t;
    	if(day_week == 0)
    	{
    	text_day = 'При отправке в понедельник: ';
    	day_add = days[0];
      }
    	if(day_week == 1)
    	{
    	text_day = '';
    	day_add = days[0];
      }
    	if(day_week == 2)
    	{
    	text_day = '';
    	day_add = days[1];
      }
    	if(day_week == 3)
    	{
    	text_day = '';
    	day_add = days[2];
      }
    	if(day_week == 4)
    	{
    	text_day = '';
    	day_add = days[3];
      }
    	if(day_week == 5)
    	{
    	text_day = '';
    	day_add = days[4];
      }
    	if(day_week == 6)
    	{
    	text_day = '';
    	day_add = days[5];
      }
          var n = 1;
          new_today = Date.today().days();
          while(n < day_add)
          {
        	new_today = new_today.add(1).days();
        	n++;
        	}
        	day = new_today.getDate();
          month = new_today.getMonth();
          if(month == '0')
          {
          month = 'января';}
          if(month == '1')
          {
          month = 'февраля';}
          if(month == '2')
          {
          month = 'марта';}
          if(month == '3')
          {
          month = 'апреля';}
          if(month == '4')
          {
          month = 'мая';}
          if(month == '5')
          {
          month = 'июня';}
          if(month == '6')
          {
          month = 'июля';}
          if(month == '7')
          {
          month = 'августа';}
          if(month == '8')
          {
          month = 'сентября';}
          if(month == '9')
          {
          month = 'октября';}
          if(month == '10')
          {
          month = 'ноября';}
          if(month == '11')
          {
          month = 'декабря';}

          year = new_today.getFullYear();
	$("#report #timing").html(text_day+day+' '+month+' '+year);
	}
	}


    	//Дверь - Дверь
    		if(type2==4)
    		{
    			if(fid>tid)
				{
					tmp=fid;
					fid=tid;
					tid=tmp;
				}

				if((fid>0) && (tid>0) && (weight>0) && (volume>0) && (cost>0))
				{
					if(weight<(volume*200))
					weight=volume*200;
					ac1=0;
					rep_ac1 = ac1;
					price=0;
					priceadd=0;
					minw = 0;
					var zone = tariff[fid][tid].zone;
					if((weight<= 10 && volume <= 0.05) && ((pack_type == "pack1") || (pack_type == "pack6") || (pack_type == "pack5")))
					{
            for (var maxw in tariffz[zone])
      			{
      				price=parseFloat(tariffz[zone][maxw].price);
      				if(maxw==pack_type)      				
     					break;
      			}
            ac1 = price;
          }
					else
					{
  					for (var maxw in tariffz[zone])
      			{
      				minw=tariffz[zone][maxw].w;
      				price=parseFloat(tariffz[zone][maxw].price);
      				priceadd=parseFloat(tariffz[zone][maxw].priceadd);
      				if((minw<=weight)&&(maxw>=weight))
     					break;
      			}
      				ac1 = price + (weight-minw)*priceadd;

      				rep_ac1 = ac1;
      				ac1=  Math.round(ac1 * 100)/100;
      				rep_ac1 = ac1;
              ac1 = parseFloat(ac1.toFixed(2));

            if(pack>0)
  					{
  						s+=pack;
  						$("#pack").html(pack+" грн");
  						pakage = pack;
  						$("#report #package").show();
  					}
					
          }
    				$("#report #door").html(ac1+" грн");
				var door = ac1;
    				$("#report #aa").show();
    				s = ac1;
			    	s+= parseFloat(calc_order);
			        $("#report #orderprice").html(calc_order);
			        s+=p_cost;
			        $("#report #pcost").html(p_cost+" грн");

				if(supermarket)
				{
					s+=45;
					if(poshtuchno)
					{
            var posht_int = 0;
            posht_int = parseInt($("#poshtuchno1").val());
            if(posht_int <= '50')
            {s += (posht_int * 0.35);}

            if(posht_int <= '150' && posht_int > '50')
            {s += (posht_int * 0.30);}

            if(posht_int <= '250' && posht_int > '150')
            {s += (posht_int * 0.25);}

            if(posht_int <= '350' && posht_int > '250')
            {s += (posht_int * 0.20);}

            if(posht_int > '351')
            {s1 += (posht_int * 0.15);}

          }
				}

				if(etaj)
				{
				  var etaj_weight = 0;
				  var etaj_int = 0;
				  etaj_int = parseInt($("#etaj1").val());
				  etaj_weight = parseInt($("#weight").val());
          if(etaj_weight <= '30')
          {s += (etaj_int * 13);}
          if(etaj_weight <= '50' && etaj_weight > '30')
          {s += (etaj_int * 18);}
          if(etaj_weight <= '100' && etaj_weight > '50')
          {s += (etaj_int * 31);}
          if(etaj_weight <= '500' && etaj_weight > '100')
          {s += (etaj_int * 50);}
          if(etaj_weight > '500')
          {s += (etaj_int * 50);
          new_weight = etaj_weight - 500;
          posto = Math.round(new_weight/100);
          s += (etaj_int * 15 * posto);}

				}

					s =  Math.round(s * 100)/100;
			    	s = parseFloat(s.toFixed(2));
			    	$("#report #total").html(s+" грн");


			    	$("#report").show("slow");
				}
    		}
    		// Доставка по области
    		else if(type2 == 0)
    		{
          if(weight<(volume*200))
					weight=volume*200;

          s1 = 0;
					s1 += parseFloat(calc_order);

					$("#report #orderprice").html(calc_order);

					s1+=p_cost;
					$("#report #pcost").html(p_cost+" грн");


    			var ac1=0;
			    var ac2=0;
			    if( ( weight < 1.1 ) && (volume < 0.0500001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = small_calc_kiev1;
				    	else
				    		ac1 = small_calc_other1;

				  	}
				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = small_calc_kiev1;
				    	else
				    		ac2 = small_calc_other1;
				  	}
			    }else if( (weight<3) && (volume< 0.0500001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = small_calc_kiev2;
				    	else
				    		ac1 = small_calc_other2;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = small_calc_kiev2;
				    	else
				    		ac2 = small_calc_other2;
				  	}
			    }else if( ( weight<11 ) && (volume< 0.0500001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = small_calc_kiev3;
				    	else
				    		ac1 = small_calc_other3;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = small_calc_kiev3;
				    	else
				    		ac2 = small_calc_other3;
				  	}
			    }else if( (weight<100) && (volume< 0.500001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = small_calc_kiev4;
				    	else
				    		ac1 = small_calc_other4;
				  	}
				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = small_calc_kiev4;
				    	else
				    		ac2 = small_calc_other4;
				  	}
			    }else if( ( weight<500 ) && (volume< 2.00001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = calc_kiev3;
				    	else
                ac1 = calc_other3;
				  	}
				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = calc_kiev3;
				    	else
				    		ac2 = calc_other3;
				  	}
			    }else if( ( weight<1000 ) && (volume< 3.500001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = calc_kiev5;
				    	else
				    		ac1 = calc_other5;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = calc_kiev5;
				    	else
				    		ac2 = calc_other5;
				  	}
			    }else if( ( weight<2000 ) && (volume< 7.00001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = calc_kiev7;
				    	else
				    		ac1 = calc_other7;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = calc_kiev7;
				    	else
				    		ac2 = calc_other7;
				  	}
			    }else if( ( weight<3000 ) && (volume< 12.00001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = calc_kiev9;
				    	else
				    		ac1 = calc_other9;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = calc_kiev9;
				    	else
				    		ac2 = calc_other9;
				  	}
			    }else
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = calc_kiev11;
				    	else
				    		ac1 = calc_other11;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = calc_kiev11;
				    	else
				    		ac2 = calc_other11;
				  	}
			    }

					if(addressee==true)
					{
						s1+=ac1;
						$("#report #ac1").html(ac1);
						$("#a1").show();
					}

					if(sender==true)
					{
						s1+=ac2;
						$("#report #ac2").html(ac2);
						$("#a3").show();
					}
        	s1 += s_small_to + s_small_from;
        	s1 = parseFloat(s1.toFixed(2));
    		  $("#report #total").html(s1+" грн");
          $("#report").show("slow");
        }
        // Склад - Склад, Склад - Дверь, Дверь - Склад
    		else if(type2 != 0) // Доставка груза
    		{
        if((weight<=10) && (volume<= 0.05))
    		{
    		    if(pack_type == "pack1"  && ((addressee) || (sender)))
      		  {
      		    var spec_pack = true;
      		    if(addressee)
    			    {
                ac1 = 35;
              }
    				  if(sender)
    			    {
    			      ac2 = 35;
              }
            }
      		  else if(pack_type == "pack5"  && ((addressee) || (sender)))
      		  {
      		    var spec_pack = true;
      		    if(addressee)
    			    {
                ac1 = 20;
              }
    				  if(sender)
    			    {
    			      ac2 = 20;
              }            }
            else if(pack_type == "pack6" && ((addressee) || (sender)))
            {
      		    var spec_pack = true;
      		    if(addressee)
    			    {
                ac1 = 25;
              }
    				  if(sender)
    			    {
    			      ac2 = 25;
              }
            }
      		  else
      		  {
    			    if( ( weight > 1 ) && (volume > 0.01 ) )
    			    {
          		  if(weight<(volume*200))
      					weight=volume*200;
    					}
        			var ac1=0;
        			rep_ac1 = ac1;
    			    var ac2=0;
    			    rep_ac2 = ac2;

    			    if( ( weight < 1.1 ) && (volume <= 0.01 ) )
    			    {
    			    	if(addressee)
    			    	{
      				    if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
      				      ac1 = small_calc_kiev1;
      				    else
      				    	ac1 = small_calc_other1;
    				  	}
    
    				  	if(sender)
    			    	{
      				    if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
      				    	ac2 = small_calc_kiev1;
      				    else
      				    	ac2 = small_calc_other1;
    				  	}
              rep_ac1 = ac1;
              rep_ac2 = ac2;
    			    }else if( ( weight<3 ) && (volume< 0.0500001 ))
    			    {
    			    	if(addressee)
    			    	{
    				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
    				    		ac1 = small_calc_kiev2;
    				    	else
    				    		ac1 = small_calc_other2;
    				  	}
    
    				  	if(sender)
    			    	{
    				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
    				    		ac2 = small_calc_kiev2;
    				    	else
    				    		ac2 = small_calc_other2;
    				  	}
              rep_ac1 = ac1;
              rep_ac2 = ac2;
    			    }else if( (weight<=10) && (volume<= 0.05) )
    			    {
    			    	if(addressee)
    			    	{
    				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
    				    		ac1 = small_calc_kiev3;
    				    	else
    				    		ac1 = small_calc_other3;
    				  	}
    
    				  	if(sender)
    			    	{
    				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
    				    		ac2 = small_calc_kiev3;
    				    	else
    				    		ac2 = small_calc_other3;
    				  	}
              rep_ac1 = ac1;
              rep_ac2 = ac2;
    			    }
  			    }
			    }
          else if( (weight<100) && (volume< 0.500001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = small_calc_kiev4;
				    	else
				    		ac1 = small_calc_other4;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = small_calc_kiev4;
				    	else
				    		ac2 = small_calc_other4;
				  	}
          rep_ac1 = ac1;
          rep_ac2 = ac2;
			    }else if((weight<500) && (volume< 2.00001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = calc_kiev3;
				    	else
                ac1 = calc_other3;
				  	}
				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = calc_kiev3;
				    	else
				    		ac2 = calc_other3;
				  	}
				    		rep_ac1 = ac1;
				    		rep_ac2 = ac2;

			    }else if( (weight<1000) && (volume< 3.500001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = calc_kiev5;
				    	else
				    		ac1 = calc_other5;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = calc_kiev5;
				    	else
				    		ac2 = calc_other5;
				  	}
				    		rep_ac1 = ac1;
				    		rep_ac2 = ac2;

			    }else if( (weight<2000) || (volume< 7.00001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = calc_kiev7;
				    	else
				    		ac1 = calc_other7;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = calc_kiev7;
				    	else
				    		ac2 = calc_other7;
				  	}
				    		rep_ac1 = ac1;
				    		rep_ac2 = ac2;

			    }else if( (weight<3000) || (volume< 12.00001 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = calc_kiev9;
				    	else
				    		ac1 = calc_other9;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = calc_kiev9;
				    	else
				    		ac2 = calc_other9;
				  	}
				    		rep_ac1 = ac1;
				    		rep_ac2 = ac2;
			    }else
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = calc_kiev11;
				    	else
				    		ac1 = calc_other11;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = calc_kiev11;
				    	else
				    		ac2 = calc_other11;
				  	}
				    		rep_ac1 = ac1;
				    		rep_ac2 = ac2;
			    }




				if(fid>tid)
				{
					tmp=fid;
					fid=tid;
					tid=tmp;
				}


				if((fid>0) && (tid>0) && (weight>0) && (volume>0) && (cost>0))
				{
				  /*if(spec_pack == true)
				  {
            s1 = ac1;
            $("#report #door").html(ac1+" грн");
            $("#report #aa").show();
          }
				  else
				  {*/
					s1= Math.round(weight * tariff[fid][tid].kg * 100)/100;
					s2= Math.round(volume * tariff[fid][tid].m3 * 100)/100;

					if(s2>s1)
					{
						s1=s2;
						$("#report #tariff").html(tariff[fid][tid].m3+" грн/м<sup>3</sup>");
						$("#report #tar1").show();
						s1 = parseFloat(s1.toFixed(2));
						$("#report #volumecost").html(s1+" грн");
					//	$("#report #timing").html(tariff[fid][tid].timing+"");
						$("#wei").hide();
						$("#vol").show();
					}
					else
					{
						$("#report #tariff").html(tariff[fid][tid].kg+" грн/кг");
						$("#report #tar1").show();
						s1 = parseFloat(s1.toFixed(2));
						$("#report #weightcost").html(s1+" грн");
            //$("#report #timing").html(tariff[fid][tid].timing+"");
						$("#wei").show();
						$("#vol").hide();
					}

          s1 = parseFloat(s1);

					if(addressee==true)
					{
						s1+=ac1;
						$("#report #ac1").html(ac1);
						$("#a1").show();
					}

					if(sender==true)
					{
						s1+=ac2;
						$("#report #ac2").html(ac2);
						$("#a3").show();
					}

				if(supermarket)
				{
					s1+=45;
					if(poshtuchno)
					{
            var posht_int = 0;
            posht_int = parseInt($("#poshtuchno1").val());
            if(posht_int <= '50')
            {s1 += (posht_int * 0.35);}

            if(posht_int <= '150' && posht_int > '51')
            {s1 += (posht_int * 0.30);}

            if(posht_int <= '250' && posht_int > '151')
            {s1 += (posht_int * 0.25);}

            if(posht_int <= '350' && posht_int > '251')
            {s1 += (posht_int * 0.20);}

            if(posht_int > '351')
            {s1 += (posht_int * 0.15);}

          }
				}

				if(etaj)
				{
				  var etaj_weight = 0;
				  var etaj_int = 0;
				  etaj_int = parseInt($("#etaj1").val());
				  etaj_weight = parseInt($("#weight").val());
          if(etaj_weight <= '30')
          {s1 += (etaj_int * 13);}
          if(etaj_weight <= '50' && etaj_weight > '30')
          {s1 += (etaj_int * 18);}
          if(etaj_weight <= '100' && etaj_weight > '50')
          {s1 += (etaj_int * 31);}
          if(etaj_weight <= '500' && etaj_weight > '100')
          {s1 += (etaj_int * 45);}
				}

					if(pack>0)
					{
						s1+=pack;
						$("#pack").html(pack+" грн");
						pakage = pack;
						$("#report #package").show();
					}

					s1 = Math.round(s1 * 100)/100;
					s1 += s_small_to + s_small_from;
					s1 = parseFloat(s1.toFixed(2));
					//}
					s1 += parseFloat(calc_order);
          $("#report #orderprice").html(calc_order);
          s1+=p_cost;
					$("#report #pcost").html(p_cost+" грн");
					$("#report #total").html(s1+" грн");

					$("#report").show("slow");
				}
    		}

    	}
    }
    if(type == 3) //Доставка колес
    {
        s=0;

    	for (var k in tariffw)
    	{
    		for (var k2 in tariffw[k])
    		{
    			wcount = $("#wheel_"+k+"_"+k2).val();
    			wcost = $("#cwheel_"+k+"_"+k2).val();
    			if(wcount > 0)
    			{
    				s+=wcost*wcount;
    			}
    		}
    	}
    	s = parseFloat(s.toFixed(2));
    	$("#report #whellscost").html(s+" грн");
    	$("#report #whe").show();

    	s+=calc_order;
        $("#report #orderprice").html(calc_order);
        s+=p_cost;
        $("#report #pcost").html(p_cost+" грн");

        if(supermarket)
					s+=45;

        if(pack>0)
		{
			s+=pack;
			$("#pack").html(pack+" грн");
			pakage = pack;
			$("#report #package").show();
		}
		  s = parseFloat(s.toFixed(2));
    	$("#report #total").html(s+" грн");


    	$("#report").show("slow");



    }


    if((fid == 0) || (tid == 0) || (weight == "NaN") || (volume == "NaN") || (weight == 0) || (volume == 0))
    {

    	$("#error #fid_err").hide();
	    $("#error #tid_err").hide();
	    $("#error").hide();
      if(fid == 0)
      {
        $("#error #fid_err").html("");
        $("#error #fid_err").show();
      }
      if(tid == 0)
      {
        $("#error #tid_err").html("");
        $("#error #tid_err").show();
      }
      if((weight == "NaN")||(weight==0))
      {
        $("#error #weight_err").html("");
        $("#error #weight_err").show();
      }
      if((volume == "NaN")||(volume==0))
      {
        $("#error #volume_err").html("");
        $("#error #volume_err").show();
      }
      $("#error").show("slow");
    }
}
  var itog = 0;
function add_calc()
{
	$("#report").hide();
	$("#error").hide();
   	$("#report #vol").hide();
   	$("#report #wei").hide();
   	$("#report #whe").hide();
  // 	$("#report #tim").hide();
   	$("#report #tar1").hide();
   	$("#report #aa").hide();
   	$("#report #a1").hide();
   	$("#report #a2").hide();
   	$("#report #a3").hide();
   	$("#report #obl").hide();
   	$("#report #sale").hide();
   	$("#report #package").hide();
r_town_from = $("#town_from option:selected").html();
r_town_to = $("#town_to option:selected").html();

  var reports;
	if((fid>0) && (tid>0) && (weight>0) && (volume>0) && (cost>0))
	{
					if(s2<s1)
					{
						s1=s2;
						r_tarr = (tariff[fid][tid].m3+" грн/м<sup>3</sup>");
						r_summ = s2;
						//r_timing = tariff[fid][tid].timing;
					}
					else
					{
						r_tarr = (tariff[fid][tid].kg+" грн/кг");
						r_summ = s1;
            //r_timing = tariff[fid][tid].timing;
					}

	r_orderprice = calc_order;
	r_pcost = p_cost;
  r_wstr = (wstr+" кг");
  r_volume = (volume+"м<sup>3</sup>");
  itog = itog + summ;
  var template = $.template('block-template', {
    rep_town_from: r_town_from,
    rep_town_to: r_town_to,
    rep_orderprice: r_orderprice,
    rep_pcost: r_pcost,
    rep_volume: r_volume,
    //rep_door: door,
    r_ac1: rep_ac1,
    r_ac2: rep_ac2,
    rep_tariff: r_tarr,
    rep_summary: r_summ,
    rep_weight: r_wstr,
    //rep_tim:r_timing,
    rep_total: summ,
    rep_pack: pakage,
    rep_itogo: itog
});
$('#bla').append(template);
return false;

	$("#reports").show("slow");
  }
  else
  {
    	$("#error #fid_err").hide();
	    $("#error #tid_err").hide();
	    $("#error").hide();
      if(fid == 0)
      {
        $("#error #fid_err").html("");
        $("#error #fid_err").show();
      }
      if(tid == 0)
      {
        $("#error #tid_err").html("");
        $("#error #tid_err").show();
      }
      if(weight <= "0,001")
      {
        $("#error #weight_err").html("");
        $("#error #weight_err").show();
      }
      if(volume <= "0,001")
      {
        $("#error #volume_err").html("");
        $("#error #volume_err").show();
      }
      $("#error").show("slow");
    }
}

// Simple JavaScript Templating
// John Resig - Genius! http://ejohn.org/ - MIT Licensed
(function($) {
  var cache = {};

  $.template = function template(str, data) {
    // Figure out if we're getting a template, or if we need to
    // load the template - and be sure to cache the result.
    var fn = /^[a-z-_]+$/.test(str) ?
      (cache[str] = cache[str] || template(document.getElementById(str).innerHTML)) :

      // Generate a reusable function that will serve as a template
      // generator (and which will be cached).
      new Function("obj",
        "var p=[],print=function(){p.push.apply(p,arguments);};" +

        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +

        // Convert the template into pure JavaScript
        str
          .replace(/[\r\t\n]/g, " ")
          .split("<%").join("\t")
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t=(.*?)%>/g, "',$1,'")
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');}return p.join('');");

    // Provide some basic currying to the user
    return data ? fn( data ) : fn;
  };
})(jQuery);

