var	medida = document.getElementsByName('medida');
var	opcao = document.getElementsByName('opcao');
var am1 = new Array(1,18897268777.743553,10000000000,1000000,39.37007874015748,3.280839895013123,1.0936132983377078,0.0006213711922373339,0.00016457925313934926,0.00016428049251291656,6.6844919786096254e-12,3.240776749371289e-17,1.0570008340246154e-16,3.3356409519815204e-9)
var am2 = new Array(18897268777.743553,1,1.8897268777743553,18897.268777743553,479990626.9546862,5759887523.456235,17279662570.368706,30412206123848.92,34997741776381.06,35054433582714.293,2.827031409150436e+21,5.831092432210773e+26,1.7878196657414815e+26,5665258656366395000);
var am3 = new Array(10000000000,1.8897268777743552,1,10000,254000000,3048000000,9144000000,16093440000000,18520000000000,18550000000000,1.496e+21,3.08568e+26,9.460730472580799e+25,2997924580000000000);
var am4 = new Array(1000000,18897.268777743553,0.0001,1,25400,304800.00000000006,914400,1609344000.0000002,1852000000,1855000000,149600000000000000,3.08568e+22,9.460730472580801e+21,299792458000000);
var am5 = new Array(39.37007874015748,2.0833740157480316e-9,3.937007874015748e-9,0.00003937007874015748,1,12.000000000000002,36,63360.00000000001,72913.38582677166,73031.49606299213,5889763779527.56,1214834645669291300,372469703644913400,11802852677.165356);
var am6 = new Array(3.280839895013123,1.7361450131233594e-10,3.280839895013123e-10,0.000003280839895013123,0.08333333333333333,1,3,5280,6076.115485564304,6085.958005249344,490813648293.96326,101236220472440940,31039141970409450,983571056.4304461);
var am7 = new Array(1.0936132983377078,5.787150043744532e-11,1.0936132983377078e-10,0.0000010936132983377078,0.027777777777777776,0.33333333333333337,1,1.1760,2025.3718285214347,2028.652668416448,163604549431.32108,33745406824146980,10346380656803150,327857018.8101487);
var am8 = new Array(0.0006213711922373339,3.2881534339457566e-14,6.213711922373339e-14,6.21371192237334e-10,0.000015782828282828283,0.0001893939393939394,0.0005681818181818182,1,1.1507794480235425,1.1526435616002544,92957130.35870516,19173526604628.965,5878625373183.607,186282.39705122088);
var am9 = new Array(0.0005399568034557236,2.8573272138228944e-14,5.399568034557236e-14,5.399568034557235e-10,0.000013714902807775378,0.00016457883369330455,0.0004937365010799136,0.8689762419006479,1,1.0016198704103672,80777537.79697624,16661339092872.57,5108385784330.886,161874.97732181425);
var am10 = new Array(0.0005390835579514825,2.852706199460916e-14,5.390835579514825e-14,5.390835579514825e-10,0.000013692722371967655,0.00016431266846361186,0.0004929380053908356,0.8675708894878706,0.9983827493261456,1,80646900.26954179,16634393530997.305,5100124243978.868,161613.18490566037);
var am11 = new Array(6.6844919786096254e-12,3.537279411764706e-22,6.684491978609626e-22,6.684491978609625e-18,1.6978609625668448e-13,2.037433155080214e-12,6.112299465240642e-12,1.075764705882353e-8,1.2379679144385027e-8,1.2399732620320856e-8,1,206262.03208556148,63240.17695575401,0.002003960280748663);
var am12 = new Array(3.240776749371289e-17,1.714944517902051e-27,3.240776749371289e-27,3.240776749371289e-23,8.231572943403075e-19,9.87788753208369e-18,2.963366259625107e-17,5.2155246169401884e-14,6.001918539835627e-14,6.011640870083741e-14,0.000004848202017059449,1,0.3066011534760831,9.715604275232688e-9);
var am13 = new Array(1.0408746558373554e-16,5.508069277520442e-27,1.0408746558373555e-26,1.0408746558373553e-22,2.6438216258268824e-18,3.1725859509922597e-17,9.517757852976778e-17,1.675125382123913e-13,1.927699862610782e-13,1.9308224865782941e-13,0.000015571484851326836,3.2118061080242106,0.9847434574617521,3.120463715433848e-8);
var am14 = new Array(3.3356409519815204e-9,1.765144472046725e-19,3.335640951981521e-19,3.3356409519815205e-15,8.472528018033061e-11,1.0167033621639674e-9,3.0501100864919023e-9,0.000005368193752225749,0.000006177607043069776,0.00000618761396592572,499.01188641643546,102927205.72710338,31557600,1);

