// JavaScript Document
var precision = 2;

function changeprecision(){
	var boxval=document.getElementById('pre_id').value;
	boxval = boxval.substring(0,1);
	document.getElementById('pre_id').value=boxval;
	if (boxval>0 && boxval<10)
	{
		precision = boxval;
		calculate();
	}
	if (boxval==1){document.getElementById('znak').innerHTML="знак";}
	if (boxval>1&&boxval<5){document.getElementById('znak').innerHTML="знака";}
	if (boxval>4){document.getElementById('znak').innerHTML="знаков";}
	document.getElementById('pre_id').select();
}

function explain(num){
	for(i=0;i<sA.length;i++){
		if(i != num){
			document.getElementById('explanation_div'+i).style.visibility = 'hidden';
			document.getElementById('name'+i).style.borderBottomColor='#FFFFFF';
		}
	}
	document.getElementById('explanation_div'+num).style.visibility = 'visible';
	document.getElementById('name'+num).style.borderBottomColor='#999999';
	document.getElementById('explanation_help_div').style.visibility = 'hidden';
}
function showhelp(){
	for(i=0;i<sA.length;i++){
			document.getElementById('explanation_div'+i).style.visibility = 'hidden';
	}
	document.getElementById('explanation_help_div').style.visibility = 'visible';
}
function expload(){
		var i=0;
		for(i=0;i<sA.length;i++){
			document.getElementById('name'+i).value=sA[i][3];
		}
		document.getElementById('pre_id').value = precision;
}
function updatethousands(){
	if (document.getElementById('thousand').value=='on')
	{
		document.getElementById('thousand').value='off';
		calculate();
	}
	else
	{
		document.getElementById('thousand').value='on';
		calculate();
	}
}
function updatedot(){
	if (document.getElementById('dot').value=='on')
	{
		document.getElementById('dot').value='off';
	}
	else
	{
		document.getElementById('dot').value='on';
	}
}

function calculate(){
	var boxvalue = document.getElementById('input'+input_id).value;
	var isdot = document.getElementById('dot').value;
	var isthousand = document.getElementById('thousand').value;
	boxvalue = boxvalue.replace(/\,/, ".");
	if((boxvalue==0) || (boxvalue/boxvalue==1)){
		var prec = Math.pow(10,precision);
		var output_value;
		for(i=0;i<sA.length;i++){
			// celsius->farenheit
			if (sA[i][1] != input_id){
			if (sA[i][1] == 1 && input_id==0){
				output_value = 1.8*boxvalue+32;
			}
			// farenheit->celsius
			if (sA[i][1] == 0 && input_id==1){
				output_value = (boxvalue*1-32)/1.8;
			}
			// celsius->kelvin
			if (sA[i][1] == 2 && input_id==0){
				output_value = boxvalue*1+273.15;
			}
			// kelvin->celsius
			if (sA[i][1] == 0 && input_id==2){
				output_value = boxvalue*1-273.15;
			}
			// fahrenheit->kelvin
			if (sA[i][1] == 2 && input_id==1){
				output_value = (1*boxvalue*1-32)/1.8+273.15;
			}
			// kelvin->fahrenheit
			if (sA[i][1] == 1 && input_id==2){
				output_value = (boxvalue*1-273.15)*1.8+32;
			}
				output_value = Math.round(output_value * prec) / prec;
				output_value = String(output_value);
				
				if(isthousand=='on')
				{
					razd=output_value.indexOf('.');
					if (razd<0)
					{
						first = output_value.substring(0,output_value.length);
						seconda = '';
						
					}
					else
					{
						first=output_value.substring(0, razd);
						seconda=output_value.substr(razd+1);
						seconda=reversestring(seconda);
						seconda=format_strings(seconda);
						seconda=reversestring(seconda);
						seconda='.'+seconda;
					}
				output_value=format_strings(first)+seconda;
				}
				else
				{
				output_value = output_value.replace(/\ /gi, "");
				}
				
				if(isdot=='off'){output_value = output_value.replace(/\./, ",");}
				document.getElementById('input'+i).value=output_value;
			
		}}
	}
}
function format_strings (sString) {
		aString = new Array();
		var sOut = '';
		for( var j=0; j<sString.length; j++ ) {
			aString[j] = sString.substr(j,1);
		}
		var count_letter = sString.length - 1;
		var first_deviver = 0;
		for( var k=0; k<sString.length; k++ ) {
			if(k % 3 == 0 && count_letter > 3 && k > 0 ) {
				sOut = aString[count_letter - k] + ' ' + sOut;
			} else {
				sOut = aString[count_letter - k] + sOut;
			}
			
		}
		return sOut;
}
function reversestring(sString){
	aString = new Array();
	var sOut = '';
	for( var j=0; j<sString.length; j++ ) {
		aString[j] = sString.substr(j,1);
	}
	var count_letter = sString.length - 1;
	var first_deviver = 0;
	for( var k=0; k<sString.length; k++ ) 
	{
			sOut = sOut+ aString[count_letter - k] ;
	}
	return sOut;
}
function lastinput(first){
	input_id=first;
}

function accent(vvod){
			var i=0;

			for(i=0;i<sA.length;i++){
				if(sA[i][1] != vvod){
					document.getElementById('input'+i).style.borderStyle = 'none';
					document.getElementById('input'+i).style.borderBottomStyle = 'dashed';
					var vala=0;
					vala = document.getElementById('input'+i).value;
					document.getElementById('input'+i).value=0;
					document.getElementById('input'+i).value=vala;
					mouseoutnumber(i);
				};
			};
			document.getElementById('input'+vvod).focus();
			document.getElementById('input'+vvod).select();
			document.getElementById('input'+vvod).style.borderStyle = 'solid';
			document.getElementById('input'+vvod).style.borderColor = '#999999';
			lastinput(vvod);

			
}
function mouseovernumber(vvod){
	if(!(input_id==vvod)){
	document.getElementById('input'+vvod).style.borderBottomColor ='#999999';

	}
	else
	{

	}
}
function mouseoutnumber(vvod){
	if(!(input_id==vvod)){
	document.getElementById('input'+vvod).style.borderBottomColor='#FFFFFF';
	}
}
function accentload(vvod){
			document.getElementById('input'+vvod).value=1000;
			var i=0;
			for(i=0;i<sA.length;i++){
				if(sA[i][1] != vvod){
					document.getElementById('input'+i).style.borderStyle = 'none';
				};
			};
			document.getElementById('input'+vvod).focus();
			document.getElementById('input'+vvod).select();
			document.getElementById('input'+vvod).style.borderStyle = 'solid';
			calculate();	
			document.getElementById('pre_id').value=precision;
			document.getElementById('input'+vvod).style.borderBottomColor='#999999';
			mouseoutnumber(vvod);
			changeprecision();
}