// Бонус-малус коэффициенты
var arKbm = new Array( 
	new Array( "М", 2.45, 0, "М", "М", "М", "М" ),
	new Array( 0, 2.3, 1, "М", "М", "М", "М" ),
	new Array( 1, 1.55, 2, "М", "М", "М", "М" ),
	new Array( 2, 1.4, 3, 1, "М", "М", "М" ),
	new Array( 3, 1, 4, 1, "М", "М", "М" ),
	new Array( 4, 0.95, 5, 2, 1, "М", "М" ),
	new Array( 5, 0.9, 6, 3, 1, "М", "М" ),
	new Array( 6, 0.85, 7, 4, 2, "М", "М" ),
	new Array( 7, 0.8, 8, 4, 2, "М", "М" ),
	new Array( 8, 0.75, 9, 5, 2, "М", "М" ),
	new Array( 9, 0.7, 10, 5, 2, 1, "М" ),
	new Array( 10, 0.65, 11, 6, 3, 1, "М" ),
	new Array( 11, 0.6, 12, 6, 3, 1, "М" ),
	new Array( 12, 0.55, 13, 6, 3, 1, "М" ),
	new Array( 13, 0.5, 13, 7, 3, 1, "М" )
	)

function getKbm()
{
	var obj = document.all.YearsOfInsurance
	var iYearsOfInsurance = obj[obj.selectedIndex].value
	var iClass = 3
	iClassIndex = ( iClass == "М" )? 0 : iClass+1 // корректируем класс, чтобы получить коэф.

	for( var i = 1; i <= iYearsOfInsurance; i++ )
	{
		eval( "var iHits = new Number( document.forms[0].YearOfInsurance"+i+"[document.forms[0].YearOfInsurance"+i+".selectedIndex].value )" )
		iClass = arKbm[iClassIndex][iHits+2]
		iClassIndex = ( iClass == "М" )? 0 : iClass+1 // корректируем класс, чтобы получить коэф.
	}

	var Kbm = arKbm[iClassIndex][1]

	return Kbm
	
}

