function count(form)
{
var textValues = "";
//variables
var price = 0;
var own = 0;
var lastPortionIn;
var lastPortion = 0;
var times = 0;
var oldCarValue = 0;
//fieldvalues
if ( form.price.value != "" )
    price = form.price.value;
//own = new Number( form.owns.options[form.owns.options.selectedIndex].value );
if ( form.owns.value != "" )
	own = new Number( form.owns.value );
if ( form.oldCarValue.value != "" )
        oldCarValue = new Number(form.oldCarValue.value);
lastPortionIn = form.lastPortion.options[form.lastPortion.options.selectedIndex].value;
if ( form.time.options[form.time.options.selectedIndex].value != "Valitse laina-aika" )
	times = 	form.time.options[form.time.options.selectedIndex].value;
if ( interestLocked == false )
{
	if ( form.interest.value != undefined )
		if ( form.interest.value != "" && form.interest.value != "0" )
			interest = form.interest.value.replace(",",".");
		else	
			form.interest.value = interest;
}
if ( portionCostLocked == false )
{
	if ( form.portionCost.value != undefined )
		if ( form.portionCost.value != "" )
			portionCost = form.portionCost.value;
		else
		{
			portionCost = 0;
			form.portionCost.value = 0;
		}
}
if ( provisionLocked == false )
{
	if ( form.provision.value != undefined )
		if ( form.provision.value != "" )
			provision = form.provision.value;
		else
		{
			provision = 0;
			form.provision.value = 0;
		}
}
//field value testing
if ( times == 0 )
{
	alert("Ole hyvä ja valitse laina-aika.");
	return false;
}
if ( isNaN( price ) )
{
	alert("Ole hyvä ja syötä hinta lukuna (ilman välilyöntejä, pisteitä tai pilkkuja).");
	return false;
}
if ( isNaN( oldCarValue ) )
{
	alert("Ole hyvä ja syötä vaihtoauton arvo lukuna (ilman välilyöntejä, pisteitä tai pilkkuja).");
	return false;
}
//conversions	
if ( lastPortionIn != "-" )
	lastPortion = price * lastPortionIn/100;    
if ( lastPortion != 0 )    
	times = 1*times - 1*lastPortionTime;
//check for own values
if ( checkOwn==true )
{	
	if ( (1*own + 1*oldCarValue) < (price * minOwn/100) )
	{
		alert("Omarahoitusosuuden on oltava vähintään "+minOwn+" % auton hinnasta, tässä tapauksessa siis vähintään "+Math.round(price * minOwn/100)+" \u20ac.");	
		return false;
	}
}	
//calculation
var financeAmount = 1*price - 1*own - 1*oldCarValue + 1*provision;
var root = 1*price - 1*own - 1*oldCarValue - 1*lastPortion + 1*provision;
var q = ( 1 + interest/1200);
var portion = ( root * Math.pow( q, times ) * ( (1*1 - 1*q) / (1*1 - Math.pow( q, times )) ) + 1*portionCost + interest/1200*lastPortion );
var totalPrice = new Number( portion * times + 1*own + 1*oldCarValue + 1*lastPortion );
var totalInterestSum = new Number( 1*totalPrice - (1*price + 1*provision + portionCost*times) );
var totalCosts = new Number( 1*totalPrice - 1*price );
var totalInterest;
if ( portionCost == 0 && provision == 0 )
	totalInterest = interest;	
else
	totalInterest = new Number( (200*12*totalCosts) / (financeAmount*(1*times+1*1)) );
var totalInterestText = totalInterest.toString();
//printing
form.financeAmount.value = financeAmount + " \u20ac"; 
form.totalInterestRate.value = totalInterestText.substr(0, totalInterestText.indexOf("."))+","+totalInterestText.substr(totalInterestText.indexOf(".")+1, 2) + " %";
form.totalInterestSum.value = Math.round(totalInterestSum) + " \u20ac";
form.totalCosts.value = Math.round(totalCosts) + " \u20ac";
form.totalPrice.value = Math.round(totalPrice) + " \u20ac";
if ( lastPortion == 0 )
	form.monthlyPaymentTimes.value = "1 - "+(1*times-1*1);
else
	form.monthlyPaymentTimes.value = "1 - "+times;
form.monthlyPayment.value = Math.round(portion) + " \u20ac";
if ( lastPortion == 0 )
	form.lastMonthlyPayment.value = Math.round(portion)+" \u20ac";
else
	form.lastMonthlyPayment.value = Math.round(lastPortion)+" \u20ac";
}
function prosToField( euros, targetFieldName )
{
	var result = "";
	var price = document.finance.price.value;
	if ( euros != "" && !isNaN(euros) && price != "" )
		 result = " = "+ convertOneDesimal((euros / price) * 100) + " %";
	document.finance.elements[targetFieldName].value = result;
}
function eurosToField( pros, targetFieldName )
{
	var result = "";
	var price = document.finance.price.value;
	if ( pros != "" && !isNaN(pros) && price != "" )
		 result = " = "+ convertOneDesimal((pros * price) / 100) + " \u20ac";
	document.finance.elements[targetFieldName].value = result;
}
function ownsTotal()
{
	var oldCarValue = document.finance.oldCarValue.value;
	var own = document.finance.owns.value;
	var price = document.finance.price.value;
	var ownTotal = new Number( 1*own + 1*oldCarValue );
	if ( price> 0 && ownTotal > 0 )
		document.finance.ownsTotalField.value = convertOneDesimal( ownTotal ) + " \u20ac = " + convertOneDesimal( ownTotal / price * 100 ) + " %";
}
function convertOneDesimal( numberValue )
{
	var textValue = "0";
	if ( ! isNaN( numberValue ) )
	{
		var textValue = numberValue.toString();
		textValue = textValue.replace(",", ".");
		if ( textValue.indexOf(".") > 0 )
			textValue =  textValue.substr(0, textValue.indexOf("."))+","+textValue.substr(textValue.indexOf(".")+1, 1);	
	}
	return textValue;
}
function LastPortionFieldCorrection( timesValue )
{	
	//set the eurofield to null
	document.finance.lastPortionEuro.value = "";
	var fieldToHandle = document.finance.lastPortion;	
	//delete old options
	var le = fieldToHandle.options.length;
	for( i = 0; i<le; i++)
	{
		deleteOption( i );
	}
	//if timevalue is chosen
	if ( timesValue=="Valitse laina-aika" )
	{		
		var newOption = new Option( lastPortionProsArray[0],  lastPortionProsArray[0] );
		fieldToHandle.options[0] = newOption;		
	}
	else
	{
		var lastIndex = -1;
		//check the last index
		for ( i=lastPortionListLength-1; i>=0 && lastIndex == -1; i-- )
			if ( lastPortionTimesArray[i] == timesValue )
				lastIndex = i;
		if ( lastIndex == -1 )
			lastIndex = lastPortionListLength - 1;
		//insert current values
		for( i=0; i<=lastIndex; i++)
		{
			var newOption = new Option( lastPortionProsArray[i],  lastPortionProsArray[i] );
			fieldToHandle.options[i] = newOption;
		}
	}
}
function deleteOption( optionToDelete )
{
	document.finance.lastPortion.options[optionToDelete] = null;
	if ( document.finance.lastPortion.options[optionToDelete] != null )
		deleteOption( optionToDelete );	
}

