$(document).ready(
  function()
  {
    $(".representations tr:nth-child(odd)").addClass("odd");
  }
);
function changeTown()
{	fid=parseInt($("#town_from").val());
	tid=parseInt($("#town_to").val());

	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);	}}

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();

		$("#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 changeType1()
{	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","");
		$("#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");
		$("#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();
		$("#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 calculate()
{	$("#report").hide();
   	$("#report #vol").hide();
   	$("#report #wei").hide();
   	$("#report #whe").hide();
   	$("#report #tar1").hide();
   	$("#report #aa").hide();
   	$("#report #a1").hide();
   	$("#report #a2").hide();
   	$("#report #a3").hide();
   	$("#report #sale").hide();
   	$("#report #package").hide();
    var pack=0;
	//город отправитель и поулчатель
	fid=parseInt($("#town_from").val());
	tid=parseInt($("#town_to").val());

	$("#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;
	if(type2==3) addressee=true;
	if(type2==2) sender=true;
	if(type2==4) {sender=true;  addressee=true;}
	if($("#supermarket").attr("checked"))
		supermarket=true;


    pack = parseFloat($("input[name='pack']:checked").val());
	wstr = $("#weight").val();
	wstr = wstr.replace(",",".");
	wstr = wstr.replace(/[^0-9.]/,"");
	if($("#weight").val()!=wstr)
		$("#weight").val(wstr);
	weight = parseFloat(wstr);


	vstr = $("#volume").val();
	vstr = vstr.replace(",",".");
	vstr = vstr.replace(/[^0-9.]/,"");
	if($("#volume").val()!=vstr)
		$("#volume").val(vstr);
	volume = parseFloat(vstr);

    if(type!=2)
    {
		v1str = $("#v1").val();
		v1str = v1str.replace(",",".");
		v1str = v1str.replace(/[^0-9.]/,"");
		if($("#v1").val()!=v1str)
			$("#v1").val(v1str);
		v1 = parseFloat(v1str);

		v2str = $("#v2").val();
		v2str = v2str.replace(",",".");
		v2str = v2str.replace(/[^0-9.]/,"");
		if($("#v2").val()!=v2str)
			$("#v2").val(v2str);
		v2 = parseFloat(v2str);

		v3str = $("#v3").val();
		v3str = v3str.replace(",",".");
		v3str = v3str.replace(/[^0-9.]/,"");
		if($("#v3").val()!=v3str)
			$("#v3").val(v3str);
		v3 = parseFloat(v3str);

		volume2=v1*v2*v3/1000000;
		if(!volume)
			volume = volume2;

		if(volume2>volume)
			volume = volume2;
	}





	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)
    	{    		//alert("Эконом доставка");
    		var ac1=0;
		    var ac2=0;
		    var as=0;
		    if((fid>0) && (tid>0) && (weight>0) && (volume>0) && (cost>0))
			{
				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>");
					$("#report #volumecost").html(s2+" грн");
					$("#wei").hide();
					$("#vol").show();
				}
				else
				{
					$("#report #tariff").html(tariffe[fid][tid].kg+" грн/кг");
					$("#report #weightcost").html(s1+" грн");
					$("#wei").show();
					$("#vol").hide();
				}

				s1+=calc_order;
				$("#report #orderprice").html(calc_order);

				s1+=p_cost;
				$("#report #pcost").html(p_cost+" грн");

				if(supermarket)
					s1+=45;


				if(pack>0)
				{					s1+=pack;					$("#pack").html(pack+" грн");
					$("#report #package").show();				}

				s1=  Math.round(s1 * 100)/100;
				$("#report #total").html(s1+" грн");
				$("#report").show("slow");
			}

    	}
    	else
    	{    		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;
					price=0;
					priceadd=0;
					minw = 0;					var zone = tariff[fid][tid].zone;
					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;
    				ac1=  Math.round(ac1 * 100)/100;

    				$("#report #door").html(ac1+" грн");
    				$("#report #aa").show();
    				s=ac1;
    				if((weight>=1000)||(volume>=4))
					{
						/*s=s*0.8;
						$("#sale1").html("20%");
						$("#report #sale").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+" грн");
						$("#report #package").show();
					}
					s=  Math.round(s * 100)/100;

			    	$("#report #total").html(s+" грн");

			    	$("#report").show("slow");
				}    		}
    		else // Доставка груза
    		{    			var ac1=0;
			    var ac2=0;
			    if( ( weight<100 ) && (volume< 0.5 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = calc_kiev1;
				    	else
				    		ac1 = calc_other1;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = calc_kiev1;
				    	else
				    		ac2 = calc_other1;
				  	}


			    }else if( ( weight<500 ) && (volume< 2 ) )
			    {
			    	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.5 ) )
			    {
			    	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 ) )
			    {
			    	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 ) )
			    {
			    	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(fid>tid)
				{
					tmp=fid;
					fid=tid;
					tid=tmp;
				}



			    //alert(fid+" "+tid);
				if((fid>0) && (tid>0) && (weight>0) && (volume>0) && (cost>0))
				{
					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();
						$("#report #volumecost").html(s2+" грн");
						$("#wei").hide();
						$("#vol").show();
					}
					else
					{
						$("#report #tariff").html(tariff[fid][tid].kg+" грн/кг");
						$("#report #tar1").show();
						$("#report #weightcost").html(s1+" грн");
						$("#wei").show();
						$("#vol").hide();
					}

					if((weight>=1000)||(volume>=4))
					{
						/*s1=s1*0.8;
						$("#sale1").html("20%");
						$("#report #sale").show();*/
					}

					s1+=calc_order;
					$("#report #orderprice").html(calc_order);

					s1+=p_cost;
					$("#report #pcost").html(p_cost+" грн");

					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(pack>0)
					{
						s1+=pack;
						$("#pack").html(pack+" грн");
						$("#report #package").show();
					}

					s1=  Math.round(s1 * 100)/100;
					$("#report #total").html(s1+" грн");
					$("#report").show("slow");
				}
    		}
    	}    }
    if(type == 3) //Доставка колес
    {
        s=0;
    	//alert("Доставка колес");
    	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;
    				//alert(wcount);    			}
    		}    	}
    	$("#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+" грн");
			$("#report #package").show();
		}
    	$("#report #total").html(s+" грн");

    	$("#report").show("slow");



    }
    if(type == 4) // доставка паллет
    {    	if((fid==21) || (tid==21))
    	{    		pid=tid;    		if(tid==21)
    			pid = fid;
	        s=0;
            weight=0;
            volume=0;
            pcount=0;
		    paletcount = parseInt($("#pallet1").val());
		    s+= tariffp[pid]["p1"]*paletcount;
		    weight+=500*paletcount;
		    volume+=1*paletcount;
		    pcount+=paletcount;


		    paletcount = parseInt($("#pallet2").val());
		    s+= tariffp[pid]["p2"]*paletcount;
		    weight+=750*paletcount;
		    volume+=1.25*paletcount;
		    pcount+=paletcount;


		    paletcount = parseInt($("#pallet3").val());
		    s+= tariffp[pid]["p3"]*paletcount;
		    weight+=1000*paletcount;
		    volume+=1.5*paletcount;
		    pcount+=paletcount;

            var ac1=0;
			var ac2=0;
			    if( ( weight<100 ) && (volume< 0.5 ) )
			    {
			    	if(addressee)
			    	{
				    	if( (fid==21) ||(fid==63) ||(fid==64) ||(fid==65) )
				    		ac1 = calc_kiev1;
				    	else
				    		ac1 = calc_other1;
				  	}

				  	if(sender)
			    	{
				    	if( (tid==21) ||(tid==63) ||(tid==64) ||(tid==65) )
				    		ac2 = calc_kiev1;
				    	else
				    		ac2 = calc_other1;
				  	}

			    }else if( ( weight<500 ) && (volume< 2 ) )
			    {
			    	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.5 ) )
			    {
			    	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 ) )
			    {
			    	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 ) )
			    {
			    	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;
				  	}
			    }


	    	$("#report #whellscost").html(s+" грн");

	    	if((weight>=1000)||(volume>=4))
					{
						/*s=s*0.8;
						$("#sale1").html("20%");
						$("#report #sale").show();*/
					}

	    	$("#report #whe").show();
	    	s+=calc_order;
	        $("#report #orderprice").html(calc_order);
	        s+=p_cost;
	        $("#report #pcost").html(p_cost+" грн");

	        		if(addressee==true)
					{
						s+=ac1;
						$("#report #ac1").html(ac1);
						$("#a1").show();
					}

					if(sender==true)
					{
						s+=ac2;
						$("#report #ac2").html(ac2);
						$("#a3").show();
					}

			if(supermarket)
					s+=45;

            if(pack>0)
			{
				s+=pack*pcount;
				$("#pack").html((pack*pcount)+" грн");
				$("#report #package").show();
			}

	    	$("#report #total").html(s+" грн");

	    	$("#report").show("slow");
	  	}
    }
}
