var BestPriceTr = 0;
var Ensur_comp = new Array ('bta','if','parex','seesam','balva','balta','ergo','baltikums','ban');//,'nord'
var Ensur_comp_names = new Array ('BTA','IF','Gjensidige','Seesam','Balva','Balta','ERGO','Baltikums','BAN');//,'Nordica'
//var BrokerTimeout = CalculatorTimeoutDelay; // 30 seconds
var rtt_interval;

var bestprice_rotate_speed = 1000;
var bestprice_rotate_box = "tr.bestpricerow";

$.ajaxSetup({
  timeout: 30000,
});

$("tr.bestpricerow")
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-150px 0)"}, {duration:500})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-300px 0)"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "0 0"})
			}})
		});

function bestprice_rotate() 
{
	if (BestPriceTr==1)
	{
		BestPriceTr=2;
		$("tr.bestpricerow").stop();
		$("tr.bestpricerow").animate({backgroundColor: "#dee3e6"}, bestprice_rotate_speed);
	}
	else if (BestPriceTr==2)
	{
		BestPriceTr=1;
		$("tr.bestpricerow").animate({backgroundColor: "#FFFFFF"}, bestprice_rotate_speed);
	}
	else
	{
		BestPriceTr=0;
		$("tr.bestpricerow").stop();
		$("tr.bestpricerow").css({"background-color" : "transparent"});
		clearInterval(rtt_interval);
	}
}
 
