/* var GlobValuta = 0;  0 - рубли; 1, 2 - Доллары/Евро */
/* var GlobPrepaymentPercent = 20;  Процент предоплаты */
/* var GlobTerm = 12; срок кредита */
/* var GlobProgram = "p1"; p1 - новый; p2 - подержанный; p3 - индивидуальная программа; p4 - "два документа"  */
// 1000000
	function calc() {
		var program = document.getElementById("curid").value;
		var GlobProgram = program;
		var GlobValuta = document.getElementById("valuta").selectedIndex;
		var GlobPrepaymentPercent = document.getElementById("fst1").value;
		var GlobTerm = getsrok();
		var percent = document.getElementById("prcnt");
		
		switch (GlobProgram)
		{
			case "p1": // Новый автомобиль
				switch (GlobValuta)
				{
					case 0: // рубли
						if (GlobPrepaymentPercent<=40) { // предоплата <= 40%
							if (GlobTerm<=24) {
								percent.value = 12.5;
							}
							else if (GlobTerm<=36) {
								percent.value = 13.5;
							}
							else if (GlobTerm<=60) {
								percent.value = 14.5;
							}
							else {
								document.getElementById("mnthpmnt").value = "Неправильный";
								document.getElementById("mndhd").value = "срок кредита!";
								return;
							}
						}
						else { // предоплата > 40%
							if (GlobTerm<=24) {
								percent.value = 11;
							}
							else if (GlobTerm<=36) {
								percent.value = 12;
							}
							else if (GlobTerm<=60) {
								percent.value = 13;
							}
							else {
								document.getElementById("mnthpmnt").value = "Неправильный";
								document.getElementById("mndhd").value = "срок кредита!";
								return;
							}
						}
					break;
					case 1: // Доллары / Евро
					case 2:
						if (GlobPrepaymentPercent<=40) { // предоплата <= 40%
							if (GlobTerm<=24) {
								percent.value = 9.5;
							}
							else if (GlobTerm<=36) {
								percent.value = 10;
							}
							else if (GlobTerm<=60) {
								percent.value = 11;
							}
							else {
								document.getElementById("mnthpmnt").value = "Неправильный";
								document.getElementById("mndhd").value = "срок кредита!";
								return;
							}
						}
						else { // предоплата > 40%
							if (GlobTerm<=24) {
								percent.value = 9;
							}
							else if (GlobTerm<=36) {
								percent.value = 9.5;
							}
							else if (GlobTerm<=60) {
								percent.value = 10.5;
							}
							else {
								document.getElementById("mnthpmnt").value = "Неправильный";
								document.getElementById("mndhd").value = "срок кредита!";
								return;
							}
						}
					break;
				}
			break; // End Новый автомобиль

			case "p2": // Подержанный автомобиль
				switch (GlobValuta)
				{
					case 0: // рубли
						if (GlobPrepaymentPercent<=40) { // предоплата <= 40%
							if (GlobTerm<=24) {
								percent.value = 13;
							}
							else if (GlobTerm<=36) {
								percent.value = 14;
							}
							else {
								document.getElementById("mnthpmnt").value = "Неправильный";
								document.getElementById("mndhd").value = "срок кредита!";
								return;
							}
						}
						else { // предоплата > 40%
							if (GlobTerm<=24) {
								percent.value = 11.5;
							}
							else if (GlobTerm<=36) {
								percent.value = 12.5;
							}
							else {
								document.getElementById("mnthpmnt").value = "Неправильный";
								document.getElementById("mndhd").value = "срок кредита!";
								return;
							}
						}
					break;
					case 1: // Доллары / Евро
					case 2:
						if (GlobPrepaymentPercent<=40) { // предоплата <= 40%
							if (GlobTerm<=24) {
								percent.value = 10;
							}
							else if (GlobTerm<=36) {
								percent.value = 10.5;
							}
							else {
								document.getElementById("mnthpmnt").value = "Неправильный";
								document.getElementById("mndhd").value = "срок кредита!";
								return;
							}
						}
						else { // предоплата > 40%
							if (GlobTerm<=24) {
								percent.value = 9.5;
							}
							else if (GlobTerm<=36) {
								percent.value = 10;
							}
							else {
								document.getElementById("mnthpmnt").value = "Неправильный";
								document.getElementById("mndhd").value = "срок кредита!";
								return;
							}
						}
					break;
				}
			break; // End Подержанный автомобиль

			case "p4": // Два документа
				switch (GlobValuta)
				{
					case 0: // рубли
							if (GlobTerm<=36) {
								percent.value = 18;
							}
							else if (GlobTerm<=60) {
								percent.value = 19;
							}
							else {
								document.getElementById("mnthpmnt").value = "Неправильный";
								document.getElementById("mndhd").value = "срок кредита!";
								return;
							}
					break;
					case 1: // Доллары / Евро
					case 2:
							if (GlobTerm<=60) {
								percent.value = 16;
							}
							else {
								document.getElementById("mnthpmnt").value = "Неправильный";
								document.getElementById("mndhd").value = "срок кредита!";
								return;
							}
					break;
				}
			break; // End Два документа
		}

		var crd2 = document.getElementById("crd2").value; //СК
		var prcnt = document.getElementById("prcnt").value / 100; //ПС
		//var srk = document.getElementById("srk").value; //M
		var srk = getsrok();
		var price = document.getElementById("price").value;
		var srkst = 0 - srk;
		//var bbs = document.getElementById("bbs").value; 
		/*****
		Аннуитетный платеж = (СК*ПС/12) / (1-1/ (1 + ПС/12) М),
		
		где СК — сумма кредита;
		ПС — ежемесячная процентная ставка;
		M — количество платежных периодов (месяцев).
		/*****/
		
		if(program=="p5")
		{
			//var mnthpmnt = (((( crd2 * (prcnt / 12) ) / ( 1 - Math.pow((1 + prcnt / 12),srkst) )) * srk) - bbs ) / srk ;
			var mnthpmnt = ( (crd2 - (price - bbs)) * (prcnt / 12) ) / ( 1 - Math.pow((1 + prcnt / 12),srkst) ) ;
			
		}else{
			
			var mnthpmnt = ( crd2 * (prcnt / 12) ) / ( 1 - Math.pow((1 + prcnt / 12),srkst) ) ;
			
		}
		
		document.getElementById("mnthpmnt").value = Math.round(mnthpmnt);
		document.getElementById("mndhd").value = Math.round(mnthpmnt/0.4);
	}
	
	
	function to_null(){
		/**document.getElementById("calculator").reset();/***/
		document.getElementById("price").value = 0;
		change3();
		/**
		document.getElementById("price").value = 0;
		document.getElementById("fst1").value = 10;
		document.getElementById("fst2").value = 0;
		document.getElementById("crd1").value = 90;
		document.getElementById("crd2").value = 0;
		/***/
	}
	
	function fstchk(){
		var fst1 = document.getElementById("fst1").value;
		var fst2 = document.getElementById("fst2").value; 
		var program = document.getElementById("curid").value;
		
		switch (program)
		{
		case "p1":
			//document.getElementById("vznos").innerHTML = "(не менее 20%)";
			//var minval = 15;
			var price = document.getElementById("price").value;
			var fst1 = document.getElementById("fst1").value;
			//var fst2 = document.getElementById("fst2").value;
			//var crd1 = document.getElementById("crd1").value;
			//var crd2 = document.getElementById("crd2").value;
			var curid = document.getElementById("curid").value;
			var valuta = document.getElementById("valuta").selectedIndex;
			/*if((price<=87500 && valuta==2) || (price<=116000  && valuta==1) || (price<=3100000 && valuta==0)) 
			{
				var minval = 15;
			} 
			else 
			{
				var minval = 20;
			}*/
			var minval = 20;
			break;
		case "p2":
			document.getElementById("vznos").innerHTML = "(не менее 20%)";
			var minval = 20;
			break;
		case "p3":
			document.getElementById("vznos").innerHTML = "(не менее 20%)";
			var minval = 20;
			break;
		case "p4":
			document.getElementById("vznos").innerHTML = "(не менее 20%)";
			var minval = 20;
			break;
		case "p5":
			document.getElementById("vznos").innerHTML = "(не менее 20%)";
			var minval = 20;
			break;
		
		}
		
		
		if (fst1<minval){
			document.getElementById("fst1").style.backgroundColor = "#FFCECF";
			document.getElementById("fst2").style.backgroundColor = "#FFCECF";
		}else{
			document.getElementById("fst1").style.backgroundColor = "#FFFFFF";
			document.getElementById("fst2").style.backgroundColor = "#FFFFFF";
		}
		
		calc();
	}
	
	function change1(){
		var price = document.getElementById("price").value;
		var fst1 = document.getElementById("fst1").value;
		var fst2 = document.getElementById("fst2").value; 
		var crd1;
		var crd2;
		
		fst1 = (fst2 /price ) * 100;
		if(price!="")
		document.getElementById("fst1").value = Math.round(fst1);
		
		fstchk();
		
		crd1 = 100-fst1;
		crd2 = price - fst2;
		document.getElementById("crd1").value = Math.round(crd1);
		document.getElementById("crd2").value = Math.round(crd2);
		
		
		calc();
	}
	
	function change2(){
		var price = document.getElementById("price").value;
		var fst1 = document.getElementById("fst1").value;
		var fst2 = document.getElementById("fst2").value;
		var crd1;
		var crd2;
		
		
		fstchk();
		
		fst2 = (price/100) * fst1;
		if(price!="")
		document.getElementById("fst2").value = fst2;
		
		crd1 = 100-fst1;
		crd2 = price - fst2;
		document.getElementById("crd1").value = Math.round(crd1);
		document.getElementById("crd2").value = Math.round(crd2);
		
		change4();	
		change7();
		calc();	
	}
	
	function change3(){
		var price = document.getElementById("price").value;
		var fst1 = document.getElementById("fst1").value;
		var fst2 = document.getElementById("fst2").value;
		var crd1 = document.getElementById("crd1").value;
		var crd2 = document.getElementById("crd2").value;
		var curid = document.getElementById("curid").value;
		var valuta = document.getElementById("valuta").selectedIndex;
		
		if(fst1==""){
			fst2 = (price/100) * 10;
			document.getElementById("fst2").value = Math.round(fst2);
			document.getElementById("fst1").value = 10;
		}else{
			fst2 = (price/100) * fst1;
			document.getElementById("fst2").value = Math.round(fst2);
		}
		if(curid=='p1')
		{
			if((price<=87500 && valuta==2) || (price<=116000  && valuta==1) || (price<=3100000 && valuta==0)) 
			{
				document.getElementById("fst1").value = 20; 
				document.getElementById("vznos").innerHTML = "(не менее 20%)";
			} 
			 else 
			 {
				document.getElementById("fst1").value = 20;  
				document.getElementById("vznos").innerHTML = "(не менее 20%)";
			 }
		}
		
		crd1 = 100-fst1;
		crd2 = price - fst2;
		document.getElementById("crd1").value = Math.round(crd1);
		document.getElementById("crd2").value = Math.round(crd2);
		
		fstchk();
		change4();
		change7();
		calc();
		
	}
	
	function change4(){
		var price = document.getElementById("price").value;
		var valuta = document.getElementById("valuta").selectedIndex;
		GlobValuta = valuta;
		var crd2 = document.getElementById("crd2").value;
		var program = document.getElementById("curid").value;
		var fst1 = document.getElementById("fst1").value;
		//alert (document.getElementById("curid").value);
		
		if(program == "p1")
		{
			if((price<=87500 && valuta==2) || (price<=116000  && valuta==1) || (price<=3100000 && valuta==0)) 
			{document.getElementById("fst1").value = 20;} else {document.getElementById("fst1").value = 20;}
		}
		
		if(program == "p3"){
		
			switch (valuta)
			{
			case 0:
				//document.getElementById("kredit").innerHTML = "(сумма кредита от 7 300 050)";
				if(crd2>=7300050)
				{
					//document.getElementById("crd1").style.backgroundColor = "#FFFFFF"
					//document.getElementById("crd2").style.backgroundColor = "#FFFFFF"
				}else{
					//document.getElementById("crd1").style.backgroundColor = "#FFCECF"
					//document.getElementById("crd2").style.backgroundColor = "#FFCECF"
				}
				break;
			case 1:
				//document.getElementById("kredit").innerHTML = "(сумма кредита от 304 200)";
				if(crd2>=304200)
				{
					//document.getElementById("crd1").style.backgroundColor = "#FFFFFF"
					//document.getElementById("crd2").style.backgroundColor = "#FFFFFF"
				}else{
					//document.getElementById("crd1").style.backgroundColor = "#FFCECF"
					//document.getElementById("crd2").style.backgroundColor = "#FFCECF"
				}
				break;
			case 2:
				//document.getElementById("kredit").innerHTML = "(сумма кредита от 200 001)";
				if(crd2>=200001)
				{
					//document.getElementById("crd1").style.backgroundColor = "#FFFFFF";
					//document.getElementById("crd2").style.backgroundColor = "#FFFFFF";
				}else{
					//document.getElementById("crd1").style.backgroundColor = "#FFCECF";
					//document.getElementById("crd2").style.backgroundColor = "#FFCECF";
				}
				break;
			}
		
		}else{
			
			switch (valuta)
			{
			case 0:
				//document.getElementById("kredit").innerHTML = "(сумма кредита от 182 500 до 7 300 000)";
				if(crd2>=182500 && crd2<=7300000)
				{
					//document.getElementById("crd1").style.backgroundColor = "#FFFFFF"
					//document.getElementById("crd2").style.backgroundColor = "#FFFFFF"
				}else{
					//document.getElementById("crd1").style.backgroundColor = "#FFCECF"
					//document.getElementById("crd2").style.backgroundColor = "#FFCECF"
				}
				break;
			case 1:
				//document.getElementById("kredit").innerHTML = "(сумма кредита от 7 600 до 304 200)";
				if(crd2>=7600 && crd2<=304200)
				{
					//document.getElementById("crd1").style.backgroundColor = "#FFFFFF"
					//document.getElementById("crd2").style.backgroundColor = "#FFFFFF"
				}else{
					//document.getElementById("crd1").style.backgroundColor = "#FFCECF"
					//document.getElementById("crd2").style.backgroundColor = "#FFCECF"
				}
				break;
			case 2:
				//document.getElementById("kredit").innerHTML = "(сумма кредита от 5 000 до 200 000)";
				if(crd2>=5000 && crd2<=200000)
				{
					//document.getElementById("crd1").style.backgroundColor = "#FFFFFF";
					//document.getElementById("crd2").style.backgroundColor = "#FFFFFF";
				}else{
					//document.getElementById("crd1").style.backgroundColor = "#FFCECF";
					//document.getElementById("crd2").style.backgroundColor = "#FFCECF";
				}
				break;
			}
		}
		change6();
		change7();
		calc();
	}
	
	
	
	function change5(){
		//var srk = document.getElementById("srk").value;
		var srk = getsrok();

		var program = document.getElementById("curid").value;
		switch (program)
		{
		case "p1":
			document.getElementById("srokkredita").innerHTML = "(от 12 до 60 месяцев)"
			if(srk>=12 && srk<=60){
				document.getElementById("srk").style.backgroundColor = "#FFFFFF"
			}else{
				document.getElementById("srk").style.backgroundColor = "#FFCECF"
			}
			break;
		case "p2":
			document.getElementById("srokkredita").innerHTML = "(от 12 до 36 месяцев)"
			if(srk>=12 && srk<=36)
				document.getElementById("srk").style.backgroundColor = "#FFFFFF"
			else
				document.getElementById("srk").style.backgroundColor = "#FFCECF"
			break;
		case "p3":
			document.getElementById("srokkredita").innerHTML = "(от 12 до 84 месяцев)"
			if(srk>=12 && srk<=84)
				document.getElementById("srk").style.backgroundColor = "#FFFFFF"
			else
				document.getElementById("srk").style.backgroundColor = "#FFCECF"
			break;
		case "p4":
			document.getElementById("srokkredita").innerHTML = "(от 12 до 60 месяцев)"
			if(srk>=12 && srk<=60)
				document.getElementById("srk").style.backgroundColor = "#FFFFFF"
			else
				document.getElementById("srk").style.backgroundColor = "#FFCECF"
			break;
		case "p5":
			document.getElementById("srokkredita").innerHTML = "(от 12 до 36 месяцев)"
			if(srk>=12 && srk<=36)
				document.getElementById("srk").style.backgroundColor = "#FFFFFF"
			else
				document.getElementById("srk").style.backgroundColor = "#FFCECF"
			break;
		}
		
		calc();
	}
	
	function change6(){
		var program = document.getElementById("curid").value;
		var prcnt = document.getElementById("prcnt").value;
		var valuta = document.getElementById("valuta").selectedIndex;
		
		/***/
		switch (program)
		{
		case "p1":
			document.getElementById("prcnt").disabled="true";
			if(valuta==0){
				document.getElementById("stavka").innerHTML = "(от 11% до 14.5%)";
				/*if(prcnt>=11 && prcnt<=14.5)
					document.getElementById("prcnt").style.backgroundColor = "#FFFFFF"
				else
					document.getElementById("prcnt").style.backgroundColor = "#FFCECF"*/
			}else{
				document.getElementById("stavka").innerHTML = "(от 9% до 11%)";
				/*if(prcnt>=9 && prcnt<=11)
					document.getElementById("prcnt").style.backgroundColor = "#FFFFFF"
				else
					document.getElementById("prcnt").style.backgroundColor = "#FFCECF"*/
			}
				
			break;
		case "p2":
			document.getElementById("prcnt").disabled="true";
			if(valuta==0){
				document.getElementById("stavka").innerHTML = "(от 11.5% до 14%)";
				/*if(prcnt>=11.5 && prcnt<=14)
					document.getElementById("prcnt").style.backgroundColor = "#FFFFFF"
				else
					document.getElementById("prcnt").style.backgroundColor = "#FFCECF"*/
			}else{
				document.getElementById("stavka").innerHTML = "от 9.5% до 10.5%";
				/*if(prcnt>=9.5 && prcnt<=10.5)
					document.getElementById("prcnt").style.backgroundColor = "#FFFFFF"
				else
					document.getElementById("prcnt").style.backgroundColor = "#FFCECF"*/
			}
			
			break;
		case "p3":
			document.getElementById("prcnt").disabled="";
			if(valuta==0){
				document.getElementById("stavka").innerHTML = "(от 16% до 19%)";
				if(prcnt>=16 && prcnt<=19)
					document.getElementById("prcnt").style.backgroundColor = "#FFFFFF"
				else
					document.getElementById("prcnt").style.backgroundColor = "#FFCECF"
			}else{
				document.getElementById("stavka").innerHTML = "(от 13.5% до 16%)";
				if(prcnt>=13.5 && prcnt<=16)
					document.getElementById("prcnt").style.backgroundColor = "#FFFFFF"
				else
					document.getElementById("prcnt").style.backgroundColor = "#FFCECF"
			}
			
			break;
		case "p4":
			document.getElementById("prcnt").disabled="true";
			if(valuta==0){
				document.getElementById("stavka").innerHTML = "(от 18% до 19%)";
				/*if(prcnt>=18 && prcnt<=19)
					document.getElementById("prcnt").style.backgroundColor = "#FFFFFF"
				else
					document.getElementById("prcnt").style.backgroundColor = "#FFCECF"*/
			}else{
				document.getElementById("stavka").innerHTML = "(16%)";
				/*if(prcnt!=16)
					document.getElementById("prcnt").style.backgroundColor = "#FFFFFF"
				else
					document.getElementById("prcnt").style.backgroundColor = "#FFCECF"*/
			}
			
			break;
		case "p5":
			if(valuta==0){
				document.getElementById("stavka").innerHTML = "(от 11% до 14%)";
				if(prcnt>=11 && prcnt<=14)
					document.getElementById("prcnt").style.backgroundColor = "#FFFFFF"
				else
					document.getElementById("prcnt").style.backgroundColor = "#FFCECF"
			}else{
				document.getElementById("stavka").innerHTML = "(от 9% до 11%)";
				if(prcnt>=9 && prcnt<=11)
					document.getElementById("prcnt").style.backgroundColor = "#FFFFFF"
				else
					document.getElementById("prcnt").style.backgroundColor = "#FFCECF"
			}
			
			break;
		}
		/**/
		
		calc();
	}
	
	function change7(){
		var price = document.getElementById("price").value;
		//var bbp = document.getElementById("bbp").value;
		//var bbs = document.getElementById("bbs").value; 
		//bbs = price / 100 * bbp;
		//document.getElementById("bbs").value = bbs
	}
	
	
	function chng(id){
		curid = document.getElementById("curid").value;
		document.getElementById(curid).style.backgroundColor = "#FFFFFF";
		document.getElementById(curid).style.borderTop = "1px solid #ffffff";
		document.getElementById(id).style.backgroundColor = "#ececec";
		document.getElementById(id).style.borderTop = "1px solid #dddddd";
		document.getElementById(id).style.fontWeight = "bold";
		document.getElementById(curid).style.fontWeight = "normal";
		document.getElementById("curid").value = id;
		/**
		if(id=="p5"){
			document.getElementById('buyback').style.display = ""
		}else{
			document.getElementById('buyback').style.display = "none"
		}
		/***/
		change4();
		change5();
		change6();
		change7();
		fstchk();
		to_null();
		calc();
		return false;
	}
	
	function getsrok(){
		var srok = document.getElementById("srk").selectedIndex;
		switch (srok){
		case 0:
			var srk = 12;
			break;
		case 1:
			var srk = 24;
			break;
		case 2:
			var srk = 36;
			break;
		case 3:
			var srk = 48;
			break;
		case 4:
			var srk = 60;
			break;
		case 5:
			var srk = 72;
			break;
		case 6:
			var srk = 84;
			break;
		}
		
		return srk;
	}
	
	function numbersonly(myfield, e, dec)
	{
		var key;
		var keychar;
		if (window.event)
		 key = window.event.keyCode;
		else if (e)
		 key = e.which;
		else
		 return true;
		keychar = String.fromCharCode(key);
		// control keys
		if ((key==null) || (key==0) || (key==8) || 
		 (key==9) || (key==13) || (key==27) )
		 return true;
		// numbers
		else if ((("0123456789.").indexOf(keychar) > -1))
		 return true;
		// decimal point jump
		else if (dec && (keychar == "."))
		 {
		 myfield.form.elements[dec].focus();
		 return false;
		 }
		else
		 return false;
	}

	