// metro  - correto
function m1(val) {
	if (document.getElementById('c1').readOnly==false) {
		val = val.replace(',','.');
		for (i=1;i<=13;i++) {
			re = am1[i]*val;
			medida[i].value = (re+'').replace('.',',');
		}
	}
}

// bohn - correto
function m2(val) {
	if (document.getElementById('c2').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=1) {
				re = val/am2[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

// angstrom
function m3(val) {
	if (document.getElementById('c3').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=2) {
				re = val/am3[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

// micron
function m4(val) {
	if (document.getElementById('c4').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=3) {
				re = val/am4[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

// polegada
function m5(val) {
	if (document.getElementById('c5').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=4) {
				re = val/am5[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

// pe
function m6(val) {
	if (document.getElementById('c6').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=5) {
				re = val/am6[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

// jarda
function m7(val) {
	if (document.getElementById('c7').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=6) {
				re = val/am7[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

// milha
function m8(val) {
	if (document.getElementById('c8').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=7) {
				re = val/am8[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

// milha  nautica
function m9(val) {
	if (document.getElementById('c9').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=8) {
				re = val/am9[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

// milha  terrestres
function m10(val) {
	if (document.getElementById('c10').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=9) {
				re = val/am10[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

// unidade atronomica
function m11(val) {
	if (document.getElementById('c11').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=10) {
				re = val/am11[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

// parsec
function m12(val) {
	if (document.getElementById('c12').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=11) {
				re = val/am12[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

// ano luz
function m13(val) {
	if (document.getElementById('c13').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=12) {
				re = val/am13[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

// segundo luz
function m14(val) {
	if (document.getElementById('c14').readOnly==false) {
		val = val.replace(',','.');
		for (i=0;i<=13;i++) {
			if (i!=13) {
				re = val/am14[i];
				medida[i].value = (re+'').replace('.',',');
			}
		}
	}
}

//--------------------------------------------------------------------

function muda() {
	for (i=0;i<opcao.length;i++) {
		if (opcao[i].checked == true) {
				medida[i].style.backgroundColor='#fff';
				medida[i].readOnly=false;
				medida[i].focus();
			} else {
				medida[i].style.backgroundColor='#E8E8E8';
				medida[i].readOnly=true;
		}
		medida[i].value='';
	}
}

function mascara(o,f) {
	v_obj=o
	v_fun=f
	setTimeout("execmascara()",1)
}

function execmascara() {
	v_obj.value=v_fun(v_obj.value);
}

function numDist(v) {
	v=v.replace(/[^0123456789,e+-]/,"");
	return v;
}

function consiste(obj,tipo) {	
	setTimeout(function(){calculoEfetivo(obj,tipo)},1);
}
function calculoEfetivo(obj,tipo) {	
	val = obj.value.replace(',','.');
	if(!consistencia(tipo,val)) obj.value = val.substring(0,obj.value.length-1);
		else obj.value = val;
}

function consistencia(tipo,valor) {
	switch(tipo) {
		case 'binario' :
				binario = /^[0-1]{0,}$/;
				return (valor.match(binario))? true : false;
			break;
		case 'numero' :
				numero = /^\d{0,}$/;
				return (valor.match(numero))? true : false;
			break;
		case 'octal' :
				octal = /^[0-7]{0,}$/;
				return (valor.match(octal))? true : false;
			break;
		case 'hexadecimal' :
				valor = valor.toUpperCase();
				hexa = /^[0-9A-F]{0,}$/;
				return (valor.match(hexa))? true : false;
			break;
		case 'numerodecimal' :
				numdec = /^[0-9.]{0,}$/;
				return (valor.match(numdec))? true : false;
			break;
		case 'operacoesbinario' :
				expr = /^[01+-\/*() ]{0,}$/;
				return (valor.match(expr))? true : false;
			break;
		case 'operacoesoctal' :
				expr = /^[0-7+-\/*() ]{0,}$/;
				return (valor.match(expr))? true : false;
			break;
		case 'operacoeshexa' :
				valor = valor.toUpperCase();
				expr = /^[0-9A-F+-\/*() ]{0,}$/;
				return (valor.match(expr))? true : false;
			break;
        case 'notacaocientifica' :
                notcient = /^[0-9.e\-\+]{0,}$/;
				return (valor.match(notcient))? true : false;
            break;
        case 'expressao' :
                valor = valor.toLowerCase();
                exp = /^[0-9.\-\+\/*()^x ]{0,}$/;
				return (valor.match(exp))? true : false;
            break;
        case 'numerointeiro' :
                exp = /^[0-9\-]{0,}$/;
				return (valor.match(exp))? true : false;
            break;
	}
}
