var lvalue,last='';
function calculate(){
	var rpm=document.getElementById('rpm').value*1.0;
	var ratio=document.getElementById('ratio').value*1.0;
	var pitch=document.getElementById('pitch').value*1.0;
	var slip=document.getElementById('slip').value*1.0;
	var speed=document.getElementById('speed').value*1.0;	
	var precision=document.getElementById('precision').value*1.0;
	var c,rez,collected=0;
		
	if( precision=='' ){
		precision=2;
		document.getElementById('precision').value=precision;
	}
	
	switch( document.getElementById('speed_unit').value ){
		case 'KPH':
			c=656.0;
			break;
		case 'KN':
			c=1215.2;
			break;
		case 'MPH':
			c=1056.0;
			break;
	}
	
	if( ( rpm<=0 && rpm!='' ) || !(/^[0-9\.]*$/.exec(rpm)) ){
		alert('Обороты двигателя должны быть полжительным числом');
		return;
	}else if( (ratio<=0 && ratio!='')  || !(/^[0-9\.]*$/.exec(ratio)) ){
		alert('Передаточное число редуктора должно быть полжительным числом');
		return;
	}else if( (pitch<=0 && pitch!='' ) || !(/^[0-9\.]*$/.exec(pitch))  ){
		alert('Шаг винта должен быть полжительным числом');
		return;	
	}else	if( (slip!='' && ( slip<0 || slip>100)) || !(/^[0-9\.]*$/.exec(slip)) ){
		alert('Потери при движении должно быть числом в промежутке от 0 до 100');
		return;	
	}else if( (speed<=0 && speed!='' ) || !(/^[0-9\.]*$/.exec(speed))  ){
		alert('Скорость должна быть полжительным числом');
		return;
	}
	
	if( rpm!='' )collected++;
	if( ratio!='' )collected++;
	if( pitch!='' )collected++;
	if( slip!='' )collected++;
	if( speed!='' )collected++;
	
	if( collected<4 ){
		alert('Введено слишком мало информации!');
		return;
	}
	
	if( collected==5 ){
		if( last=='' ){
			alert('Введено слишком много информации!');
			return;
		}else if( document.getElementById(last).value!=lvalue ){
			alert('Неизвестно что вы хотите сосчитать!');
			return;
		}
	}
	
	if( rpm=='' ){
		last='rpm';
	}else if( ratio=='' ){
		last='ratio';
	}else if( pitch=='' ){
		last='pitch';
	}else if( slip=='' ){
		last='slip';
	}else if( speed=='' ){
		last='speed';
	}

	switch( last ){
		case 'rpm':
			rez=(speed*ratio*c)/(pitch*(1.0-slip/100.0));
			//alert(c+"  "+speed*ratio*c+"   "+pitch*(1.0-slip/100.0));
			break;
		case 'ratio':
			rez=(rpm*pitch*(1-slip/100.0))/(speed*c);
			break;
		case 'pitch':
			rez=(speed*ratio*c)/(rpm*(1-slip/100.0));
			break;
		case 'slip':
			rez=100*(1-((speed*ratio*c)/(rpm*pitch)));
			break;
		case 'speed':
			rez=rpm/ratio*pitch/c*(1.0-slip/100.0);
			break;
	}

	c=1;
	for( var i=0; i<precision ; i++ )c*=10.0;
	rez*=c;
	rez=Math.round(rez);
	rez/=c;
	
	document.getElementById(last).value=rez;
	lvalue=rez;
}


function open_form(){
/*	var inht='';
	inht+='<html><head><link rel=stylesheet href=/float.css type=text/css>';
	inht+='<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1251"/>';
	inht+='</head><body><form action=\'boat_send.php\' method=post><table align=center>';
	inht+='<tr><td class=p7>Модель лодки</td><td class=p7><input type=text name=model id=model class=p7></td></tr>';
	inht+='<tr><td class=p7>Загрузка, кг</td><td class=p7><input type=text name=loading id=loading class=p7></td></tr>';
	inht+='<tr><td class=p7>Марка мотора</td><td class=p7><input type=text name=marka id=marka class=p7></td></tr>';
	inht+='<tr><td class=p7>Мощность мотора, л.с.</td><td><input type=text name=power id=power class=p7></td></tr>';
	inht+='<tr><td class=p7>Тип мотора</td><td><select name=mtype class=p7><option value=2>2х-тактные</option><option value=4>4х-тактные</option></select></td></tr>';
	inht+='<tr><td class=p7>Обороты двигателя</td><td><input type=text name=rota id=rota class=p7></td></tr>';
	inht+='<tr><td class=p7>Диаметр винта, \'\'</td><td><input type=text name=diametr id=diametr class=p7></td></tr>';
	inht+='<tr><td class=p7>Шаг винта, \'\'</td><td><input type=text name=step id=step class=p7></td></tr>';
	inht+='<tr><td class=p7>Скорость движения, км/ч</td><td><input type=text name=speed id=speed class=p7></td></tr>';
	inht+='<tr><td class=p7>Режим движения</td><td><select name=rezhim class=p7><option value=0>Водоизмещающий</option><option value=1>Переходный</option><option value=2>Глиссирование</option></select></td></tr>';
	inht+='<tr><td class=p7>Ваш email</td><td><input type=text name=email id=email class=p7></td></tr>';
	inht+='<tr><td colspan=2 align=center><input type=submit value=\'Отправить данные\' class=p7></td></tr>';
	inht+='</table></form></body></html>'; */
	
	win=window.open('/form.html','win','width=350,height=350,status=0,toolbar=0,menubar=0');
//	win.document.write(inht);
	win.focus();
}