$(function() {
	$('#period').sSelect();

	$("#count").ajaxStart(function() {
		clearInterval(rtt_interval);
		$('#automark').val('');
		$('#automodel').val('');
		$('#autoyear').val('');
		$('#tooltip').remove();
		$('#autonum').attr("disabled","disabled");
		$('#autoapl').attr("disabled","disabled");
		$('#period').attr("disabled","disabled");
		$('#periodtxt_lock').show();
		$('#periodtxt_lock').text($('#period').getSetSSValue());
		$('#count').attr("disabled","disabled");
		$('#reminder').attr("disabled","disabled");
		$('#count').val(HOME_PAGE_DICT[1]); // s4itaem
		$.ajax({url: url_path+'step2.php'});
	});

	$("#count").ajaxStop(function() {
	if($('span#amodel').text()=='') $('span#amodel').text(HOME_PAGE_DICT[2]);
	$('#autonum').removeAttr("disabled");
	$('#autoapl').removeAttr("disabled");
	$('#period').removeAttr("disabled");
	$('#periodtxt_lock').hide();
	$('#count').removeAttr("disabled");
	$('#reminder').removeAttr("disabled");
	$('#count').val(HOME_PAGE_DICT[0]); // pos4itatj
	var autonum=$('#autonum').val();
	var autoapl=$('#autoapl').val();
	var period=$('#period').val();
	var automark=$('#automark').val();
	var automodel=$('#automodel').val();
	var autoyear=$('#autoyear').val();

	var enddate='';
	if($('span#enddate').text()!=HOME_PAGE_DICT[2]) enddate=$('span#enddate').text();
	var prices_a='';
	var can_buy=$('#can_buy').val();
	if(can_buy=='1' || can_buy=='2' || can_buy=='3' || can_buy=='') can_buy=true;
	else can_buy=false;

	for(i=0; i<Ensur_comp.length; i++) {
		price=$('tr#'+Ensur_comp[i]+' td.price').text();
		if(price!='' && !isNaN(price)) {
			prices_a=prices_a+Ensur_comp_names[i]+'|'+period+'|'+price+'|';
			buy_url=Base64.encode(autonum+'|'+autoapl+'|'+period+'|'+lang+'|'+Ensur_comp_names[i]+'|'+price+'|'+automark+'|'+automodel+'|'+autoyear+'|'+enddate);
			if(can_buy) {
				$('tr#'+Ensur_comp[i]+' td.price').html('<a href="'+lurl_path+'step_number/3/buy/'+buy_url+'/">'+price+'</a>');
			} else {
				$('tr#'+Ensur_comp[i]+' td.price').html(price);
			}
		} else $('tr#'+Ensur_comp[i]+' td.price').text(price);

		price=$('tr#'+Ensur_comp[i]+' td.price12m').text();
		if(price!='' && !isNaN(price)) {
			prices_a=prices_a+Ensur_comp_names[i]+'|12|'+price+'|';
			buy_url=Base64.encode(autonum+'|'+autoapl+'|'+period+'|'+lang+'|'+Ensur_comp_names[i]+'|'+price+'|'+automark+'|'+automodel+'|'+autoyear+'|'+enddate);
			if(can_buy) {
				$('tr#'+Ensur_comp[i]+' td.price12m').html('<a href="' +lurl_path+ 'step_number/3/buy/'+buy_url+'/">'+price+'</a>');
			} else {
				$('tr#'+Ensur_comp[i]+' td.price12m').html(price);
			}
		} else $('tr#'+Ensur_comp[i]+' td.price12m').text(price);
	}

	$.cookie("prices_list", prices_a, {expires: 365});

	$('#reminder').click(function() {
		var autonum=$('#autonum').val();
		var autoapl=$('#autoapl').val();
		var period=$('#period').val();
		var automark=$('#automark').val();
		var automodel=$('#automodel').val();
		var autoyear=$('#autoyear').val();
		var enddate='';
		if($('span#enddate').text()!=HOME_PAGE_DICT[2]) enddate=$('span#enddate').text();
		document.location.assign(lurl_path+ 'step_number/reminder/buy/'+Base64.encode(autonum+'|'+autoapl+'|'+period+'|'+lang+'|'+automark+'|'+automodel+'|'+autoyear+'|'+enddate) + "/");
	});

	$('#reminder').show();
	});

	// * Recounting * /
	$('.cname').click(function() {
		var id = $(this).parent().attr("id"); // get ID value of corresponding TR

		var autonum = $.cookie("autonum");
		var autoapl = $.cookie("autoapl");
		var lang = $.cookie("lang");
		var period = $.cookie("period");

		$.ajax({
			url: url_path+'inc/brokers/get_'+id+'.php',
			timeout: BrokerTimeout,
			type: 'POST',
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=' + period,
			beforeSend: function(){
				$('tr#'+id+' td.price').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				$('tr#'+id+' td.economy').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
			},
			complete: function() {
				if($('tr#'+id+' td.price').text()=='') {
					$('tr#'+id+' td.price').text(HOME_PAGE_DICT[2]);
				}
			},
			success: function(result) {
				//$('#if').remove();
				var price;
				var arr;
				if (id=='parex' || id=='seesam' || id=='nord')
				{
					arr = result.split("|");
					price = arr[0];
				}
				else
				{
					price = result;
				};
				$('tr#'+id+' td.price').text(price);
		
				var price = $('tr#'+id+' td.price').text();
				var price12m = $('tr#'+id+' td.price12m').text();
				if (!$('tr#'+id+' td.price12m img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#'+id+' td.economy').text(econom);
					}
					else
					{
						$('tr#'+id+' td.economy').text('-');
					}
				}
			}
		});

		if (period !=12)
		{
			$.ajax({
				url: url_path+'inc/brokers/get_'+id+'.php',
				type: 'POST',
				timeout: BrokerTimeout,
				data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=12',
				beforeSend: function(){$('tr#'+id+' td.price12m').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');},
				complete: function() {
				if($('tr#'+id+' td.price12m').text()=='') {
					$('tr#'+id+' td.price12m').text(HOME_PAGE_DICT[2]);
				}
				},
				success: function(result) {
					var price;
					var arr;
					if (id=='parex' || id=='seesam')
					{
						arr = result.split("|");
						price = arr[0];
					}
					/*else if (id=='nord')
					{
						arr = result.split("|");
						price = arr[0];
					}*/
					else
					{
						price = result;
					}
					$('tr#'+id+' td.price12m').text(price);

					var price = $('tr#'+id+' td.price').text();
					var price12m = $('tr#'+id+' td.price12m').text();
					if (!$('tr#'+id+' td.price img').length) {
						if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
							var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
							econom = econom.toFixed(2);
							if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
							$('tr#'+id+' td.economy').text(econom);
						}
						else
						{
							$('tr#'+id+' td.economy').text('-');
						}
					}
				}
			});
		}

	});

	// Show/hide boxes
	$('#count').click(function() {
		if($('#autonum').val()=='') { alert(CHECK_AUTONUM); return; }
		if($('#autoapl').val()=='') { alert(CHECK_TEHNOMER); return; }
		$('#bestprice').val(1000000);
		$('tr').removeClass('bestpricerow');

		$('#content').css('background','url('+url_path+'img/bg3-1.jpg)');
		$('#jointxt').css('top','666px');

		$('#step1txt').hide();
		$('#step2txt').hide();
		$('#step3txt').hide();
		$('#step4txt').hide();
		$('#aboutocta').hide();
		$('#becomepartner').hide();

		$('#toptextbig2').show();
		$('#toptextsmall2').show();
		$('#bonusmalus').show();
		$('#polisfinish').show();
		$('#vehicle').show();
		$('#footnote').show();
		$('#prices').show();
		//$('#star').show();

		// Get numbers
		var autonum = $('#autonum').val().toUpperCase();
		var autoapl = $('#autoapl').val().toUpperCase();
		//var lang = 'LV';//$('input[@name=lang]').attr("value");
		var period = $('#period').val();

		$.cookie("autonum", autonum, {expires: 365});
		$.cookie("autoapl", autoapl, {expires: 365});
		$.cookie("period", period, {expires: 365});
		
		$('#periodvalue').text(period);

		if (period != '12'){
			$('#allprices td.price').css("padding-left","0");
			$('#headerprice').css("padding-left","0");
			$('tr#bta td.btaeconomy').css("padding-left","0");
			$('th#header12m').show();
			$('th#economy12m').show();
			$('td.price12m').show();
			$('td.economy').show();
		} else {
			$('#allprices td.price').css("padding-left","68px");
			$('#headerprice').css("padding-left","68px");
			$('tr#bta td.btaeconomy').css("padding-left","68px");
			$('th#header12m').hide();
			$('th#economy12m').hide();
			$('td.price12m').hide();
			$('td.economy').hide();
		}

		$.ajax({ // save auto and partner_id
			url: 'inc/save_auto.php',
			type: 'POST',
			data: 'autonum=' + autonum + '&autoapl=' + autoapl,
			beforeSend: function() {},
			success: function(result) { }
		});
	
		$.ajax({
			url: url_path+'inc/brokers/get_parex.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=' + period,
			beforeSend: function(){
				$('tr#parex td.price').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				$('span#bmclass').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" align="absmiddle" />');
				$('span#enddate').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" align="absmiddle" />');
			},
			complete: function() {
				if($('tr#parex td.price').text()=='') {
					$('tr#parex td.price').text(HOME_PAGE_DICT[2]);
					$('span#enddate').text(HOME_PAGE_DICT[2]);
					$('span#bmclass').text(HOME_PAGE_DICT[2]);
				}
			},
			success: function(result) {
				var arr = result.split("|");
				if (parseFloat(arr[0]) < parseFloat($('#bestprice').val()))
				{
					BestPriceTr=0;
					bestprice_rotate();
					remoweTooltips();
					$('#bestprice').val(arr[0]);
					$('tr').removeClass('bestpricerow');
					$('tr#parex').addClass('bestpricerow');
					BestPriceTr=2;
					bestprice_rotate_box = "tr#parex td.price";
					rtt_interval = setInterval(bestprice_rotate,bestprice_rotate_speed);		
					bestprice_rotate();
					$('tr#parex td.price').bind('mouseover', mouseOverHandler);
					$('tr#parex td.price').bind('mouseout', mouseOutHandler);
					$('tr#parex td.price12m').bind('mouseover', mouseOverHandler);
					$('tr#parex td.price12m').bind('mouseout', mouseOutHandler);
					$('tr#parex td.economy').bind('mouseover', mouseOverHandler);
					$('tr#parex td.economy').bind('mouseout', mouseOutHandler);
					$('tr#parex').bind('mousemove', mouseMoveHandler);
				}
				arr[0] = parseFloat(arr[0]).toFixed(2);
				$('tr#parex td.price').text(arr[0]);
				$('span#bmclass').text(arr[1]);
				if(arr[2]=='') {
					$('span#enddate').text(HOME_PAGE_DICT[2]);
				} else {
					$('span#enddate').text(arr[2]);
					$('#can_buy').val(arr[3])
					 if(arr[3]==1) {
						msg=PAZINJOJUMI_DICT[0];
						msg=msg.replace(/NNNNN/g,autonum);
						var messanger = new WPMdialog('alerter_octa',410,-211,300,110,HOME_PAGE_DICT[5],msg);
						messanger.show();
					} else if(arr[3]==2) {
						msg=PAZINJOJUMI_DICT[1];
						msg=msg.replace(/NNNNN/g,autonum);
						msg=msg.replace(/YYYYYY/g,arr[2]);
						var messanger = new WPMdialog('alerter_octa',410,-211,300,110,HOME_PAGE_DICT[5],msg);
						messanger.show();
					} else if(arr[3]==3) {
						msg=PAZINJOJUMI_DICT[3];
						msg=msg.replace(/NNNNN/g,autonum);
						msg=msg.replace(/YYYYYY/g,arr[2]);
						var messanger = new WPMdialog('alerter_octa',410,-211,300,110,HOME_PAGE_DICT[5],msg);
						messanger.show();
					} else {
						msg=PAZINJOJUMI_DICT[2];
						msg=msg.replace(/NNNNN/g,autonum);
						msg=msg.replace(/YYYYYY/g,arr[2]);
						msg=msg.replace(/ZZZZZZ/g,arr[4]);
						msg=msg.replace(/TTTTTT/g,arr[5]);
						var messanger = new WPMdialog('alerter_octa',410,-211,300,110,HOME_PAGE_DICT[5],msg);
						messanger.show();
					}
					document.onclick=function(event) {
					event = event || window.event;
					messanger.close();
					event.cancelBubble = true;
					}
				}

				var price = $('tr#parex td.price').text();
				var price12m = $('tr#parex td.price12m').text();
				if (!$('tr#parex td.price12m img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#parex td.economy').text(econom);
					}
					else
					{
						$('tr#parex td.economy').text('-');
					}
				}
			}
		});
		$.ajax({
			url: url_path+'inc/brokers/get_if.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=' + period,
			beforeSend: function(){$('tr#if td.price').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');},
			complete: function() {
				if($('tr#if td.price').text()=='') {
					$('tr#if td.price').text(HOME_PAGE_DICT[2]);
				}
			},
			success: function(result) {
				//$('#if').remove();
				if (parseFloat(result) < parseFloat($('#bestprice').val()))
				{
					BestPriceTr=0;
					bestprice_rotate();
					remoweTooltips();
					$('#bestprice').val(result);
					$('tr').removeClass('bestpricerow');
					$('tr#if').addClass('bestpricerow');
					BestPriceTr=2;
					rtt_interval = setInterval(bestprice_rotate,bestprice_rotate_speed);
					bestprice_rotate_box = "tr#if td.price";
					bestprice_rotate();
					$('tr#if td.price').bind('mouseover', mouseOverHandler);
					$('tr#if td.price').bind('mouseout', mouseOutHandler);
					$('tr#if td.price12m').bind('mouseover', mouseOverHandler);
					$('tr#if td.price12m').bind('mouseout', mouseOutHandler);
					$('tr#if td.economy').bind('mouseover', mouseOverHandler);
					$('tr#if td.economy').bind('mouseout', mouseOutHandler);
					$('tr#if').bind('mousemove', mouseMoveHandler);
				}

				$('tr#if td.price').text(result);

				var price = $('tr#if td.price').text();
				var price12m = $('tr#if td.price12m').text();

				if (!$('tr#if td.price12m img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#if td.economy').text(econom);
					}
					else
					{
						$('tr#if td.economy').text('-');
					}
				}
			}
		});
		$.ajax({
			url: url_path+'inc/brokers/get_ban.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=' + period,
			beforeSend: function(){$('tr#ban td.price').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');},
			complete: function() {
				if($('tr#ban td.price').text()=='') {
					$('tr#ban td.price').text(HOME_PAGE_DICT[2]);
				}
			},
			success: function(result) {
				//$('#if').remove();
				if (parseFloat(result) < parseFloat($('#bestprice').val()))
				{
					BestPriceTr=0;
					bestprice_rotate();
					remoweTooltips();
					$('#bestprice').val(result);
					$('tr').removeClass('bestpricerow');
					$('tr#ban').addClass('bestpricerow');
					BestPriceTr=2;
					bestprice_rotate_box = "tr#ban td.price";
					rtt_interval = setInterval(bestprice_rotate,bestprice_rotate_speed);		
					bestprice_rotate();
					$('tr#ban td.price').bind('mouseover', mouseOverHandler);
					$('tr#ban td.price').bind('mouseout', mouseOutHandler);
					$('tr#ban td.price12m').bind('mouseover', mouseOverHandler);
					$('tr#ban td.price12m').bind('mouseout', mouseOutHandler);
					$('tr#ban td.economy').bind('mouseover', mouseOverHandler);
					$('tr#ban td.economy').bind('mouseout', mouseOutHandler);
					$('tr#ban').bind('mousemove', mouseMoveHandler);
				}
				$('tr#ban td.price').text(result);

				var price = $('tr#ban td.price').text();
				var price12m = $('tr#ban td.price12m').text();
				if (!$('tr#ban td.price12m img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#ban td.economy').text(econom);
					}
					else
					{
						$('tr#ban td.economy').text('-');
					}
				}
			}
		});
		$.ajax({
			url: url_path+'inc/brokers/get_balva.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=' + period,
			beforeSend: function(){$('tr#balva td.price').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');},
			complete: function() {
				if($('tr#balva td.price').text()=='') {
					$('tr#balva td.price').text(HOME_PAGE_DICT[2]);
				}
			},
			success: function(result) {
				//$('#if').remove();
				if (parseFloat(result) < parseFloat($('#bestprice').val()))
				{
					BestPriceTr=0;
					bestprice_rotate();
					remoweTooltips();
					$('#bestprice').val(result);
					$('tr').removeClass('bestpricerow');
					$('tr#balva').addClass('bestpricerow');
					BestPriceTr=2;
					bestprice_rotate_box = "tr#balva td.price";
					rtt_interval = setInterval(bestprice_rotate,bestprice_rotate_speed);		
					bestprice_rotate();
					$('tr#balva td.price').bind('mouseover', mouseOverHandler);
					$('tr#balva td.price').bind('mouseout', mouseOutHandler);
					$('tr#balva td.price12m').bind('mouseover', mouseOverHandler);
					$('tr#balva td.price12m').bind('mouseout', mouseOutHandler);
					$('tr#balva td.economy').bind('mouseover', mouseOverHandler);
					$('tr#balva td.economy').bind('mouseout', mouseOutHandler);
					$('tr#balva').bind('mousemove', mouseMoveHandler);
				}
				$('tr#balva td.price').text(result);

				var price = $('tr#balva td.price').text();
				var price12m = $('tr#balva td.price12m').text();
				if (!$('tr#balva td.price12m img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#balva td.economy').text(econom);
					}
					else
					{
						$('tr#balva td.economy').text('-');
					}
				}
			}
		});
		/*$.ajax({
			url: url_path+'inc/brokers/get_nord.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=' + period,
			beforeSend: function(){
				$('tr#nord td.price').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
			},
			complete: function() {
				if($('tr#nord td.price').text()=='') {
					$('tr#nord td.price').text(HOME_PAGE_DICT[2]);
				}
			},
			success: function(result) {
				//$('#if').remove();
				arr = result.split("|");
				price = arr[0];
				if(arr[1]!='' && arr[1]!= null && arr[1]!='undefined') {
					$('#autoyear').val(arr[1]);
					if($('span#amodel').text()!='') $('span#amodel').text($('span#amodel').text()+' '+arr[1]+HOME_PAGE_DICT[6]);
					else $('span#amodel').text(arr[1]+HOME_PAGE_DICT[6]);
				} else $('#autoyear').val('');

				if (parseFloat(result) < parseFloat($('#bestprice').val()))
				{
					BestPriceTr=0;
					bestprice_rotate();
					remoweTooltips();
					$('#bestprice').val(result);
					$('tr').removeClass('bestpricerow');
					$('tr#nord').addClass('bestpricerow');
					BestPriceTr=2;
					bestprice_rotate_box = "tr#nord td.price";
					rtt_interval = setInterval(bestprice_rotate,bestprice_rotate_speed);		
					bestprice_rotate();
					$('tr#nord td.price').bind('mouseover', mouseOverHandler);
					$('tr#nord td.price').bind('mouseout', mouseOutHandler);
					$('tr#nord td.price12m').bind('mouseover', mouseOverHandler);
					$('tr#nord td.price12m').bind('mouseout', mouseOutHandler);
					$('tr#nord td.economy').bind('mouseover', mouseOverHandler);
					$('tr#nord td.economy').bind('mouseout', mouseOutHandler);
					$('tr#nord').bind('mousemove', mouseMoveHandler);
				}

				$('tr#nord td.price').text(price);

				var price = $('tr#nord td.price').text();
				var price12m = $('tr#nord td.price12m').text();
				if (!$('tr#nord td.price12m img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#nord td.economy').text(econom);
					}
					else
					{
						$('tr#nord td.economy').text('-');
					}
				}
			}
		});*/
		$.ajax({
			url: url_path+'inc/brokers/get_baltikums.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=' + period,
			beforeSend: function(){$('tr#baltikums td.price').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');},
			complete: function() {
				if($('tr#baltikums td.price').text()=='') {
					$('tr#baltikums td.price').text(HOME_PAGE_DICT[2]);
				}
			},
			success: function(result) {
				//$('#if').remove();
				if (parseFloat(result) < parseFloat($('#bestprice').val()))
				{
					BestPriceTr=0;
					bestprice_rotate();
					remoweTooltips();
					$('#bestprice').val(result);
					$('tr').removeClass('bestpricerow');
					$('tr#baltikums').addClass('bestpricerow');
					BestPriceTr=2;
					bestprice_rotate_box = "tr#baltikums td.price";
					rtt_interval = setInterval(bestprice_rotate,bestprice_rotate_speed);		
					bestprice_rotate();
					$('tr#baltikums td.price').bind('mouseover', mouseOverHandler);
					$('tr#baltikums td.price').bind('mouseout', mouseOutHandler);
					$('tr#baltikums td.price12m').bind('mouseover', mouseOverHandler);
					$('tr#baltikums td.price12m').bind('mouseout', mouseOutHandler);
					$('tr#baltikums td.economy').bind('mouseover', mouseOverHandler);
					$('tr#baltikums td.economy').bind('mouseout', mouseOutHandler);
					$('tr#baltikums').bind('mousemove', mouseMoveHandler);
				}

				$('tr#baltikums td.price').text(result);

				var price = $('tr#baltikums td.price').text();
				var price12m = $('tr#baltikums td.price12m').text();
				if (!$('tr#baltikums td.price12m img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#baltikums td.economy').text(econom);
					}
					else
					{
						$('tr#baltikums td.economy').text('-');
					}
				}
			}
		});
		$.ajax({
			url: url_path+'inc/brokers/get_ergo.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=' + period,
			beforeSend: function(){$('tr#ergo td.price').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');},
			complete: function() {
				if($('tr#ergo td.price').text()=='') {
					$('tr#ergo td.price').text(HOME_PAGE_DICT[2]);
				}
			},
			success: function(result) {
				//$('#if').remove();
				if (parseFloat(result) < parseFloat($('#bestprice').val()))
				{
					BestPriceTr=0;
					bestprice_rotate();
					remoweTooltips();
					$('#bestprice').val(result);
					$('tr').removeClass('bestpricerow');
					$('tr#ergo').addClass('bestpricerow');
					BestPriceTr=2;
					bestprice_rotate_box = "tr#ergo td.price";
					rtt_interval = setInterval(bestprice_rotate,bestprice_rotate_speed);		
					bestprice_rotate();
					$('tr#ergo td.price').bind('mouseover', mouseOverHandler);
					$('tr#ergo td.price').bind('mouseout', mouseOutHandler);
					$('tr#ergo td.price12m').bind('mouseover', mouseOverHandler);
					$('tr#ergo td.price12m').bind('mouseout', mouseOutHandler);
					$('tr#ergo td.economy').bind('mouseover', mouseOverHandler);
					$('tr#ergo td.economy').bind('mouseout', mouseOutHandler);
					$('tr#ergo').bind('mousemove', mouseMoveHandler);
				}
				$('tr#ergo td.price').text(result);

				var price = $('tr#ergo td.price').text();
				var price12m = $('tr#ergo td.price12m').text();
				if (!$('tr#ergo td.price12m img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#ergo td.economy').text(econom);
					}
					else
					{
						$('tr#ergo td.economy').text('-');
					}
				}
			}
		});
		$.ajax({
			url: url_path+'inc/brokers/get_balta.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=' + period,
			beforeSend: function(){$('tr#balta td.price').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');},
			complete: function() {
				if($('tr#balta td.price').text()=='') {
					$('tr#balta td.price').text(HOME_PAGE_DICT[2]);
				}
			},
			success: function(result) {
				//$('#if').remove();
				if (parseFloat(result) < parseFloat($('#bestprice').val()))
				{
					BestPriceTr=0;
					bestprice_rotate();
					remoweTooltips();
					$('#bestprice').val(result);
					$('tr').removeClass('bestpricerow');
					$('tr#balta').addClass('bestpricerow');
					BestPriceTr=2;
					bestprice_rotate_box = "tr#balta td.price";
					rtt_interval = setInterval(bestprice_rotate,bestprice_rotate_speed);		
					bestprice_rotate();
					$('tr#balta td.price').bind('mouseover', mouseOverHandler);
					$('tr#balta td.price').bind('mouseout', mouseOutHandler);
					$('tr#balta td.price12m').bind('mouseover', mouseOverHandler);
					$('tr#balta td.price12m').bind('mouseout', mouseOutHandler);
					$('tr#balta td.economy').bind('mouseover', mouseOverHandler);
					$('tr#balta td.economy').bind('mouseout', mouseOutHandler);
					$('tr#balta').bind('mousemove', mouseMoveHandler);
				}
				$('tr#balta td.price').text(result);

				var price = $('tr#balta td.price').text();
				var price12m = $('tr#balta td.price12m').text();
				if (!$('tr#balta td.price12m img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#balta td.economy').text(econom);
					}
					else
					{
						$('tr#balta td.economy').text('-');
					}
				}
			}
		});
		$.ajax({
			url: url_path+'inc/brokers/get_seesam.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=' + period,
			beforeSend: function(){
				$('tr#seesam td.price').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				$('span#amodel').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" align="absmiddle"/>');
			},
			complete: function() {
				if($('tr#seesam td.price').text()=='') {
					$('tr#seesam td.price').text(HOME_PAGE_DICT[2]);
				}
			},
			success: function(result) {
				//$('#if').remove();
				var arr = result.split("|");
				if (parseFloat(arr[0]) < parseFloat($('#bestprice').val()))
				{
					BestPriceTr=0;
					bestprice_rotate();
					remoweTooltips();
					$('#bestprice').val(arr[0]);
					$('tr').removeClass('bestpricerow');
					$('tr#seesam').addClass('bestpricerow');
					BestPriceTr=2;
					bestprice_rotate_box = "tr#seesam td.price";
					rtt_interval = setInterval(bestprice_rotate,bestprice_rotate_speed);		
					bestprice_rotate();
					$('tr#seesam td.price').bind('mouseover', mouseOverHandler);
					$('tr#seesam td.price').bind('mouseout', mouseOutHandler);
					$('tr#seesam td.price12m').bind('mouseover', mouseOverHandler);
					$('tr#seesam td.price12m').bind('mouseout', mouseOutHandler);
					$('tr#seesam td.economy').bind('mouseover', mouseOverHandler);
					$('tr#seesam td.economy').bind('mouseout', mouseOutHandler);
					$('tr#seesam').bind('mousemove', mouseMoveHandler);
				}
				if(arr[1]!='' && arr[2]!='' && arr[1] != null && arr[2] != null) {
				if($('span#amodel').text()!='') $('span#amodel').text(arr[1]+' '+arr[2]+' '+$('span#amodel').text());
				else $('span#amodel').text(arr[1]+' '+arr[2]);
				$('#automark').val(arr[1]);
				$('#automodel').val(arr[2]);
				} else $('span#amodel').text('-');

				$('tr#seesam td.price').text(arr[0]);

				var price = $('tr#seesam td.price').text();
				var price12m = $('tr#seesam td.price12m').text();
				if (!$('tr#seesam td.price12m img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#seesam td.economy').text(econom);
					}
					else
					{
						$('tr#seesam td.economy').text('-');
					}
				}
			}
		});

	// * Cost per Year Column * /
	if (period !=12)
	{
		$.ajax({
			url: url_path+'inc/brokers/get_if.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=12',
			beforeSend: function(){
				$('tr#if td.price12m').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				$('tr#if td.economy').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
			},
			complete: function() {
				if($('tr#if td.price12m').text()=='') {
					$('tr#if td.price12m').text(HOME_PAGE_DICT[2]);
					$('tr#if td.economy').text('-');
				}
			},
			success: function(result) {
				$('tr#if td.price12m').text(result);

				var price = $('tr#if td.price').text();
				var price12m = $('tr#if td.price12m').text();
				if (!$('tr#if td.price img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#if td.economy').text(econom);
					}
					else
					{
						$('tr#if td.economy').text('-');
					}
				}
			}
		});
		$.ajax({
			url: url_path+'inc/brokers/get_parex.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=12',
			beforeSend: function(){
				$('tr#parex td.price12m').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				$('tr#parex td.economy').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
			},
			complete: function() {
				if($('tr#parex td.price12m').text()=='') {
					$('tr#parex td.price12m').text(HOME_PAGE_DICT[2]);
					$('tr#parex td.economy').text('-');
				}
			},
			success: function(result) {
				var arr = result.split("|");
				$('tr#parex td.price12m').text(parseFloat(arr[0]).toFixed(2));

				var price = $('tr#parex td.price').text();
				var price12m = $('tr#parex td.price12m').text();
				if (!$('tr#parex td.price img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#parex td.economy').text(econom);
					}
					else
					{
						$('tr#parex td.economy').text('-');
					}
				}
			}
		});
		$.ajax({
			url: url_path+'inc/brokers/get_ban.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=12',
			beforeSend: function(){
				$('tr#ban td.price12m').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				$('tr#ban td.economy').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				},
			complete: function() {
				if($('tr#ban td.price12m').text()=='') {
					$('tr#ban td.price12m').text(HOME_PAGE_DICT[2]);
					$('tr#ban td.economy').text('-');
				}
			},
			success: function(result) {
				$('tr#ban td.price12m').text(result);

				var price = $('tr#ban td.price').text();
				var price12m = $('tr#ban td.price12m').text();
				if (!$('tr#ban td.price img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#ban td.economy').text(econom);
					}
					else
					{
						$('tr#ban td.economy').text('-');
					}
				}
			}
		});
		$.ajax({
			url: url_path+'inc/brokers/get_balva.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=12',
			beforeSend: function(){
				$('tr#balva td.price12m').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				$('tr#balva td.economy').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
			},
			complete: function() {
				if($('tr#balva td.price12m').text()=='') {
					$('tr#balva td.price12m').text(HOME_PAGE_DICT[2]);
					$('tr#balva td.economy').text('-');
				}
			},
			success: function(result) {
				$('tr#balva td.price12m').text(result);

				var price = $('tr#balva td.price').text();
				var price12m = $('tr#balva td.price12m').text();
				if (!$('tr#balva td.price img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#balva td.economy').text(econom);
					}
					else
					{
						$('tr#balva td.economy').text('-');
					}
				}
			}
		});
		/*
		$.ajax({
			url: url_path+'inc/brokers/get_nord.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=12',
			beforeSend: function(){
				$('tr#nord td.price12m').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				$('tr#nord td.economy').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
			},
			complete: function() {
				if($('tr#nord td.price12m').text()=='') {
					$('tr#nord td.price12m').text(HOME_PAGE_DICT[2]);
					$('tr#nord td.economy').text('-');
				}
			},
			success: function(result) {
				var arr = result.split("|");
				$('tr#nord td.price12m').text(arr[0]);

				var price = $('tr#nord td.price').text();
				var price12m = $('tr#nord td.price12m').text();
				if (!$('tr#nord td.price img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#nord td.economy').text(econom);
					}
					else
					{
						$('tr#nord td.economy').text('-');
					}
				}
			}
		});*/
		$.ajax({
			url: url_path+'inc/brokers/get_baltikums.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=12',
			beforeSend: function(){
				$('tr#baltikums td.price12m').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				$('tr#baltikums td.economy').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
			},
			complete: function() {
				if($('tr#baltikums td.price12m').text()=='') {
					$('tr#baltikums td.price12m').text(HOME_PAGE_DICT[2]);
					$('tr#baltikums td.economy').text('-');
				}
			},
			success: function(result) {
				$('tr#baltikums td.price12m').text(result);

				var price = $('tr#baltikums td.price').text();
				var price12m = $('tr#baltikums td.price12m').text();
				if (!$('tr#baltikums td.price img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#baltikums td.economy').text(econom);
					}
					else
					{
						$('tr#baltikums td.economy').text('-');
					}
				}
			}
		});
		$.ajax({
			url: url_path+'inc/brokers/get_ergo.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=12',
			beforeSend: function(){
				$('tr#ergo td.price12m').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				$('tr#ergo td.economy').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
			},
			complete: function() {
				if($('tr#ergo td.price12m').text()=='') {
					$('tr#ergo td.price12m').text(HOME_PAGE_DICT[2]);
					$('tr#ergo td.economy').text('-');
				}
			},
			success: function(result) {
				$('tr#ergo td.price12m').text(result);

				var price = $('tr#ergo td.price').text();
				var price12m = $('tr#ergo td.price12m').text();
				if (!$('tr#ergo td.price img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#ergo td.economy').text(econom);
					}
					else
					{
						$('tr#ergo td.economy').text('-');
					}
				}
			}
		});
		$.ajax({
			url: url_path+'inc/brokers/get_balta.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=12',
			beforeSend: function(){
				$('tr#balta td.price12m').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				$('tr#balta td.economy').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
			},
			complete: function() {
				if($('tr#balta td.price12m').text()=='') {
					$('tr#balta td.price12m').text(HOME_PAGE_DICT[2]);
					$('tr#balta td.economy').text('-');
				}
			},
			success: function(result) {
				$('tr#balta td.price12m').text(result);

				var price = $('tr#balta td.price').text();
				var price12m = $('tr#balta td.price12m').text();
				if (!$('tr#balta td.price img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#balta td.economy').text(econom);
					}
					else
					{
						$('tr#balta td.economy').text('-');
					}
				}
			}
		});
		$.ajax({
			url: url_path+'inc/brokers/get_seesam.php',
			type: 'POST',
			timeout: BrokerTimeout,
			data: 'autonum=' + autonum + '&autoapl=' + autoapl + '&lang=' + lang + '&period=12',
			beforeSend: function(){
				$('tr#seesam td.price12m').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
				$('tr#seesam td.economy').html('<img src="'+url_path+'img/ajax-loader.gif" alt="" />');
			},
			complete: function() {
				if($('tr#seesam td.price12m').text()=='') {
					$('tr#seesam td.price12m').text(HOME_PAGE_DICT[2]);
					$('tr#seesam td.economy').text('-');
				}
			},
			success: function(result) {
				var arr = result.split("|");
				$('tr#seesam td.price12m').text(arr[0]);

				var price = $('tr#seesam td.price').text();
				var price12m = $('tr#seesam td.price12m').text();
				if (!$('tr#seesam td.price img').length) {
					if (!isNaN(parseInt(price12m, 10)) && !isNaN(parseInt(price, 10))) {
						var econom = parseFloat(price)*(12/period)-parseFloat(price12m);
						econom = econom.toFixed(2);
						if(!isNaN(econom) && (econom==0 || econom<0)) econom='-';
						$('tr#seesam td.economy').text(econom);
					}
					else
					{
						$('tr#seesam td.economy').text('-');
					}
				}
			}
		});		
		//
		//
	}//endif
	});

	function remoweTooltips()
	{
		for(i=0; i<Ensur_comp.length; i++) {
			$('tr#'+Ensur_comp[i]+' td.price').unbind('mouseover', mouseOverHandler);
			$('tr#'+Ensur_comp[i]+' td.price').unbind('mouseout', mouseOutHandler);
			$('tr#'+Ensur_comp[i]+' td.price12m').unbind('mouseover', mouseOverHandler);
			$('tr#'+Ensur_comp[i]+' td.price12m').unbind('mouseout', mouseOutHandler);
			$('tr#'+Ensur_comp[i]+' td.economy').unbind('mouseover', mouseOverHandler);
			$('tr#'+Ensur_comp[i]+' td.economy').unbind('mouseout', mouseOutHandler);
		}
	}

	function mouseOverHandler(e) {
		var html = '<div id="tooltip">'+HOME_PAGE_DICT[3]+'</div>';

		$('body').append(html).children('#tooltip').hide().show();
		$('#tooltip').css('top', e.pageY + -30).css('left', e.pageX + 20);

	};
	function mouseOutHandler() {
		$('#tooltip').remove();
	};

	function mouseMoveHandler(e) {
		$('#tooltip').css('top', e.pageY + -30).css('left', e.pageX + 20);
	};
	//

	// Tooltip Recount
	$('td.cname span').hover(function(e) {
		var html = '<div id="tooltip_recount">'+HOME_PAGE_DICT[4]+'</div>';

		$('body').append(html).children('#tooltip_recount').hide().show();
		$('#tooltip_recount').css('top', e.pageY + -30).css('left', e.pageX + 20);

	}, function() {
		$('#tooltip_recount').remove();
	});

	$('td.cname span').mousemove(function(e) {
		$('#tooltip_recount').css('top', e.pageY + -30).css('left', e.pageX + 20);
	});
	//

});