function Recalculate(id)
{
	licence = $('#licence_'+id).val();
	format = $('#format_'+id).val();
	if (empty(licence))
	{
		$('#licence_'+id).css('background-color','red');
		$('#licence_'+id).css('color','white');
	}
	if (empty(format))
	{
		$('#format_'+id).css('background-color','red');
		$('#format_'+id).css('color','white');
	}
	if (empty(licence) && empty(format))
	{
		cartSelectUnset(id);
		totalsum(0,$('#crcost_'+id).text());
		/*
		 * used to work with UAH cost
		totalsum(0,$('#crcost_'+id).text(),0$('#uahcost_'+id).text());
		*/
		$('#crcost_'+id).text(0);
		/*
		 * used to work with UAH cost
		$('#uahcost_'+id).text(0);
		*/
	}
	if (!empty(licence) && !empty(format))
	{
		cartCheckbox(id);
		cartSelectUnset(id);
		$.post(
			base_url + 'cart.html',
			{
				cmd: 'recalculate',
				id: id,
				licence: licence,
				format: format
			},
			onRecalculate
		);	
	}
	return false;
}

function onRecalculate(responce)
{
	if (!empty($('cost',responce).text()))
	{
		totalsum($('crd',responce).text(),$('#crcost_'+$('id',responce).text()).text())
		/*
		 * used to work with UAH cost
		totalsum($('crd',responce).text(),$('#crcost_'+$('id',responce).text()).text(),$('uah',responce).text(),$('#uahcost_'+$('id',responce).text()).text())
		*/
		$('#crcost_'+$('id',responce).text()).text($('crd',responce).text());
		$('#crcosthid_'+$('id',responce).text()).val($('crd',responce).text());
		/*
		 * used to work with UAH cost
		$('#uahcost_'+$('id',responce).text()).text($('uah',responce).text());
		*/
	}
	addDocuments(true);
}

function cartSelectUnset(id)
{
	$('#licence_'+id).css('background-color','white');
	$('#licence_'+id).css('color','black');
	$('#format_'+id).css('background-color','white');
	$('#format_'+id).css('color','black');
}

function cartCheckbox(id)
{
	$('#checkbox_'+id).attr('checked',true);
}

var totalcrd = 0;
var totaluah = 0;

function totalsum(amountcrd,oldcrd,amountuah,olduah)
{
	if (!empty(amountcrd))
	{
		totalcrd -= parseInt(oldcrd);
		totalcrd += parseInt(amountcrd);
	}
	if (amountcrd == 0)
	{
		totalcrd -= parseInt(oldcrd);
	}
	$('#totalcrd').text(totalcrd);
	if (olduah != null && amountuah != null)
	{
		if (!empty(amountuah))
		{
			totaluah -= parseInt(olduah);
			totaluah += parseInt(amountuah);
		}
		if (amountuah == 0)
		{
			totaluah -= parseInt(olduah);
		}
		$('#totaluah').text(totaluah);
	}
}

function markLicenceAndFormat(id)
{
	licence = $('#licence_'+id).val();
	format = $('#format_'+id).val();
	checkbox = $('#checkbox_'+id).attr('checked');
	if (!empty(checkbox))
	{
		if (empty(licence))
		{
			$('#licence_'+id).css('background-color','red');
			$('#licence_'+id).css('color','white');
		}
		if (empty(format))
		{
			$('#format_'+id).css('background-color','red');
			$('#format_'+id).css('color','white');
		}
	}
	else
	{
		cartSelectUnset(id);
	}
}

function addDocuments(recalculate)
{
	if (!empty($('#documents').attr('checked')))
	{
		$('#totalcrd').text(parseInt($('#totalcrd').text())+1);
	}
	else
	{
		if (empty(recalculate))
		{
			$('#totalcrd').text(parseInt($('#totalcrd').text())-1);
		}
	}
}