//required fields:
arrRequiredFields = new Array(
					"owner",
					"region",
					"category",
					"power",
					"controlLimit",
					"stage0",
					"age0")

	function validForm() {
		var currentForm = document.osago;	// current form
		if (currentForm.controlLimit.value=="YES")
         {
		 if (currentForm.age0.value=="")
		  {
          alert("Выберите возраст водителя");
          currentForm.age0.focus();
          return false;
          }
		 if (currentForm.stage0.value=="")
		  {
          alert("Выберите стаж водителя");
          currentForm.stage0.focus();
          return false;
          }
         }
		calculate();
		return true
	}	
	
	
	function calculate() {

		var i;
		var tmpStr;
		var currentForm = document.osago;	// current form

		// choosing values
		var currOwner = currentForm.owner.value;
		var currCategory = currentForm.category[currentForm.category.selectedIndex].value;
		var currCategoryIndex = currentForm.category.selectedIndex;
		if ( currCategory == "L") {
			currCategory = currCategory + currOwner;
		}
		var currPower = currentForm.power[currentForm.power.selectedIndex].value;
		var currPeriod = currentForm.period[currentForm.period.selectedIndex].value;
		var currTerm = currentForm.term[currentForm.term.selectedIndex].value;
		var currViolat = currentForm.breach[currentForm.breach.selectedIndex].value;
		var currLimit = currentForm.controlLimit[currentForm.controlLimit.selectedIndex].value;
		
		// base tarif
		var Tb = currentForm.all(currCategory).value;
		//Kt
		var Kt;
		if (currCategory == "TR" || currCategory == "PT") {
//			tmpStr = "T" + currRegion + "T";
			Kt = 1
		} else {
			Kt = 1.8
//			tmpStr = "T" + currRegion;
		}	
//		if ( currRegion != "IN" )
//			Kt = currentForm.all(tmpStr).value;
		
		//Kv
		var Kv = 0;
		if ( currOwner == "F" )
		{
//alert( tmpStr )
			for ( var i = 0; i < 5; i++ )
			{
				if ( currentForm.all( "age" + i ).selectedIndex == 1 )
					tmpStr = "VS"
				else
					tmpStr = "VB"		
				if ( currentForm.all( "stage" + i ).selectedIndex == 1 || currentForm.all( "stage" + i ).selectedIndex == 2 )
					tmpStr += "S"		
				else
					tmpStr += "B"
				Kvi = currentForm.all( tmpStr ).value;
//alert( tmpStr )
				if ( Kv < Kvi ) Kv = Kvi;
			}
		} else {
			Kv = 1;
		}

		//Ko
		var Ko;
		if ( currOwner == 'F' )
		{
			tmpStr = "O" + currLimit;
			Ko = currentForm.all(tmpStr).value;
			if ( Number( Ko ) == 1.5 )
			{
				Kv = 1;
			}
		}
		else
			Ko = 1.5;

		//Km
		var Km;
		tmpStr = "M" + currPower;
		Km = currentForm.all(tmpStr).value;		

		//Kc
		var Kc;
		tmpStr = "C" + currPeriod;
		Kc = currentForm.all(tmpStr).value;		

		//Kp
		var Kp = 1;
		
		//Kn
		var Kn;
		tmpStr = "N" + currViolat;
		Kn = currentForm.all(tmpStr).value;		
		// Kbm
		var Kbm = getKbm()
		//foreign transport
//		if (currRegion == "IN") {
//			Kt = 2;
//			Kv = 1.3;
//			Ko = 1;
//			Kc = 1;
//			Kn = 1;
//		}

		//choose formula
		if (currCategory == "LU" || currCategory == "LF" || currCategory == "TX") {
//alert( "Tb" + Tb )
//alert( "Kt" + Kt )
//alert( "Kv" + Kv )
//alert( "Kbm" + Kbm )
//alert( "Ko" + Ko )
//alert( "Km" + Km )
//alert( "Kc" + Kc )
//alert( "Kp" + Kp )
//alert( "Kn" + Kn )
			result = Tb*Kt*Kv*Kbm*Ko*Km*Kc*Kp*Kn
		}
		if (currCategory == "GS" || currCategory == "GB") {
			result = Tb*Kt*Kv*Kbm*Ko*Kc*Kp*Kn
		}
		if (currCategory == "PL" || currCategory == "PG" || currCategory == "PT") {
			result = Tb*Kt*Kc
		}
		if (currCategory == "AS" || currCategory == "AB" || currCategory == "TL"
			 || currCategory == "TM" || currCategory == "TR" || currCategory == "MT") {
			result = Tb*Kt*Kv*Kbm*Ko*Kc*Kp*Kn
		}
		max3 = 3*Tb*Kt;
		if ( result > max3 )
		{
			if (Kn > 1)
			{
				max5 = 5*Tb*Kt;
				if ( result > max5 ) {
					result = max5
				}
			} 
			else
			{
				result = max3
			}
		}
		resultListDiv = document.getElementById('resultList');
		showShopDiv= document.getElementById('shopBut');
		HTML=Math.round(result*100)/100;
		currentForm.osagoBonus.value = HTML;
		resultListDiv.innerHTML= "Страховая премия по ОСАГО составляет: <b>"+HTML+" руб.</b>";
		resultListDiv.style.display = "";
		showShopDiv.style.display = "";
		
		
	}


	function  YearsOfInsurance_onchange( obj )
	{
		var iYearsOfInsurance = obj[obj.selectedIndex].value
		trYearOfInsurance1.style.display = "none"
		trYearOfInsurance2.style.display = "none"
		trYearOfInsurance3.style.display = "none"
		trYearOfInsurance4.style.display = "none"
		if( iYearsOfInsurance > 0 )	trYearOfInsurance1.style.display = ""
		if( iYearsOfInsurance > 1 ) trYearOfInsurance2.style.display = "" 
		if( iYearsOfInsurance > 2 ) trYearOfInsurance3.style.display = ""
		if( iYearsOfInsurance > 3 )	trYearOfInsurance4.style.display = ""		
		if( iYearsOfInsurance == 0 )
		{
			trYearsOfInsurance.style.display = "none"
		} else {
			trYearsOfInsurance.style.display = ""
		}
	}
	function  controlLimit_onchange()
	{
		var currentForm = document.osago;	// current form
		if ( currentForm.controlLimit[ currentForm.controlLimit.selectedIndex ].value != "YES" )
		{
		tdLimitStage.style.display = "none"		
		}
		else
		{
		tdLimitStage.style.display = ""
		}
	}
